2016-11-11 13:46:04 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-11-11 13:46:04 +00:00
|
|
|
*/
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
#include "ui/effects/ripple_animation.h"
|
2016-11-21 17:46:29 +00:00
|
|
|
#include "ui/effects/cross_animation.h"
|
2017-11-24 15:47:07 +00:00
|
|
|
#include "ui/effects/numbers_animation.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image_prepare.h"
|
2018-10-25 10:02:37 +00:00
|
|
|
#include "window/themes/window_theme.h"
|
2017-05-30 15:21:05 +00:00
|
|
|
#include "lang/lang_instance.h"
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
namespace Ui {
|
|
|
|
|
2018-05-07 17:44:33 +00:00
|
|
|
LinkButton::LinkButton(
|
|
|
|
QWidget *parent,
|
|
|
|
const QString &text,
|
|
|
|
const style::LinkButton &st)
|
|
|
|
: AbstractButton(parent)
|
|
|
|
, _st(st)
|
2016-11-16 16:04:25 +00:00
|
|
|
, _text(text)
|
2018-05-07 17:44:33 +00:00
|
|
|
, _textWidth(st.font->width(_text)) {
|
|
|
|
resize(
|
|
|
|
naturalWidth(),
|
|
|
|
_st.padding.top() + _st.font->height + _st.padding.bottom());
|
2016-11-16 16:04:25 +00:00
|
|
|
setCursor(style::cur_pointer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LinkButton::naturalWidth() const {
|
2018-05-07 17:44:33 +00:00
|
|
|
return _st.padding.left() + _textWidth + _st.padding.right();
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
2018-05-07 17:44:33 +00:00
|
|
|
|
2018-04-10 11:26:21 +00:00
|
|
|
const auto &font = (isOver() ? _st.overFont : _st.font);
|
|
|
|
const auto pen = _textFgOverride.has_value()
|
|
|
|
? QPen(*_textFgOverride)
|
|
|
|
: isOver()
|
|
|
|
? _st.overColor
|
|
|
|
: _st.color;
|
2016-11-16 16:04:25 +00:00
|
|
|
p.setFont(font);
|
|
|
|
p.setPen(pen);
|
2018-05-07 17:44:33 +00:00
|
|
|
const auto left = _st.padding.left();
|
|
|
|
const auto top = _st.padding.top() + font->ascent;
|
2018-09-07 12:19:56 +00:00
|
|
|
if (width() < naturalWidth()) {
|
2018-05-07 17:44:33 +00:00
|
|
|
const auto available = width() - left - _st.padding.right();
|
|
|
|
p.drawText(left, top, font->elided(_text, available));
|
2016-11-16 16:04:25 +00:00
|
|
|
} else {
|
2018-05-07 17:44:33 +00:00
|
|
|
p.drawText(left, top, _text);
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkButton::setText(const QString &text) {
|
|
|
|
_text = text;
|
|
|
|
_textWidth = _st.font->width(_text);
|
2018-05-07 17:44:33 +00:00
|
|
|
resize(naturalWidth(), _st.font->height);
|
2016-11-16 16:04:25 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
void LinkButton::setColorOverride(std::optional<QColor> textFg) {
|
2018-04-10 11:26:21 +00:00
|
|
|
_textFgOverride = textFg;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void LinkButton::onStateChanged(State was, StateChangeSource source) {
|
2016-11-16 16:04:25 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:14:15 +00:00
|
|
|
RippleButton::RippleButton(QWidget *parent, const style::RippleAnimation &st)
|
|
|
|
: AbstractButton(parent)
|
2016-11-16 16:04:25 +00:00
|
|
|
, _st(st) {
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:14:15 +00:00
|
|
|
void RippleButton::clearState() {
|
|
|
|
AbstractButton::clearState();
|
|
|
|
if (_ripple) {
|
|
|
|
_ripple.reset();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 18:40:23 +00:00
|
|
|
void RippleButton::setForceRippled(
|
|
|
|
bool rippled,
|
|
|
|
anim::type animated) {
|
2016-11-16 16:04:25 +00:00
|
|
|
if (_forceRippled != rippled) {
|
|
|
|
_forceRippled = rippled;
|
|
|
|
if (_forceRippled) {
|
2018-10-25 10:02:37 +00:00
|
|
|
_forceRippledSubscription = base::ObservableViewer(
|
|
|
|
*Window::Theme::Background()
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const Window::Theme::BackgroundUpdate &update) {
|
|
|
|
if (update.paletteChanged() && _ripple) {
|
|
|
|
_ripple->forceRepaint();
|
|
|
|
}
|
|
|
|
});
|
2016-11-16 16:04:25 +00:00
|
|
|
ensureRipple();
|
|
|
|
if (_ripple->empty()) {
|
|
|
|
_ripple->addFading();
|
|
|
|
} else {
|
|
|
|
_ripple->lastUnstop();
|
|
|
|
}
|
2018-10-25 10:02:37 +00:00
|
|
|
} else {
|
|
|
|
if (_ripple) {
|
|
|
|
_ripple->lastStop();
|
|
|
|
}
|
|
|
|
_forceRippledSubscription.destroy();
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-20 18:40:23 +00:00
|
|
|
if (animated == anim::type::instant && _ripple) {
|
2016-11-16 16:04:25 +00:00
|
|
|
_ripple->lastFinish();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void RippleButton::paintRipple(QPainter &p, int x, int y, crl::time ms, const QColor *colorOverride) {
|
2016-11-16 16:04:25 +00:00
|
|
|
if (_ripple) {
|
2016-12-02 19:16:35 +00:00
|
|
|
_ripple->paint(p, x, y, width(), ms, colorOverride);
|
2016-11-16 16:04:25 +00:00
|
|
|
if (_ripple->empty()) {
|
|
|
|
_ripple.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void RippleButton::onStateChanged(State was, StateChangeSource source) {
|
2016-11-16 16:04:25 +00:00
|
|
|
update();
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto wasDown = static_cast<bool>(was & StateFlag::Down);
|
|
|
|
auto down = isDown();
|
2016-11-16 16:04:25 +00:00
|
|
|
if (!_st.showDuration || down == wasDown || _forceRippled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (down && (source == StateChangeSource::ByPress)) {
|
|
|
|
// Start a ripple only from mouse press.
|
2016-12-02 19:16:35 +00:00
|
|
|
auto position = prepareRippleStartPosition();
|
2017-07-09 15:06:27 +00:00
|
|
|
if (position != DisabledRippleStartPosition()) {
|
2016-12-02 19:16:35 +00:00
|
|
|
ensureRipple();
|
|
|
|
_ripple->add(position);
|
|
|
|
}
|
2016-11-16 16:04:25 +00:00
|
|
|
} else if (!down && _ripple) {
|
|
|
|
// Finish ripple anyway.
|
|
|
|
_ripple->lastStop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RippleButton::ensureRipple() {
|
|
|
|
if (!_ripple) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_ripple = std::make_unique<RippleAnimation>(_st, prepareRippleMask(), [this] { update(); });
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage RippleButton::prepareRippleMask() const {
|
|
|
|
return RippleAnimation::rectMask(size());
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint RippleButton::prepareRippleStartPosition() const {
|
|
|
|
return mapFromGlobal(QCursor::pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
RippleButton::~RippleButton() = default;
|
|
|
|
|
|
|
|
FlatButton::FlatButton(QWidget *parent, const QString &text, const style::FlatButton &st) : RippleButton(parent, st.ripple)
|
2016-11-11 13:46:04 +00:00
|
|
|
, _text(text)
|
2016-12-07 13:32:25 +00:00
|
|
|
, _st(st) {
|
2016-11-11 13:46:04 +00:00
|
|
|
if (_st.width < 0) {
|
|
|
|
_width = textWidth() - _st.width;
|
|
|
|
} else if (!_st.width) {
|
|
|
|
_width = textWidth() + _st.height - _st.font->height;
|
|
|
|
} else {
|
|
|
|
_width = _st.width;
|
|
|
|
}
|
|
|
|
resize(_width, _st.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatButton::setText(const QString &text) {
|
|
|
|
_text = text;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FlatButton::setWidth(int32 w) {
|
|
|
|
_width = w;
|
|
|
|
if (_width < 0) {
|
|
|
|
_width = textWidth() - _st.width;
|
|
|
|
} else if (!_width) {
|
|
|
|
_width = textWidth() + _st.height - _st.font->height;
|
|
|
|
}
|
|
|
|
resize(_width, height());
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 FlatButton::textWidth() const {
|
|
|
|
return _st.font->width(_text);
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void FlatButton::onStateChanged(State was, StateChangeSource source) {
|
|
|
|
RippleButton::onStateChanged(was, source);
|
2016-12-07 13:32:25 +00:00
|
|
|
update();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FlatButton::paintEvent(QPaintEvent *e) {
|
|
|
|
QPainter p(this);
|
|
|
|
|
|
|
|
QRect r(0, height() - _st.height, width(), _st.height);
|
2016-12-07 13:32:25 +00:00
|
|
|
p.fillRect(r, isOver() ? _st.overBgColor : _st.bgColor);
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
paintRipple(p, 0, 0, crl::now());
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
p.setFont(isOver() ? _st.overFont : _st.font);
|
2016-11-11 13:46:04 +00:00
|
|
|
p.setRenderHint(QPainter::TextAntialiasing);
|
2016-12-07 13:32:25 +00:00
|
|
|
p.setPen(isOver() ? _st.overColor : _st.color);
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
r.setTop(_st.textTop);
|
2016-11-11 13:46:04 +00:00
|
|
|
p.drawText(r, _text, style::al_top);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
RoundButton::RoundButton(QWidget *parent, Fn<QString()> textFactory, const style::RoundButton &st) : RippleButton(parent, st.ripple)
|
2017-05-30 15:21:05 +00:00
|
|
|
, _textFactory(std::move(textFactory))
|
2016-11-11 13:46:04 +00:00
|
|
|
, _st(st) {
|
2017-05-30 15:21:05 +00:00
|
|
|
subscribe(Lang::Current().updated(), [this] { refreshText(); });
|
|
|
|
refreshText();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RoundButton::setTextTransform(TextTransform transform) {
|
|
|
|
_transform = transform;
|
2017-05-30 15:21:05 +00:00
|
|
|
refreshText();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void RoundButton::setText(Fn<QString()> textFactory) {
|
2017-05-30 15:21:05 +00:00
|
|
|
_textFactory = std::move(textFactory);
|
|
|
|
refreshText();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-29 16:31:01 +00:00
|
|
|
void RoundButton::setNumbersText(const QString &numbersText, int numbers) {
|
|
|
|
if (numbersText.isEmpty()) {
|
|
|
|
_numbers.reset();
|
|
|
|
} else {
|
|
|
|
if (!_numbers) {
|
2017-11-24 15:47:07 +00:00
|
|
|
_numbers = std::make_unique<NumbersAnimation>(_st.font, [this] {
|
|
|
|
numbersAnimationCallback();
|
|
|
|
});
|
2016-12-29 16:31:01 +00:00
|
|
|
}
|
|
|
|
_numbers->setText(numbersText, numbers);
|
|
|
|
}
|
2017-05-30 15:21:05 +00:00
|
|
|
refreshText();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void RoundButton::setWidthChangedCallback(Fn<void()> callback) {
|
2016-12-29 16:31:01 +00:00
|
|
|
if (!_numbers) {
|
2017-11-24 15:47:07 +00:00
|
|
|
_numbers = std::make_unique<NumbersAnimation>(_st.font, [this] {
|
|
|
|
numbersAnimationCallback();
|
|
|
|
});
|
2016-12-29 16:31:01 +00:00
|
|
|
}
|
2017-02-21 13:45:56 +00:00
|
|
|
_numbers->setWidthChangedCallback(std::move(callback));
|
2016-12-29 16:31:01 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
void RoundButton::stepNumbersAnimation(crl::time ms) {
|
2016-12-29 16:31:01 +00:00
|
|
|
if (_numbers) {
|
|
|
|
_numbers->stepAnimation(ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoundButton::finishNumbersAnimation() {
|
|
|
|
if (_numbers) {
|
2017-09-30 19:20:40 +00:00
|
|
|
_numbers->finishAnimating();
|
2016-12-29 16:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoundButton::numbersAnimationCallback() {
|
|
|
|
resizeToText();
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void RoundButton::setFullWidth(int newFullWidth) {
|
|
|
|
_fullWidthOverride = newFullWidth;
|
2018-06-27 15:05:26 +00:00
|
|
|
refreshText();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 20:09:26 +00:00
|
|
|
void RoundButton::setFullRadius(bool enabled) {
|
|
|
|
_fullRadius = enabled;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
void RoundButton::refreshText() {
|
|
|
|
_text = computeFullText();
|
2016-11-11 13:46:04 +00:00
|
|
|
_textWidth = _text.isEmpty() ? 0 : _st.font->width(_text);
|
|
|
|
|
|
|
|
resizeToText();
|
2016-11-24 19:28:23 +00:00
|
|
|
update();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 15:21:05 +00:00
|
|
|
QString RoundButton::computeFullText() const {
|
|
|
|
auto result = _textFactory ? _textFactory() : QString();
|
|
|
|
return (_transform == TextTransform::ToUpper) ? result.toUpper() : result;
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void RoundButton::resizeToText() {
|
|
|
|
int innerWidth = contentWidth();
|
2018-06-08 19:26:08 +00:00
|
|
|
if (_fullWidthOverride > 0) {
|
2018-06-27 15:05:26 +00:00
|
|
|
if (_fullWidthOverride < innerWidth + (_st.height - _st.font->height)) {
|
|
|
|
_text = _st.font->elided(computeFullText(), qMax(_fullWidthOverride - (_st.height - _st.font->height), 1));
|
|
|
|
_textWidth = _st.font->width(_text);
|
|
|
|
}
|
2018-06-08 19:26:08 +00:00
|
|
|
resize(_fullWidthOverride, _st.height + _st.padding.top() + _st.padding.bottom());
|
|
|
|
} else if (_fullWidthOverride < 0) {
|
2016-11-11 13:46:04 +00:00
|
|
|
resize(innerWidth - _fullWidthOverride, _st.height + _st.padding.top() + _st.padding.bottom());
|
|
|
|
} else if (_st.width <= 0) {
|
|
|
|
resize(innerWidth - _st.width + _st.padding.left() + _st.padding.right(), _st.height + _st.padding.top() + _st.padding.bottom());
|
|
|
|
} else {
|
|
|
|
if (_st.width < innerWidth + (_st.height - _st.font->height)) {
|
2017-05-30 15:21:05 +00:00
|
|
|
_text = _st.font->elided(computeFullText(), qMax(_st.width - (_st.height - _st.font->height), 1));
|
2016-12-07 13:32:25 +00:00
|
|
|
_textWidth = _st.font->width(_text);
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
resize(_st.width + _st.padding.left() + _st.padding.right(), _st.height + _st.padding.top() + _st.padding.bottom());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int RoundButton::contentWidth() const {
|
2016-12-29 16:31:01 +00:00
|
|
|
auto result = _textWidth;
|
|
|
|
if (_numbers) {
|
|
|
|
result += (result ? _st.numbersSkip : 0) + _numbers->countWidth();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
2018-04-12 10:20:54 +00:00
|
|
|
if (!_st.icon.empty() && _st.iconPosition.x() < 0) {
|
|
|
|
result += _st.icon.width() - _st.iconPosition.x();
|
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RoundButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
auto innerWidth = contentWidth();
|
|
|
|
auto rounded = rect().marginsRemoved(_st.padding);
|
2016-11-11 13:46:04 +00:00
|
|
|
if (_fullWidthOverride < 0) {
|
|
|
|
rounded = QRect(0, rounded.top(), innerWidth - _fullWidthOverride, rounded.height());
|
|
|
|
}
|
2018-09-13 20:09:26 +00:00
|
|
|
const auto drawRect = [&](const style::color &color) {
|
|
|
|
const auto fill = myrtlrect(rounded);
|
|
|
|
if (_fullRadius) {
|
|
|
|
const auto radius = rounded.height() / 2;
|
|
|
|
PainterHighQualityEnabler hq(p);
|
|
|
|
p.setPen(Qt::NoPen);
|
|
|
|
p.setBrush(color);
|
|
|
|
p.drawRoundedRect(fill, radius, radius);
|
|
|
|
} else {
|
|
|
|
App::roundRect(p, fill, color, ImageRoundRadius::Small);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
drawRect(_st.textBg);
|
2016-11-11 13:46:04 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto over = isOver();
|
|
|
|
auto down = isDown();
|
2016-11-16 10:44:06 +00:00
|
|
|
if (over || down) {
|
2018-09-13 20:09:26 +00:00
|
|
|
drawRect(_st.textBgOver);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-11-16 16:04:25 +00:00
|
|
|
paintRipple(p, rounded.x(), rounded.y(), ms);
|
2016-11-11 13:46:04 +00:00
|
|
|
|
|
|
|
p.setFont(_st.font);
|
2018-04-12 10:20:54 +00:00
|
|
|
const auto textTop = _st.padding.top() + _st.textTop;
|
|
|
|
auto textLeft = _st.padding.left()
|
|
|
|
+ ((width()
|
|
|
|
- innerWidth
|
|
|
|
- _st.padding.left()
|
|
|
|
- _st.padding.right()) / 2);
|
2016-11-11 13:46:04 +00:00
|
|
|
if (_fullWidthOverride < 0) {
|
|
|
|
textLeft = -_fullWidthOverride / 2;
|
|
|
|
}
|
2018-04-12 10:20:54 +00:00
|
|
|
if (!_st.icon.empty() && _st.iconPosition.x() < 0) {
|
|
|
|
textLeft += _st.icon.width() - _st.iconPosition.x();
|
|
|
|
}
|
|
|
|
const auto iconLeft = (_st.iconPosition.x() >= 0)
|
|
|
|
? _st.iconPosition.x()
|
|
|
|
: (textLeft + _st.iconPosition.x() - _st.icon.width());
|
|
|
|
const auto iconTop = (_st.iconPosition.y() >= 0)
|
|
|
|
? _st.iconPosition.y()
|
|
|
|
: (textTop + _st.iconPosition.y());
|
2016-11-11 13:46:04 +00:00
|
|
|
if (!_text.isEmpty()) {
|
2016-11-15 11:56:49 +00:00
|
|
|
p.setPen((over || down) ? _st.textFgOver : _st.textFg);
|
2016-11-11 13:46:04 +00:00
|
|
|
p.drawTextLeft(textLeft, textTop, width(), _text);
|
|
|
|
}
|
2016-12-29 16:31:01 +00:00
|
|
|
if (_numbers) {
|
|
|
|
textLeft += _textWidth + (_textWidth ? _st.numbersSkip : 0);
|
|
|
|
p.setPen((over || down) ? _st.numbersTextFgOver : _st.numbersTextFg);
|
|
|
|
_numbers->paint(p, textLeft, textTop, width());
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
2018-04-12 10:20:54 +00:00
|
|
|
if (!_st.icon.empty()) {
|
|
|
|
_st.icon.paint(p, QPoint(iconLeft, iconTop), width());
|
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 11:56:49 +00:00
|
|
|
QImage RoundButton::prepareRippleMask() const {
|
|
|
|
auto innerWidth = contentWidth();
|
|
|
|
auto rounded = rtlrect(rect().marginsRemoved(_st.padding), width());
|
|
|
|
if (_fullWidthOverride < 0) {
|
|
|
|
rounded = QRect(0, rounded.top(), innerWidth - _fullWidthOverride, rounded.height());
|
|
|
|
}
|
2018-09-13 20:09:26 +00:00
|
|
|
return RippleAnimation::roundRectMask(
|
|
|
|
rounded.size(),
|
|
|
|
_fullRadius ? (rounded.height() / 2) : st::buttonRadius);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
QPoint RoundButton::prepareRippleStartPosition() const {
|
|
|
|
return mapFromGlobal(QCursor::pos()) - QPoint(_st.padding.left(), _st.padding.top());
|
|
|
|
}
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-12-29 16:31:01 +00:00
|
|
|
RoundButton::~RoundButton() = default;
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
IconButton::IconButton(QWidget *parent, const style::IconButton &st) : RippleButton(parent, st.ripple)
|
2016-11-11 13:46:04 +00:00
|
|
|
, _st(st) {
|
|
|
|
resize(_st.width, _st.height);
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void IconButton::setIconOverride(const style::icon *iconOverride, const style::icon *iconOverOverride) {
|
|
|
|
_iconOverride = iconOverride;
|
|
|
|
_iconOverrideOver = iconOverOverride;
|
2016-11-11 13:46:04 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void IconButton::setRippleColorOverride(const style::color *colorOverride) {
|
|
|
|
_rippleColorOverride = colorOverride;
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void IconButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
paintRipple(p, _st.rippleAreaPosition.x(), _st.rippleAreaPosition.y(), ms, _rippleColorOverride ? &(*_rippleColorOverride)->c : nullptr);
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto down = isDown();
|
2019-02-19 06:57:53 +00:00
|
|
|
auto overIconOpacity = (down || forceRippled()) ? 1. : _a_over.current(crl::now(), isOver() ? 1. : 0.);
|
2016-11-11 13:46:04 +00:00
|
|
|
auto overIcon = [this] {
|
|
|
|
if (_iconOverrideOver) {
|
|
|
|
return _iconOverrideOver;
|
|
|
|
} else if (!_st.iconOver.empty()) {
|
|
|
|
return &_st.iconOver;
|
|
|
|
} else if (_iconOverride) {
|
|
|
|
return _iconOverride;
|
|
|
|
}
|
|
|
|
return &_st.icon;
|
|
|
|
};
|
|
|
|
auto justIcon = [this] {
|
|
|
|
if (_iconOverride) {
|
|
|
|
return _iconOverride;
|
|
|
|
}
|
|
|
|
return &_st.icon;
|
|
|
|
};
|
2016-11-16 12:06:02 +00:00
|
|
|
auto icon = (overIconOpacity == 1.) ? overIcon() : justIcon();
|
2016-11-16 16:04:25 +00:00
|
|
|
auto position = _st.iconPosition;
|
2016-11-11 13:46:04 +00:00
|
|
|
if (position.x() < 0) {
|
|
|
|
position.setX((width() - icon->width()) / 2);
|
|
|
|
}
|
|
|
|
if (position.y() < 0) {
|
|
|
|
position.setY((height() - icon->height()) / 2);
|
|
|
|
}
|
|
|
|
icon->paint(p, position, width());
|
2016-11-16 12:06:02 +00:00
|
|
|
if (overIconOpacity > 0. && overIconOpacity < 1.) {
|
2016-11-11 13:46:04 +00:00
|
|
|
auto iconOver = overIcon();
|
|
|
|
if (iconOver != icon) {
|
2016-11-16 12:06:02 +00:00
|
|
|
p.setOpacity(overIconOpacity);
|
2016-11-11 13:46:04 +00:00
|
|
|
iconOver->paint(p, position, width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void IconButton::onStateChanged(State was, StateChangeSource source) {
|
|
|
|
RippleButton::onStateChanged(was, source);
|
2016-11-16 16:04:25 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto over = isOver();
|
|
|
|
auto wasOver = static_cast<bool>(was & StateFlag::Over);
|
|
|
|
if (over != wasOver) {
|
2016-11-11 13:46:04 +00:00
|
|
|
if (_st.duration) {
|
|
|
|
auto from = over ? 0. : 1.;
|
|
|
|
auto to = over ? 1. : 0.;
|
|
|
|
_a_over.start([this] { update(); }, from, to, _st.duration);
|
|
|
|
} else {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 16:04:25 +00:00
|
|
|
}
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
QPoint IconButton::prepareRippleStartPosition() const {
|
2017-09-30 18:26:45 +00:00
|
|
|
auto result = mapFromGlobal(QCursor::pos())
|
|
|
|
- _st.rippleAreaPosition;
|
|
|
|
auto rect = QRect(0, 0, _st.rippleAreaSize, _st.rippleAreaSize);
|
|
|
|
return rect.contains(result)
|
|
|
|
? result
|
|
|
|
: DisabledRippleStartPosition();
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
QImage IconButton::prepareRippleMask() const {
|
|
|
|
return RippleAnimation::ellipseMask(QSize(_st.rippleAreaSize, _st.rippleAreaSize));
|
|
|
|
}
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
LeftOutlineButton::LeftOutlineButton(QWidget *parent, const QString &text, const style::OutlineButton &st) : RippleButton(parent, st.ripple)
|
|
|
|
, _text(text)
|
|
|
|
, _fullText(text)
|
|
|
|
, _textWidth(st.font->width(_text))
|
|
|
|
, _fullTextWidth(_textWidth)
|
|
|
|
, _st(st) {
|
|
|
|
resizeToWidth(_textWidth + _st.padding.left() + _st.padding.right());
|
|
|
|
|
|
|
|
setCursor(style::cur_pointer);
|
2016-11-16 12:06:02 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
void LeftOutlineButton::setText(const QString &text) {
|
|
|
|
_text = text;
|
|
|
|
_fullText = text;
|
|
|
|
_fullTextWidth = _textWidth = _st.font->width(_text);
|
|
|
|
resizeToWidth(width());
|
|
|
|
update();
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
int LeftOutlineButton::resizeGetHeight(int newWidth) {
|
|
|
|
int availableWidth = qMax(newWidth - _st.padding.left() - _st.padding.right(), 1);
|
|
|
|
if ((availableWidth < _fullTextWidth) || (_textWidth < availableWidth)) {
|
|
|
|
_text = _st.font->elided(_fullText, availableWidth);
|
|
|
|
_textWidth = _st.font->width(_text);
|
2016-11-15 11:56:49 +00:00
|
|
|
}
|
2016-11-16 16:04:25 +00:00
|
|
|
return _st.padding.top() + _st.font->height + _st.padding.bottom();
|
2016-11-11 13:46:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 16:04:25 +00:00
|
|
|
void LeftOutlineButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto over = isOver();
|
|
|
|
auto down = isDown();
|
2016-11-16 16:04:25 +00:00
|
|
|
if (width() > _st.outlineWidth) {
|
|
|
|
p.fillRect(rtlrect(_st.outlineWidth, 0, width() - _st.outlineWidth, height(), width()), (over || down) ? _st.textBgOver : _st.textBg);
|
2019-02-19 06:57:53 +00:00
|
|
|
paintRipple(p, 0, 0, crl::now());
|
2016-11-16 16:04:25 +00:00
|
|
|
p.fillRect(rtlrect(0, 0, _st.outlineWidth, height(), width()), (over || down) ? _st.outlineFgOver : _st.outlineFg);
|
|
|
|
}
|
|
|
|
p.setFont(_st.font);
|
|
|
|
p.setPen((over || down) ? _st.textFgOver : _st.textFg);
|
|
|
|
p.drawTextLeft(_st.padding.left(), _st.padding.top(), width(), _text, _textWidth);
|
|
|
|
}
|
2016-11-15 11:56:49 +00:00
|
|
|
|
2016-11-21 17:46:29 +00:00
|
|
|
CrossButton::CrossButton(QWidget *parent, const style::CrossButton &st) : RippleButton(parent, st.ripple)
|
2017-04-04 13:19:49 +00:00
|
|
|
, _st(st)
|
2019-02-04 13:34:50 +00:00
|
|
|
, _a_loading([=](crl::time duration) { return loadingCallback(duration); }) {
|
2016-11-21 17:46:29 +00:00
|
|
|
resize(_st.width, _st.height);
|
|
|
|
setCursor(style::cur_pointer);
|
2017-11-27 11:43:57 +00:00
|
|
|
setVisible(false);
|
2016-11-21 17:46:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:34:50 +00:00
|
|
|
bool CrossButton::loadingCallback(crl::time duration) {
|
|
|
|
const auto result = !stopLoadingAnimation(duration);
|
|
|
|
if (!result || !anim::Disabled()) {
|
2017-04-04 13:19:49 +00:00
|
|
|
update();
|
|
|
|
}
|
2019-02-04 13:34:50 +00:00
|
|
|
return result;
|
2017-04-04 13:19:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
void CrossButton::toggle(bool visible, anim::type animated) {
|
|
|
|
if (_shown != visible) {
|
|
|
|
_shown = visible;
|
|
|
|
if (animated == anim::type::normal) {
|
|
|
|
if (isHidden()) {
|
|
|
|
setVisible(true);
|
|
|
|
}
|
|
|
|
_a_show.start(
|
2019-02-04 13:34:50 +00:00
|
|
|
[=] { animationCallback(); },
|
2017-11-27 11:43:57 +00:00
|
|
|
_shown ? 0. : 1.,
|
|
|
|
_shown ? 1. : 0.,
|
|
|
|
_st.duration);
|
|
|
|
}
|
2017-03-31 15:50:02 +00:00
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
if (animated == anim::type::instant) {
|
|
|
|
finishAnimating();
|
2017-03-31 15:50:02 +00:00
|
|
|
}
|
2016-11-21 17:46:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CrossButton::animationCallback() {
|
|
|
|
update();
|
2017-11-27 11:43:57 +00:00
|
|
|
if (!_a_show.animating()) {
|
|
|
|
setVisible(_shown);
|
2016-11-21 17:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrossButton::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2016-12-05 11:01:08 +00:00
|
|
|
auto over = isOver();
|
2019-02-04 13:34:50 +00:00
|
|
|
auto shown = _a_show.value(_shown ? 1. : 0.);
|
2016-11-21 17:46:29 +00:00
|
|
|
p.setOpacity(shown);
|
|
|
|
|
|
|
|
paintRipple(p, _st.crossPosition.x(), _st.crossPosition.y(), ms);
|
|
|
|
|
2017-04-04 13:19:49 +00:00
|
|
|
auto loading = 0.;
|
|
|
|
if (_a_loading.animating()) {
|
2019-02-04 13:34:50 +00:00
|
|
|
const auto duration = (ms - _loadingStartMs);
|
|
|
|
if (stopLoadingAnimation(duration)) {
|
2017-04-04 13:19:49 +00:00
|
|
|
_a_loading.stop();
|
2018-09-20 16:39:59 +00:00
|
|
|
} else if (anim::Disabled()) {
|
|
|
|
CrossAnimation::paintStaticLoading(
|
|
|
|
p,
|
|
|
|
_st.cross,
|
|
|
|
over ? _st.crossFgOver : _st.crossFg,
|
|
|
|
_st.crossPosition.x(),
|
|
|
|
_st.crossPosition.y(),
|
|
|
|
width(),
|
|
|
|
shown);
|
|
|
|
return;
|
2017-04-04 13:19:49 +00:00
|
|
|
} else {
|
2019-02-04 13:34:50 +00:00
|
|
|
loading = (duration % _st.loadingPeriod)
|
2018-09-20 16:39:59 +00:00
|
|
|
/ float64(_st.loadingPeriod);
|
2017-04-04 13:19:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 16:39:59 +00:00
|
|
|
CrossAnimation::paint(
|
|
|
|
p,
|
|
|
|
_st.cross,
|
|
|
|
over ? _st.crossFgOver : _st.crossFg,
|
|
|
|
_st.crossPosition.x(),
|
|
|
|
_st.crossPosition.y(),
|
|
|
|
width(),
|
|
|
|
shown,
|
|
|
|
loading);
|
2017-04-04 13:19:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 13:34:50 +00:00
|
|
|
bool CrossButton::stopLoadingAnimation(crl::time duration) {
|
2017-04-04 13:19:49 +00:00
|
|
|
if (!_loadingStopMs) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-04 13:34:50 +00:00
|
|
|
const auto stopPeriod = (_loadingStopMs - _loadingStartMs)
|
|
|
|
/ _st.loadingPeriod;
|
|
|
|
const auto currentPeriod = duration / _st.loadingPeriod;
|
2017-04-04 13:19:49 +00:00
|
|
|
if (currentPeriod != stopPeriod) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(currentPeriod > stopPeriod);
|
2017-04-04 13:19:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrossButton::setLoadingAnimation(bool enabled) {
|
|
|
|
if (enabled) {
|
|
|
|
_loadingStopMs = 0;
|
|
|
|
if (!_a_loading.animating()) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_loadingStartMs = crl::now();
|
2017-04-04 13:19:49 +00:00
|
|
|
_a_loading.start();
|
|
|
|
}
|
|
|
|
} else if (_a_loading.animating()) {
|
2019-02-19 06:57:53 +00:00
|
|
|
_loadingStopMs = crl::now();
|
2017-04-04 13:19:49 +00:00
|
|
|
if (!((_loadingStopMs - _loadingStartMs) % _st.loadingPeriod)) {
|
|
|
|
_a_loading.stop();
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 16:39:59 +00:00
|
|
|
if (anim::Disabled()) {
|
|
|
|
update();
|
|
|
|
}
|
2016-11-21 17:46:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void CrossButton::onStateChanged(State was, StateChangeSource source) {
|
|
|
|
RippleButton::onStateChanged(was, source);
|
2016-11-21 17:46:29 +00:00
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
auto over = isOver();
|
|
|
|
auto wasOver = static_cast<bool>(was & StateFlag::Over);
|
|
|
|
if (over != wasOver) {
|
2016-11-21 17:46:29 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint CrossButton::prepareRippleStartPosition() const {
|
|
|
|
return mapFromGlobal(QCursor::pos()) - _st.crossPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage CrossButton::prepareRippleMask() const {
|
|
|
|
return RippleAnimation::ellipseMask(QSize(_st.cross.size, _st.cross.size));
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
} // namespace Ui
|