From a7d2611400b65bbdd5b8e16579d30a49c027ba84 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sun, 31 Mar 2024 13:42:38 +0200 Subject: [PATCH] Allow re-applying the default icon theme independently of palette This is useful under Android where the palette is apparently not set to something matching the darkmode setting but the Qt Quick Material style nevertheless seems to follow the darkmode setting. So we just want to take the darkmode setting into account for assigning the appropriate icon theme and ignore the palette. --- settingsdialog/qtsettings.cpp | 36 +++++++++++++++++++++++++++-------- settingsdialog/qtsettings.h | 1 + 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/settingsdialog/qtsettings.cpp b/settingsdialog/qtsettings.cpp index afb5209..462b478 100644 --- a/settingsdialog/qtsettings.cpp +++ b/settingsdialog/qtsettings.cpp @@ -365,6 +365,33 @@ void QtSettings::apply() } } +/*! + * \brief Re-applies default icon theme assuming the palette is dark or not depending on \a isPaletteDark. + * + * Re-assigns the appropriate default icon theme depending on the current palette. Call this function after the + * darkmode setting has changed without the current palette reflecting that but you want the darkmode setting take + * precedence over the palette (e.g. when using Qt Quick Material style under Android). If your application makes + * actually use of the palette and you want the palette take precedence than call \a reevaluatePaletteAndDefaultIconTheme() + * instead. + * + * Note that QtSettings::isPaletteDark() will return \a isPaletteDark after calling this function (even though the current + * palette is not actually reflecting that). + * + * \remarks + * - The default icon theme must have been assigned before using the apply() function. + * - This function has no effect if a custom icon theme is configured. + */ +void QtSettings::reapplyDefaultIconTheme(bool isPaletteDark) +{ + if (isPaletteDark == m_d->isPaletteDark) { + return; // no need to do anything if there's no change + } + m_d->isPaletteDark = isPaletteDark; + if (auto iconTheme = QIcon::themeName(); iconTheme == QStringLiteral("default") || iconTheme == QStringLiteral("default-dark")) { + QIcon::setThemeName(m_d->isPaletteDark ? QStringLiteral("default-dark") : QStringLiteral("default")); + } +} + /*! * \brief Re-evaluates whether the palette is dark and re-applies default icon theme. * @@ -377,14 +404,7 @@ void QtSettings::apply() */ void QtSettings::reevaluatePaletteAndDefaultIconTheme() { - const auto isPaletteDark = QtUtilities::isPaletteDark(); - if (isPaletteDark == m_d->isPaletteDark) { - return; // no need to do anything if there's no change - } - m_d->isPaletteDark = isPaletteDark; - if (auto iconTheme = QIcon::themeName(); iconTheme == QStringLiteral("default") || iconTheme == QStringLiteral("default-dark")) { - QIcon::setThemeName(m_d->isPaletteDark ? QStringLiteral("default-dark") : QStringLiteral("default")); - } + reapplyDefaultIconTheme(QtUtilities::isPaletteDark()); } /*! diff --git a/settingsdialog/qtsettings.h b/settingsdialog/qtsettings.h index 7e77980..a3e14e8 100644 --- a/settingsdialog/qtsettings.h +++ b/settingsdialog/qtsettings.h @@ -51,6 +51,7 @@ public: void restore(QSettings &settings); void save(QSettings &settings) const; void apply(); + void reapplyDefaultIconTheme(bool isPaletteDark); void reevaluatePaletteAndDefaultIconTheme(); bool isPaletteDark(); bool hasCustomFont() const;