Add a new color and style::complex_color.

This commit is contained in:
John Preston 2021-04-02 14:14:47 +04:00
parent f288c5649c
commit d1dda0b2ac
4 changed files with 89 additions and 4 deletions

View file

@ -140,6 +140,8 @@ boxTitleCloseFgOver: cancelIconFgOver; // settings close icon and box search can
//boxSearchCancelIconFg: cancelIconFg; // search cancel X button icon (like in contacts box) (not implemented yet)
//boxSearchCancelIconFgOver: cancelIconFgOver; // search cancel X button icon with mouse over (not implemented yet)
paymentsTipActive: #01ad0f; // tip button text in payments checkout form
membersAboutLimitFg: windowSubTextFgOver; // text in channel members box about the limit (max 200 last members are shown)
contactsBg: windowBg; // contacts (and some other) box row background

View file

@ -12,11 +12,14 @@ namespace style {
namespace internal {
Color::Proxy Color::operator[](const style::palette &paletteOverride) const {
auto index = main_palette::indexOfColor(*this);
return Proxy((index >= 0) ? paletteOverride.colorAtIndex(index) : (*this));
const auto index = main_palette::indexOfColor(*this);
return { (index >= 0) ? paletteOverride.colorAtIndex(index) : (*this) };
}
ColorData::ColorData(uchar r, uchar g, uchar b, uchar a) : c(int(r), int(g), int(b), int(a)), p(c), b(c) {
ColorData::ColorData(uchar r, uchar g, uchar b, uchar a)
: c(int(r), int(g), int(b), int(a))
, p(c)
, b(c) {
}
void ColorData::set(uchar r, uchar g, uchar b, uchar a) {
@ -25,5 +28,12 @@ void ColorData::set(uchar r, uchar g, uchar b, uchar a) {
this->b = QBrush(c);
}
void ComplexColor::subscribeToPaletteChanges() {
style::PaletteChanged(
) | rpl::start_with_next([=] {
_owned.update(_generator());
}, _lifetime);
}
} // namespace internal
} // namespace style

View file

@ -10,6 +10,8 @@
#include <QtGui/QPen>
#include <QtGui/QBrush>
#include <rpl/lifetime.h>
namespace style {
class palette;
@ -17,13 +19,15 @@ class palette;
namespace internal {
class Color;
class OwnedColor;
class ColorData {
public:
QColor c;
QPen p;
QBrush b;
QColor transparent() const {
[[nodiscard]] QColor transparent() const {
return QColor(c.red(), c.green(), c.blue(), 0);
}
@ -35,6 +39,7 @@ private:
void set(uchar r, uchar g, uchar b, uchar a);
friend class Color;
friend class OwnedColor;
friend class style::palette;
};
@ -73,7 +78,9 @@ public:
Proxy operator[](const style::palette &paletteOverride) const;
private:
friend class OwnedColor;
friend class style::palette;
Color(ColorData *data) : _data(data) {
}
@ -81,6 +88,70 @@ private:
};
class OwnedColor final {
public:
explicit OwnedColor(const QColor &color)
: _data(color.red(), color.green(), color.blue(), color.alpha())
, _color(&_data) {
}
OwnedColor(const OwnedColor &other)
: _data(other._data)
, _color(&_data) {
}
OwnedColor &operator=(const OwnedColor &other) {
_data = other._data;
return *this;
}
void update(const QColor &color) {
_data.set(color.red(), color.green(), color.blue(), color.alpha());
}
[[nodiscard]] const Color &color() const {
return _color;
}
private:
ColorData _data;
Color _color;
};
class ComplexColor final {
public:
explicit ComplexColor(Fn<QColor()> generator)
: _owned(generator())
, _generator(std::move(generator)) {
subscribeToPaletteChanges();
}
ComplexColor(const ComplexColor &other)
: _owned(other._owned)
, _generator(other._generator) {
subscribeToPaletteChanges();
}
ComplexColor &operator=(const ComplexColor &other) {
_owned = other._owned;
_generator = other._generator;
return *this;
}
[[nodiscard]] const Color &color() const {
return _owned.color();
}
private:
void subscribeToPaletteChanges();
OwnedColor _owned;
Fn<QColor()> _generator;
rpl::lifetime _lifetime;
};
class Color::Proxy {
public:
Proxy(Color color) : _color(color) {

View file

@ -28,6 +28,8 @@ using align = Qt::Alignment;
using margins = QMargins;
using font = internal::Font;
using color = internal::Color;
using owned_color = internal::OwnedColor;
using complex_color = internal::ComplexColor;
using icon = internal::Icon;
static constexpr cursor cur_default = Qt::ArrowCursor;