Skip to content

Commit

Permalink
on Windows 10 follow color scheme changes and apply them
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <[email protected]>
  • Loading branch information
mgallien committed Nov 19, 2024
1 parent 5977bd2 commit 330eb29
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 deletions.
23 changes: 5 additions & 18 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,6 @@ namespace {

// ----------------------------------------------------------------------------------

#ifdef Q_OS_WIN
class WindowsNativeEventFilter : public QAbstractNativeEventFilter {
public:
bool nativeEventFilter(const QByteArray &eventType, void *message, qintptr *result) override {
const auto msg = static_cast<MSG *>(message);
if(msg->message == WM_SYSCOLORCHANGE || msg->message == WM_SETTINGCHANGE) {
if (const auto ptr = qobject_cast<QGuiApplication *>(QGuiApplication::instance())) {
emit ptr->paletteChanged(ptr->palette());
}
}
return false;
}
};
#endif

bool Application::configVersionMigration()
{
QStringList deleteKeys, ignoreKeys;
Expand Down Expand Up @@ -237,9 +222,6 @@ Application::Application(int &argc, char **argv)
// Ensure OpenSSL config file is only loaded from app directory
QString opensslConf = QCoreApplication::applicationDirPath() + QString("/openssl.cnf");
qputenv("OPENSSL_CONF", opensslConf.toLocal8Bit());

// Set up event listener for Windows theme changing
installNativeEventFilter(new WindowsNativeEventFilter());
#endif

// TODO: Can't set this without breaking current config paths
Expand Down Expand Up @@ -384,6 +366,8 @@ Application::Application(int &argc, char **argv)

_theme->setSystrayUseMonoIcons(ConfigFile().monoIcons());
connect(_theme, &Theme::systrayUseMonoIconsChanged, this, &Application::slotUseMonoIconsChanged);
connect(this, &Application::systemPaletteChanged,
_theme, &Theme::systemPaletteHasChanged);

#if defined(Q_OS_WIN)
_shellExtensionsServer.reset(new ShellExtensionsServer);
Expand Down Expand Up @@ -1127,6 +1111,9 @@ bool Application::event(QEvent *event)
qCInfo(lcApplication) << errorParsingLocalFileEditingUrl;
showHint(errorParsingLocalFileEditingUrl.toStdString());
}
} else if (event->type() == QEvent::ApplicationPaletteChange) {
qCInfo(lcApplication) << "application palette changed";
emit systemPaletteChanged();
}
return SharedTools::QtSingleApplication::event(event);
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public slots:
void folderRemoved();
void folderStateChanged(OCC::Folder *);
void isShowingSettingsDialog();
void systemPaletteChanged();

protected slots:
void slotParseMessage(const QString &, QObject *);
Expand Down
22 changes: 12 additions & 10 deletions src/libsync/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,16 +969,8 @@ QColor Theme::defaultColor()
void Theme::connectToPaletteSignal()
{
if (!_paletteSignalsConnected) {
if (const auto ptr = qobject_cast<QGuiApplication *>(QGuiApplication::instance())) {
#ifdef Q_OS_WIN
// Windows 10 does not have proper dark mode support via Qt 6 so hack detection
if (!isWindows11OrGreater()) {
connect(ptr, &QGuiApplication::paletteChanged, this, &Theme::darkModeChanged);
} else
#endif
{
connect(ptr->styleHints(), &QStyleHints::colorSchemeChanged, this, &Theme::darkModeChanged);
}
if (const auto ptr = qobject_cast<QGuiApplication*>(qApp)) {
connect(ptr->styleHints(), &QStyleHints::colorSchemeChanged, this, &Theme::darkModeChanged);
_paletteSignalsConnected = true;
}
}
Expand Down Expand Up @@ -1088,4 +1080,14 @@ void Theme::setStartLoginFlowAutomatically(bool startLoginFlowAuto)
}
}

void Theme::systemPaletteHasChanged()
{
qCInfo(lcTheme()) << "system palette changed";
#ifdef Q_OS_WIN
if (darkMode() && !isWindows11OrGreater()) {
qApp->setPalette(reserveDarkPalette);
}
#endif
}

} // end namespace client
1 change: 1 addition & 0 deletions src/libsync/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ public slots:
void setForceOverrideServerUrl(bool forceOverride);
void setVfsEnabled(bool enabled);
void setStartLoginFlowAutomatically(bool startLoginFlowAuto);
void systemPaletteHasChanged();

protected:
#ifndef TOKEN_AUTH_ONLY
Expand Down

0 comments on commit 330eb29

Please sign in to comment.