File 003_keepassxc_trayicon.patch of Package keepassxc

From 18393787d094e1f4da576aed33dd3d43a980d50d Mon Sep 17 00:00:00 2001
From: Thomas Fischer <fischer@unix-ag.uni-kl.de>
Date: Fri, 24 Oct 2025 21:03:00 +0200
Subject: [PATCH] Adding support for theme-aware tray icon in KDE/Plasma

If a KDE/Plasma session is detected, offer to use a monochrome but theme-aware icon
for the system tray. Here, 'theme-aware' means that the SVG icon is augmented so
that the KDE SVG renderer can replace the main color to fit the current theme, e.g.
use a light color if the theme is dark.
---
 ...keepassxc-monochrome-kde-plasma-locked.svg |  7 +++++
 .../apps/keepassxc-monochrome-kde-plasma.svg  |  7 +++++
 share/icons/icons.qrc                         |  2 ++
 src/gui/ApplicationSettingsWidget.cpp         |  6 +++++
 src/gui/Icons.cpp                             | 27 +++++++++++++++++++
 5 files changed, 49 insertions(+)
 create mode 100644 share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg
 create mode 100644 share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma.svg

diff --git a/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg
new file mode 100644
index 0000000000..03660c8904
--- /dev/null
+++ b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?><svg id="b" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128">
+<style id="current-color-scheme" type="text/css">
+ .ColorScheme-Text {
+  color:#0f0f0d;
+ }
+</style>
+<g id="c" class="ColorScheme-Text" fill="currentColor"><rect width="128" height="128" style="fill:none; stroke-width:0px;"/><path d="m64,28.4885c4.8287,0,8.7679-3.9401,8.7679-8.7698,0-.6355-.1271-1.2709-.2542-1.9065-2.7955-.5084-5.5911-.8896-8.5138-.7625-2.7955,0-5.7182.2542-8.5137.7625-.1271.6356-.2542,1.271-.2542,1.9065,0,4.8297,3.9392,8.7698,8.7679,8.7698Z" style="stroke-width:0px;"/><path d="m86.6088,58.0631c-1.8394,1.1351-2.9459,3.1661-2.9459,5.3275v8.3641h16.38v-7c0-6.0711-6.6846-10.8567-13.4341-6.6917Z" style="stroke-width:0px;"/><path d="m53.4531,50.7306c-8.3867-3.9401-14.1049-12.4556-14.1049-22.2421,0-1.6523.2542-3.1775.5083-4.8298-14.2319,8.5156-22.8728,23.7673-22.8728,40.29,0,24.1918,18.1885,43.989,41.5388,46.6983v-10.2347l-5.0693-5.0704v-44.6113Z" style="stroke-width:0px;"/><path d="m123.8043,75.4549c.7097-3.7126,1.0956-7.5386,1.0956-11.4548,0-33.5805-27.3193-60.9-60.8999-60.9S3.0999,30.4196,3.0999,64.0001s27.3196,60.9,60.9001,60.9c5.0994,0,10.0512-.6386,14.7881-1.8249l-9.0723.0002c-.3044,0-.6034-.0223-.9017-.0463.2983.024.5974.0457.9018.0457h44.1338c6.1719,0,11.1934-5.0205,11.1934-11.1924v-30.8877c0-1.9575-.4447-3.8377-1.2388-5.5397Zm-66.7931-18.8778v35.2061h1.5113v-10.7879c0-.3262.0132-.65.0375-.9714-.024.3212-.037.6448-.037.9707v30.8877c0,3.0512,1.2302,5.8181,3.2169,7.8391-.001-.001-.002-.0018-.003-.0028-29.6951-1.1935-53.4883-25.723-53.4883-55.7184,0-30.755,25.0099-55.7759,55.7514-55.7759s55.7512,25.0209,55.7512,55.7759c0,2.0822-.1242,4.1353-.3475,6.1589-.7296-.5236-1.5109-.9811-2.3406-1.3529v-2.791c0-8.027-3.7652-15.1869-9.6173-19.8212-3.8016-9.2543-10.4838-17.2112-19.3022-22.5352.3812,1.6523.5083,3.1774.5083,4.8297,0,4.8487-1.4382,9.3832-3.8906,13.2061.0031-.0009.0061-.0013.0092-.0022-1.7772.4825-3.5391,1.1374-5.2742,1.9848-7.8931,3.8555-12.9932,12.1621-12.9932,21.1621v3.9668c-2.4653,1.1048-4.5251,2.9335-5.937,5.1815.0011-.0017.002-.0035.0031-.0052v-17.4053h-3.5579Zm64.0317,55.3047c0,3.9725-3.2204,7.1929-7.193,7.1929h-44.134c-3.9726,0-7.193-3.2203-7.193-7.1929v-30.887c0-4.62,3.5-8.54,7.98-9.24v-6.9156c0-7.4088,4.0921-14.3167,10.7493-17.5682,16.0264-7.8276,31.8107,3.6011,31.8107,18.7438v5.74c4.48.7,7.98,4.62,7.98,9.24v30.887Z" style="stroke-width:0px;"/></g></svg>
diff --git a/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma.svg b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma.svg
new file mode 100644
index 0000000000..8f10aa398f
--- /dev/null
+++ b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma.svg
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?><svg id="b" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 128 128">
+<style id="current-color-scheme" type="text/css">
+ .ColorScheme-Text {
+  color:#0f0f0d;
+ }
+</style>
+<g id="c" class="ColorScheme-Text" fill="currentColor"><rect width="128" height="128" style="fill:none; stroke-width:0px;"/><g id="d"><rect x="57.0112" y="56.5771" width="3.558" height="35.2061" style="stroke-width:0px;"/><path d="m64,28.4885c4.8287,0,8.7679-3.94,8.7679-8.7698,0-.6355-.1271-1.2709-.2542-1.9064-2.7956-.5084-5.5911-.8897-8.5138-.7626-2.7956,0-5.7182.2542-8.5138.7626-.1271.6355-.2542,1.271-.2542,1.9064,0,4.8297,3.9392,8.7698,8.7679,8.7698Z" style="stroke-width:0px;"/><path d="m88.1436,23.6587c.3812,1.6523.5083,3.1775.5083,4.8297,0,9.7865-5.8453,18.3021-14.1049,22.2421l-.6354,10.422,7.4972,7.4988-7.4972,7.4988,4.9558,4.9568-4.9558,4.9568.6354,9.2781-10.5469,10.5491-10.5469-10.5491v-44.6113c-8.3867-3.94-14.1049-12.4556-14.1049-22.2421,0-1.6523.2541-3.1775.5083-4.8297-14.2319,8.5155-22.8728,23.7673-22.8728,40.29,0,26.055,21.0938,47.0261,47.0163,47.0261,26.0496,0,47.0163-21.0982,47.0163-47.0261-.1271-16.5227-8.7679-31.7744-22.8728-40.29Z" style="stroke-width:0px;"/><path d="m64,3.1001C30.4195,3.1001,3.0999,30.4196,3.0999,64s27.3196,60.9,60.9001,60.9,60.8999-27.3198,60.8999-60.9S97.5806,3.1001,64,3.1001Zm0,116.6759c-30.7415,0-55.7514-25.0212-55.7514-55.7759S33.2586,8.2241,64,8.2241s55.7512,25.0209,55.7512,55.7759-25.0097,55.7759-55.7512,55.7759Z" style="stroke-width:0px;"/></g></g></svg>
diff --git a/share/icons/icons.qrc b/share/icons/icons.qrc
index 3e82e172d5..fe08a0f5b1 100644
--- a/share/icons/icons.qrc
+++ b/share/icons/icons.qrc
@@ -107,6 +107,8 @@
         <file>application/scalable/apps/keepassxc-monochrome-dark-locked.svg</file>
         <file>application/scalable/apps/keepassxc-monochrome-light.svg</file>
         <file>application/scalable/apps/keepassxc-monochrome-light-locked.svg</file>
