Windows: change the systray icon for light/dark themes

This commit is contained in:
Albert Vaca Cintora 2023-06-12 15:34:43 +02:00
parent 6396fc8e5a
commit fb73fe2ecc
6 changed files with 37 additions and 27 deletions

View file

@ -46,7 +46,8 @@ target_include_directories(kdeconnect-indicator PUBLIC ${CMAKE_CURRENT_SOURCE_DI
target_link_libraries(kdeconnect-indicator Qt::Widgets KF5::CoreAddons KF5::I18n KF5::Notifications KF5::DBusAddons KF5::KCMUtils kdeconnectinterfaces kdeconnectcore kdeconnectversion)
if (WIN32)
add_compile_definitions(QSYSTRAY)
target_link_libraries(kdeconnect-indicator windowsapp)
target_compile_features(kdeconnect-indicator PUBLIC cxx_std_17)
endif()
if (APPLE)

View file

@ -35,14 +35,7 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
return 0;
}
#ifdef QSYSTRAY
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{
systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnectindicatordark")));
}
#else
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
}
#endif

View file

@ -10,7 +10,7 @@
#include <QProcess>
#include <QSplashScreen>
#ifdef QSYSTRAY
#ifdef Q_OS_WIN
#include <QSystemTrayIcon>
#else
#include <KStatusNotifierItem>
@ -46,7 +46,7 @@ public:
int daemonHook(QProcess &kdeconnectd);
#ifdef QSYSTRAY
#ifdef Q_OS_WIN
void systrayIconHook(QSystemTrayIcon &systray);
#else
void systrayIconHook(KStatusNotifierItem &systray);

View file

@ -142,12 +142,6 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
return 0;
}
#ifdef QSYSTRAY
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{
Q_UNUSED(systray);
}
#else
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("kdeconnect-icons"), QStandardPaths::LocateDirectory);
@ -160,4 +154,3 @@ void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
qWarning() << "Fail to find indicator icon, continue anyway";
}
}
#endif

View file

@ -8,18 +8,25 @@
#include <QFile>
#include <QIcon>
#include <QStandardPaths>
#include <QSettings>
#include <iostream>
#include <Windows.h>
#include <tlhelp32.h>
#include <winrt/Windows.UI.ViewManagement.h>
#include <winrt/Windows.Foundation.Collections.h>
#include "indicator_debug.h"
#include "indicatorhelper.h"
winrt::Windows::UI::ViewManagement::UISettings uiSettings;
IndicatorHelper::IndicatorHelper(const QUrl &indicatorUrl)
: m_indicatorUrl(indicatorUrl)
{
uiSettings = winrt::Windows::UI::ViewManagement::UISettings();
}
IndicatorHelper::~IndicatorHelper()
@ -42,6 +49,13 @@ void IndicatorHelper::postInit()
void IndicatorHelper::iconPathHook()
{
// FIXME: This doesn't seem to be enough for QIcon::fromTheme to find the icons, so we still have to use the full path when setting the icon
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("icons"), QStandardPaths::LocateDirectory);
if (!iconPath.isNull()) {
QStringList themeSearchPaths = QIcon::themeSearchPaths();
themeSearchPaths << iconPath;
QIcon::setThemeSearchPaths(themeSearchPaths);
}
}
int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
@ -50,17 +64,26 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
return 0;
}
#ifdef QSYSTRAY
void onThemeChanged(QSystemTrayIcon &systray)
{
// Since this is a system tray icon, we care about the system theme and not the app theme
QSettings registry(QStringLiteral("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"), QSettings::Registry64Format);
bool isLightTheme = registry.value(QStringLiteral("SystemUsesLightTheme")).toBool();
if (isLightTheme) {
systray.setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicator.svg"))));
} else {
systray.setIcon(QIcon(QStandardPaths::locate(QStandardPaths::AppLocalDataLocation, QStringLiteral("icons/hicolor/scalable/apps/kdeconnectindicatordark.svg"))));
}
}
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
{
systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnect-tray")));
// Set a callback so we can detect changes to light/dark themes and manually call the callback once the first time
uiSettings.ColorValuesChanged([&systray](auto &&unused1, auto &&unused2) {
onThemeChanged(systray);
});
onThemeChanged(systray);
}
#else
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
{
Q_UNUSED(systray);
}
#endif
bool IndicatorHelper::terminateProcess(const QString &processName, const QUrl &indicatorUrl) const
{

View file

@ -13,7 +13,7 @@
#include <Windows.h>
#endif
#ifdef QSYSTRAY
#ifdef Q_OS_WIN
#include <QSystemTrayIcon>
#else
#include <KStatusNotifierItem>
@ -146,7 +146,7 @@ int main(int argc, char **argv)
// Run icon to add icon path (if necessary)
helper.iconPathHook();
#ifdef QSYSTRAY
#ifdef Q_OS_WIN
QSystemTrayIcon systray;
helper.systrayIconHook(systray);
systray.setVisible(true);