fix(theme): read GNOME color-scheme setting for dark mode detection
- commit
- 3817fdf97cc8509c9507849e81bde9aa9eecba70
- author
- bnema · · 2025-10-23 20:57 UTC
- parent
- 752d9d6
- signature
- unsigned
1 file · +13 -2
M pkg/webkit/helpers.go +13 -2
| @@ -2,6 +2,7 @@ package webkit | |||
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | 4 | "github.com/diamondburned/gotk4/pkg/gdk/v4" | |
| 5 | + | "github.com/diamondburned/gotk4/pkg/gio/v2" | |
| 5 | 6 | "github.com/diamondburned/gotk4/pkg/glib/v2" | |
| 6 | 7 | "github.com/diamondburned/gotk4/pkg/gtk/v4" | |
| 7 | 8 | ) | |
| @@ -239,10 +240,20 @@ func WidgetAllocation(w gtk.Widgetter) (x, y, width, height int) { | |||
| 239 | 240 | ||
| 240 | 241 | // PrefersDarkTheme checks if the system prefers dark theme | |
| 241 | 242 | func PrefersDarkTheme() bool { | |
| 243 | + | // First try to read GNOME's color-scheme setting (GTK4+) | |
| 244 | + | if gnomeSettings := gio.NewSettings("org.gnome.desktop.interface"); gnomeSettings != nil { | |
| 245 | + | colorScheme := gnomeSettings.String("color-scheme") | |
| 246 | + | if colorScheme == "prefer-dark" { | |
| 247 | + | return true | |
| 248 | + | } | |
| 249 | + | if colorScheme == "prefer-light" { | |
| 250 | + | return false | |
| 251 | + | } | |
| 252 | + | } | |
| 253 | + | ||
| 254 | + | // Fallback to GTK's application-level preference | |
| 242 | 255 | settings := gtk.SettingsGetDefault() | |
| 243 | 256 | if settings != nil { | |
| 244 | - | // In GTK4, check the color scheme preference | |
| 245 | - | // The property is "gtk-application-prefer-dark-theme" | |
| 246 | 257 | obj := settings.Object | |
| 247 | 258 | if obj != nil { | |
| 248 | 259 | prop := obj.ObjectProperty("gtk-application-prefer-dark-theme") | |
| @@ -2,6 +2,7 @@ package webkit | |||||
| 2 | 2 | ||||
| 3 | import ( | 3 | import ( | ||
| 4 | "github.com/diamondburned/gotk4/pkg/gdk/v4" | 4 | "github.com/diamondburned/gotk4/pkg/gdk/v4" | ||
| 5 | + | "github.com/diamondburned/gotk4/pkg/gio/v2" | |||
| 5 | "github.com/diamondburned/gotk4/pkg/glib/v2" | 6 | "github.com/diamondburned/gotk4/pkg/glib/v2" | ||
| 6 | "github.com/diamondburned/gotk4/pkg/gtk/v4" | 7 | "github.com/diamondburned/gotk4/pkg/gtk/v4" | ||
| 7 | ) | 8 | ) | ||
| @@ -239,10 +240,20 @@ func WidgetAllocation(w gtk.Widgetter) (x, y, width, height int) { | |||||
| 239 | 240 | ||||
| 240 | // PrefersDarkTheme checks if the system prefers dark theme | 241 | // PrefersDarkTheme checks if the system prefers dark theme | ||
| 241 | func PrefersDarkTheme() bool { | 242 | func PrefersDarkTheme() bool { | ||
| 243 | + | // First try to read GNOME's color-scheme setting (GTK4+) | |||
| 244 | + | if gnomeSettings := gio.NewSettings("org.gnome.desktop.interface"); gnomeSettings != nil { | |||
| 245 | + | colorScheme := gnomeSettings.String("color-scheme") | |||
| 246 | + | if colorScheme == "prefer-dark" { | |||
| 247 | + | return true | |||
| 248 | + | } | |||
| 249 | + | if colorScheme == "prefer-light" { | |||
| 250 | + | return false | |||
| 251 | + | } | |||
| 252 | + | } | |||
| 253 | + | ||||
| 254 | + | // Fallback to GTK's application-level preference | |||
| 242 | settings := gtk.SettingsGetDefault() | 255 | settings := gtk.SettingsGetDefault() | ||
| 243 | if settings != nil { | 256 | if settings != nil { | ||
| 244 | - | // In GTK4, check the color scheme preference | |||
| 245 | - | // The property is "gtk-application-prefer-dark-theme" | |||
| 246 | obj := settings.Object | 257 | obj := settings.Object | ||
| 247 | if obj != nil { | 258 | if obj != nil { | ||
| 248 | prop := obj.ObjectProperty("gtk-application-prefer-dark-theme") | 259 | prop := obj.ObjectProperty("gtk-application-prefer-dark-theme") | ||