-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an API to get the color scheme setting
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |