From 6420c56cea8c177b2faa6283bb5cabd2f1c06305 Mon Sep 17 00:00:00 2001 From: lenemter Date: Sun, 8 Jun 2025 11:13:21 +0300 Subject: [PATCH] Cache dynamic accent color in account settings and use it in Portal --- ...mentary.SettingsDaemon.AccountsService.xml | 4 + settings-portal/Settings.vala | 28 ++++++- src/AccountsService.vala | 1 + src/Application.vala | 2 +- src/Backends/AccentColorManager.vala | 73 ++++++++++++------- 5 files changed, 76 insertions(+), 32 deletions(-) diff --git a/data/io.elementary.SettingsDaemon.AccountsService.xml b/data/io.elementary.SettingsDaemon.AccountsService.xml index 16dcd644..0b926827 100644 --- a/data/io.elementary.SettingsDaemon.AccountsService.xml +++ b/data/io.elementary.SettingsDaemon.AccountsService.xml @@ -84,6 +84,10 @@ + + + + diff --git a/settings-portal/Settings.vala b/settings-portal/Settings.vala index ba154b93..db57959c 100644 --- a/settings-portal/Settings.vala +++ b/settings-portal/Settings.vala @@ -35,15 +35,20 @@ private interface Pantheon.AccountsService : Object { public abstract int prefers_accent_color { owned get; set; } } +[DBus (name = "io.elementary.SettingsDaemon.AccountsService")] +private interface SettingsDaemon.AccountsService : Object { + public abstract int accent_color { get; set; } +} + [DBus (name = "org.freedesktop.Accounts")] interface FDO.Accounts : Object { public abstract string find_user_by_name (string username) throws GLib.Error; } -/* Copied from Granite.Settings */ private class AccountsServiceMonitor : GLib.Object { private FDO.Accounts? accounts_service = null; private Pantheon.AccountsService? pantheon_act = null; + private SettingsDaemon.AccountsService? settings_daemon_act = null; private string user_path; public int32 color_scheme { get; set; } @@ -52,6 +57,7 @@ private class AccountsServiceMonitor : GLib.Object { construct { setup_user_path (); setup_prefers_color_scheme (); + setup_prefers_accent_color (); } private void setup_user_path () { @@ -78,15 +84,31 @@ private class AccountsServiceMonitor : GLib.Object { ); color_scheme = pantheon_act.prefers_color_scheme; - accent_color = pantheon_act.prefers_accent_color; ((GLib.DBusProxy) pantheon_act).g_properties_changed.connect ((changed, invalid) => { var value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i")); if (value != null) { color_scheme = value.get_int32 (); } + }); + } catch (Error e) { + critical (e.message); + } + } + + private void setup_prefers_accent_color () { + try { + settings_daemon_act = GLib.Bus.get_proxy_sync ( + GLib.BusType.SYSTEM, + "org.freedesktop.Accounts", + user_path, + GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES + ); + + accent_color = settings_daemon_act.accent_color; - value = changed.lookup_value ("PrefersAccentColor", new VariantType ("i")); + ((GLib.DBusProxy) settings_daemon_act).g_properties_changed.connect ((changed, invalid) => { + var value = changed.lookup_value ("AccentColor", new VariantType ("i")); if (value != null) { accent_color = value.get_int32 (); } diff --git a/src/AccountsService.vala b/src/AccountsService.vala index 79196bc4..7f845516 100644 --- a/src/AccountsService.vala +++ b/src/AccountsService.vala @@ -56,6 +56,7 @@ public interface SettingsDaemon.AccountsService : Object { public abstract bool touchpad_two_finger_scrolling { get; set; } /* Interface */ + public abstract int accent_color { get; set; } public abstract bool cursor_blink { get; set; } public abstract int cursor_blink_time { get; set; } public abstract int cursor_blink_timeout { get; set; } diff --git a/src/Application.vala b/src/Application.vala index ae9f8542..3e9de932 100644 --- a/src/Application.vala +++ b/src/Application.vala @@ -125,7 +125,7 @@ public sealed class SettingsDaemon.Application : Gtk.Application { try { pantheon_service = yield connection.get_proxy (FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES); prefers_color_scheme_settings = new Backends.PrefersColorSchemeSettings (pantheon_service); - accent_color_manager = new Backends.AccentColorManager (pantheon_service); + accent_color_manager = new Backends.AccentColorManager (pantheon_service, accounts_service); } catch { warning ("Unable to get pantheon's AccountsService proxy, color scheme preference may be incorrect"); } diff --git a/src/Backends/AccentColorManager.vala b/src/Backends/AccentColorManager.vala index a5d329d3..d2bf45d4 100644 --- a/src/Backends/AccentColorManager.vala +++ b/src/Backends/AccentColorManager.vala @@ -5,7 +5,8 @@ */ public class SettingsDaemon.Backends.AccentColorManager : Object { - public unowned Pantheon.AccountsService accounts_service { get; construct; } + public unowned Pantheon.AccountsService pantheon_accounts_service { get; construct; } + public unowned AccountsService accounts_service { get; construct; } private Settings background_settings; private Settings interface_settings; @@ -15,6 +16,7 @@ public class SettingsDaemon.Backends.AccentColorManager : Object { } private struct Theme { + int index; string name; string stylesheet; Gdk.RGBA color; @@ -30,56 +32,74 @@ public class SettingsDaemon.Backends.AccentColorManager : Object { } private static Theme[] themes = { - { "Blue", "io.elementary.stylesheet.blueberry", rgba_from_int (0x3689e6) }, // vala-lint=double-spaces - { "Mint", "io.elementary.stylesheet.mint", rgba_from_int (0x28bca3) }, // vala-lint=double-spaces - { "Green", "io.elementary.stylesheet.lime", rgba_from_int (0x68b723) }, // vala-lint=double-spaces - { "Yellow", "io.elementary.stylesheet.banana", rgba_from_int (0xf9c440) }, // vala-lint=double-spaces - { "Orange", "io.elementary.stylesheet.orange", rgba_from_int (0xffa154) }, // vala-lint=double-spaces - { "Red", "io.elementary.stylesheet.strawberry", rgba_from_int (0xed5353) }, // vala-lint=double-spaces - { "Pink", "io.elementary.stylesheet.bubblegum", rgba_from_int (0xde3e80) }, // vala-lint=double-spaces - { "Purple", "io.elementary.stylesheet.grape", rgba_from_int (0xa56de2) }, // vala-lint=double-spaces - { "Brown", "io.elementary.stylesheet.cocoa", rgba_from_int (0x8a715e) }, // vala-lint=double-spaces - { "Gray", "io.elementary.stylesheet.slate", rgba_from_int (0x667885) } // vala-lint=double-spaces + { 1, "Red", "io.elementary.stylesheet.strawberry", rgba_from_int (0xed5353) }, // vala-lint=double-spaces + { 2, "Orange", "io.elementary.stylesheet.orange", rgba_from_int (0xffa154) }, // vala-lint=double-spaces + { 3, "Yellow", "io.elementary.stylesheet.banana", rgba_from_int (0xf9c440) }, // vala-lint=double-spaces + { 4, "Green", "io.elementary.stylesheet.lime", rgba_from_int (0x68b723) }, // vala-lint=double-spaces + { 5, "Mint", "io.elementary.stylesheet.mint", rgba_from_int (0x28bca3) }, // vala-lint=double-spaces + { 6, "Blue", "io.elementary.stylesheet.blueberry", rgba_from_int (0x3689e6) }, // vala-lint=double-spaces + { 7, "Purple", "io.elementary.stylesheet.grape", rgba_from_int (0xa56de2) }, // vala-lint=double-spaces + { 8, "Pink", "io.elementary.stylesheet.bubblegum", rgba_from_int (0xde3e80) }, // vala-lint=double-spaces + { 9, "Brown", "io.elementary.stylesheet.cocoa", rgba_from_int (0x8a715e) }, // vala-lint=double-spaces + { 10, "Gray", "io.elementary.stylesheet.slate", rgba_from_int (0x667885) } // vala-lint=double-spaces }; - public AccentColorManager (Pantheon.AccountsService accounts_service) { - Object (accounts_service: accounts_service); + public AccentColorManager (Pantheon.AccountsService pantheon_accounts_service, AccountsService accounts_service) { + Object ( + pantheon_accounts_service: pantheon_accounts_service, + accounts_service: accounts_service + ); } construct { background_settings = new Settings ("org.gnome.desktop.background"); interface_settings = new Settings ("org.gnome.desktop.interface"); - ((DBusProxy) accounts_service).g_properties_changed.connect ((props) => { - int accent_color; + update_accent_color (); + if (pantheon_accounts_service.prefers_accent_color == 0) { + background_settings.changed["picture-options"].connect (update_accent_color); + background_settings.changed["picture-uri"].connect (update_accent_color); + background_settings.changed["primary-color"].connect (update_accent_color); + } + ((DBusProxy) pantheon_accounts_service).g_properties_changed.connect ((props) => { + int accent_color; if (!props.lookup ("PrefersAccentColor", "i", out accent_color)) { return; }; + update_accent_color (); if (accent_color == 0) { background_settings.changed["picture-options"].connect (update_accent_color); background_settings.changed["picture-uri"].connect (update_accent_color); background_settings.changed["primary-color"].connect (update_accent_color); - update_accent_color (); } else { background_settings.changed["picture-options"].disconnect (update_accent_color); background_settings.changed["picture-uri"].disconnect (update_accent_color); background_settings.changed["primary-color"].disconnect (update_accent_color); } }); + } - if (accounts_service.prefers_accent_color == 0) { - background_settings.changed["picture-options"].connect (update_accent_color); - background_settings.changed["picture-uri"].connect (update_accent_color); - background_settings.changed["primary-color"].connect (update_accent_color); - update_accent_color (); + private void update_accent_color () { + Theme? new_theme = null; + var prefers_accent_color = pantheon_accounts_service.prefers_accent_color; + if (prefers_accent_color == 0) { + new_theme = get_dynamic_accent_color_theme_name (); + } else if (prefers_accent_color < themes.length + 1) { + new_theme = themes[prefers_accent_color - 1]; + } else { + critical ("Incorrect accent color in pantheon accounts service"); + return; } + + interface_settings.set_string ("gtk-theme", new_theme.stylesheet); + debug ("New stylesheet: %s", new_theme.stylesheet); + + accounts_service.accent_color = new_theme.index; } - private void update_accent_color () { - var current_stylesheet = interface_settings.get_string ("gtk-theme"); - debug ("Current stylesheet: %s", current_stylesheet); + private Theme get_dynamic_accent_color_theme_name () { Theme? new_theme = null; if (background_settings.get_enum ("picture-options") != BackgroundStyle.NONE) { @@ -95,10 +115,7 @@ public class SettingsDaemon.Backends.AccentColorManager : Object { new_theme = get_theme_for_primary_color (primary_color); } - if (new_theme.stylesheet != current_stylesheet) { - debug ("New stylesheet: %s", new_theme.stylesheet); - interface_settings.set_string ("gtk-theme", new_theme.stylesheet); - } + return new_theme; } private Theme? get_theme_for_primary_color (string primary_color) {