Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add KDE Plasma dark mode support #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,76 @@ private const val REGDWORD_TOKEN = "REG_DWORD"
private const val DARK_THEME_CMD = "$REGQUERY_UTIL\"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize\" /v AppsUseLightTheme"
private val DARK_THEME_PATTERN = Regex(".*dark.*", RegexOption.IGNORE_CASE)

fun isDarkMode() = when (operatingSystem) {
OperatingSystem.WINDOWS -> isWindowsDarkMode()
OperatingSystem.MACOS -> isMacOsDarkMode()
OperatingSystem.LINUX -> isGnome() && isGnomeDarkMode()
else -> false
}

private fun isMacOsDarkMode() = query("defaults read -g AppleInterfaceStyle") == "Dark"

private fun isWindowsDarkMode() = try {
query(DARK_THEME_CMD)
.substringAfter(REGDWORD_TOKEN)
.ifEmpty { null }
?.trim()
?.substring(2)
?.toInt(16) == 0
} catch (e: Exception) {
false
}

private fun isGnomeDarkMode() = query("gsettings get org.gnome.desktop.interface gtk-theme") matches DARK_THEME_PATTERN

private fun isGnome() = operatingSystem == OperatingSystem.LINUX && (
queryResultContains("echo \$XDG_CURRENT_DESKTOP", "gnome") ||
queryResultContains("echo \$XDG_DATA_DIRS | grep -Eo 'gnome'", "gnome") ||
queryResultContains("ps -e | grep -E -i \"gnome\"", "gnome")
)

private fun queryResultContains(cmd: String, subResult: String) = query(cmd).contains(subResult, ignoreCase = true)

private fun query(cmd: String) = try {
Runtime.getRuntime()
.exec(cmd)
.inputStream
.bufferedReader()
.use(BufferedReader::readText)
} catch (e: IOException) {
logger.error(e) { "Exception caught while querying the OS" }
""
fun isDarkMode(): Boolean {
return when (operatingSystem) {
OperatingSystem.WINDOWS -> isWindowsDarkMode()
OperatingSystem.MACOS -> isMacOsDarkMode()
OperatingSystem.LINUX -> (isGnome() && isGnomeDarkMode()) || (isKde() && isKdeDarkMode())
else -> false
}
}

private fun isMacOsDarkMode(): Boolean {
val result = query("defaults read -g AppleInterfaceStyle")
return result == "Dark"
}

private fun isWindowsDarkMode(): Boolean {
return try {
val result = query(DARK_THEME_CMD)
result.substringAfter(REGDWORD_TOKEN)
.ifEmpty { null }
?.trim()
?.substring(2)
?.toInt(16) == 0
} catch (e: Exception) {
false
}
}

private fun isGnomeDarkMode(): Boolean {
val result = query("gsettings get org.gnome.desktop.interface gtk-theme")
return result matches DARK_THEME_PATTERN
}

private fun isGnome(): Boolean {
return operatingSystem == OperatingSystem.LINUX && (
queryResultContains("echo \$XDG_CURRENT_DESKTOP", "gnome") ||
queryResultContains("echo \$XDG_DATA_DIRS | grep -Eo 'gnome'", "gnome") ||
queryResultContains("ps -e | grep -E -i \"gnome\"", "gnome")
)
}

private fun isKdeDarkMode(): Boolean {
val currentLookAndFeel = query("lookandfeeltool --current")
if (currentLookAndFeel.isEmpty()) {
val alternativeLookAndFeel = query("kreadconfig5 --group KDE --key LookAndFeelPackage")
return alternativeLookAndFeel.contains("dark", ignoreCase = true)
}
return currentLookAndFeel.contains("dark", ignoreCase = true)
}

private fun isKde(): Boolean {
return operatingSystem == OperatingSystem.LINUX && (
queryResultContains("echo \$XDG_CURRENT_DESKTOP", "KDE") ||
queryResultContains("echo \$XDG_DATA_DIRS | grep -Eo 'kde'", "kde") ||
queryResultContains("ps -e | grep -E -i \"kde\"", "kde")
)
}

private fun queryResultContains(cmd: String, subResult: String): Boolean {
return query(cmd).contains(subResult, ignoreCase = true)
}

private fun query(cmd: String): String {
return try {
Runtime.getRuntime()
.exec(cmd)
.inputStream
.bufferedReader()
.use(BufferedReader::readText)
} catch (e: IOException) {
logger.error { "Exception caught while querying the OS" }
""
}
}