StyleSheetLoader: Cleanup code and add workaround for macOS dark theme

Fixes #778
This commit is contained in:
Jonas Kvinge
2021-09-28 17:24:26 +02:00
parent 44a3bad278
commit 2b445fb563
3 changed files with 42 additions and 64 deletions

View File

@@ -51,10 +51,10 @@ void Appearance::LoadUserTheme() {
bool use_a_custom_color_set = s.value(AppearanceSettingsPage::kUseCustomColorSet).toBool(); bool use_a_custom_color_set = s.value(AppearanceSettingsPage::kUseCustomColorSet).toBool();
s.endGroup(); s.endGroup();
if (!use_a_custom_color_set) return; if (use_a_custom_color_set) {
ChangeForegroundColor(foreground_color_);
ChangeForegroundColor(foreground_color_); ChangeBackgroundColor(background_color_);
ChangeBackgroundColor(background_color_); }
} }

View File

@@ -21,7 +21,7 @@
#include "config.h" #include "config.h"
#include <chrono> #include <memory>
#include <QtGlobal> #include <QtGlobal>
#include <QObject> #include <QObject>
@@ -40,18 +40,7 @@
#include "core/logging.h" #include "core/logging.h"
#include "stylesheetloader.h" #include "stylesheetloader.h"
using namespace std::chrono_literals; StyleSheetLoader::StyleSheetLoader(QObject *parent) : QObject(parent) {}
StyleSheetLoader::StyleSheetLoader(QObject *parent)
: QObject(parent),
timer_reset_counter_(new QTimer(this)) {
timer_reset_counter_->setSingleShot(true);
timer_reset_counter_->setInterval(1s);
QObject::connect(timer_reset_counter_, &QTimer::timeout, this, &StyleSheetLoader::ResetCounters);
}
void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) { void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
@@ -70,31 +59,38 @@ void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) {
} }
file.close(); file.close();
StyleSheetData styledata; std::shared_ptr<StyleSheetData> styledata = std::make_shared<StyleSheetData>();
styledata.filename_ = filename; styledata->filename_ = filename;
styledata.stylesheet_template_ = stylesheet; styledata->stylesheet_template_ = stylesheet;
styledata.stylesheet_current_ = widget->styleSheet(); styledata->stylesheet_current_ = widget->styleSheet();
styledata_[widget] = styledata; styledata_.insert(widget, styledata);
widget->installEventFilter(this); widget->installEventFilter(this);
UpdateStyleSheet(widget, styledata); UpdateStyleSheet(widget, styledata);
} }
void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, StyleSheetData styledata) { void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, std::shared_ptr<StyleSheetData> styledata) {
QString stylesheet = styledata.stylesheet_template_; QString stylesheet = styledata->stylesheet_template_;
// Replace %palette-role with actual colours // Replace %palette-role with actual colours
QPalette p(widget->palette()); QPalette p(widget->palette());
QColor alt = p.color(QPalette::AlternateBase); {
alt.setAlpha(50); QColor alt = p.color(QPalette::AlternateBase);
stylesheet.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4%)") #ifdef Q_OS_MACOS
.arg(alt.red()) if (alt.lightness() > 180) {
.arg(alt.green()) alt.setAlpha(130);
.arg(alt.blue()) }
.arg(alt.alpha())); else {
alt.setAlpha(16);
}
#else
alt.setAlpha(130);
#endif
stylesheet.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4)").arg(alt.red()).arg(alt.green()).arg(alt.blue()).arg(alt.alpha()));
}
ReplaceColor(&stylesheet, "Window", p, QPalette::Window); ReplaceColor(&stylesheet, "Window", p, QPalette::Window);
ReplaceColor(&stylesheet, "Background", p, QPalette::Window); ReplaceColor(&stylesheet, "Background", p, QPalette::Window);
@@ -121,15 +117,14 @@ void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, StyleSheetData styledat
stylesheet.replace("macos", "*"); stylesheet.replace("macos", "*");
#endif #endif
if (stylesheet != styledata.stylesheet_current_) { if (stylesheet != styledata->stylesheet_current_) {
styledata->stylesheet_current_ = stylesheet;
widget->setStyleSheet(stylesheet); widget->setStyleSheet(stylesheet);
styledata.stylesheet_current_ = widget->styleSheet();
styledata_[widget] = styledata;
} }
} }
void StyleSheetLoader::ReplaceColor(QString *css, const QString &name, const QPalette &palette, QPalette::ColorRole role) { void StyleSheetLoader::ReplaceColor(QString *css, const QString &name, const QPalette &palette, const QPalette::ColorRole role) {
css->replace("%palette-" + name + "-lighter", palette.color(role).lighter().name(), Qt::CaseInsensitive); css->replace("%palette-" + name + "-lighter", palette.color(role).lighter().name(), Qt::CaseInsensitive);
css->replace("%palette-" + name + "-darker", palette.color(role).darker().name(), Qt::CaseInsensitive); css->replace("%palette-" + name + "-darker", palette.color(role).darker().name(), Qt::CaseInsensitive);
@@ -139,26 +134,13 @@ void StyleSheetLoader::ReplaceColor(QString *css, const QString &name, const QPa
bool StyleSheetLoader::eventFilter(QObject *obj, QEvent *event) { bool StyleSheetLoader::eventFilter(QObject *obj, QEvent *event) {
if (event->type() != QEvent::PaletteChange) return false; if (event->type() == QEvent::PaletteChange) {
QWidget *widget = qobject_cast<QWidget*>(obj);
QWidget *widget = qobject_cast<QWidget*>(obj); if (widget && styledata_.contains(widget)) {
if (!widget || !styledata_.contains(widget)) return false; UpdateStyleSheet(widget, styledata_[widget]);
}
StyleSheetData styledata = styledata_[widget];
++styledata.count_;
styledata_[widget] = styledata;
timer_reset_counter_->start();
if (styledata.count_ < 5) {
UpdateStyleSheet(widget, styledata);
} }
return false; return false;
} }
void StyleSheetLoader::ResetCounters() {
for (QHash<QWidget*, StyleSheetData>::iterator it = styledata_.begin(); it != styledata_.end(); ++it) {
it.value().count_ = 0;
}
}

View File

@@ -24,6 +24,8 @@
#include "config.h" #include "config.h"
#include <memory>
#include <QObject> #include <QObject>
#include <QPair> #include <QPair>
#include <QHash> #include <QHash>
@@ -31,7 +33,6 @@
#include <QString> #include <QString>
class QWidget; class QWidget;
class QTimer;
class QEvent; class QEvent;
class StyleSheetLoader : public QObject { class StyleSheetLoader : public QObject {
@@ -50,23 +51,18 @@ class StyleSheetLoader : public QObject {
private: private:
struct StyleSheetData { struct StyleSheetData {
StyleSheetData() : count_(0) {} StyleSheetData() {}
QString filename_; QString filename_;
QString stylesheet_template_; QString stylesheet_template_;
QString stylesheet_current_; QString stylesheet_current_;
int count_;
}; };
private: private:
void UpdateStyleSheet(QWidget *widget, StyleSheetData styledata); void UpdateStyleSheet(QWidget *widget, std::shared_ptr<StyleSheetData> styledata);
static void ReplaceColor(QString *css, const QString &name, const QPalette &palette, QPalette::ColorRole role); static void ReplaceColor(QString *css, const QString &name, const QPalette &palette, const QPalette::ColorRole role);
private slots:
void ResetCounters();
private: private:
QHash<QWidget*, StyleSheetData> styledata_; QHash<QWidget*, std::shared_ptr<StyleSheetData>> styledata_;
QTimer *timer_reset_counter_;
}; };
#endif // STYLESHEETLOADER_H #endif // STYLESHEETLOADER_H