Skip to content

Commit

Permalink
add support for dark/light mode switch
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjianqiao authored and gamecreature committed Apr 23, 2024
1 parent 40dc420 commit 1381b93
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
49 changes: 32 additions & 17 deletions QtAwesome/QtAwesome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QFontDatabase>
#include <QFontMetrics>
#include <QString>
#include <QStyleHints>


// Initializing namespaces need to happen outside a namespace
Expand Down Expand Up @@ -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();

Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions QtAwesome/QtAwesome.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ Q_OBJECT
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
Expand Down

0 comments on commit 1381b93

Please sign in to comment.