Follow the desktop accent colour and colour scheme (XDG portal) (#3)
This commit was merged in pull request #3.
This commit is contained in:
1 parent
02b08cb471
commit
1746884744
16 files changed
+947
-12
No files matched your search
+62
@@ -286,6 +286,68 @@ pub fn load_css(css : String) -> Unit {
|
||||
ffi_load_css(cstr(css))
|
||||
}
|
||||
|
||||
///|
|
||||
extern "C" fn ffi_portal_watch(tag : Int32) -> Int32 = "mm_gtk_portal_watch"
|
||||
|
||||
///|
|
||||
extern "C" fn ffi_portal_accent(i : Int32) -> Double = "mm_gtk_portal_accent"
|
||||
|
||||
///|
|
||||
extern "C" fn ffi_portal_color_scheme() -> Int32 = "mm_gtk_portal_color_scheme"
|
||||
|
||||
///|
|
||||
#borrow(css)
|
||||
extern "C" fn ffi_set_accent_css(css : Bytes) = "mm_gtk_set_accent_css"
|
||||
|
||||
///|
|
||||
extern "C" fn ffi_prefers_dark() -> Int32 = "mm_gtk_prefers_dark"
|
||||
|
||||
///|
|
||||
extern "C" fn ffi_set_color_scheme(scheme : Int32) = "mm_gtk_set_color_scheme"
|
||||
|
||||
///|
|
||||
/// The desktop's appearance settings, from the XDG Desktop Portal.
|
||||
pub(all) struct Appearance {
|
||||
/// accent-color as reported (each channel -1 when unset).
|
||||
accent : (Double, Double, Double)
|
||||
/// color-scheme: 0 no preference, 1 dark, 2 light.
|
||||
color_scheme : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
/// Follow org.freedesktop.appearance (accent-color, color-scheme) through
|
||||
/// the portal's Settings interface: `f` runs once with the current values
|
||||
/// and again on every change. Never runs without a portal. False without a
|
||||
/// session bus.
|
||||
pub fn watch_appearance(f : (Appearance) -> Unit) -> Bool {
|
||||
let tag = register(fn(_) {
|
||||
f({
|
||||
accent: (ffi_portal_accent(0), ffi_portal_accent(1), ffi_portal_accent(2)),
|
||||
color_scheme: ffi_portal_color_scheme(),
|
||||
})
|
||||
})
|
||||
ffi_portal_watch(tag) != 0
|
||||
}
|
||||
|
||||
///|
|
||||
/// Replace the accent stylesheet (APPLICATION priority); "" removes it.
|
||||
pub fn set_accent_css(css : String) -> Unit {
|
||||
ffi_set_accent_css(cstr(css))
|
||||
}
|
||||
|
||||
///|
|
||||
/// Apply a color-scheme preference (0 none: back to the configured theme
|
||||
/// variant, 1 dark, 2 light).
|
||||
pub fn set_color_scheme(scheme : Int32) -> Unit {
|
||||
ffi_set_color_scheme(scheme)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Whether the theme is in its dark variant.
|
||||
pub fn prefers_dark() -> Bool {
|
||||
ffi_prefers_dark() != 0
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn window(
|
||||
title : String,
|
||||
|
||||
+190
@@ -165,6 +165,196 @@ MOONBIT_FFI_EXPORT void mm_gtk_load_css(moonbit_bytes_t css) {
|
||||
g_object_unref(p);
|
||||
}
|
||||
|
||||
/* ---- desktop appearance (XDG Desktop Portal Settings) -------------------
|
||||
*
|
||||
* Reads org.freedesktop.appearance accent-color ((ddd), sRGB 0..1; outside
|
||||
* that range means unset) and color-scheme (u: 0 none, 1 dark, 2 light) from
|
||||
* org.freedesktop.portal.Settings on the session bus, and follows
|
||||
* SettingChanged. Every change is reported to MoonBit as emit(tag, 0); it
|
||||
* then reads the values with mm_gtk_portal_accent / _color_scheme. Without
|
||||
* a session bus or portal nothing is ever reported: the theme stays as is. */
|
||||
|
||||
#define PORTAL_NAME "org.freedesktop.portal.Desktop"
|
||||
#define PORTAL_PATH "/org/freedesktop/portal/desktop"
|
||||
#define PORTAL_IFACE "org.freedesktop.portal.Settings"
|
||||
#define APPEARANCE_NS "org.freedesktop.appearance"
|
||||
|
||||
static GDBusConnection *g_portal_bus;
|
||||
static int32_t g_portal_tag;
|
||||
static double g_accent[3] = {-1, -1, -1};
|
||||
static int32_t g_color_scheme;
|
||||
static GtkCssProvider *g_accent_provider;
|
||||
|
||||
/* Store one appearance setting; 1 if it is one we track. */
|
||||
static int portal_store(const char *key, GVariant *value) {
|
||||
GVariant *v = g_variant_ref(value);
|
||||
while (g_variant_is_of_type(v, G_VARIANT_TYPE_VARIANT)) {
|
||||
GVariant *inner = g_variant_get_variant(v);
|
||||
g_variant_unref(v);
|
||||
v = inner;
|
||||
}
|
||||
int known = 1;
|
||||
if (strcmp(key, "accent-color") == 0 &&
|
||||
g_variant_is_of_type(v, G_VARIANT_TYPE("(ddd)")))
|
||||
g_variant_get(v, "(ddd)", &g_accent[0], &g_accent[1], &g_accent[2]);
|
||||
else if (strcmp(key, "color-scheme") == 0 &&
|
||||
g_variant_is_of_type(v, G_VARIANT_TYPE_UINT32))
|
||||
g_color_scheme = (int32_t)g_variant_get_uint32(v);
|
||||
else
|
||||
known = 0;
|
||||
g_variant_unref(v);
|
||||
return known;
|
||||
}
|
||||
|
||||
static void on_portal_setting_changed(GDBusConnection *c, const char *sender,
|
||||
const char *path, const char *iface,
|
||||
const char *signal, GVariant *params,
|
||||
gpointer d) {
|
||||
(void)c; (void)sender; (void)path; (void)iface; (void)signal; (void)d;
|
||||
const char *ns, *key;
|
||||
GVariant *value;
|
||||
if (!g_variant_is_of_type(params, G_VARIANT_TYPE("(ssv)")))
|
||||
return;
|
||||
g_variant_get(params, "(&s&sv)", &ns, &key, &value);
|
||||
int known = strcmp(ns, APPEARANCE_NS) == 0 && portal_store(key, value);
|
||||
g_variant_unref(value);
|
||||
if (known)
|
||||
emit(g_portal_tag, 0);
|
||||
}
|
||||
|
||||
static void on_portal_read_all(GObject *src, GAsyncResult *res, gpointer d) {
|
||||
(void)d;
|
||||
GError *err = NULL;
|
||||
GVariant *ret = g_dbus_connection_call_finish(G_DBUS_CONNECTION(src), res, &err);
|
||||
if (!ret) {
|
||||
/* No portal (or no Settings interface): keep the theme's accent. */
|
||||
g_debug("moonmix: no desktop portal settings: %s", err->message);
|
||||
g_error_free(err);
|
||||
return;
|
||||
}
|
||||
GVariantIter *namespaces;
|
||||
const char *ns;
|
||||
GVariant *entries;
|
||||
g_variant_get(ret, "(a{sa{sv}})", &namespaces);
|
||||
while (g_variant_iter_next(namespaces, "{&s@a{sv}}", &ns, &entries)) {
|
||||
if (strcmp(ns, APPEARANCE_NS) == 0) {
|
||||
GVariantIter it;
|
||||
const char *key;
|
||||
GVariant *value;
|
||||
g_variant_iter_init(&it, entries);
|
||||
while (g_variant_iter_next(&it, "{&sv}", &key, &value)) {
|
||||
portal_store(key, value);
|
||||
g_variant_unref(value);
|
||||
}
|
||||
}
|
||||
g_variant_unref(entries);
|
||||
}
|
||||
g_variant_iter_free(namespaces);
|
||||
g_variant_unref(ret);
|
||||
emit(g_portal_tag, 0);
|
||||
}
|
||||
|
||||
/* Start following the portal's appearance settings. 0 without a session
|
||||
* bus. */
|
||||
MOONBIT_FFI_EXPORT int32_t mm_gtk_portal_watch(int32_t tag) {
|
||||
g_portal_tag = tag;
|
||||
if (g_portal_bus)
|
||||
return 1;
|
||||
GError *err = NULL;
|
||||
g_portal_bus = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &err);
|
||||
if (!g_portal_bus) {
|
||||
g_debug("moonmix: no session bus: %s", err->message);
|
||||
g_error_free(err);
|
||||
return 0;
|
||||
}
|
||||
/* Subscribe first so no change between the read and the subscription is
|
||||
* lost. */
|
||||
g_dbus_connection_signal_subscribe(
|
||||
g_portal_bus, PORTAL_NAME, PORTAL_IFACE, "SettingChanged", PORTAL_PATH,
|
||||
APPEARANCE_NS, G_DBUS_SIGNAL_FLAGS_NONE, on_portal_setting_changed, NULL,
|
||||
NULL);
|
||||
const char *namespaces[] = {APPEARANCE_NS, NULL};
|
||||
g_dbus_connection_call(
|
||||
g_portal_bus, PORTAL_NAME, PORTAL_PATH, PORTAL_IFACE, "ReadAll",
|
||||
g_variant_new("(^as)", namespaces), G_VARIANT_TYPE("(a{sa{sv}})"),
|
||||
G_DBUS_CALL_FLAGS_NONE, 5000, NULL, on_portal_read_all, NULL);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Channel i (0 r, 1 g, 2 b) of the portal accent colour; -1 when unset. */
|
||||
MOONBIT_FFI_EXPORT double mm_gtk_portal_accent(int32_t i) {
|
||||
return i >= 0 && i < 3 ? g_accent[i] : -1;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT int32_t mm_gtk_portal_color_scheme(void) {
|
||||
return g_color_scheme;
|
||||
}
|
||||
|
||||
/* Replace the accent stylesheet ("" removes it, restoring the theme's). */
|
||||
MOONBIT_FFI_EXPORT void mm_gtk_set_accent_css(moonbit_bytes_t css) {
|
||||
GdkDisplay *d = gdk_display_get_default();
|
||||
if (!d)
|
||||
return;
|
||||
if (!*s(css)) {
|
||||
if (g_accent_provider) {
|
||||
gtk_style_context_remove_provider_for_display(
|
||||
d, GTK_STYLE_PROVIDER(g_accent_provider));
|
||||
g_clear_object(&g_accent_provider);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!g_accent_provider) {
|
||||
g_accent_provider = gtk_css_provider_new();
|
||||
gtk_style_context_add_provider_for_display(
|
||||
d, GTK_STYLE_PROVIDER(g_accent_provider),
|
||||
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
|
||||
}
|
||||
gtk_css_provider_load_from_string(g_accent_provider, s(css));
|
||||
}
|
||||
|
||||
/* 1 if the theme is in its dark variant now. */
|
||||
MOONBIT_FFI_EXPORT int32_t mm_gtk_prefers_dark(void) {
|
||||
GtkSettings *st = gtk_settings_get_default();
|
||||
gboolean dark = FALSE;
|
||||
if (st)
|
||||
g_object_get(st, "gtk-application-prefer-dark-theme", &dark, NULL);
|
||||
return dark;
|
||||
}
|
||||
|
||||
/* Apply the color-scheme preference: 1 dark, 2 light, else whatever was set
|
||||
* before moonmix first touched it (settings.ini, GTK_THEME...). */
|
||||
MOONBIT_FFI_EXPORT void mm_gtk_set_color_scheme(int32_t scheme) {
|
||||
static int saved = -1;
|
||||
#if GTK_CHECK_VERSION(4, 20, 0)
|
||||
static int saved_scheme = -1;
|
||||
#endif
|
||||
GtkSettings *st = gtk_settings_get_default();
|
||||
if (!st)
|
||||
return;
|
||||
if (saved < 0) {
|
||||
gboolean dark = FALSE;
|
||||
g_object_get(st, "gtk-application-prefer-dark-theme", &dark, NULL);
|
||||
saved = dark;
|
||||
}
|
||||
gboolean dark = scheme == 1 ? TRUE : scheme == 2 ? FALSE : saved;
|
||||
g_object_set(st, "gtk-application-prefer-dark-theme", dark, NULL);
|
||||
#if GTK_CHECK_VERSION(4, 20, 0)
|
||||
/* Also drives CSS prefers-color-scheme on GTK 4.20+. */
|
||||
if (g_object_class_find_property(G_OBJECT_GET_CLASS(st),
|
||||
"gtk-interface-color-scheme")) {
|
||||
int cur;
|
||||
g_object_get(st, "gtk-interface-color-scheme", &cur, NULL);
|
||||
if (saved_scheme < 0)
|
||||
saved_scheme = cur;
|
||||
int want = scheme == 1 ? GTK_INTERFACE_COLOR_SCHEME_DARK
|
||||
: scheme == 2 ? GTK_INTERFACE_COLOR_SCHEME_LIGHT
|
||||
: saved_scheme;
|
||||
if (want != cur)
|
||||
g_object_set(st, "gtk-interface-color-scheme", want, NULL);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ---- window ------------------------------------------------------------- */
|
||||
|
||||
static gboolean on_close(GtkWindow *w, gpointer d) {
|
||||
|
||||
@@ -24,6 +24,8 @@ pub fn notebook(on_switch? : (Int32) -> Unit) -> Widget
|
||||
|
||||
pub fn now_us() -> Int64
|
||||
|
||||
pub fn prefers_dark() -> Bool
|
||||
|
||||
pub fn quit() -> Unit
|
||||
|
||||
pub fn resolve_icon(Array[String]) -> String
|
||||
@@ -36,6 +38,10 @@ pub fn scrolled(Widget) -> Widget
|
||||
|
||||
pub fn separator() -> Widget
|
||||
|
||||
pub fn set_accent_css(String) -> Unit
|
||||
|
||||
pub fn set_color_scheme(Int32) -> Unit
|
||||
|
||||
pub fn timeout(Int32, () -> Unit) -> Unit
|
||||
|
||||
pub fn toggle(String, String, (Bool) -> Unit) -> (Widget, Int32)
|
||||
@@ -46,11 +52,18 @@ pub fn updating(() -> Unit) -> Unit
|
||||
|
||||
pub fn vbox(Int32) -> Widget
|
||||
|
||||
pub fn watch_appearance((Appearance) -> Unit) -> Bool
|
||||
|
||||
pub fn window(String, Int32, Int32, on_close? : () -> Unit) -> Widget
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
pub(all) struct Appearance {
|
||||
accent : (Double, Double, Double)
|
||||
color_scheme : Int32
|
||||
}
|
||||
|
||||
#external
|
||||
pub type Widget
|
||||
pub fn Widget::add_css_class(Self, String) -> Unit
|
||||
|
||||
Reference in new issue
Block a user