-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_theme_linux.go
More file actions
68 lines (61 loc) · 1.67 KB
/
Copy pathsystem_theme_linux.go
File metadata and controls
68 lines (61 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//go:build linux
package main
import (
"os/exec"
"strings"
)
// IsSystemDark reports whether the desktop environment prefers a dark theme.
// WebKitGTK on Linux does not reliably expose this via prefers-color-scheme, so
// the frontend asks the backend, which probes GNOME/KDE settings.
func (a *App) IsSystemDark() bool {
if dark, ok := gnomeColorScheme(); ok {
return dark
}
if dark, ok := gnomeGtkTheme(); ok {
return dark
}
if dark, ok := kdeColorScheme(); ok {
return dark
}
return false
}
func gsettingsGet(schema, key string) (string, bool) {
out, err := exec.Command("gsettings", "get", schema, key).Output()
if err != nil {
return "", false
}
return strings.TrimSpace(string(out)), true
}
func gnomeColorScheme() (bool, bool) {
val, ok := gsettingsGet("org.gnome.desktop.interface", "color-scheme")
if !ok {
return false, false
}
switch val {
case "'prefer-dark'":
return true, true
case "'prefer-light'", "'default'":
return false, true
default:
return false, false
}
}
func gnomeGtkTheme() (bool, bool) {
val, ok := gsettingsGet("org.gnome.desktop.interface", "gtk-theme")
if !ok {
return false, false
}
theme := strings.ToLower(strings.Trim(val, "'"))
return strings.Contains(theme, "dark"), true
}
func kdeColorScheme() (bool, bool) {
out, err := exec.Command("kreadconfig6", "--file", "kdeglobals", "--group", "General", "--key", "ColorScheme").Output()
if err != nil {
out, err = exec.Command("kreadconfig5", "--file", "kdeglobals", "--group", "General", "--key", "ColorScheme").Output()
if err != nil {
return false, false
}
}
scheme := strings.ToLower(strings.TrimSpace(string(out)))
return strings.Contains(scheme, "dark"), true
}