From 2b445fb5632792035bb670603d8ef33b2d7479a0 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Tue, 28 Sep 2021 17:24:26 +0200 Subject: [PATCH] StyleSheetLoader: Cleanup code and add workaround for macOS dark theme Fixes #778 --- src/core/appearance.cpp | 8 ++-- src/core/stylesheetloader.cpp | 82 ++++++++++++++--------------------- src/core/stylesheetloader.h | 16 +++---- 3 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src/core/appearance.cpp b/src/core/appearance.cpp index 9bed14101..6d15a3da7 100644 --- a/src/core/appearance.cpp +++ b/src/core/appearance.cpp @@ -51,10 +51,10 @@ void Appearance::LoadUserTheme() { bool use_a_custom_color_set = s.value(AppearanceSettingsPage::kUseCustomColorSet).toBool(); s.endGroup(); - if (!use_a_custom_color_set) return; - - ChangeForegroundColor(foreground_color_); - ChangeBackgroundColor(background_color_); + if (use_a_custom_color_set) { + ChangeForegroundColor(foreground_color_); + ChangeBackgroundColor(background_color_); + } } diff --git a/src/core/stylesheetloader.cpp b/src/core/stylesheetloader.cpp index 7628b9d08..5f2306bf9 100644 --- a/src/core/stylesheetloader.cpp +++ b/src/core/stylesheetloader.cpp @@ -21,7 +21,7 @@ #include "config.h" -#include +#include #include #include @@ -40,18 +40,7 @@ #include "core/logging.h" #include "stylesheetloader.h" -using namespace std::chrono_literals; - -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); - -} +StyleSheetLoader::StyleSheetLoader(QObject *parent) : QObject(parent) {} void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) { @@ -70,31 +59,38 @@ void StyleSheetLoader::SetStyleSheet(QWidget *widget, const QString &filename) { } file.close(); - StyleSheetData styledata; - styledata.filename_ = filename; - styledata.stylesheet_template_ = stylesheet; - styledata.stylesheet_current_ = widget->styleSheet(); - styledata_[widget] = styledata; + std::shared_ptr styledata = std::make_shared(); + styledata->filename_ = filename; + styledata->stylesheet_template_ = stylesheet; + styledata->stylesheet_current_ = widget->styleSheet(); + styledata_.insert(widget, styledata); widget->installEventFilter(this); UpdateStyleSheet(widget, styledata); } -void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, StyleSheetData styledata) { +void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, std::shared_ptr styledata) { - QString stylesheet = styledata.stylesheet_template_; + QString stylesheet = styledata->stylesheet_template_; // Replace %palette-role with actual colours QPalette p(widget->palette()); - QColor alt = p.color(QPalette::AlternateBase); - alt.setAlpha(50); - stylesheet.replace("%palette-alternate-base", QString("rgba(%1,%2,%3,%4%)") - .arg(alt.red()) - .arg(alt.green()) - .arg(alt.blue()) - .arg(alt.alpha())); + { + QColor alt = p.color(QPalette::AlternateBase); +#ifdef Q_OS_MACOS + if (alt.lightness() > 180) { + alt.setAlpha(130); + } + 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, "Background", p, QPalette::Window); @@ -121,15 +117,14 @@ void StyleSheetLoader::UpdateStyleSheet(QWidget *widget, StyleSheetData styledat stylesheet.replace("macos", "*"); #endif - if (stylesheet != styledata.stylesheet_current_) { + if (stylesheet != styledata->stylesheet_current_) { + styledata->stylesheet_current_ = 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 + "-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) { - if (event->type() != QEvent::PaletteChange) return false; - - QWidget *widget = qobject_cast(obj); - if (!widget || !styledata_.contains(widget)) return false; - - StyleSheetData styledata = styledata_[widget]; - ++styledata.count_; - styledata_[widget] = styledata; - timer_reset_counter_->start(); - if (styledata.count_ < 5) { - UpdateStyleSheet(widget, styledata); + if (event->type() == QEvent::PaletteChange) { + QWidget *widget = qobject_cast(obj); + if (widget && styledata_.contains(widget)) { + UpdateStyleSheet(widget, styledata_[widget]); + } } + return false; } - -void StyleSheetLoader::ResetCounters() { - - for (QHash::iterator it = styledata_.begin(); it != styledata_.end(); ++it) { - it.value().count_ = 0; - } - -} diff --git a/src/core/stylesheetloader.h b/src/core/stylesheetloader.h index a6a702d6c..7e21e95d6 100644 --- a/src/core/stylesheetloader.h +++ b/src/core/stylesheetloader.h @@ -24,6 +24,8 @@ #include "config.h" +#include + #include #include #include @@ -31,7 +33,6 @@ #include class QWidget; -class QTimer; class QEvent; class StyleSheetLoader : public QObject { @@ -50,23 +51,18 @@ class StyleSheetLoader : public QObject { private: struct StyleSheetData { - StyleSheetData() : count_(0) {} + StyleSheetData() {} QString filename_; QString stylesheet_template_; QString stylesheet_current_; - int count_; }; private: - void UpdateStyleSheet(QWidget *widget, StyleSheetData styledata); - static void ReplaceColor(QString *css, const QString &name, const QPalette &palette, QPalette::ColorRole role); - - private slots: - void ResetCounters(); + void UpdateStyleSheet(QWidget *widget, std::shared_ptr styledata); + static void ReplaceColor(QString *css, const QString &name, const QPalette &palette, const QPalette::ColorRole role); private: - QHash styledata_; - QTimer *timer_reset_counter_; + QHash> styledata_; }; #endif // STYLESHEETLOADER_H