From 3ef71f63b8ab3262a63582ca32a7fcd73fcf506b Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 25 Sep 2019 15:53:59 +0300 Subject: [PATCH] Use RoundRect in RoundButton. --- ui/round_rect.cpp | 4 ++++ ui/round_rect.h | 3 ++- ui/widgets/buttons.cpp | 19 ++++++++----------- ui/widgets/buttons.h | 3 +++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/ui/round_rect.cpp b/ui/round_rect.cpp index 3de09bf..cade79f 100644 --- a/ui/round_rect.cpp +++ b/ui/round_rect.cpp @@ -74,6 +74,10 @@ RoundRect::RoundRect( }, _lifetime); } +const style::color &RoundRect::color() const { + return _color; +} + void RoundRect::paint( QPainter &p, const QRect &rect, diff --git a/ui/round_rect.h b/ui/round_rect.h index 672605a..ee59179 100644 --- a/ui/round_rect.h +++ b/ui/round_rect.h @@ -18,13 +18,14 @@ void DrawRoundedRect( QPainter &p, const QRect &rect, const QBrush &brush, - const std::array & corners, + const std::array &corners, RectParts parts = RectPart::Full); class RoundRect final { public: RoundRect(ImageRoundRadius radius, const style::color &color); + [[nodiscard]] const style::color &color() const; void paint( QPainter &p, const QRect &rect, diff --git a/ui/widgets/buttons.cpp b/ui/widgets/buttons.cpp index 1507927..905efe7 100644 --- a/ui/widgets/buttons.cpp +++ b/ui/widgets/buttons.cpp @@ -232,7 +232,9 @@ RoundButton::RoundButton( const style::RoundButton &st) : RippleButton(parent, st.ripple) , _textFull(std::move(text)) -, _st(st) { +, _st(st) +, _roundRect(ImageRoundRadius::Small, _st.textBg) +, _roundRectOver(ImageRoundRadius::Small, _st.textBgOver) { _textFull.value( ) | rpl::start_with_next([=](const QString &text) { resizeToText(text); @@ -336,29 +338,24 @@ void RoundButton::paintEvent(QPaintEvent *e) { if (_fullWidthOverride < 0) { rounded = QRect(0, rounded.top(), innerWidth - _fullWidthOverride, rounded.height()); } - const auto drawRect = [&](const style::color &color) { + const auto drawRect = [&](const RoundRect &rect) { const auto fill = myrtlrect(rounded); if (_fullRadius) { const auto radius = rounded.height() / 2; PainterHighQualityEnabler hq(p); p.setPen(Qt::NoPen); - p.setBrush(color); + p.setBrush(rect.color()); p.drawRoundedRect(fill, radius, radius); } else { - PainterHighQualityEnabler hq(p); - p.setPen(Qt::NoPen); - p.setBrush(color); - p.drawRoundedRect(fill, st::buttonRadius, st::buttonRadius); - // #TODO ui - //App::roundRect(p, fill, color, ImageRoundRadius::Small); + rect.paint(p, fill); } }; - drawRect(_st.textBg); + drawRect(_roundRect); auto over = isOver(); auto down = isDown(); if (over || down) { - drawRect(_st.textBgOver); + drawRect(_roundRectOver); } paintRipple(p, rounded.x(), rounded.y()); diff --git a/ui/widgets/buttons.h b/ui/widgets/buttons.h index 9d0e3db..4cfffc4 100644 --- a/ui/widgets/buttons.h +++ b/ui/widgets/buttons.h @@ -7,6 +7,7 @@ #pragma once #include "ui/abstract_button.h" +#include "ui/round_rect.h" #include "ui/effects/animations.h" #include "styles/style_widgets.h" @@ -151,6 +152,8 @@ private: int _fullWidthOverride = 0; const style::RoundButton &_st; + RoundRect _roundRect; + RoundRect _roundRectOver; TextTransform _transform = TextTransform::ToUpper; bool _fullRadius = false;