From cf54d9fb12406694a5f8807b93a90c1b34003fb3 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 11 Oct 2022 16:13:20 +0300 Subject: [PATCH] Moved out glare effect to separate file. --- Telegram/SourceFiles/ui/effects/glare.cpp | 76 +++++++++++++++++++ Telegram/SourceFiles/ui/effects/glare.h | 30 ++++++++ .../ui/widgets/gradient_round_button.cpp | 56 ++------------ .../ui/widgets/gradient_round_button.h | 14 +--- Telegram/cmake/td_ui.cmake | 2 + 5 files changed, 116 insertions(+), 62 deletions(-) create mode 100644 Telegram/SourceFiles/ui/effects/glare.cpp create mode 100644 Telegram/SourceFiles/ui/effects/glare.h diff --git a/Telegram/SourceFiles/ui/effects/glare.cpp b/Telegram/SourceFiles/ui/effects/glare.cpp new file mode 100644 index 000000000..e5cda2a29 --- /dev/null +++ b/Telegram/SourceFiles/ui/effects/glare.cpp @@ -0,0 +1,76 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "ui/effects/glare.h" + +#include "styles/style_boxes.h" + +namespace Ui { +namespace { + +constexpr auto kMaxGlareOpaque = 0.5; + +} // namespace + +float64 GlareEffect::progress(crl::time now) const { + return (now - glare.birthTime) + / float64(glare.deathTime - glare.birthTime); +} + +void GlareEffect::validate( + const QColor &color, + Fn updateCallback, + crl::time timeout, + crl::time duration) { + if (anim::Disabled()) { + return; + } + if (!width) { + width = st::gradientButtonGlareWidth; + } + animation.init([=](crl::time now) { + if (const auto diff = (now - glare.deathTime); diff > 0) { + if (diff > timeout && !paused) { + glare = { + .birthTime = now, + .deathTime = now + duration, + }; + updateCallback(); + } + } else { + updateCallback(); + } + }); + animation.start(); + { + auto newPixmap = QPixmap(QSize(width, 1) + * style::DevicePixelRatio()); + newPixmap.setDevicePixelRatio(style::DevicePixelRatio()); + newPixmap.fill(Qt::transparent); + { + auto p = QPainter(&newPixmap); + auto gradient = QLinearGradient( + QPointF(0, 0), + QPointF(width, 0)); + + auto tempColor = color; + tempColor.setAlphaF(0); + const auto edge = tempColor; + tempColor.setAlphaF(kMaxGlareOpaque); + const auto middle = tempColor; + gradient.setStops({ + { 0., edge }, + { .5, middle }, + { 1., edge }, + }); + p.fillRect(newPixmap.rect(), QBrush(gradient)); + } + pixmap = std::move(newPixmap); + } +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/effects/glare.h b/Telegram/SourceFiles/ui/effects/glare.h new file mode 100644 index 000000000..cebf1a9a0 --- /dev/null +++ b/Telegram/SourceFiles/ui/effects/glare.h @@ -0,0 +1,30 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Ui { + +struct GlareEffect final { + void validate( + const QColor &color, + Fn updateCallback, + crl::time timeout, + crl::time duration); + [[nodiscard]] float64 progress(crl::time now) const; + + Ui::Animations::Basic animation; + struct { + crl::time birthTime = 0; + crl::time deathTime = 0; + } glare; + QPixmap pixmap; + int width = 0; + bool paused = false; +}; + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/widgets/gradient_round_button.cpp b/Telegram/SourceFiles/ui/widgets/gradient_round_button.cpp index 1510c19ae..f108d1646 100644 --- a/Telegram/SourceFiles/ui/widgets/gradient_round_button.cpp +++ b/Telegram/SourceFiles/ui/widgets/gradient_round_button.cpp @@ -11,11 +11,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_boxes.h" namespace Ui { -namespace { - -constexpr auto kMaxGlareOpaque = 0.5; - -} // namespace GradientButton::GradientButton(QWidget *widget, QGradientStops stops) : RippleButton(widget, st::defaultRippleAnimation) @@ -37,8 +32,7 @@ void GradientButton::paintGlare(QPainter &p) { if (!_glare.glare.birthTime) { return; } - const auto progress = (crl::now() - _glare.glare.birthTime) - / float64(_glare.glare.deathTime - _glare.glare.birthTime); + const auto progress = _glare.progress(crl::now()); const auto x = (-_glare.width) + (width() + _glare.width * 2) * progress; const auto h = height(); @@ -86,49 +80,11 @@ void GradientButton::setGlarePaused(bool paused) { } void GradientButton::validateGlare() { - if (anim::Disabled()) { - return; - } - _glare.width = st::gradientButtonGlareWidth; - _glare.animation.init([=](crl::time now) { - if (const auto diff = (now - _glare.glare.deathTime); diff > 0) { - if (diff > st::gradientButtonGlareTimeout && !_glare.paused) { - _glare.glare = Glare{ - .birthTime = now, - .deathTime = now + st::gradientButtonGlareDuration, - }; - update(); - } - } else { - update(); - } - }); - _glare.animation.start(); - { - auto pixmap = QPixmap(QSize(_glare.width, 1) - * style::DevicePixelRatio()); - pixmap.setDevicePixelRatio(style::DevicePixelRatio()); - pixmap.fill(Qt::transparent); - { - auto p = QPainter(&pixmap); - auto gradient = QLinearGradient( - QPointF(0, 0), - QPointF(_glare.width, 0)); - - auto color = st::premiumButtonFg->c; - color.setAlphaF(0); - const auto edge = color; - color.setAlphaF(kMaxGlareOpaque); - const auto middle = color; - gradient.setStops({ - { 0., edge }, - { .5, middle }, - { 1., edge }, - }); - p.fillRect(pixmap.rect(), QBrush(gradient)); - } - _glare.pixmap = std::move(pixmap); - } + _glare.validate( + st::premiumButtonFg->c, + [=] { update(); }, + st::gradientButtonGlareTimeout, + st::gradientButtonGlareDuration); } void GradientButton::startGlareAnimation() { diff --git a/Telegram/SourceFiles/ui/widgets/gradient_round_button.h b/Telegram/SourceFiles/ui/widgets/gradient_round_button.h index 0c0e5e277..aab2622ac 100644 --- a/Telegram/SourceFiles/ui/widgets/gradient_round_button.h +++ b/Telegram/SourceFiles/ui/widgets/gradient_round_button.h @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "ui/widgets/buttons.h" +#include "ui/effects/glare.h" namespace Ui { @@ -24,21 +25,10 @@ private: void validateBg(); void validateGlare(); - struct Glare final { - crl::time birthTime = 0; - crl::time deathTime = 0; - }; - QGradientStops _stops; QImage _bg; - struct { - Ui::Animations::Basic animation; - Glare glare; - QPixmap pixmap; - int width = 0; - bool paused = false; - } _glare; + GlareEffect _glare; }; diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 0465b7658..048610152 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -234,6 +234,8 @@ PRIVATE ui/controls/window_outdated_bar.h ui/effects/fireworks_animation.cpp ui/effects/fireworks_animation.h + ui/effects/glare.cpp + ui/effects/glare.h ui/effects/premium_graphics.cpp ui/effects/premium_graphics.h ui/effects/premium_stars.cpp