+        <file>application/scalable/apps/keepassxc-monochrome-kde-plasma.svg</file>
+        <file>application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg</file>
         <file>application/scalable/apps/keepassxc-locked.svg</file>
         <file>application/scalable/apps/keepassxc-unlocked.svg</file>
         <file>application/scalable/apps/preferences-desktop-icons.svg</file>
diff --git a/src/gui/ApplicationSettingsWidget.cpp b/src/gui/ApplicationSettingsWidget.cpp
index c7bea73f3d..bd70de3054 100644
--- a/src/gui/ApplicationSettingsWidget.cpp
+++ b/src/gui/ApplicationSettingsWidget.cpp
@@ -311,6 +311,12 @@ void ApplicationSettingsWidget::loadSettings()
 #else
     m_generalUi->trayIconAppearance->addItem(tr("Monochrome (light)"), "monochrome-light");
     m_generalUi->trayIconAppearance->addItem(tr("Monochrome (dark)"), "monochrome-dark");
+#ifdef Q_OS_LINUX
+    if (qEnvironmentVariable("XDG_CURRENT_DESKTOP").split(';').contains("KDE") || qEnvironmentVariable("XDG_SESSION_DESKTOP") == "KDE") {
+        // Most likely inside a KDE/Plasma session, so add option to use theme-aware icon
+        m_generalUi->trayIconAppearance->addItem(tr("Monochrome (theme-aware)"), "monochrome-kde-plasma");
+    }
+#endif
 #endif
     m_generalUi->trayIconAppearance->addItem(tr("Colorful"), "colorful");
     int trayIconIndex = m_generalUi->trayIconAppearance->findData(icons()->trayIconAppearance());
