Windows: change the systray icon for light/dark themes
This commit is contained in:
parent
6396fc8e5a
commit
fb73fe2ecc
6 changed files with 37 additions and 27 deletions
|
@ -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)
|
target_link_libraries(kdeconnect-indicator Qt::Widgets KF5::CoreAddons KF5::I18n KF5::Notifications KF5::DBusAddons KF5::KCMUtils kdeconnectinterfaces kdeconnectcore kdeconnectversion)
|
||||||
|
|
||||||
if (WIN32)
|
if (WIN32)
|
||||||
add_compile_definitions(QSYSTRAY)
|
target_link_libraries(kdeconnect-indicator windowsapp)
|
||||||
|
target_compile_features(kdeconnect-indicator PUBLIC cxx_std_17)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if (APPLE)
|
if (APPLE)
|
||||||
|
|
|
@ -35,14 +35,7 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
|
||||||
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
|
|
||||||
{
|
|
||||||
systray.setIcon(QIcon::fromTheme(QStringLiteral("kdeconnectindicatordark")));
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
|
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
|
||||||
{
|
{
|
||||||
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
|
systray.setIconByName(QStringLiteral("kdeconnectindicatordark"));
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
#include <QSplashScreen>
|
#include <QSplashScreen>
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
#ifdef Q_OS_WIN
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
#else
|
#else
|
||||||
#include <KStatusNotifierItem>
|
#include <KStatusNotifierItem>
|
||||||
|
@ -46,7 +46,7 @@ public:
|
||||||
|
|
||||||
int daemonHook(QProcess &kdeconnectd);
|
int daemonHook(QProcess &kdeconnectd);
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
#ifdef Q_OS_WIN
|
||||||
void systrayIconHook(QSystemTrayIcon &systray);
|
void systrayIconHook(QSystemTrayIcon &systray);
|
||||||
#else
|
#else
|
||||||
void systrayIconHook(KStatusNotifierItem &systray);
|
void systrayIconHook(KStatusNotifierItem &systray);
|
||||||
|
|
|
@ -142,12 +142,6 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
|
||||||
void IndicatorHelper::systrayIconHook(QSystemTrayIcon &systray)
|
|
||||||
{
|
|
||||||
Q_UNUSED(systray);
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
|
void IndicatorHelper::systrayIconHook(KStatusNotifierItem &systray)
|
||||||
{
|
{
|
||||||
const QString iconPath = QStandardPaths::locate(QStandardPaths::AppDataLocation, QStringLiteral("kdeconnect-icons"), QStandardPaths::LocateDirectory);
|
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";
|
qWarning() << "Fail to find indicator icon, continue anyway";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
|
@ -8,18 +8,25 @@
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
|
#include <QSettings>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#include <tlhelp32.h>
|
#include <tlhelp32.h>
|
||||||
|
|
||||||
|
#include <winrt/Windows.UI.ViewManagement.h>
|
||||||
|
#include <winrt/Windows.Foundation.Collections.h>
|
||||||
|
|
||||||
#include "indicator_debug.h"
|
#include "indicator_debug.h"
|
||||||
#include "indicatorhelper.h"
|
#include "indicatorhelper.h"
|
||||||
|
|
||||||
|
winrt::Windows::UI::ViewManagement::UISettings uiSettings;
|
||||||
|
|
||||||
IndicatorHelper::IndicatorHelper(const QUrl &indicatorUrl)
|
IndicatorHelper::IndicatorHelper(const QUrl &indicatorUrl)
|
||||||
: m_indicatorUrl(indicatorUrl)
|
: m_indicatorUrl(indicatorUrl)
|
||||||
{
|
{
|
||||||
|
uiSettings = winrt::Windows::UI::ViewManagement::UISettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
IndicatorHelper::~IndicatorHelper()
|
IndicatorHelper::~IndicatorHelper()
|
||||||
|
@ -42,6 +49,13 @@ void IndicatorHelper::postInit()
|
||||||
|
|
||||||
void IndicatorHelper::iconPathHook()
|
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)
|
int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
||||||
|
@ -50,17 +64,26 @@ int IndicatorHelper::daemonHook(QProcess &kdeconnectd)
|
||||||
return 0;
|
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)
|
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
|
bool IndicatorHelper::terminateProcess(const QString &processName, const QUrl &indicatorUrl) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
#ifdef Q_OS_WIN
|
||||||
#include <QSystemTrayIcon>
|
#include <QSystemTrayIcon>
|
||||||
#else
|
#else
|
||||||
#include <KStatusNotifierItem>
|
#include <KStatusNotifierItem>
|
||||||
|
@ -146,7 +146,7 @@ int main(int argc, char **argv)
|
||||||
// Run icon to add icon path (if necessary)
|
// Run icon to add icon path (if necessary)
|
||||||
helper.iconPathHook();
|
helper.iconPathHook();
|
||||||
|
|
||||||
#ifdef QSYSTRAY
|
#ifdef Q_OS_WIN
|
||||||
QSystemTrayIcon systray;
|
QSystemTrayIcon systray;
|
||||||
helper.systrayIconHook(systray);
|
helper.systrayIconHook(systray);
|
||||||
systray.setVisible(true);
|
systray.setVisible(true);
|
||||||
|
|
Loading…
Reference in a new issue