From 50360ae6dc734c680b2bd863479d8b33d00dced3 Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 14 Mar 2024 21:19:11 +0100 Subject: [PATCH] Add an API to get the color scheme setting --- settings/color.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 settings/color.go diff --git a/settings/color.go b/settings/color.go new file mode 100644 index 0000000..51fecc2 --- /dev/null +++ b/settings/color.go @@ -0,0 +1,43 @@ +package settings + +import ( + "github.com/godbus/dbus/v5" + "github.com/rymdport/portal" +) + +const readOneCallPath = settingsCallPath + ".ReadOne" + +// ColorScheme is the type of color scheme preference that the user has set. +type ColorScheme uint8 + +const ( + NoPreference = ColorScheme(iota) // Indicates that no appearance preference was set. + Dark // Indicates that dark mode is preferred. + Light // Indicates that light mode is preferred. +) + +// GetColorScheme returns the currently set color scheme. +func GetColorScheme() (ColorScheme, error) { + dbusConn, err := dbus.SessionBus() + if err != nil { + return NoPreference, err + } + + dbusObj := dbusConn.Object(portal.ObjectName, portal.ObjectPath) + call := dbusObj.Call( + readOneCallPath, + dbus.FlagNoAutoStart, + "org.freedesktop.appearance", + "color-scheme", + ) + if call.Err != nil { + return NoPreference, err + } + + var value uint8 + if err = call.Store(&value); err != nil { + return NoPreference, err + } + + return ColorScheme(value), nil +}