From 1381b93aed36de44061c2c656913f961dd9437c9 Mon Sep 17 00:00:00 2001 From: Jianqiao Han Date: Thu, 18 Apr 2024 21:39:47 +0800 Subject: [PATCH] add support for dark/light mode switch --- QtAwesome/QtAwesome.cpp | 49 +++++++++++++++++++++++++++-------------- QtAwesome/QtAwesome.h | 8 +++++++ 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/QtAwesome/QtAwesome.cpp b/QtAwesome/QtAwesome.cpp index 62c7204..688e7d8 100644 --- a/QtAwesome/QtAwesome.cpp +++ b/QtAwesome/QtAwesome.cpp @@ -17,6 +17,7 @@ #include #include #include +#include // Initializing namespaces need to happen outside a namespace @@ -228,24 +229,8 @@ QtAwesome::QtAwesome(QObject* parent) , _namedCodepointsByStyle() , _namedCodepointsList() { - setDefaultOption("color", QApplication::palette().color(QPalette::Normal, QPalette::Text)); - setDefaultOption("color-disabled", QApplication::palette().color(QPalette::Disabled, QPalette::Text)); - setDefaultOption("color-active", QApplication::palette().color(QPalette::Active, QPalette::Text)); - setDefaultOption("color-selected", QApplication::palette().color(QPalette::Active, QPalette::Text)); // TODO: check how to get the correct highlighted color - setDefaultOption("scale-factor", 1.0 ); - -#ifdef FONT_AWESOME_PRO - setDefaultOption("duotone-color", QApplication::palette().color(QPalette::Normal, QPalette::BrightText) ); - setDefaultOption("duotone-color-disabled", - QApplication::palette().color(QPalette::Disabled, QPalette::BrightText)); - setDefaultOption("duotone-color-active", QApplication::palette().color(QPalette::Active, QPalette::BrightText)); - setDefaultOption("duotone-color-selected", QApplication::palette().color(QPalette::Active, QPalette::BrightText)); -#endif - setDefaultOption("text", QVariant()); - setDefaultOption("text-disabled", QVariant()); - setDefaultOption("text-active", QVariant()); - setDefaultOption("text-selected", QVariant()); + resetDefaultOptions(); _fontIconPainter = new QtAwesomeCharIconPainter(); @@ -261,6 +246,36 @@ QtAwesome::QtAwesome(QObject* parent) _fontDetails.insert(fa::fa_sharp_light, QtAwesomeFontData(FA_SHARP_LIGHT_FONT_FILENAME, FA_SHARP_LIGHT_FONT_WEIGHT)); _fontDetails.insert(fa::fa_sharp_thin, QtAwesomeFontData(FA_SHARP_THIN_FONT_FILENAME, FA_SHARP_THIN_FONT_WEIGHT)); #endif + + // support dark/light mode + QObject::connect(QApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, [this](Qt::ColorScheme colorScheme){ + resetDefaultOptions(); + }); +} + +void QtAwesome::resetDefaultOptions(){ + _defaultOptions.clear(); + + setDefaultOption("color", QApplication::palette().color(QPalette::Normal, QPalette::Text)); + setDefaultOption("color-disabled", QApplication::palette().color(QPalette::Disabled, QPalette::Text)); + setDefaultOption("color-active", QApplication::palette().color(QPalette::Active, QPalette::Text)); + setDefaultOption("color-selected", QApplication::palette().color(QPalette::Active, QPalette::Text)); // TODO: check how to get the correct highlighted color + setDefaultOption("scale-factor", 1.0 ); + + +#ifdef FONT_AWESOME_PRO + setDefaultOption("duotone-color", QApplication::palette().color(QPalette::Normal, QPalette::BrightText) ); + setDefaultOption("duotone-color-disabled", + QApplication::palette().color(QPalette::Disabled, QPalette::BrightText)); + setDefaultOption("duotone-color-active", QApplication::palette().color(QPalette::Active, QPalette::BrightText)); + setDefaultOption("duotone-color-selected", QApplication::palette().color(QPalette::Active, QPalette::BrightText)); +#endif + setDefaultOption("text", QVariant()); + setDefaultOption("text-disabled", QVariant()); + setDefaultOption("text-active", QVariant()); + setDefaultOption("text-selected", QVariant()); + + Q_EMIT defaultOptionsReset(); } QtAwesome::~QtAwesome() diff --git a/QtAwesome/QtAwesome.h b/QtAwesome/QtAwesome.h index 813a6bd..ad01764 100644 --- a/QtAwesome/QtAwesome.h +++ b/QtAwesome/QtAwesome.h @@ -131,6 +131,14 @@ protected: const QString styleEnumToString(int style) const; void addToNamedCodePoints(int style, const fa::QtAwesomeNamedIcon* faCommonIconArray, int size); +Q_SIGNALS: + // signal about default options being reset + void defaultOptionsReset(); + +public Q_SLOTS: + // (re)set default options according to current QApplication::palette() + void resetDefaultOptions(); + private: QHash _fontDetails; ///< The fonts name used for each style QHash*> _namedCodepointsByStyle; ///< A map with names mapped to code-points for each style