Skip to content

Commit

Permalink
Fix applying darkmode under Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Martchus committed Jan 19, 2025
1 parent 4513079 commit a75eca6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions tray/gui/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 31 additions & 2 deletions tray/gui/quick/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:;
}
Expand Down Expand Up @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions tray/gui/quick/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a75eca6

Please sign in to comment.