Skip to content

Commit

Permalink
Add dynamic theme switching on Windows 10
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious committed Jan 7, 2021
1 parent 80c1b9b commit 9a7b20c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/gui/ApplicationSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ void ApplicationSettingsWidget::loadSettings()
}

m_generalUi->trayIconAppearance->clear();
#ifdef Q_OS_MACOS
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
m_generalUi->trayIconAppearance->addItem(tr("Monochrome"), "monochrome");
#else
m_generalUi->trayIconAppearance->addItem(tr("Monochrome (light)"), "monochrome-light");
Expand Down
2 changes: 1 addition & 1 deletion src/gui/Icons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ QIcon Icons::trayIcon(QString style)
}

QIcon i;
#ifdef Q_OS_MACOS
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
if (osUtils->isStatusBarDark()) {
i = icon(QString("keepassxc-monochrome-light%1").arg(style), false);
} else {
Expand Down
21 changes: 13 additions & 8 deletions src/gui/osutils/winutils/WinUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ WinUtils* WinUtils::instance()
{
if (!m_instance) {
m_instance = new WinUtils(qApp);
m_instance->m_darkAppThemeActive = m_instance->isDarkMode();
m_instance->m_darkSystemThemeActive = m_instance->isStatusBarDark();

#ifdef QT_DEBUG
// Attach console to enable debug output
Expand Down Expand Up @@ -63,15 +65,17 @@ bool WinUtils::nativeEventFilter(const QByteArray& eventType, void* message, lon

auto* msg = static_cast<MSG*>(message);
switch (msg->message) {
/* TODO: indicate dark mode support for black title bar
case WM_CREATE:
case WM_INITDIALOG: {
if (msg->hwnd && winUtils()->isDarkMode()) {
case WM_SETTINGCHANGE:
if (m_darkAppThemeActive != isDarkMode()) {
m_darkAppThemeActive = !m_darkAppThemeActive;
emit interfaceThemeChanged();
}

if (m_darkSystemThemeActive != isStatusBarDark()) {
m_darkSystemThemeActive = !m_darkSystemThemeActive;
emit statusbarThemeChanged();
}
break;
}
*/
case WM_HOTKEY:
triggerGlobalShortcut(msg->wParam);
break;
Expand All @@ -89,8 +93,9 @@ bool WinUtils::isDarkMode() const

bool WinUtils::isStatusBarDark() const
{
// TODO: implement
return isDarkMode();
QSettings settings(R"(HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)",
QSettings::NativeFormat);
return settings.value("SystemUsesLightTheme", 0).toInt() == 0;
}

bool WinUtils::isLaunchAtStartupEnabled() const
Expand Down
3 changes: 3 additions & 0 deletions src/gui/osutils/winutils/WinUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class WinUtils : public OSUtilsBase, QAbstractNativeEventFilter
int m_nextShortcutId = 1;
QHash<QString, QSharedPointer<globalShortcut>> m_globalShortcuts;

bool m_darkAppThemeActive;
bool m_darkSystemThemeActive;

Q_DISABLE_COPY(WinUtils)
};

Expand Down

0 comments on commit 9a7b20c

Please sign in to comment.