From 18393787d094e1f4da576aed33dd3d43a980d50d Mon Sep 17 00:00:00 2001 From: Thomas Fischer 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 000000000..03660c890 --- /dev/null +++ b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg @@ -0,0 +1,7 @@ + + + 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 000000000..8f10aa398 --- /dev/null +++ b/share/icons/application/scalable/apps/keepassxc-monochrome-kde-plasma.svg @@ -0,0 +1,7 @@ + + + diff --git a/share/icons/icons.qrc b/share/icons/icons.qrc index 3e82e172d..fe08a0f5b 100644 --- a/share/icons/icons.qrc +++ b/share/icons/icons.qrc @@ -107,6 +107,8 @@ application/scalable/apps/keepassxc-monochrome-dark-locked.svg application/scalable/apps/keepassxc-monochrome-light.svg application/scalable/apps/keepassxc-monochrome-light-locked.svg + application/scalable/apps/keepassxc-monochrome-kde-plasma.svg + application/scalable/apps/keepassxc-monochrome-kde-plasma-locked.svg application/scalable/apps/keepassxc-locked.svg application/scalable/apps/keepassxc-unlocked.svg application/scalable/apps/preferences-desktop-icons.svg diff --git a/src/gui/ApplicationSettingsWidget.cpp b/src/gui/ApplicationSettingsWidget.cpp index c7bea73f3..bd70de305 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 7cccf406a..19e1bf3ba 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;