Merge pull request #68 from amir1376/fix/open-folder-two-times-in-linux

Fix folder opened two times when clicking on open folder in linux
This commit is contained in:
AmirHossein Abdolmotallebi 2024-09-10 05:50:30 +03:30 committed by GitHub
commit 873667540e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ import ir.amirab.util.platform.Platform
import java.awt.Desktop import java.awt.Desktop
import java.io.File import java.io.File
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.util.concurrent.TimeUnit
interface FileUtils { interface FileUtils {
fun openFile(file: File) fun openFile(file: File)
@ -31,7 +32,7 @@ private open class DefaultFileUtils : FileUtils {
if (!file.exists()) { if (!file.exists()) {
throw FileNotFoundException("$file not found") throw FileNotFoundException("$file not found")
} }
val desktop = Desktop.getDesktop() ?: return; val desktop = Desktop.getDesktop() ?: return
desktop.open(file) desktop.open(file)
} }
@ -74,50 +75,56 @@ private open class DefaultFileUtils : FileUtils {
private class WindowsFileUtils : DefaultFileUtils() { private class WindowsFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean { override fun fallBackOpenFolderOfFile(file: File): Boolean {
return kotlin.runCatching { return execAndWait(arrayOf("cmd", "/c", "explorer.exe", "/select,", file.path))
Runtime.getRuntime()
.exec(
arrayOf(
"cmd", "/c",
"explorer.exe",
"/select,",
file.path
)
).exitValue() == 0
}.getOrElse { false }
} }
} }
private class LinuxFileUtils : DefaultFileUtils() { private class LinuxFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean { override fun fallBackOpenFolderOfFile(file: File): Boolean {
val dbusSendResult = kotlin.runCatching { val dbusSendResult = execAndWait(
Runtime.getRuntime().exec( arrayOf(
arrayOf( "dbus-send",
"dbus-send", "--print-reply",
"--print-reply", "--dest=org.freedesktop.FileManager1",
"--dest=org.freedesktop.FileManager1", "/org/freedesktop/FileManager1",
"/org/freedesktop/FileManager1", "org.freedesktop.FileManager1.ShowItems",
"org.freedesktop.FileManager1.ShowItems", "array:string:file://${file.path}",
"array:string:file://${file.path}", "string:"
"string:" )
) )
).exitValue() == 0
}.getOrElse { false }
if (dbusSendResult) { if (dbusSendResult) {
return true return true
} }
val xdgOpenResult = kotlin.runCatching { val xdgOpenResult = execAndWait(
Runtime.getRuntime().exec(arrayOf("xdg-open", file.parent)).exitValue() == 0 arrayOf("xdg-open", file.parent)
}.getOrElse { false } )
return xdgOpenResult return xdgOpenResult
} }
} }
private class MacOsFileUtils : DefaultFileUtils() { private class MacOsFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean { override fun fallBackOpenFolderOfFile(file: File): Boolean {
return kotlin.runCatching { return execAndWait(arrayOf("open", "-R", file.path))
Runtime.getRuntime()
.exec(arrayOf("open", "-R", file.path)).exitValue() == 0
}.getOrElse { false }
} }
}
/**
* this helper function is here to execute a command and waits for the process to finish and return the result based on exit code
* @param command the command
* @param waitFor maximum time allowed process finish ( in milliseconds )
* @return `true` when process exits with `0` exit code, `false` if the process fails with non-zero exit code or execution time exceeds the [waitFor]
*/
private fun execAndWait(
command: Array<String>,
waitFor: Long = 2_000,
): Boolean {
return runCatching {
val p = Runtime.getRuntime().exec(command)
val exited = p.waitFor(waitFor, TimeUnit.MILLISECONDS)
if (exited) {
p.exitValue() == 0
} else {
false
}
}.getOrElse { false }
} }