diff --git a/src/gui/Icons.cpp b/src/gui/Icons.cpp
index 7cccf406a8..19e1bf3bad 100644
--- a/src/gui/Icons.cpp
+++ b/src/gui/Icons.cpp
@@ -69,11 +69,38 @@ QIcon Icons::applicationIcon()
 QString Icons::trayIconAppearance() const
 {
     auto iconAppearance = config()->get(Config::GUI_TrayIconAppearance).toString();
+
+    // If loaded an icon appearance that was set to 'monochrome-kde-plasma',
+    // but the current session is not KDE/Plasma, clear the icon appearance setting
+    // and let the auto detecting a few lines later determine a new appearance
+#ifndef Q_OS_LINUX
+    // Any operating system but Linux
+    if (iconAppearance == "monochrome-kde-plasma") {
+        iconAppearance.clear();
+    }
+#else
+    if (!qEnvironmentVariable("XDG_CURRENT_DESKTOP").split(';').contains("KDE") && qEnvironmentVariable("XDG_SESSION_DESKTOP") != "KDE"
+        && iconAppearance == "monochrome-kde-plasma"
+    ) {
+        // If running Linux but no KDE/Plasma session
+        iconAppearance.clear();
+    }
+#endif
+
     if (iconAppearance.isNull()) {
 #ifdef Q_OS_MACOS
         iconAppearance = osUtils->isDarkMode() ? "monochrome-light" : "monochrome-dark";
+#else
+#ifdef Q_OS_LINUX
+        if (qEnvironmentVariable("XDG_CURRENT_DESKTOP").split(';').contains("KDE") || qEnvironmentVariable("XDG_SESSION_DESKTOP") == "KDE") {
+            // Educated guess that KeePassXC runs in a KDE/Plasma session, use the theme-aware icon
+            iconAppearance = "monochrome-kde-plasma";
+        } else {
+            iconAppearance = "monochrome-light";
+        }
 #else
         iconAppearance = "monochrome-light";
+#endif
 #endif
     }
     return iconAppearance;
openSUSE Build Service is sponsored by