add support for dark/light mode switch

This commit is contained in:
Jianqiao Han 2024-04-18 21:39:47 +08:00 committed by Rick Blommers
parent 40dc420047
commit 1381b93aed
2 changed files with 40 additions and 17 deletions

View File

@ -17,6 +17,7 @@
#include <QFontDatabase> #include <QFontDatabase>
#include <QFontMetrics> #include <QFontMetrics>
#include <QString> #include <QString>
#include <QStyleHints>
// Initializing namespaces need to happen outside a namespace // Initializing namespaces need to happen outside a namespace
@ -228,24 +229,8 @@ QtAwesome::QtAwesome(QObject* parent)
, _namedCodepointsByStyle() , _namedCodepointsByStyle()
, _namedCodepointsList() , _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 );
resetDefaultOptions();
#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());
_fontIconPainter = new QtAwesomeCharIconPainter(); _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_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)); _fontDetails.insert(fa::fa_sharp_thin, QtAwesomeFontData(FA_SHARP_THIN_FONT_FILENAME, FA_SHARP_THIN_FONT_WEIGHT));
#endif #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() QtAwesome::~QtAwesome()

View File

@ -131,6 +131,14 @@ protected:
const QString styleEnumToString(int style) const; const QString styleEnumToString(int style) const;
void addToNamedCodePoints(int style, const fa::QtAwesomeNamedIcon* faCommonIconArray, int size); 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: private:
QHash<int, QtAwesomeFontData> _fontDetails; ///< The fonts name used for each style QHash<int, QtAwesomeFontData> _fontDetails; ///< The fonts name used for each style
QHash<int, QHash<QString, int>*> _namedCodepointsByStyle; ///< A map with names mapped to code-points for each style QHash<int, QHash<QString, int>*> _namedCodepointsByStyle; ///< A map with names mapped to code-points for each style