Moved out gradient round button to td_ui.

This commit is contained in:
23rd 2022-05-19 03:30:46 +03:00
parent 8a87f2996a
commit 8e80b9cb06
5 changed files with 79 additions and 48 deletions

View File

@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/chat/chat_theme.h"
#include "ui/layers/generic_box.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/gradient_round_button.h"
#include "ui/wrap/padding_wrap.h"
#include "lottie/lottie_single_player.h"
#include "history/view/media/history_view_sticker.h"
@ -139,56 +140,10 @@ void PreloadSticker(const std::shared_ptr<Data::DocumentMedia> &media) {
return result;
}
class GradientButton final : public Ui::RippleButton {
public:
GradientButton(QWidget *widget, QGradientStops stops);
private:
void paintEvent(QPaintEvent *e);
void validateBg();
QGradientStops _stops;
QImage _bg;
};
GradientButton::GradientButton(QWidget *widget, QGradientStops stops)
: RippleButton(widget, st::defaultRippleAnimation)
, _stops(std::move(stops)) {
}
void GradientButton::paintEvent(QPaintEvent *e) {
QPainter p(this);
validateBg();
p.drawImage(0, 0, _bg);
const auto ripple = QColor(0, 0, 0, 36);
paintRipple(p, 0, 0, &ripple);
}
void GradientButton::validateBg() {
const auto factor = devicePixelRatio();
if (!_bg.isNull()
&& (_bg.devicePixelRatio() == factor)
&& (_bg.size() == size() * factor)) {
return;
}
_bg = QImage(size() * factor, QImage::Format_ARGB32_Premultiplied);
_bg.setDevicePixelRatio(factor);
auto p = QPainter(&_bg);
auto gradient = QLinearGradient(QPointF(0, 0), QPointF(width(), 0));
gradient.setStops(_stops);
p.fillRect(rect(), gradient);
p.end();
_bg = Images::Round(std::move(_bg), ImageRoundRadius::Large);
}
[[nodiscard]] object_ptr<Ui::AbstractButton> CreateGradientButton(
QWidget *parent,
QGradientStops stops) {
return object_ptr<GradientButton>(parent, std::move(stops));
return object_ptr<Ui::GradientButton>(parent, std::move(stops));
}
[[nodiscard]] object_ptr<Ui::AbstractButton> CreatePremiumButton(

View File

@ -0,0 +1,47 @@
/*
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/widgets/gradient_round_button.h"
#include "ui/image/image_prepare.h"
namespace Ui {
GradientButton::GradientButton(QWidget *widget, QGradientStops stops)
: RippleButton(widget, st::defaultRippleAnimation)
, _stops(std::move(stops)) {
}
void GradientButton::paintEvent(QPaintEvent *e) {
QPainter p(this);
validateBg();
p.drawImage(0, 0, _bg);
const auto ripple = QColor(0, 0, 0, 36);
paintRipple(p, 0, 0, &ripple);
}
void GradientButton::validateBg() {
const auto factor = devicePixelRatio();
if (!_bg.isNull()
&& (_bg.devicePixelRatio() == factor)
&& (_bg.size() == size() * factor)) {
return;
}
_bg = QImage(size() * factor, QImage::Format_ARGB32_Premultiplied);
_bg.setDevicePixelRatio(factor);
auto p = QPainter(&_bg);
auto gradient = QLinearGradient(QPointF(0, 0), QPointF(width(), 0));
gradient.setStops(_stops);
p.fillRect(rect(), gradient);
p.end();
_bg = Images::Round(std::move(_bg), ImageRoundRadius::Large);
}
} // namespace Ui

View File

@ -0,0 +1,27 @@
/*
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
#include "ui/widgets/buttons.h"
namespace Ui {
class GradientButton final : public Ui::RippleButton {
public:
GradientButton(QWidget *widget, QGradientStops stops);
private:
void paintEvent(QPaintEvent *e);
void validateBg();
QGradientStops _stops;
QImage _bg;
};
} // namespace Ui

View File

@ -241,6 +241,8 @@ PRIVATE
ui/widgets/continuous_sliders.h
ui/widgets/discrete_sliders.cpp
ui/widgets/discrete_sliders.h
ui/widgets/gradient_round_button.cpp
ui/widgets/gradient_round_button.h
ui/widgets/multi_select.cpp
ui/widgets/multi_select.h
ui/widgets/sent_code_field.cpp

@ -1 +1 @@
Subproject commit 9c09b1fad07d5048b55a1dfcb27c7d1a585bba55
Subproject commit cea7f128a55a3f93df956ad21eb51634f851cc70