Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dev.gajim.org/gajim/gajim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichel Le Bihan <michel@lebihan.pl>2023-04-21 18:03:41 +0300
committerPhilipp Hörist <philipp@hoerist.com>2023-04-21 18:03:41 +0300
commit99dad790f42d6b191621042a29c7b592d5ce5c4d (patch)
tree10ccb3e889dcfa362757635e042105fb333331c8
parent5f77a2545226bc9d0e0782e44744af3cc54358c7 (diff)
feat: Detect dark theme on Windows
-rw-r--r--README.md1
-rw-r--r--gajim/common/winapi/__init__.py0
-rw-r--r--gajim/common/winapi/system_style.py60
-rw-r--r--gajim/gtk/css_config.py6
-rw-r--r--pyproject.toml1
-rwxr-xr-xwin/_base.sh1
-rwxr-xr-xwin/dev_env.sh1
7 files changed, 69 insertions, 1 deletions
diff --git a/README.md b/README.md
index 6a9afbeeb..93f2c580d 100644
--- a/README.md
+++ b/README.md
@@ -23,6 +23,7 @@
- [sqlite](https://www.sqlite.org/) (>=3.33.0)
- [omemo-dr](https://dev.gajim.org/gajim/omemo-dr) (>=1.0.0)
- [qrcode](https://pypi.org/project/qrcode/) (>=7.3.1)
+- [winsdk](https://pypi.org/project/winsdk/) (Only on Windows)
### Optional Runtime Requirements
diff --git a/gajim/common/winapi/__init__.py b/gajim/common/winapi/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/gajim/common/winapi/__init__.py
diff --git a/gajim/common/winapi/system_style.py b/gajim/common/winapi/system_style.py
new file mode 100644
index 000000000..704dc98d4
--- /dev/null
+++ b/gajim/common/winapi/system_style.py
@@ -0,0 +1,60 @@
+# 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/>.
+
+# Based on https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes
+
+from __future__ import annotations
+
+from typing import Callable
+from typing import Optional
+
+import logging
+
+from winsdk.windows.ui import Color
+from winsdk.windows.ui.viewmanagement import UIColorType
+from winsdk.windows.ui.viewmanagement import UISettings
+
+from gajim.common import app
+from gajim.common.events import StyleChanged
+
+log = logging.getLogger('gajim.c.winapi.system_style')
+
+
+class SystemStyleListener:
+ def __init__(self, callback: Callable[..., None]) -> None:
+ self._prefer_dark: Optional[bool] = None
+ self._callback = callback
+
+ self._ui_settings = UISettings()
+ self._ui_settings.add_color_values_changed(
+ self._signal_color_values_changed)
+ foreground_color = self._ui_settings.get_color_value(
+ UIColorType.FOREGROUND)
+ self._prefer_dark = self._is_color_light(foreground_color)
+
+ @staticmethod
+ def _is_color_light(clr: Color) -> bool:
+ return ((5 * clr.g) + (2 * clr.r) + clr.b) > (8 * 128)
+
+ def _signal_color_values_changed(self, ui_settings: UISettings, _) -> None:
+ foreground_color = ui_settings.get_color_value(UIColorType.FOREGROUND)
+ dark_theme = self._is_color_light(foreground_color)
+ if dark_theme != self._prefer_dark:
+ self._prefer_dark = dark_theme
+ self._callback()
+ app.ged.raise_event(StyleChanged())
+
+ @property
+ def prefer_dark(self) -> Optional[bool]:
+ return self._prefer_dark
diff --git a/gajim/gtk/css_config.py b/gajim/gtk/css_config.py
index fe27be51e..a83b9d273 100644
--- a/gajim/gtk/css_config.py
+++ b/gajim/gtk/css_config.py
@@ -36,7 +36,11 @@ from gajim.common import app
from gajim.common import configpaths
from gajim.common.const import CSSPriority
from gajim.common.const import StyleAttr
-from gajim.common.dbus.system_style import SystemStyleListener
+
+if sys.platform == 'win32':
+ from gajim.common.winapi.system_style import SystemStyleListener
+else:
+ from gajim.common.dbus.system_style import SystemStyleListener
from gajim.gtk.const import Theme
diff --git a/pyproject.toml b/pyproject.toml
index 553b08ba9..4545e450b 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -36,6 +36,7 @@ dependencies = [
"PyGObject>=3.42.0",
"qrcode>=7.3.1",
"omemo-dr>=0.99.0,<2.0.0",
+ "winsdk ; platform_system == 'Windows'",
]
dynamic = ["version"]
diff --git a/win/_base.sh b/win/_base.sh
index 69ce39056..5302c4ef3 100755
--- a/win/_base.sh
+++ b/win/_base.sh
@@ -81,6 +81,7 @@ function install_deps {
"${MINGW_PACKAGE_PREFIX}"-python-setuptools \
"${MINGW_PACKAGE_PREFIX}"-python-setuptools-scm \
"${MINGW_PACKAGE_PREFIX}"-python-six \
+ "${MINGW_PACKAGE_PREFIX}"-python-winsdk \
"${MINGW_PACKAGE_PREFIX}"-gtk3 \
"${MINGW_PACKAGE_PREFIX}"-gtksourceview4 \
"${MINGW_PACKAGE_PREFIX}"-gstreamer \
diff --git a/win/dev_env.sh b/win/dev_env.sh
index 66aa6c873..2d352cd0e 100755
--- a/win/dev_env.sh
+++ b/win/dev_env.sh
@@ -24,6 +24,7 @@ function main {
${MINGW_PACKAGE_PREFIX}-python-setuptools \
${MINGW_PACKAGE_PREFIX}-python-setuptools-scm \
${MINGW_PACKAGE_PREFIX}-python-six \
+ ${MINGW_PACKAGE_PREFIX}-python-winsdk \
${MINGW_PACKAGE_PREFIX}-gtk3 \
${MINGW_PACKAGE_PREFIX}-gtksourceview4 \
${MINGW_PACKAGE_PREFIX}-gstreamer \