From a75eca60737256f2b457bcde8312f0436a71a488 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 18 Jan 2025 17:12:27 +0100 Subject: [PATCH] Fix applying darkmode under Android --- tray/gui/qml/Main.qml | 3 +++ tray/gui/quick/app.cpp | 33 +++++++++++++++++++++++++++++++-- tray/gui/quick/app.h | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tray/gui/qml/Main.qml b/tray/gui/qml/Main.qml index 76fed8a9..70eceab3 100644 --- a/tray/gui/qml/Main.qml +++ b/tray/gui/qml/Main.qml @@ -16,6 +16,7 @@ ApplicationWindow { Material.theme: App.darkmodeEnabled ? Material.Dark : Material.Light Material.primary: pageStack.currentPage.isDangerous ? Material.Red : Material.LightBlue Material.accent: Material.primary + Material.onForegroundChanged: App.setPalette(Material.foreground, Material.background) header: ToolBar { Material.theme: Material.Dark ColumnLayout { @@ -412,6 +413,8 @@ ApplicationWindow { // handle global keyboard and mouse events Component.onCompleted: { + App.setPalette(Material.foreground, Material.background); + window.contentItem.forceActiveFocus(Qt.ActiveWindowFocusReason); window.contentItem.Keys.released.connect((event) => { const key = event.key; diff --git a/tray/gui/quick/app.cpp b/tray/gui/quick/app.cpp index fb229745..0ab44d04 100644 --- a/tray/gui/quick/app.cpp +++ b/tray/gui/quick/app.cpp @@ -59,6 +59,16 @@ #define SYNCTHING_APP_PATH_CONVERSION(s) QString::fromLocal8Bit(s.string()) #endif +#ifdef Q_OS_ANDROID +#define SYNCTHING_APP_HANDLE_DARK_MODE_CHANGED_EXPLICITLY +#endif + +#ifdef SYNCTHING_APP_HANDLE_DARK_MODE_CHANGED_EXPLICITLY +#define SYNCTHING_APP_IS_PALETTE_DARK(palette) false +#else +#define SYNCTHING_APP_IS_PALETTE_DARK(palette) QtUtilities::isPaletteDark(palette) +#endif + using namespace Data; namespace QtGui { @@ -121,7 +131,7 @@ App::App(bool insecure, QObject *parent) #endif , m_darkmodeEnabled(false) , m_darkColorScheme(false) - , m_darkPalette(QtUtilities::isPaletteDark()) + , m_darkPalette(SYNCTHING_APP_IS_PALETTE_DARK(m_app->palette())) , m_isGuiLoaded(false) , m_alwaysUnloadGuiWhenHidden(false) , m_unloadGuiWhenHidden(false) @@ -556,8 +566,10 @@ bool App::eventFilter(QObject *object, QEvent *event) if (m_imageProvider) { m_imageProvider->setDefaultColor(palette.color(QPalette::Normal, QPalette::Text)); } +#ifndef SYNCTHING_APP_HANDLE_DARK_MODE_CHANGED_EXPLICITLY + applyDarkmodeChange(m_darkColorScheme, SYNCTHING_APP_IS_PALETTE_DARK(palette)); +#endif } - applyDarkmodeChange(m_darkColorScheme, QtUtilities::isPaletteDark()); break; default:; } @@ -1017,6 +1029,23 @@ bool App::minimize() return true; } +void App::setPalette(const QColor &foreground, const QColor &background) +{ +#ifdef SYNCTHING_APP_HANDLE_DARK_MODE_CHANGED_EXPLICITLY + if (m_app) { + auto palette = m_app->palette(); + palette.setColor(QPalette::Active, QPalette::Text, foreground); + palette.setColor(QPalette::Active, QPalette::Base, background); + palette.setColor(QPalette::Active, QPalette::WindowText, foreground); + palette.setColor(QPalette::Active, QPalette::Window, background); + m_app->setPalette(palette); + } +#else + Q_UNUSED(foreground) + Q_UNUSED(background) +#endif +} + bool App::openSettings() { if (!m_settingsDir.has_value()) { diff --git a/tray/gui/quick/app.h b/tray/gui/quick/app.h index b1de013b..4c0998c6 100644 --- a/tray/gui/quick/app.h +++ b/tray/gui/quick/app.h @@ -251,6 +251,7 @@ class App : public QObject { Q_INVOKABLE QString dirDisplayName(const QString &id) const; Q_INVOKABLE QVariantList computeDirsNeedingItems(const QModelIndex &devProxyModelIndex) const; Q_INVOKABLE bool minimize(); + Q_INVOKABLE void setPalette(const QColor &foreground, const QColor &background); Q_SIGNALS: void darkmodeEnabledChanged(bool darkmodeEnabled);