|
4 | 4 | */ |
5 | 5 |
|
6 | 6 | namespace Gala.Background.Utils { |
| 7 | + public enum ColorScheme { |
| 8 | + NO_PREFERENCE, |
| 9 | + DARK, |
| 10 | + LIGHT |
| 11 | + } |
| 12 | + |
| 13 | + public delegate void OnStyleChange (ColorScheme new_style); |
| 14 | + |
| 15 | + [DBus (name = "org.freedesktop.Accounts")] |
| 16 | + private interface Accounts : Object { |
| 17 | + public abstract string find_user_by_name (string name) throws IOError, DBusError; |
| 18 | + } |
| 19 | + |
| 20 | + [DBus (name = "io.elementary.pantheon.AccountsService")] |
| 21 | + private interface AccountsService : DBusProxy { |
| 22 | + public abstract int prefers_color_scheme { get; set; } |
| 23 | + public abstract int prefers_accent_color { get; set; } |
| 24 | + } |
| 25 | + |
| 26 | + private const string FDO_ACCOUNTS_NAME = "org.freedesktop.Accounts"; |
| 27 | + private const string FDO_ACCOUNTS_PATH = "/org/freedesktop/Accounts"; |
| 28 | + |
| 29 | + private static AccountsService? accounts_service_proxy; |
| 30 | + |
| 31 | + public static void init_color_scheme_watcher (OnStyleChange style_change_callback) { |
| 32 | + try { |
| 33 | + var accounts = Bus.get_proxy_sync<Accounts> (SYSTEM, FDO_ACCOUNTS_NAME, FDO_ACCOUNTS_PATH); |
| 34 | + |
| 35 | + var path = accounts.find_user_by_name (Environment.get_user_name ()); |
| 36 | + |
| 37 | + accounts_service_proxy = Bus.get_proxy_sync<AccountsService> (SYSTEM, FDO_ACCOUNTS_NAME, path, GET_INVALIDATED_PROPERTIES); |
| 38 | + } catch { |
| 39 | + warning ("Could not connect to AccountsService. Default style will be used"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + accounts_service_proxy.g_properties_changed.connect ((changed, invalid) => { |
| 44 | + var value = changed.lookup_value ("PrefersColorScheme", new VariantType ("i")); |
| 45 | + if (value != null) { |
| 46 | + style_change_callback (value.get_int32 ()); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + public static ColorScheme get_color_scheme () { |
| 52 | + if (accounts_service_proxy != null) { |
| 53 | + return accounts_service_proxy.prefers_color_scheme; |
| 54 | + } |
| 55 | + |
| 56 | + return LIGHT; |
| 57 | + } |
| 58 | + |
7 | 59 | private const double SATURATION_WEIGHT = 1.5; |
8 | 60 | private const double WEIGHT_THRESHOLD = 1.0; |
9 | 61 |
|
|
0 commit comments