Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for dark/light mode switch #59

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ icons to your [Qt application](http://qt-project.org/).

## Table of Contents

- [Latest Release 6.4.2](#latest-release-642)
- [Latest Release 6.5.1](#latest-release-651)
- [Font Awesome 6 Release](#font-awesome-6-release)
- [Installation Free Version](#installation-free-version)
- [Installation Pro version](#installation-pro-version)
Expand Down
Loading