Skip to content

Commit

Permalink
Fix file explorer not opening sometimes on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislo27 committed Sep 26, 2020
1 parent f68926c commit d0bbbf9
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ import io.github.chrislo27.toolboks.i18n.ToolboksBundle
import io.github.chrislo27.toolboks.registry.AssetRegistry
import io.github.chrislo27.toolboks.ui.*
import io.github.chrislo27.toolboks.util.MathHelper
import io.github.chrislo27.toolboks.util.gdxutils.getInputX
import io.github.chrislo27.toolboks.util.gdxutils.isAltDown
import io.github.chrislo27.toolboks.util.gdxutils.isControlDown
import io.github.chrislo27.toolboks.util.gdxutils.isShiftDown
import io.github.chrislo27.toolboks.util.gdxutils.*
import java.util.*
import kotlin.math.min
import kotlin.math.roundToInt
Expand Down Expand Up @@ -810,19 +807,16 @@ class EditorStage(parent: UIElement<EditorScreen>?,
this.fontScaleMultiplier = 0.9f
}
pickerStage.elements += gameStageText
customSoundsFolderButton = object : Button<EditorScreen>(palette, pickerStage, pickerStage) {
override fun onLeftClick(xPercent: Float, yPercent: Float) {
super.onLeftClick(xPercent, yPercent)

Gdx.net.openURI("file:///${SFXDatabase.CUSTOM_SFX_FOLDER.file().absolutePath}")
}
}.apply {
customSoundsFolderButton = Button(palette, pickerStage, pickerStage).apply {
setLocation(Editor.ICON_COUNT_X, 0)
this.location.set(screenY = 0.5f - this.location.screenHeight / 2f)
this.addLabel(ImageLabel(palette, this, this.stage).apply {
renderType = ImageLabel.ImageRendering.ASPECT_RATIO
image = TextureRegion(AssetRegistry.get<Texture>("ui_icon_folder"))
})
this.leftClickAction = { _, _ ->
Gdx.net.openFileExplorer(SFXDatabase.CUSTOM_SFX_FOLDER.file())
}
this.tooltipTextIsLocalizationKey = true
this.tooltipText = "editor.customSfx.openFolder"
this.visible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.github.chrislo27.rhre3.theme.Theme
import io.github.chrislo27.rhre3.theme.Themes
import io.github.chrislo27.toolboks.registry.AssetRegistry
import io.github.chrislo27.toolboks.ui.*
import io.github.chrislo27.toolboks.util.gdxutils.openFileExplorer


class ThemeChooserStage(val editor: Editor, val palette: UIPalette, parent: EditorStage, camera: OrthographicCamera, pixelsWidth: Float, pixelsHeight: Float)
Expand Down Expand Up @@ -114,18 +115,15 @@ class ThemeChooserStage(val editor: Editor, val palette: UIPalette, parent: Edit
})
}

