From d0bbbf98ead3d2a6943699c34b3ac9927a8f13c0 Mon Sep 17 00:00:00 2001 From: chrislo27 Date: Sat, 26 Sep 2020 09:54:04 -0700 Subject: [PATCH] Fix file explorer not opening sometimes on Windows --- .../rhre3/editor/stage/EditorStage.kt | 16 +++++--------- .../editor/stage/theme/ThemeChooserStage.kt | 12 +++++----- .../editor/stage/theme/ThemeEditorStage.kt | 3 ++- .../rhre3/screen/AdvancedOptionsScreen.kt | 3 ++- .../rhre3/screen/ExportRemixScreen.kt | 2 +- .../chrislo27/rhre3/screen/info/InfoStage.kt | 5 ++++- .../toolboks/util/gdxutils/URIUtils.kt | 22 +++++++++++++++++++ 7 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 core/src/main/kotlin/io/github/chrislo27/toolboks/util/gdxutils/URIUtils.kt diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/EditorStage.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/EditorStage.kt index a7165d536..eed5a1b35 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/EditorStage.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/EditorStage.kt @@ -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 @@ -810,19 +807,16 @@ class EditorStage(parent: UIElement?, this.fontScaleMultiplier = 0.9f } pickerStage.elements += gameStageText - customSoundsFolderButton = object : Button(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("ui_icon_folder")) }) + this.leftClickAction = { _, _ -> + Gdx.net.openFileExplorer(SFXDatabase.CUSTOM_SFX_FOLDER.file()) + } this.tooltipTextIsLocalizationKey = true this.tooltipText = "editor.customSfx.openFolder" this.visible = false diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeChooserStage.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeChooserStage.kt index 43739e042..26efbb094 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeChooserStage.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeChooserStage.kt @@ -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) @@ -114,18 +115,15 @@ class ThemeChooserStage(val editor: Editor, val palette: UIPalette, parent: Edit }) } - this.elements += object : Button(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("ui_icon_folder")) }) + this.leftClickAction = { _, _ -> + Gdx.net.openFileExplorer(LoadedThemes.THEMES_FOLDER) + } this.tooltipTextIsLocalizationKey = true this.tooltipText = "editor.themeEditor.openContainingFolder" } diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeEditorStage.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeEditorStage.kt index e1aa6b532..3ebcb0159 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeEditorStage.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/editor/stage/theme/ThemeEditorStage.kt @@ -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 @@ -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()) } } } diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/AdvancedOptionsScreen.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/AdvancedOptionsScreen.kt index 5bfdac19e..105f5eb5d 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/AdvancedOptionsScreen.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/AdvancedOptionsScreen.kt @@ -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 @@ -241,7 +242,7 @@ class AdvancedOptionsScreen(main: RHRE3Application) : ToolboksScreen("ui_icon_folder")) }) this.leftClickAction = { _, _ -> - Gdx.net.openURI("file:///${SFXDatabase.CUSTOM_MODDING_METADATA_FOLDER.file().absolutePath}") + Gdx.net.openFileExplorer(SFXDatabase.CUSTOM_MODDING_METADATA_FOLDER) } } diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/ExportRemixScreen.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/ExportRemixScreen.kt index df8caa062..c4561cd5d 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/ExportRemixScreen.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/ExportRemixScreen.kt @@ -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 diff --git a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/info/InfoStage.kt b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/info/InfoStage.kt index de62213f0..190a8d175 100644 --- a/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/info/InfoStage.kt +++ b/core/src/main/kotlin/io/github/chrislo27/rhre3/screen/info/InfoStage.kt @@ -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?, camera: OrthographicCamera, val infoScreen: InfoScreen) @@ -161,7 +164,7 @@ class InfoStage(parent: UIElement?, camera: OrthographicCamera, val image = TextureRegion(AssetRegistry.get("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" diff --git a/core/src/main/kotlin/io/github/chrislo27/toolboks/util/gdxutils/URIUtils.kt b/core/src/main/kotlin/io/github/chrislo27/toolboks/util/gdxutils/URIUtils.kt new file mode 100644 index 000000000..45822e711 --- /dev/null +++ b/core/src/main/kotlin/io/github/chrislo27/toolboks/util/gdxutils/URIUtils.kt @@ -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())