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.io.File
import java.io.FileNotFoundException
import java.util.concurrent.TimeUnit
interface FileUtils {
fun openFile(file: File)
@ -31,7 +32,7 @@ private open class DefaultFileUtils : FileUtils {
if (!file.exists()) {
throw FileNotFoundException("$file not found")
}
val desktop = Desktop.getDesktop() ?: return;
val desktop = Desktop.getDesktop() ?: return
desktop.open(file)
}
@ -74,24 +75,13 @@ private open class DefaultFileUtils : FileUtils {
private class WindowsFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean {
return kotlin.runCatching {
Runtime.getRuntime()
.exec(
arrayOf(
"cmd", "/c",
"explorer.exe",
"/select,",
file.path
)
).exitValue() == 0
}.getOrElse { false }
return execAndWait(arrayOf("cmd", "/c", "explorer.exe", "/select,", file.path))
}
}
private class LinuxFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean {
val dbusSendResult = kotlin.runCatching {
Runtime.getRuntime().exec(
val dbusSendResult = execAndWait(
arrayOf(
"dbus-send",
"--print-reply",
@ -101,23 +91,40 @@ private class LinuxFileUtils : DefaultFileUtils() {
"array:string:file://${file.path}",
"string:"
)
).exitValue() == 0
}.getOrElse { false }
)
if (dbusSendResult) {
return true
}
val xdgOpenResult = kotlin.runCatching {
Runtime.getRuntime().exec(arrayOf("xdg-open", file.parent)).exitValue() == 0
}.getOrElse { false }
val xdgOpenResult = execAndWait(
arrayOf("xdg-open", file.parent)
)
return xdgOpenResult
}
}
private class MacOsFileUtils : DefaultFileUtils() {
override fun fallBackOpenFolderOfFile(file: File): Boolean {
return kotlin.runCatching {
Runtime.getRuntime()
.exec(arrayOf("open", "-R", file.path)).exitValue() == 0
return execAndWait(arrayOf("open", "-R", file.path))
}
}
/**
* 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 }
}
}