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 <QFontMetrics>
#include <QString>
#include <QStyleHints>
// Initializing namespaces need to happen outside a namespace
@ -228,6 +229,33 @@ QtAwesome::QtAwesome(QObject* parent)
, _namedCodepointsByStyle()
, _namedCodepointsList()
{
resetDefaultOptions();
_fontIconPainter = new QtAwesomeCharIconPainter();
_fontDetails.insert(fa::fa_solid, QtAwesomeFontData(FA_SOLID_FONT_FILENAME, FA_SOLID_FONT_WEIGHT));
_fontDetails.insert(fa::fa_regular, QtAwesomeFontData(FA_REGULAR_FONT_FILENAME, FA_REGULAR_FONT_WEIGHT));
_fontDetails.insert(fa::fa_brands, QtAwesomeFontData(FA_BRANDS_FONT_FILENAME, FA_BRANDS_FONT_WEIGHT));
#ifdef FONT_AWESOME_PRO
_fontDetails.insert(fa::fa_light, QtAwesomeFontData(FA_LIGHT_FONT_FILENAME, FA_LIGHT_FONT_WEIGHT));
_fontDetails.insert(fa::fa_duotone, QtAwesomeFontData(FA_DUOTONE_FONT_FILENAME, FA_DUOTONE_FONT_WEIGHT));
_fontDetails.insert(fa::fa_thin, QtAwesomeFontData(FA_THIN_FONT_FILENAME, FA_THIN_FONT_WEIGHT));
_fontDetails.insert(fa::fa_sharp_solid, QtAwesomeFontData(FA_SHARP_SOLID_FONT_FILENAME, FA_SHARP_SOLID_FONT_WEIGHT));
_fontDetails.insert(fa::fa_sharp_regular, QtAwesomeFontData(FA_SHARP_REGULAR_FONT_FILENAME, FA_SHARP_REGULAR_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));
#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));
@ -247,20 +275,7 @@ QtAwesome::QtAwesome(QObject* parent)
setDefaultOption("text-active", QVariant());
setDefaultOption("text-selected", QVariant());
_fontIconPainter = new QtAwesomeCharIconPainter();
_fontDetails.insert(fa::fa_solid, QtAwesomeFontData(FA_SOLID_FONT_FILENAME, FA_SOLID_FONT_WEIGHT));
_fontDetails.insert(fa::fa_regular, QtAwesomeFontData(FA_REGULAR_FONT_FILENAME, FA_REGULAR_FONT_WEIGHT));
_fontDetails.insert(fa::fa_brands, QtAwesomeFontData(FA_BRANDS_FONT_FILENAME, FA_BRANDS_FONT_WEIGHT));
#ifdef FONT_AWESOME_PRO
_fontDetails.insert(fa::fa_light, QtAwesomeFontData(FA_LIGHT_FONT_FILENAME, FA_LIGHT_FONT_WEIGHT));
_fontDetails.insert(fa::fa_duotone, QtAwesomeFontData(FA_DUOTONE_FONT_FILENAME, FA_DUOTONE_FONT_WEIGHT));
_fontDetails.insert(fa::fa_thin, QtAwesomeFontData(FA_THIN_FONT_FILENAME, FA_THIN_FONT_WEIGHT));
_fontDetails.insert(fa::fa_sharp_solid, QtAwesomeFontData(FA_SHARP_SOLID_FONT_FILENAME, FA_SHARP_SOLID_FONT_WEIGHT));
_fontDetails.insert(fa::fa_sharp_regular, QtAwesomeFontData(FA_SHARP_REGULAR_FONT_FILENAME, FA_SHARP_REGULAR_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));
#endif
Q_EMIT defaultOptionsReset();
}
QtAwesome::~QtAwesome()

View File

@ -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<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