Skip to content

Commit

Permalink
fixed the crash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyzev committed Jun 13, 2024
1 parent 7847679 commit a592e8a
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/main/kotlin/dev/lyzev/api/theme/OSTheme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package dev.lyzev.api.theme
import com.sun.jna.platform.win32.*
import com.sun.jna.platform.win32.WinReg.HKEYByReference
import dev.lyzev.api.events.*
import kotlinx.coroutines.Job
import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.concurrent.thread
Expand Down Expand Up @@ -48,11 +47,7 @@ object WindowsTheme : OSTheme {
while (true) {
val handle = Kernel32.INSTANCE.CreateEvent(null, true, false, null)
Advapi32.INSTANCE.RegNotifyChangeKeyValue(
keyRef.value,
true,
WinNT.REG_NOTIFY_CHANGE_LAST_SET or WinNT.REG_NOTIFY_CHANGE_NAME,
handle,
true
keyRef.value, true, WinNT.REG_NOTIFY_CHANGE_LAST_SET or WinNT.REG_NOTIFY_CHANGE_NAME, handle, true
)
val res = Kernel32.INSTANCE.WaitForSingleObject(handle, 1000)
if (res == WinBase.WAIT_OBJECT_0 && !isShuttingDown) {
Expand Down Expand Up @@ -93,14 +88,20 @@ object WindowsTheme : OSTheme {
// MacOS implementation
object MacOSTheme : OSTheme {

private var job: Job? = null
private var cantFetch = false
private var lastTheme = getCurrentTheme()

override fun getCurrentTheme(): OSTheme.Theme {
val process = ProcessBuilder("defaults", "read", "-g", "AppleInterfaceStyle").start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val theme = reader.readLine()
return if (theme == "Dark") OSTheme.Theme.Dark else OSTheme.Theme.Light
if (cantFetch) return OSTheme.Theme.Dark
try {
val process = ProcessBuilder("defaults", "read", "-g", "AppleInterfaceStyle").start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val theme = reader.readLine()
return if (theme == "Dark") OSTheme.Theme.Dark else OSTheme.Theme.Light
} catch (e: Exception) {
cantFetch = true
return OSTheme.Theme.Dark
}
}

override fun startListenForUpdatesThread() {
Expand All @@ -117,14 +118,20 @@ object MacOSTheme : OSTheme {
// Linux implementation
object LinuxTheme : OSTheme {

private var job: Job? = null
private var lastTheme = MacOSTheme.getCurrentTheme()
private var cantFetch = false
private var lastTheme = getCurrentTheme()

override fun getCurrentTheme(): OSTheme.Theme {
val process = ProcessBuilder("gsettings", "get", "org.gnome.desktop.interface", "gtk-theme").start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val theme = reader.readLine()
return if (theme.contains("dark", ignoreCase = true)) OSTheme.Theme.Dark else OSTheme.Theme.Light
if (cantFetch) return OSTheme.Theme.Dark
try {
val process = ProcessBuilder("gsettings", "get", "org.gnome.desktop.interface", "gtk-theme").start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val theme = reader.readLine()
return if (theme.contains("dark", ignoreCase = true)) OSTheme.Theme.Dark else OSTheme.Theme.Light
} catch (e: Exception) {
cantFetch = true
return OSTheme.Theme.Dark
}
}

override fun startListenForUpdatesThread() {
Expand Down

0 comments on commit a592e8a

Please sign in to comment.