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();
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_);
}
}

View File

@@ -21,7 +21,7 @@
#include "config.h"
#include <chrono>
#include <memory>
#include <QtGlobal>
#include <QObject>
@@ -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<StyleSheetData> styledata = std::make_shared<StyleSheetData>();
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<StyleSheetData> 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<QWidget*>(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<QWidget*>(obj);
if (widget && styledata_.contains(widget)) {
UpdateStyleSheet(widget, styledata_[widget]);
}
}
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 <memory>
#include <QObject>
#include <QPair>
#include <QHash>
@@ -31,7 +33,6 @@
#include <QString>
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<StyleSheetData> styledata);
static void ReplaceColor(QString *css, const QString &name, const QPalette &palette, const QPalette::ColorRole role);
private:
QHash<QWidget*, StyleSheetData> styledata_;
QTimer *timer_reset_counter_;
QHash<QWidget*, std::shared_ptr<StyleSheetData>> styledata_;
};
#endif // STYLESHEETLOADER_H