Replaced java calls for dialogs by native OS calls

This commit is contained in:
Abdelilah El Aissaoui 2022-08-07 17:16:15 +02:00
parent 975b0730d9
commit 8e65bff8e9
3 changed files with 123 additions and 6 deletions

View File

@ -1,7 +1,11 @@
package app.extensions package app.extensions
import app.logging.printLog
import java.io.IOException
import java.util.* import java.util.*
private const val TAG = "Shell"
fun runCommand(command: String): String? { fun runCommand(command: String): String? {
return try { return try {
var result: String? var result: String?
@ -19,5 +23,46 @@ fun runCommand(command: String): String? {
ex.printStackTrace() ex.printStackTrace()
null null
} }
}
fun runCommandWithoutResult(command: String, args: String, file: String): Boolean {
val parts: Array<String> = prepareCommand(command, args, file)
printLog(TAG, "Running command $parts")
return try {
val p = Runtime.getRuntime().exec(parts) ?: return false
try {
val exitValue = p.exitValue()
if (exitValue == 0) {
printLog(TAG, "Process ended immediately.")
false
} else {
printLog(TAG, "Process crashed.")
false
}
} catch (itse: IllegalThreadStateException) {
printLog(TAG, "Process is running.")
true
}
} catch (e: IOException) {
printLog(TAG, "Error running command: ${e.message}")
e.printStackTrace()
false
}
}
private fun prepareCommand(command: String, args: String?, file: String): Array<String> {
val parts: MutableList<String> = ArrayList()
parts.add(command)
if (args != null) {
for (s in args.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
val stringFormatted = String.format(s, file) // put in the filename thing
parts.add(stringFormatted.trim { it <= ' ' })
}
}
return parts.toTypedArray()
} }

View File

@ -15,6 +15,33 @@ val systemSeparator: String by lazy {
} }
fun openUrlInBrowser(url: String) { fun openUrlInBrowser(url: String) {
if (!openSystemSpecific(url)) {
openUrlInBrowserJdk(url)
}
}
private fun openSystemSpecific(url: String): Boolean {
val os = System.getProperty("os.name")
if (os.contains("linux")) {
if (runCommandWithoutResult("xdg-open", "%s", url))
return true
if (runCommandWithoutResult("kde-open", "%s", url))
return true
if (runCommandWithoutResult("gnome-open", "%s", url))
return true
} else if (os.contains("windows")) {
if (runCommandWithoutResult("explorer", "%s", url))
return true
} else if (os.contains("mac")) {
if (runCommandWithoutResult("open", "%s", url))
return true
}
return false
}
fun openUrlInBrowserJdk(url: String) {
try { try {
Desktop.getDesktop().browse(URI(url)) Desktop.getDesktop().browse(URI(url))
} catch (ex: Exception) { } catch (ex: Exception) {

View File

@ -1,9 +1,12 @@
package app.ui package app.ui
import app.extensions.runCommand
import app.logging.printLog
import app.viewmodels.TabViewModel import app.viewmodels.TabViewModel
import javax.swing.JFileChooser import javax.swing.JFileChooser
import javax.swing.UIManager import javax.swing.UIManager
private const val TAG = "SystemDialogs"
fun openDirectoryDialog(basePath: String? = null): String? { fun openDirectoryDialog(basePath: String? = null): String? {
return openPickerDialog( return openPickerDialog(
@ -36,6 +39,54 @@ private fun openPickerDialog(
val os = System.getProperty("os.name") val os = System.getProperty("os.name")
val isLinux = os.lowercase().contains("linux") val isLinux = os.lowercase().contains("linux")
return if (isLinux) {
openDirectoryDialogLinux(pickerType)
} else
openJvmDialog(pickerType, basePath, false)
}
enum class PickerType(val value: Int) {
FILES(JFileChooser.FILES_ONLY),
DIRECTORIES(JFileChooser.DIRECTORIES_ONLY),
FILES_AND_DIRECTORIES(JFileChooser.FILES_AND_DIRECTORIES);
}
fun openDirectoryDialogLinux(pickerType: PickerType): String? {
val os = System.getProperty("os.name")
var dirToOpen: String? = null
if (os.lowercase() == "linux") {
val checkZenityInstalled = runCommand("which zenity 2>/dev/null")
val isZenityInstalled = !checkZenityInstalled.isNullOrEmpty()
printLog(TAG, "IsZenityInstalled $isZenityInstalled")
if (isZenityInstalled) {
val command = when (pickerType) {
PickerType.FILES, PickerType.FILES_AND_DIRECTORIES -> "zenity --file-selection --title=Open"
PickerType.DIRECTORIES -> "zenity --file-selection --title=Open --directory"
}
val openDirectory = runCommand(command)?.replace("\n", "")
if (!openDirectory.isNullOrEmpty())
dirToOpen = openDirectory
} else
dirToOpen = openJvmDialog(pickerType, "", true)
} else {
dirToOpen = openJvmDialog(pickerType, "", false)
}
return dirToOpen
}
private fun openJvmDialog(
pickerType: PickerType,
basePath: String?,
isLinux: Boolean,
): String? {
if (!isLinux) { if (!isLinux) {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} }
@ -52,10 +103,4 @@ private fun openPickerDialog(
fileChooser.selectedFile.absolutePath fileChooser.selectedFile.absolutePath
else else
null null
}
enum class PickerType(val value: Int) {
FILES(JFileChooser.FILES_ONLY),
DIRECTORIES(JFileChooser.DIRECTORIES_ONLY),
FILES_AND_DIRECTORIES(JFileChooser.FILES_AND_DIRECTORIES);
} }