Daniel Brötzmann pushed to branch master at gajim / gajim


Commits:
b4182c9c by André at 2022-06-06T10:01:14+00:00
feat: Add freedesktop colorscheme preference support

- - - - -


2 changed files:

- + gajim/common/dbus/system_style.py
- gajim/gtk/css_config.py


Changes:

=====================================
gajim/common/dbus/system_style.py
=====================================
@@ -0,0 +1,88 @@
+# This file is part of Gajim.
+#
+# Gajim is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published
+# by the Free Software Foundation; version 3 only.
+#
+# Gajim is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import annotations
+from typing import cast
+
+import sys
+import logging
+
+from gi.repository import Gio
+from gi.repository import GLib
+
+from gajim.common import app
+from gajim.common.events import StyleChanged
+
+log = logging.getLogger('gajim.c.dbus.system_style')
+
+
+class SystemStyleListener:
+    def __init__(self, callback) -> None:
+        self._prefer_dark = None
+        self._callback = callback
+
+        if sys.platform in ('win32', 'darwin'):
+            return
+
+        try:
+            self.dbus_proxy = Gio.DBusProxy.new_for_bus_sync(
+                Gio.BusType.SESSION,
+                Gio.DBusProxyFlags.NONE,
+                None,
+                'org.freedesktop.portal.Desktop',
+                '/org/freedesktop/portal/desktop',
+                'org.freedesktop.portal.Settings',
+                None
+            )
+        except GLib.Error as error:
+            log.info('Settings portal not found: %s', error)
+            return
+
+        self.dbus_proxy.connect('g-signal', self._signal_setting_changed)
+        self.read_color_scheme()
+
+    def read_color_scheme(self) -> None:
+        try:
+            result = self.dbus_proxy.call_sync(
+                'Read',
+                GLib.Variant('(ss)', ('org.freedesktop.appearance',
+                                       'color-scheme')),
+                Gio.DBusCallFlags.NO_AUTO_START,
+                -1,
+                None)
+            (self._prefer_dark,) = cast(tuple[bool], result)
+        except GLib.Error as error:
+            log.error("Couldn't read the color-scheme setting: %s",
+                      error.message)
+            return
+
+    def _signal_setting_changed(self,
+                                   _proxy,
+                                   _sender_name,
+                                   signal_name,
+                                   parameters,
+                                   *_user_data) -> None:
+        if signal_name != 'SettingChanged':
+            return
+
+        namespace, name, value = parameters
+        if (namespace == "org.freedesktop.appearance" and
+            name == "color-scheme"):
+            self._prefer_dark = (value == 1)
+            self._callback()
+            app.ged.raise_event(StyleChanged())
+
+    @property
+    def prefer_dark(self) -> bool:
+        return self._prefer_dark


=====================================
gajim/gtk/css_config.py
=====================================
@@ -32,6 +32,7 @@
 from gajim.common import app
 from gajim.common import configpaths
 from gajim.common.const import StyleAttr, CSSPriority
+from gajim.common.dbus.system_style import SystemStyleListener
 
 from .const import Theme
 
@@ -106,6 +107,8 @@ def __init__(self) -> None:
         # Holds all currently available themes
         self.themes: list[str] = []
 
+        self._system_style = SystemStyleListener(callback=self.set_dark_theme)
+
         self.set_dark_theme()
         self._load_css()
         self._gather_available_themes()
@@ -122,6 +125,8 @@ def __init__(self) -> None:
     def prefer_dark(self) -> bool:
         setting = app.settings.get('dark_theme')
         if setting == Theme.SYSTEM:
+            if self._system_style.prefer_dark is not None:
+                return self._system_style.prefer_dark
             if settings is None:
                 return False
             return settings.get_property('gtk-application-prefer-dark-theme')
@@ -136,8 +141,10 @@ def set_dark_theme(self, value: Optional[int] = None) -> 
None:
         if settings is None:
             return
         if value == Theme.SYSTEM:
-            settings.reset_property('gtk-application-prefer-dark-theme')
-            return
+            if self._system_style.prefer_dark is None:
+                settings.reset_property('gtk-application-prefer-dark-theme')
+                return
+            value = self._system_style.prefer_dark
         settings.set_property('gtk-application-prefer-dark-theme', bool(value))
         self._load_css()
 



View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/b4182c9c14f88feb065d12566135b0d19d981221

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/gajim/-/commit/b4182c9c14f88feb065d12566135b0d19d981221
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to