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 {
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
}
}

View File

@ -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<KeybindingOption, List<Keybinding>> {
}
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 {

View File

@ -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
}

View File

@ -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%"),