5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-12 06:59:30 +08:00

[v3 linux] use dbus for monitoring theme changes

This commit is contained in:
Travis McLane 2023-11-03 16:36:02 -05:00
parent 1a90b45f18
commit a773da2651
2 changed files with 53 additions and 6 deletions

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/godbus/dbus/v5"
"github.com/wailsapp/wails/v3/internal/operatingsystem" "github.com/wailsapp/wails/v3/internal/operatingsystem"
"github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/events"
) )
@ -29,6 +30,8 @@ type linuxApp struct {
// Native -> uint // Native -> uint
windows map[windowPointer]uint windows map[windowPointer]uint
windowsLock sync.Mutex windowsLock sync.Mutex
theme string
} }
func (m *linuxApp) GetFlags(options Options) map[string]any { func (m *linuxApp) GetFlags(options Options) map[string]any {
@ -52,7 +55,7 @@ func (m *linuxApp) show() {
func (m *linuxApp) on(eventID uint) { func (m *linuxApp) on(eventID uint) {
// TODO: What do we need to do here? // TODO: What do we need to do here?
// log.Println("linuxApp.on()", eventID) log.Println("linuxApp.on()", eventID)
} }
func (m *linuxApp) setIcon(icon []byte) { func (m *linuxApp) setIcon(icon []byte) {
@ -108,7 +111,7 @@ func (m *linuxApp) run() error {
// Do we need to do anything now? // Do we need to do anything now?
fmt.Println("events.Mac.ApplicationDidFinishLaunching received!") fmt.Println("events.Mac.ApplicationDidFinishLaunching received!")
}) })
m.monitorThemeChanges()
return appRun(m.application) return appRun(m.application)
} }
@ -128,9 +131,52 @@ func (m *linuxApp) registerWindow(window pointer, id uint) {
} }
func (m *linuxApp) isDarkMode() bool { func (m *linuxApp) isDarkMode() bool {
// FIXME: How do we detect this? return strings.Contains(m.theme, "dark")
// Maybe this helps: https://askubuntu.com/questions/1469869/how-does-firefox-detect-light-dark-theme-change-on-kde-systems }
return false
func (m *linuxApp) monitorThemeChanges() {
go func() {
conn, err := dbus.ConnectSessionBus()
if err != nil {
m.parent.info("[WARNING] Failed to connect to session bus; monitoring for theme changes will not function:", err)
return
}
defer conn.Close()
if err = conn.AddMatchSignal(
dbus.WithMatchObjectPath("/org/freedesktop/portal/desktop"),
); err != nil {
panic(err)
}
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
getTheme := func(body []interface{}) (string, bool) {
if body[0].(string) != "org.gnome.desktop.interface" {
return "", false
}
if body[1].(string) == "color-scheme" {
return body[2].(dbus.Variant).Value().(string), true
}
return "", false
}
for v := range c {
theme, ok := getTheme(v.Body)
if !ok {
continue
}
if theme != m.theme {
m.theme = theme
event := newApplicationEvent(events.Common.ThemeChanged)
event.Context().setIsDarkMode(m.isDarkMode())
applicationEvents <- event
}
}
}()
} }
func newPlatformApp(parent *App) *linuxApp { func newPlatformApp(parent *App) *linuxApp {

View File

@ -5,6 +5,7 @@ import (
"sync" "sync"
"github.com/samber/lo" "github.com/samber/lo"
"github.com/wailsapp/wails/v3/pkg/events"
) )
type Event struct { type Event struct {
@ -17,7 +18,7 @@ func (w *Event) Context() *ApplicationEventContext {
return w.ctx return w.ctx
} }
func newApplicationEvent(id int) *Event { func newApplicationEvent(id events.ApplicationEventType) *Event {
return &Event{ return &Event{
Id: uint(id), Id: uint(id),
ctx: newApplicationEventContext(), ctx: newApplicationEventContext(),