Unified OS checking calls

This commit is contained in:
Abdelilah El Aissaoui 2022-08-07 17:37:08 +02:00
parent 8e65bff8e9
commit c04fc09247
4 changed files with 63 additions and 45 deletions

View File

@ -21,20 +21,19 @@ fun openUrlInBrowser(url: String) {
} }
private fun openSystemSpecific(url: String): Boolean { private fun openSystemSpecific(url: String): Boolean {
val os = System.getProperty("os.name") when(getCurrentOs()) {
if (os.contains("linux")) { OS.LINUX -> {
if (runCommandWithoutResult("xdg-open", "%s", url)) if (runCommandWithoutResult("xdg-open", "%s", url))
return true return true
if (runCommandWithoutResult("kde-open", "%s", url)) if (runCommandWithoutResult("kde-open", "%s", url))
return true return true
if (runCommandWithoutResult("gnome-open", "%s", url)) if (runCommandWithoutResult("gnome-open", "%s", url))
return true return true
} else if (os.contains("windows")) { }
if (runCommandWithoutResult("explorer", "%s", url))
return true OS.WINDOWS -> if (runCommandWithoutResult("explorer", "%s", url)) return true
} else if (os.contains("mac")) { OS.MAC -> if (runCommandWithoutResult("open", "%s", url)) return true
if (runCommandWithoutResult("open", "%s", url)) else -> printLog(TAG, "Unknown OS")
return true
} }
return false return false
@ -59,4 +58,29 @@ fun copyInBrowser(textToCopy: String) {
printLog(TAG, "Failed to copy text") printLog(TAG, "Failed to copy text")
ex.printStackTrace() 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
}
} }

View File

@ -4,6 +4,8 @@ package app.keybindings
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.input.key.* import androidx.compose.ui.input.key.*
import app.extensions.OS
import app.extensions.getCurrentOs
data class Keybinding( data class Keybinding(
val alt: Boolean = false, val alt: Boolean = false,
@ -66,16 +68,12 @@ private fun macKeybindings(): Map<KeybindingOption, List<Keybinding>> {
} }
val keybindings by lazy { val keybindings by lazy {
val os = System.getProperty("os.name").lowercase() return@lazy when(getCurrentOs()) {
OS.LINUX -> linuxKeybindings()
return@lazy if (os.contains("linux")) OS.WINDOWS -> windowsKeybindings()
linuxKeybindings() OS.MAC -> macKeybindings()
else if (os.contains("windows")) OS.UNKNOWN -> baseKeybindings()
windowsKeybindings() }
else if (os.contains("mac"))
macKeybindings()
else // In case FreeBSD gets supported in the future?
baseKeybindings()
} }
fun KeyEvent.matchesBinding(keybindingOption: KeybindingOption): Boolean { fun KeyEvent.matchesBinding(keybindingOption: KeybindingOption): Boolean {

View File

@ -1,5 +1,6 @@
package app.ui package app.ui
import app.extensions.getCurrentOs
import app.extensions.runCommand import app.extensions.runCommand
import app.logging.printLog import app.logging.printLog
import app.viewmodels.TabViewModel import app.viewmodels.TabViewModel
@ -35,9 +36,8 @@ private fun openPickerDialog(
pickerType: PickerType, pickerType: PickerType,
basePath: String?, basePath: String?,
): String? { ): String? {
val os = getCurrentOs()
val os = System.getProperty("os.name") val isLinux = os.isLinux()
val isLinux = os.lowercase().contains("linux")
return if (isLinux) { return if (isLinux) {
openDirectoryDialogLinux(pickerType) openDirectoryDialogLinux(pickerType)
@ -53,31 +53,26 @@ enum class PickerType(val value: Int) {
fun openDirectoryDialogLinux(pickerType: PickerType): String? { fun openDirectoryDialogLinux(pickerType: PickerType): String? {
val os = System.getProperty("os.name")
var dirToOpen: String? = null var dirToOpen: String? = null
if (os.lowercase() == "linux") { val checkZenityInstalled = runCommand("which zenity 2>/dev/null")
val checkZenityInstalled = runCommand("which zenity 2>/dev/null") val isZenityInstalled = !checkZenityInstalled.isNullOrEmpty()
val isZenityInstalled = !checkZenityInstalled.isNullOrEmpty()
printLog(TAG, "IsZenityInstalled $isZenityInstalled") printLog(TAG, "IsZenityInstalled $isZenityInstalled")
if (isZenityInstalled) { if (isZenityInstalled) {
val command = when (pickerType) { val command = when (pickerType) {
PickerType.FILES, PickerType.FILES_AND_DIRECTORIES -> "zenity --file-selection --title=Open" PickerType.FILES, PickerType.FILES_AND_DIRECTORIES -> "zenity --file-selection --title=Open"
PickerType.DIRECTORIES -> "zenity --file-selection --title=Open --directory" PickerType.DIRECTORIES -> "zenity --file-selection --title=Open --directory"
} }
val openDirectory = runCommand(command)?.replace("\n", "") val openDirectory = runCommand(command)?.replace("\n", "")
if (!openDirectory.isNullOrEmpty()) if (!openDirectory.isNullOrEmpty())
dirToOpen = openDirectory dirToOpen = openDirectory
} else } else
dirToOpen = openJvmDialog(pickerType, "", true) dirToOpen = openJvmDialog(pickerType, "", true)
} else {
dirToOpen = openJvmDialog(pickerType, "", false)
}
return dirToOpen return dirToOpen
} }

View File

@ -184,6 +184,7 @@ fun UiSettings(settingsViewModel: SettingsViewModel) {
mutableStateOf( mutableStateOf(
listOf( listOf(
ScaleDropDown(1f, "100%"), ScaleDropDown(1f, "100%"),
ScaleDropDown(1.25f, "125%"),
ScaleDropDown(1.5f, "150%"), ScaleDropDown(1.5f, "150%"),
ScaleDropDown(2f, "200%"), ScaleDropDown(2f, "200%"),
ScaleDropDown(2.5f, "250%"), ScaleDropDown(2.5f, "250%"),