From 2be50a906ce711f263788f8225e555d3f168504d Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Thu, 25 Jul 2024 19:01:10 -0700 Subject: [PATCH] switch back to working variant detection from 2.4.x --- app/app_xdg.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/app/app_xdg.go b/app/app_xdg.go index 2bf9327fbc..33780e5ec8 100644 --- a/app/app_xdg.go +++ b/app/app_xdg.go @@ -13,7 +13,6 @@ import ( "github.com/rymdport/portal/notification" "github.com/rymdport/portal/openuri" portalSettings "github.com/rymdport/portal/settings" - "github.com/rymdport/portal/settings/appearance" "fyne.io/fyne/v2" internalapp "fyne.io/fyne/v2/internal/app" @@ -37,15 +36,38 @@ func (a *fyneApp) OpenURL(url *url.URL) error { // fetch color variant from dbus portal desktop settings. func findFreedesktopColorScheme() fyne.ThemeVariant { - colourScheme, err := appearance.GetColorScheme() + dbusConn, err := dbus.SessionBus() if err != nil { + fyne.LogError("Unable to connect to session D-Bus", err) + return theme.VariantDark + } + + dbusObj := dbusConn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop") + call := dbusObj.Call( + "org.freedesktop.portal.Settings.Read", + dbus.FlagNoAutoStart, + "org.freedesktop.appearance", + "color-scheme", + ) + if call.Err != nil { + // many desktops don't have this exported yet + return theme.VariantDark + } + + var value uint8 + if err = call.Store(&value); err != nil { + fyne.LogError("failed to read theme variant from D-Bus", err) return theme.VariantDark } - switch colourScheme { - case appearance.Light: + // See: https://github.com/flatpak/xdg-desktop-portal/blob/1.16.0/data/org.freedesktop.impl.portal.Settings.xml#L32-L46 + // 0: No preference + // 1: Prefer dark appearance + // 2: Prefer light appearance + switch value { + case 2: return theme.VariantLight - case appearance.Dark: + case 1: return theme.VariantDark default: // Default to light theme to support Gnome's default see https://github.com/fyne-io/fyne/pull/3561 @@ -119,6 +141,8 @@ func watchTheme() { go func() { // with portal this may not be immediate, so we update a cache instead internalapp.CurrentVariant.Store(uint64(findFreedesktopColorScheme())) + // ensure initial variant setting is applied at startup + fyne.CurrentApp().Settings().(*settings).setupTheme() portalSettings.OnSignalSettingChanged(func(changed portalSettings.Changed) { if changed.Namespace == "org.freedesktop.appearance" && changed.Key == "color-scheme" {