From c04fc092478e0d576851ac9d653a1e052086a5b0 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sun, 7 Aug 2022 17:37:08 +0200 Subject: [PATCH] Unified OS checking calls --- src/main/kotlin/app/extensions/SystemUtils.kt | 52 ++++++++++++++----- src/main/kotlin/app/keybindings/Keybinding.kt | 18 +++---- src/main/kotlin/app/ui/SystemDialogs.kt | 37 ++++++------- .../app/ui/dialogs/settings/SettingsDialog.kt | 1 + 4 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/app/extensions/SystemUtils.kt b/src/main/kotlin/app/extensions/SystemUtils.kt index 901d653..40dc8c9 100644 --- a/src/main/kotlin/app/extensions/SystemUtils.kt +++ b/src/main/kotlin/app/extensions/SystemUtils.kt @@ -21,20 +21,19 @@ fun openUrlInBrowser(url: String) { } 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 + when(getCurrentOs()) { + OS.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 + } + + OS.WINDOWS -> if (runCommandWithoutResult("explorer", "%s", url)) return true + OS.MAC -> if (runCommandWithoutResult("open", "%s", url)) return true + else -> printLog(TAG, "Unknown OS") } return false @@ -59,4 +58,29 @@ fun copyInBrowser(textToCopy: String) { printLog(TAG, "Failed to copy text") ex.printStackTrace() } +} + +enum class OS { + LINUX, + WINDOWS, + MAC, + UNKNOWN; + + fun isLinux() = this == LINUX + + fun isWindows() = this == WINDOWS + + fun isMac() = this == MAC +} + +fun getCurrentOs(): OS { + val os = System.getProperty("os.name").lowercase() + printLog(TAG, "OS is $os") + + return when { + os.contains("linux") -> OS.LINUX + os.contains("windows") -> OS.WINDOWS + os.contains("mac") -> OS.MAC + else -> OS.UNKNOWN + } } \ No newline at end of file diff --git a/src/main/kotlin/app/keybindings/Keybinding.kt b/src/main/kotlin/app/keybindings/Keybinding.kt index 7de1c26..fd4de16 100644 --- a/src/main/kotlin/app/keybindings/Keybinding.kt +++ b/src/main/kotlin/app/keybindings/Keybinding.kt @@ -4,6 +4,8 @@ package app.keybindings import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.input.key.* +import app.extensions.OS +import app.extensions.getCurrentOs data class Keybinding( val alt: Boolean = false, @@ -66,16 +68,12 @@ private fun macKeybindings(): Map> { } val keybindings by lazy { - val os = System.getProperty("os.name").lowercase() - - return@lazy if (os.contains("linux")) - linuxKeybindings() - else if (os.contains("windows")) - windowsKeybindings() - else if (os.contains("mac")) - macKeybindings() - else // In case FreeBSD gets supported in the future? - baseKeybindings() + return@lazy when(getCurrentOs()) { + OS.LINUX -> linuxKeybindings() + OS.WINDOWS -> windowsKeybindings() + OS.MAC -> macKeybindings() + OS.UNKNOWN -> baseKeybindings() + } } fun KeyEvent.matchesBinding(keybindingOption: KeybindingOption): Boolean { diff --git a/src/main/kotlin/app/ui/SystemDialogs.kt b/src/main/kotlin/app/ui/SystemDialogs.kt index 307abeb..c0cba39 100644 --- a/src/main/kotlin/app/ui/SystemDialogs.kt +++ b/src/main/kotlin/app/ui/SystemDialogs.kt @@ -1,5 +1,6 @@ package app.ui +import app.extensions.getCurrentOs import app.extensions.runCommand import app.logging.printLog import app.viewmodels.TabViewModel @@ -35,9 +36,8 @@ private fun openPickerDialog( pickerType: PickerType, basePath: String?, ): String? { - - val os = System.getProperty("os.name") - val isLinux = os.lowercase().contains("linux") + val os = getCurrentOs() + val isLinux = os.isLinux() return if (isLinux) { openDirectoryDialogLinux(pickerType) @@ -53,31 +53,26 @@ enum class PickerType(val value: Int) { 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() + val checkZenityInstalled = runCommand("which zenity 2>/dev/null") + val isZenityInstalled = !checkZenityInstalled.isNullOrEmpty() - printLog(TAG, "IsZenityInstalled $isZenityInstalled") + printLog(TAG, "IsZenityInstalled $isZenityInstalled") - if (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 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", "") + val openDirectory = runCommand(command)?.replace("\n", "") - if (!openDirectory.isNullOrEmpty()) - dirToOpen = openDirectory - } else - dirToOpen = openJvmDialog(pickerType, "", true) - } else { - dirToOpen = openJvmDialog(pickerType, "", false) - } + if (!openDirectory.isNullOrEmpty()) + dirToOpen = openDirectory + } else + dirToOpen = openJvmDialog(pickerType, "", true) return dirToOpen } diff --git a/src/main/kotlin/app/ui/dialogs/settings/SettingsDialog.kt b/src/main/kotlin/app/ui/dialogs/settings/SettingsDialog.kt index abd0d45..ed1c52f 100644 --- a/src/main/kotlin/app/ui/dialogs/settings/SettingsDialog.kt +++ b/src/main/kotlin/app/ui/dialogs/settings/SettingsDialog.kt @@ -184,6 +184,7 @@ fun UiSettings(settingsViewModel: SettingsViewModel) { mutableStateOf( listOf( ScaleDropDown(1f, "100%"), + ScaleDropDown(1.25f, "125%"), ScaleDropDown(1.5f, "150%"), ScaleDropDown(2f, "200%"), ScaleDropDown(2.5f, "250%"),