this.elements += object : Button<EditorScreen>(palette, this, this.stage) {
override fun onLeftClick(xPercent: Float, yPercent: Float) {
super.onLeftClick(xPercent, yPercent)

Gdx.net.openURI("file:///${LoadedThemes.THEMES_FOLDER.file().canonicalPath}")
}
}.apply {
this.elements += Button(palette, this, this.stage).apply {
this.location.set(0f, 0f, 0f, 1f, 346f - 34f, 0f, 34f, 0f)
this.addLabel(ImageLabel(palette, this, this.stage).apply {
this.renderType = ImageLabel.ImageRendering.ASPECT_RATIO
this.image = TextureRegion(AssetRegistry.get<Texture>("ui_icon_folder"))
})
this.leftClickAction = { _, _ ->
Gdx.net.openFileExplorer(LoadedThemes.THEMES_FOLDER)
}
this.tooltipTextIsLocalizationKey = true
this.tooltipText = "editor.themeEditor.openContainingFolder"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.github.chrislo27.rhre3.util.*
import io.github.chrislo27.toolboks.i18n.Localization
import io.github.chrislo27.toolboks.registry.AssetRegistry
import io.github.chrislo27.toolboks.ui.*
import io.github.chrislo27.toolboks.util.gdxutils.openFileExplorer
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.io.File
Expand Down Expand Up @@ -256,7 +257,7 @@ class ThemeEditorStage(val editor: Editor, val palette: UIPalette, parent: Theme
leftClickAction = { _, _ ->
val f = themeFile
if (f != null) {
Gdx.net.openURI("file:///${f.parent().file().canonicalPath}")
Gdx.net.openFileExplorer(f.parent())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import io.github.chrislo27.toolboks.ui.TextLabel
import io.github.chrislo27.toolboks.ui.UIElement
import io.github.chrislo27.toolboks.util.gdxutils.fillRect
import io.github.chrislo27.toolboks.util.gdxutils.getInputX
import io.github.chrislo27.toolboks.util.gdxutils.openFileExplorer
import java.util.*
import kotlin.math.sign
import kotlin.system.measureNanoTime
Expand Down Expand Up @@ -241,7 +242,7 @@ class AdvancedOptionsScreen(main: RHRE3Application) : ToolboksScreen<RHRE3Applic
image = TextureRegion(AssetRegistry.get<Texture>("ui_icon_folder"))
})
this.leftClickAction = { _, _ ->
Gdx.net.openURI("file:///${SFXDatabase.CUSTOM_MODDING_METADATA_FOLDER.file().absolutePath}")
Gdx.net.openFileExplorer(SFXDatabase.CUSTOM_MODDING_METADATA_FOLDER)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class ExportRemixScreen(main: RHRE3Application)
this.leftClickAction = { _, _ ->
val ff = folderFile
if (ff != null) {
Gdx.net.openURI("file:///${(ff.takeUnless { it.isFile } ?: ff.parentFile).absolutePath}")
Gdx.net.openFileExplorer((ff.takeUnless { it.isFile } ?: ff.parentFile))
}
}
this.visible = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import io.github.chrislo27.toolboks.util.MathHelper
import io.github.chrislo27.toolboks.util.gdxutils.isAltDown
import io.github.chrislo27.toolboks.util.gdxutils.isControlDown
import io.github.chrislo27.toolboks.util.gdxutils.isShiftDown
import io.github.chrislo27.toolboks.util.gdxutils.openFileExplorer
import java.awt.Desktop
import java.net.URI


class InfoStage(parent: UIElement<InfoScreen>?, camera: OrthographicCamera, val infoScreen: InfoScreen)
Expand Down Expand Up @@ -161,7 +164,7 @@ class InfoStage(parent: UIElement<InfoScreen>?, camera: OrthographicCamera, val
image = TextureRegion(AssetRegistry.get<Texture>("ui_icon_folder"))
})
this.leftClickAction = { _, _ ->
Gdx.net.openURI("file:///${SFXDatabase.CUSTOM_SFX_FOLDER.file().absolutePath}")
Gdx.net.openFileExplorer(SFXDatabase.CUSTOM_SFX_FOLDER)
}
this.tooltipTextIsLocalizationKey = true
this.tooltipText = "editor.customSfx.openFolder"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.chrislo27.toolboks.util.gdxutils

import com.badlogic.gdx.Net
import com.badlogic.gdx.files.FileHandle
import java.awt.Desktop
import java.io.File
import java.net.URI


fun Net.openFileExplorer(absPath: String): Boolean {
return try {
Desktop.getDesktop().browse(URI("file:///${absPath.replace("\\", "/")}"))
true
} catch (t: Throwable) {
t.printStackTrace()
false
}
}

fun Net.openFileExplorer(file: File): Boolean = openFileExplorer(file.absolutePath)

fun Net.openFileExplorer(fileHandle: FileHandle): Boolean = openFileExplorer(fileHandle.file())

0 comments on commit d0bbbf9

Please sign in to comment.