Skip to content

Commit

Permalink
Fix theme switching for layers
Browse files Browse the repository at this point in the history
  • Loading branch information
vicr123 committed Mar 24, 2024
1 parent bbc6762 commit 43b8cef
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ target_sources(contemporary-corecontrols-plugin PRIVATE
contemporarystyleplugin.h contemporarystyleplugin.cpp
contemporarystyle.cpp contemporarystyle.h
contemporaryinitialisation.h contemporaryinitialisation.cpp
layercalculator.h layercalculator.cpp
)

qt_add_resources(contemporary-corecontrols-plugin libcontemporary-qml-corestyles
Expand Down
32 changes: 19 additions & 13 deletions controls/com/vicr123/Contemporary/CoreStyles/contemporarystyle.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "contemporarystyle.h"

#include "layercalculator.h"

#include <QColor>

struct ContemporaryStylePrivate {
Expand Down Expand Up @@ -149,7 +151,11 @@ QColor ContemporaryStyle::disabled(QColor color)
{
if (color.alpha() == 0) return color;

return QColor::fromHsvF(color.hsvHueF(), color.hsvSaturationF() / 2, color.valueF() / 2);
if (color.valueF() < 0.5) {
return QColor::fromHsvF(color.hsvHueF(), color.hsvSaturationF() / 2, (1 - color.valueF()) / 2);
} else {
return QColor::fromHsvF(color.hsvHueF(), color.hsvSaturationF() / 2, color.valueF() / 2);
}
}

QColor ContemporaryStyle::calculateColor(QColor color, bool hovered, bool pressed, bool disabled) {
Expand All @@ -159,20 +165,20 @@ QColor ContemporaryStyle::calculateColor(QColor color, bool hovered, bool presse
return color;
}

QColor ContemporaryStyle::calculateLayer(uint layer) {
return this->calculateLayer(layer, this->background());
LayerCalculator* ContemporaryStyle::calculateLayer(uint layer) {
auto calculator = this->calculateLayer(layer, this->background());
connect(this, &ContemporaryStyle::backgroundChanged, calculator, [this, calculator] {
calculator->setLayerBase(this->background());
});
return calculator;
}

QColor ContemporaryStyle::calculateLayer(uint layer, QColor base) {
if (layer <= 0) return base;

auto layerColor = this->layer();
for (auto i = 0; i < layer; i++) {
base.setRedF(layerColor.alphaF() * layerColor.redF() + (1 - layerColor.alphaF()) * base.redF());
base.setGreenF(layerColor.alphaF() * layerColor.greenF() + (1 - layerColor.alphaF()) * base.greenF());
base.setBlueF(layerColor.alphaF() * layerColor.blueF() + (1 - layerColor.alphaF()) * base.blueF());
}
return base;
LayerCalculator* ContemporaryStyle::calculateLayer(uint layer, QColor base) {
auto calculator = new LayerCalculator(layer, this->layer(), base);
connect(this, &ContemporaryStyle::layerChanged, calculator, [this, calculator] {
calculator->setLayerColor(this->layer());
});
return calculator;
}

void ContemporaryStyle::setColorTheme(ColorTheme colorTheme) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#ifndef CONTEMPORARYSTYLE_H
#define CONTEMPORARYSTYLE_H

#include <QObject>
#include "layercalculator.h"

#include <QColor>
#include <QObject>
#include <QtQml>

struct ContemporaryStylePrivate;
Expand Down Expand Up @@ -71,8 +73,8 @@ class ContemporaryStyle : public QObject
Q_INVOKABLE static QColor pressed(QColor color);
Q_INVOKABLE static QColor disabled(QColor color);
Q_INVOKABLE QColor calculateColor(QColor color, bool hovered, bool pressed, bool disabled);
Q_INVOKABLE QColor calculateLayer(uint layer);
Q_INVOKABLE QColor calculateLayer(uint layer, QColor base);
Q_INVOKABLE LayerCalculator* calculateLayer(uint layer);
Q_INVOKABLE LayerCalculator* calculateLayer(uint layer, QColor base);
// ReSharper disable once CppRedundantQualifier
Q_INVOKABLE void setColorTheme(ContemporaryStyle::ColorTheme colorTheme);

Expand Down
64 changes: 64 additions & 0 deletions controls/com/vicr123/Contemporary/CoreStyles/layercalculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "layercalculator.h"

#include <QColor>

struct LayerCalculatorPrivate {
uint layer = 1;
QColor layerBase = Qt::transparent;
QColor layerColor;
};

LayerCalculator::LayerCalculator(uint layer, QColor layerColor, QColor layerBase, QObject* parent) :
QObject{parent}, d{new LayerCalculatorPrivate} {
d->layer = layer;
d->layerColor = layerColor;
d->layerBase = layerBase;
}

LayerCalculator::~LayerCalculator() {
delete d;
}

uint LayerCalculator::layer() const {
return d->layer;
}

QColor LayerCalculator::layerBase() const {
return d->layerBase;
}

QColor LayerCalculator::value() const {
if (d->layer <= 0) return d->layerBase;

auto layerColor = this->layerColor();
auto base = this->layerBase();
for (auto i = 0; i < d->layer; i++) {
base.setRedF(layerColor.alphaF() * layerColor.redF() + (1 - layerColor.alphaF()) * base.redF());
base.setGreenF(layerColor.alphaF() * layerColor.greenF() + (1 - layerColor.alphaF()) * base.greenF());
base.setBlueF(layerColor.alphaF() * layerColor.blueF() + (1 - layerColor.alphaF()) * base.blueF());
}
return base;
}

void LayerCalculator::setLayer(uint layer) {
d->layer = layer;
emit layerChanged();
emit valueChanged();
}

void LayerCalculator::setLayerBase(const QColor& color) {
d->layerBase = color;
emit layerBaseChanged();
emit valueChanged();
}

QColor LayerCalculator::layerColor() const {
return d->layerColor;
}

void LayerCalculator::setLayerColor(const QColor& color) {
d->layerColor = color;
emit layerColorChanged();
emit valueChanged();
}

40 changes: 40 additions & 0 deletions controls/com/vicr123/Contemporary/CoreStyles/layercalculator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef LAYERCALCULATOR_H
#define LAYERCALCULATOR_H

#include <QObject>
#include <QColor>

struct LayerCalculatorPrivate;
class LayerCalculator : public QObject {
Q_OBJECT
Q_PROPERTY(uint layer READ layer WRITE setLayer NOTIFY layerChanged)
Q_PROPERTY(QColor layerBase READ layerBase WRITE setLayerBase NOTIFY layerBaseChanged)
Q_PROPERTY(QColor layerColor READ layerColor WRITE setLayerColor NOTIFY layerColorChanged)
Q_PROPERTY(QColor value READ value NOTIFY valueChanged)

public:
explicit LayerCalculator(uint layer, QColor layerColor, QColor layerBase = Qt::transparent, QObject* parent = nullptr);
~LayerCalculator();

uint layer() const;
void setLayer(uint layer);

QColor layerBase() const;
void setLayerBase(const QColor& color);

QColor layerColor() const;
void setLayerColor(const QColor& color);

QColor value() const;

signals:
void layerChanged();
void layerBaseChanged();
void layerColorChanged();
void valueChanged();

private:
LayerCalculatorPrivate* d;
};

#endif // LAYERCALCULATOR_H
2 changes: 1 addition & 1 deletion playground/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ ContemporaryWindow {
z: 20

text: qsTr("Extra Surface")
color: Contemporary.calculateLayer(1)
color: Contemporary.calculateLayer(1).value

backButtonVisible: true
onBackButtonClicked: outerStack.pop()
Expand Down
2 changes: 1 addition & 1 deletion playground/components/Buttons.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Control {

z: 10
text: "Buttons";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

ColumnLayout {
Expand Down
2 changes: 1 addition & 1 deletion playground/components/CheckboxesRadioButtons.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Control {

z: 10
text: "Checkboxes and Radio Buttons";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

RowLayout {
Expand Down
2 changes: 1 addition & 1 deletion playground/components/ComboBoxPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Control {

z: 10
text: "Combo Box";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

ColumnLayout {
Expand Down
2 changes: 1 addition & 1 deletion playground/components/Progress.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Control {

z: 10
text: "Progress";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

ColumnLayout {
Expand Down
2 changes: 1 addition & 1 deletion playground/components/Ranges.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Control {

z: 10
text: "Ranges";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

ColumnLayout {
Expand Down
4 changes: 2 additions & 2 deletions playground/components/Root.qml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Item {
anchors.bottom: parent.bottom
anchors.left: parent.left
anchors.top: parent.top
color: Contemporary.calculateLayer(1)
color: Contemporary.calculateLayer(1).value
radius: 4
width: 300
z: 10
Expand All @@ -22,7 +22,7 @@ Item {
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
innerTopMargin: SafeZone.top
text: qsTr("Components")
z: 10
Expand Down
2 changes: 1 addition & 1 deletion playground/components/TextInput.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Control {

z: 10
text: "Text Input";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

ColumnLayout {
Expand Down
2 changes: 1 addition & 1 deletion playground/components/TumblerPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Control {

z: 10
text: "Tumbler";
color: Contemporary.calculateLayer(2)
color: Contemporary.calculateLayer(2).value
}

RowLayout {
Expand Down

0 comments on commit 43b8cef

Please sign in to comment.