Page Menu
Home
GnuPG
Search
Configure Global Search
Log In
Files
F20064522
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Size
7 KB
Subscribers
None
View Options
diff --git a/patches/kiconthemes/dark-mode-detection.patch b/patches/kiconthemes/dark-mode-detection.patch
index bb46d7f7..5eb2bf7b 100755
--- a/patches/kiconthemes/dark-mode-detection.patch
+++ b/patches/kiconthemes/dark-mode-detection.patch
@@ -1,218 +1,218 @@
#! /bin/sh
patch -p1 -l -f $* < $0
exit $?
From bac91fa8ab819ebd4e9ceaf9eac7c1a8b2832929 Mon Sep 17 00:00:00 2001
From: Sune Vuorela <sune@vuorela.dk>
Date: Mon, 23 Oct 2023 09:48:51 +0200
Subject: [PATCH] Draft: Load binary icons based on windows theme
This adds detection code for high contrast / dark mode
to KIconTheme so that it can detect early on which theme
to load when binary icons are used and loaded from a file.
GnuPG-Bug-Id: T6076
---
src/CMakeLists.txt | 5 +++
src/DarkModeSetup.cpp | 98 +++++++++++++++++++++++++++++++++++++++++++
src/DarkModeSetup.h | 20 +++++++++
src/kicontheme.cpp | 28 ++++++++++++-
4 files changed, 150 insertions(+), 1 deletion(-)
create mode 100644 src/DarkModeSetup.cpp
create mode 100644 src/DarkModeSetup.h
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ce4ca1f..2772401 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -29,6 +29,11 @@ target_sources(KF5IconThemes PRIVATE
hicolor.qrc
)
+if (WIN32)
+ target_sources(KF5IconThemes PRIVATE
+ DarkModeSetup.cpp)
+endif()
+
# TODO what's that PRIVATE about above?
ki18n_wrap_ui(KF5IconThemes kicondialog.ui)
diff --git a/src/DarkModeSetup.cpp b/src/DarkModeSetup.cpp
new file mode 100644
index 0000000..341efc4
--- /dev/null
+++ b/src/DarkModeSetup.cpp
@@ -0,0 +1,99 @@
+/*
+ SPDX-FileCopyrightText: 2023 g10 Code GmbH
+ SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
+ SPDX-FileContributor: Andre Heinecke <aheinecke@gnupg.org>
+ SPDX-FileContributor: Sune Stolborg Vuorela <sune@vuorela.dk>
+
+ SPDX-License-Identifier: LGPL-2.0-or-later
+*/
+
+#include "DarkModeSetup.h"
+#include "debug.h"
+
-+#include <QColor>
+#include <QSettings>
+
+#include "windows.h"
+
+namespace
+{
+bool win_isHighContrastModeActive()
+{
+ HIGHCONTRAST result;
+ result.cbSize = sizeof(HIGHCONTRAST);
+ if (SystemParametersInfo(SPI_GETHIGHCONTRAST, result.cbSize, &result, 0)) {
+ return (result.dwFlags & HCF_HIGHCONTRASTON);
+ }
+ return false;
+}
+
+bool win_isDarkModeActive()
+{
+ if (win_isHighContrastModeActive()) {
-+ // First check for white background. That is set in High contrast white theme.
++ // First check for a light high contrast theme.
+ DWORD color = GetSysColor(COLOR_WINDOW);
-+ QColor qtColor{GetRValue(color), GetGValue(color), GetBValue(color)};
-+ if (qtColor == Qt::white) {
-+ qCDebug(KICONTHEMES) << "Detected light high-contrast mode";
++ // use relative luminance defined in Rec. 709 for HDTV as reasonable estimate of the perceived lightness of the background color
++ const double luminance709 = (0.2126 * GetRValue(color) + 0.7152 * GetGValue(color) + 0.0722 * GetBValue(color)) / 255.;
++ if (luminance709 > 0.5) {
++ qCDebug(KICONTHEMES) << "Detected light high-contrast mode; relative luminance =" << luminance709;
+ return false;
+ }
+ // The other three High Contrast modes are dark.
-+ qCDebug(KICONTHEMES) << "Detected dark high-contrast mode";
++ qCDebug(KICONTHEMES) << "Detected dark high-contrast mode; relative luminance =" << luminance709;
+ return true;
+ }
+
+ // This is what KColorschemeWatcher and Qt also look for to check for dark mode. I would use this as a fallback. Because it only works good
+ // for dark mode so the style and not the accessibility feature it is not enough for us. But having this as the default behavior is the
+ // best way to keep it all in line.
+ const QSettings regSettings{QStringLiteral("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"),
+ QSettings::NativeFormat};
+
+ // Nothing set -> default to bright. / Leave it to KColorSchemeManager.
+ const bool appsUseLightTheme = regSettings.value(QStringLiteral("AppsUseLightTheme"), true).value<bool>();
+ if (!appsUseLightTheme) {
+ qCDebug(KICONTHEMES) << "Bright icons for AppsUseLightTheme false";
+ return true;
+ }
+ // Default to no dark mode active
+ return false;
+}
+
+}
+
+bool DarkModeSetup::isDarkModeActive()
+{
+ return win_isDarkModeActive();
+}
+
+void DarkModeSetup::tellQt(int mode) {
+ int qtm = qtMode();
+ if (qtm > 0) {
+ qWarning("Qt darkmode already enabled");
+ return;
+ }
+ QByteArray arr = qgetenv("QT_QPA_PLATFORM");
+ if (!arr.isEmpty()) {
+ arr.append(',');
+ }
+ arr.append("windows:darkmode=" + QByteArray::number(mode));
+ qputenv("QT_QPA_PLATFORM", arr);
+}
+
+int DarkModeSetup::qtMode() {
+ const auto envvar = qgetenv("QT_QPA_PLATFORM");
+ if (envvar.isEmpty()) {
+ return 0;
+ }
+ const auto list = envvar.split(',');
+ for (const auto &element : list) {
+ if (element.startsWith("windows:darkmode=")) {
+ auto data = element.right(element.length() - 17);
+ bool success;
+ int number = data.toInt(&success);
+ if (success && number >= 0 && number <= 2) {
+ return number;
+ }
+ }
+ }
+ return 0;
+}
diff --git a/src/DarkModeSetup.h b/src/DarkModeSetup.h
new file mode 100644
index 0000000..87ecfd7
--- /dev/null
+++ b/src/DarkModeSetup.h
@@ -0,0 +1,20 @@
+/*
+ SPDX-FileCopyrightText: 2019 Richard Yu
+ SPDX-FileCopyrightText: 2023 g10 Code GmbH
+ SPDX-FileContributor: Sune Stolborg Vuorela <sune@vuorela.dk>
+ SPDX-License-Identifier: MIT
+*/
+
+
+class DarkModeSetup {
+public:
+ // Queries thru dark magic if dark mode is active or not using weird windows non-api's
+ // This is valid before QApplication has been created
+ bool isDarkModeActive();
+ // Tell Qt about our capabilities
+ // See https://doc.qt.io/qt-6/qguiapplication.html#platform-specific-arguments for details
+ // must be called before creating QApplication
+ void tellQt(int mode = 2);
+ // gets previously mode told qt
+ int qtMode();
+};
diff --git a/src/kicontheme.cpp b/src/kicontheme.cpp
index 4eee2ff..3b35b55 100644
--- a/src/kicontheme.cpp
+++ b/src/kicontheme.cpp
@@ -34,14 +34,40 @@
#include <array>
#include <cmath>
+#ifdef Q_OS_WIN
+#include "DarkModeSetup.h"
+#endif
+
Q_GLOBAL_STATIC(QString, _themeOverride)
+#ifdef Q_OS_WIN
+bool useDarkMode() {
+ DarkModeSetup s;
+ if (!s.isDarkModeActive()) {
+ return false;
+ }
+ return s.qtMode() == 2 || s.qtMode() == 1;
+}
+#else
+inline bool useDarkMode() {
+ return false;
+}
+#endif
+
+
+
// Support for icon themes in RCC files.
// The intended use case is standalone apps on Windows / MacOS / etc.
// For this reason we use AppDataLocation: BINDIR/data on Windows, Resources on OS X
void initRCCIconTheme()
{
- const QString iconThemeRcc = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icontheme.rcc"));
+ QString iconThemeRcc;
+ if (useDarkMode()) {
+ iconThemeRcc = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icontheme-dark.rcc"));
+ }
+ if (iconThemeRcc.isEmpty()) {
+ iconThemeRcc = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icontheme.rcc"));
+ }
if (!iconThemeRcc.isEmpty()) {
const QString iconThemeName = QStringLiteral("kf5_rcc_theme");
const QString iconSubdir = QStringLiteral("/icons/") + iconThemeName;
--
2.43.0
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sun, Feb 23, 7:17 PM (47 m, 57 s)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f9/a2/ba0600591479eed6b3a36df79207
Attached To
rW Gpg4win
Event Timeline
Log In to Comment