Skip to content

Commit 31f265d

Browse files
authored
Cache dynamic accent color in account settings and use it in Portal (#192)
1 parent cb7fb56 commit 31f265d

5 files changed

Lines changed: 76 additions & 32 deletions

File tree

data/io.elementary.SettingsDaemon.AccountsService.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
<annotation name="org.freedesktop.Accounts.DefaultValue" value="true"/>
8585
</property>
8686

87+
<property name="AccentColor" type="i" access="readwrite">
88+
<annotation name="org.freedesktop.Accounts.DefaultValue" value="1"/>
89+
</property>
90+
8791
<property name="CursorBlink" type="b" access="readwrite">
8892
<annotation name="org.freedesktop.Accounts.DefaultValue" value="true"/>
8993
</property>

settings-portal/Settings.vala

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ private interface Pantheon.AccountsService : Object {
3535
public abstract int prefers_accent_color { owned get; set; }
3636
}
3737

38+
[DBus (name = "io.elementary.SettingsDaemon.AccountsService")]
39+
private interface SettingsDaemon.AccountsService : Object {
40+
public abstract int accent_color { get; set; }
41+
}
42+
3843
[DBus (name = "org.freedesktop.Accounts")]
3944
interface FDO.Accounts : Object {
4045
public abstract string find_user_by_name (string username) throws GLib.Error;
4146
}
4247

43-
/* Copied from Granite.Settings */
4448
private class AccountsServiceMonitor : GLib.Object {
4549
private FDO.Accounts? accounts_service = null;
4650
private Pantheon.AccountsService? pantheon_act = null;
51+
private SettingsDaemon.AccountsService? settings_daemon_act = null;
4752
private string user_path;
4853

4954
public int32 color_scheme { get; set; }
@@ -52,6 +57,7 @@ private class AccountsServiceMonitor : GLib.Object {
5257
construct {
5358
setup_user_path ();
5459
setup_prefers_color_scheme ();
60+
setup_prefers_accent_color ();
5561
}
5662

5763
private void setup_user_path () {
@@ -78,15 +84,31 @@ private class AccountsServiceMonitor : GLib.Object {
7884
);
7985

8086
color_scheme = pantheon_act.prefers_color_scheme;
81-
accent_color = pantheon_act.prefers_accent_color;
8287

8388
((GLib.DBusProxy) pantheon_act).g_properties_changed.connect ((changed, invalid) => {
8489
var value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i"));
8590
if (value != null) {
8691
color_scheme = value.get_int32 ();
8792
}
93+
});
94+
} catch (Error e) {
95+
critical (e.message);
96+
}
97+
}
98+
99+
private void setup_prefers_accent_color () {
100+
try {
101+
settings_daemon_act = GLib.Bus.get_proxy_sync (
102+
GLib.BusType.SYSTEM,
103+
"org.freedesktop.Accounts",
104+
user_path,
105+
GLib.DBusProxyFlags.GET_INVALIDATED_PROPERTIES
106+
);
107+
108+
accent_color = settings_daemon_act.accent_color;
88109

89-
value = changed.lookup_value ("PrefersAccentColor", new VariantType ("i"));
110+
((GLib.DBusProxy) settings_daemon_act).g_properties_changed.connect ((changed, invalid) => {
111+
var value = changed.lookup_value ("AccentColor", new VariantType ("i"));
90112
if (value != null) {
91113
accent_color = value.get_int32 ();
92114
}

src/AccountsService.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public interface SettingsDaemon.AccountsService : Object {
5656
public abstract bool touchpad_two_finger_scrolling { get; set; }
5757

5858
/* Interface */
59+
public abstract int accent_color { get; set; }
5960
public abstract bool cursor_blink { get; set; }
6061
public abstract int cursor_blink_time { get; set; }
6162
public abstract int cursor_blink_timeout { get; set; }

src/Application.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public sealed class SettingsDaemon.Application : Gtk.Application {
125125
try {
126126
pantheon_service = yield connection.get_proxy (FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES);
127127
prefers_color_scheme_settings = new Backends.PrefersColorSchemeSettings (pantheon_service);
128-
accent_color_manager = new Backends.AccentColorManager (pantheon_service);
128+
accent_color_manager = new Backends.AccentColorManager (pantheon_service, accounts_service);
129129
} catch {
130130
warning ("Unable to get pantheon's AccountsService proxy, color scheme preference may be incorrect");
131131
}

src/Backends/AccentColorManager.vala

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66

77
public class SettingsDaemon.Backends.AccentColorManager : Object {
8-
public unowned Pantheon.AccountsService accounts_service { get; construct; }
8+
public unowned Pantheon.AccountsService pantheon_accounts_service { get; construct; }
9+
public unowned AccountsService accounts_service { get; construct; }
910

1011
private Settings background_settings;
1112
private Settings interface_settings;
@@ -15,6 +16,7 @@ public class SettingsDaemon.Backends.AccentColorManager : Object {
1516
}
1617

1718
private struct Theme {
19+
int index;
1820
string name;
1921
string stylesheet;
2022
Gdk.RGBA color;
@@ -30,56 +32,74 @@ public class SettingsDaemon.Backends.AccentColorManager : Object {
3032
}
3133

3234
private static Theme[] themes = {
33-
{ "Blue", "io.elementary.stylesheet.blueberry", rgba_from_int (0x3689e6) }, // vala-lint=double-spaces
34-
{ "Mint", "io.elementary.stylesheet.mint", rgba_from_int (0x28bca3) }, // vala-lint=double-spaces
35-
{ "Green", "io.elementary.stylesheet.lime", rgba_from_int (0x68b723) }, // vala-lint=double-spaces
36-
{ "Yellow", "io.elementary.stylesheet.banana", rgba_from_int (0xf9c440) }, // vala-lint=double-spaces
37-
{ "Orange", "io.elementary.stylesheet.orange", rgba_from_int (0xffa154) }, // vala-lint=double-spaces
38-
{ "Red", "io.elementary.stylesheet.strawberry", rgba_from_int (0xed5353) }, // vala-lint=double-spaces
39-
{ "Pink", "io.elementary.stylesheet.bubblegum", rgba_from_int (0xde3e80) }, // vala-lint=double-spaces
40-
{ "Purple", "io.elementary.stylesheet.grape", rgba_from_int (0xa56de2) }, // vala-lint=double-spaces
41-
{ "Brown", "io.elementary.stylesheet.cocoa", rgba_from_int (0x8a715e) }, // vala-lint=double-spaces
42-
{ "Gray", "io.elementary.stylesheet.slate", rgba_from_int (0x667885) } // vala-lint=double-spaces
35+
{ 1, "Red", "io.elementary.stylesheet.strawberry", rgba_from_int (0xed5353) }, // vala-lint=double-spaces
36+
{ 2, "Orange", "io.elementary.stylesheet.orange", rgba_from_int (0xffa154) }, // vala-lint=double-spaces
37+
{ 3, "Yellow", "io.elementary.stylesheet.banana", rgba_from_int (0xf9c440) }, // vala-lint=double-spaces
38+
{ 4, "Green", "io.elementary.stylesheet.lime", rgba_from_int (0x68b723) }, // vala-lint=double-spaces
39+
{ 5, "Mint", "io.elementary.stylesheet.mint", rgba_from_int (0x28bca3) }, // vala-lint=double-spaces
40+
{ 6, "Blue", "io.elementary.stylesheet.blueberry", rgba_from_int (0x3689e6) }, // vala-lint=double-spaces
41+
{ 7, "Purple", "io.elementary.stylesheet.grape", rgba_from_int (0xa56de2) }, // vala-lint=double-spaces
42+
{ 8, "Pink", "io.elementary.stylesheet.bubblegum", rgba_from_int (0xde3e80) }, // vala-lint=double-spaces
43+
{ 9, "Brown", "io.elementary.stylesheet.cocoa", rgba_from_int (0x8a715e) }, // vala-lint=double-spaces
44+
{ 10, "Gray", "io.elementary.stylesheet.slate", rgba_from_int (0x667885) } // vala-lint=double-spaces
4345
};
4446

45-
public AccentColorManager (Pantheon.AccountsService accounts_service) {
46-
Object (accounts_service: accounts_service);
47+
public AccentColorManager (Pantheon.AccountsService pantheon_accounts_service, AccountsService accounts_service) {
48+
Object (
49+
pantheon_accounts_service: pantheon_accounts_service,
50+
accounts_service: accounts_service
51+
);
4752
}
4853

4954
construct {
5055
background_settings = new Settings ("org.gnome.desktop.background");
5156
interface_settings = new Settings ("org.gnome.desktop.interface");
5257

53-
((DBusProxy) accounts_service).g_properties_changed.connect ((props) => {
54-
int accent_color;
58+
update_accent_color ();
59+
if (pantheon_accounts_service.prefers_accent_color == 0) {
60+
background_settings.changed["picture-options"].connect (update_accent_color);
61+
background_settings.changed["picture-uri"].connect (update_accent_color);
62+
background_settings.changed["primary-color"].connect (update_accent_color);
63+
}
5564

65+
((DBusProxy) pantheon_accounts_service).g_properties_changed.connect ((props) => {
66+
int accent_color;
5667
if (!props.lookup ("PrefersAccentColor", "i", out accent_color)) {
5768
return;
5869
};
5970

71+
update_accent_color ();
6072
if (accent_color == 0) {
6173
background_settings.changed["picture-options"].connect (update_accent_color);
6274
background_settings.changed["picture-uri"].connect (update_accent_color);
6375
background_settings.changed["primary-color"].connect (update_accent_color);
64-
update_accent_color ();
6576
} else {
6677
background_settings.changed["picture-options"].disconnect (update_accent_color);
6778
background_settings.changed["picture-uri"].disconnect (update_accent_color);
6879
background_settings.changed["primary-color"].disconnect (update_accent_color);
6980
}
7081
});
82+
}
7183

72-
if (accounts_service.prefers_accent_color == 0) {
73-
background_settings.changed["picture-options"].connect (update_accent_color);
74-
background_settings.changed["picture-uri"].connect (update_accent_color);
75-
background_settings.changed["primary-color"].connect (update_accent_color);
76-
update_accent_color ();
84+
private void update_accent_color () {
85+
Theme? new_theme = null;
86+
var prefers_accent_color = pantheon_accounts_service.prefers_accent_color;
87+
if (prefers_accent_color == 0) {
88+
new_theme = get_dynamic_accent_color_theme_name ();
89+
} else if (prefers_accent_color < themes.length + 1) {
90+
new_theme = themes[prefers_accent_color - 1];
91+
} else {
92+
critical ("Incorrect accent color in pantheon accounts service");
93+
return;
7794
}
95+
96+
interface_settings.set_string ("gtk-theme", new_theme.stylesheet);
97+
debug ("New stylesheet: %s", new_theme.stylesheet);
98+
99+
accounts_service.accent_color = new_theme.index;
78100
}
79101

80-
private void update_accent_color () {
81-
var current_stylesheet = interface_settings.get_string ("gtk-theme");
82-
debug ("Current stylesheet: %s", current_stylesheet);
102+
private Theme get_dynamic_accent_color_theme_name () {
83103
Theme? new_theme = null;
84104

85105
if (background_settings.get_enum ("picture-options") != BackgroundStyle.NONE) {
@@ -95,10 +115,7 @@ public class SettingsDaemon.Backends.AccentColorManager : Object {
95115
new_theme = get_theme_for_primary_color (primary_color);
96116
}
97117

98-
if (new_theme.stylesheet != current_stylesheet) {
99-
debug ("New stylesheet: %s", new_theme.stylesheet);
100-
interface_settings.set_string ("gtk-theme", new_theme.stylesheet);
101-
}
118+
return new_theme;
102119
}
103120

104121
private Theme? get_theme_for_primary_color (string primary_color) {

0 commit comments

Comments
 (0)