2017-09-25 09:02:55 +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.
|
2017-09-25 09:02:55 +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
|
2017-09-25 09:02:55 +00:00
|
|
|
*/
|
|
|
|
#include "info/profile/info_profile_button.h"
|
|
|
|
|
|
|
|
#include <rpl/never.h>
|
|
|
|
#include "ui/widgets/checkbox.h"
|
|
|
|
#include "styles/style_info.h"
|
|
|
|
|
|
|
|
namespace Info {
|
|
|
|
namespace Profile {
|
|
|
|
|
|
|
|
Button::Button(
|
|
|
|
QWidget *parent,
|
|
|
|
rpl::producer<QString> &&text)
|
|
|
|
: Button(parent, std::move(text), st::infoProfileButton) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Button::Button(
|
|
|
|
QWidget *parent,
|
|
|
|
rpl::producer<QString> &&text,
|
|
|
|
const style::InfoProfileButton &st)
|
|
|
|
: RippleButton(parent, st.ripple)
|
|
|
|
, _st(st) {
|
2017-12-22 07:05:20 +00:00
|
|
|
std::move(
|
|
|
|
text
|
|
|
|
) | rpl::start_with_next([this](QString &&value) {
|
|
|
|
setText(std::move(value));
|
|
|
|
}, lifetime());
|
2017-09-25 09:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Button *Button::toggleOn(rpl::producer<bool> &&toggled) {
|
2017-09-25 16:06:53 +00:00
|
|
|
Expects(_toggle == nullptr);
|
2017-09-25 09:02:55 +00:00
|
|
|
_toggle = std::make_unique<Ui::ToggleView>(
|
|
|
|
isOver() ? _st.toggleOver : _st.toggle,
|
|
|
|
false,
|
|
|
|
[this] { rtlupdate(toggleRect()); });
|
2017-10-03 14:57:11 +00:00
|
|
|
addClickHandler([this] {
|
2018-09-26 11:28:16 +00:00
|
|
|
_toggle->setChecked(!_toggle->checked(), anim::type::normal);
|
2017-10-03 14:57:11 +00:00
|
|
|
});
|
2017-12-22 07:05:20 +00:00
|
|
|
std::move(
|
|
|
|
toggled
|
|
|
|
) | rpl::start_with_next([this](bool toggled) {
|
2018-09-26 11:28:16 +00:00
|
|
|
_toggle->setChecked(toggled, anim::type::normal);
|
2017-12-22 07:05:20 +00:00
|
|
|
}, lifetime());
|
2017-09-30 19:20:40 +00:00
|
|
|
_toggle->finishAnimating();
|
2017-09-25 09:02:55 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:47:28 +00:00
|
|
|
bool Button::toggled() const {
|
|
|
|
return _toggle ? _toggle->checked() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<bool> Button::toggledChanges() const {
|
2017-09-27 12:04:19 +00:00
|
|
|
if (_toggle) {
|
2018-12-06 15:47:28 +00:00
|
|
|
return _toggle->checkedChanges();
|
2017-09-27 12:04:19 +00:00
|
|
|
}
|
|
|
|
return rpl::never<bool>();
|
2017-09-25 09:02:55 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 15:47:28 +00:00
|
|
|
rpl::producer<bool> Button::toggledValue() const {
|
|
|
|
if (_toggle) {
|
|
|
|
return _toggle->checkedValue();
|
|
|
|
}
|
|
|
|
return rpl::never<bool>();
|
2018-09-07 09:40:25 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 16:28:46 +00:00
|
|
|
void Button::setColorOverride(std::optional<QColor> textColorOverride) {
|
2018-04-13 10:54:17 +00:00
|
|
|
_textColorOverride = textColorOverride;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-09-25 09:02:55 +00:00
|
|
|
void Button::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
auto ms = getms();
|
2018-09-13 20:09:26 +00:00
|
|
|
auto paintOver = (isOver() || isDown()) && !isDisabled();
|
2017-09-25 09:02:55 +00:00
|
|
|
p.fillRect(e->rect(), paintOver ? _st.textBgOver : _st.textBg);
|
|
|
|
|
|
|
|
paintRipple(p, 0, 0, ms);
|
|
|
|
|
|
|
|
auto outerw = width();
|
|
|
|
p.setFont(_st.font);
|
2018-04-13 10:54:17 +00:00
|
|
|
p.setPen(_textColorOverride
|
|
|
|
? QPen(*_textColorOverride)
|
|
|
|
: paintOver
|
|
|
|
? _st.textFgOver
|
|
|
|
: _st.textFg);
|
2017-09-25 09:02:55 +00:00
|
|
|
p.drawTextLeft(
|
|
|
|
_st.padding.left(),
|
|
|
|
_st.padding.top(),
|
|
|
|
outerw,
|
|
|
|
_text,
|
|
|
|
_textWidth);
|
|
|
|
|
|
|
|
if (_toggle) {
|
|
|
|
auto rect = toggleRect();
|
|
|
|
_toggle->paint(p, rect.left(), rect.top(), outerw, ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Button::toggleRect() const {
|
|
|
|
Expects(_toggle != nullptr);
|
2018-09-07 09:40:25 +00:00
|
|
|
|
2017-09-25 09:02:55 +00:00
|
|
|
auto size = _toggle->getSize();
|
|
|
|
auto left = width() - _st.toggleSkip - size.width();
|
|
|
|
auto top = (height() - size.height()) / 2;
|
|
|
|
return { QPoint(left, top), size };
|
|
|
|
}
|
|
|
|
|
|
|
|
int Button::resizeGetHeight(int newWidth) {
|
|
|
|
updateVisibleText(newWidth);
|
|
|
|
return _st.padding.top() + _st.height + _st.padding.bottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::onStateChanged(
|
|
|
|
State was,
|
|
|
|
StateChangeSource source) {
|
2018-09-13 20:09:26 +00:00
|
|
|
if (!isDisabled() || !isDown()) {
|
|
|
|
RippleButton::onStateChanged(was, source);
|
|
|
|
}
|
2017-09-25 09:02:55 +00:00
|
|
|
if (_toggle) {
|
|
|
|
_toggle->setStyle(isOver() ? _st.toggleOver : _st.toggle);
|
|
|
|
}
|
2018-09-13 20:09:26 +00:00
|
|
|
setPointerCursor(!isDisabled());
|
2017-09-25 09:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Button::setText(QString &&text) {
|
|
|
|
_original = std::move(text);
|
|
|
|
_originalWidth = _st.font->width(_original);
|
|
|
|
updateVisibleText(width());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Button::updateVisibleText(int newWidth) {
|
|
|
|
auto availableWidth = newWidth
|
|
|
|
- _st.padding.left()
|
|
|
|
- _st.padding.right();
|
|
|
|
if (_toggle) {
|
|
|
|
availableWidth -= (width() - toggleRect().x());
|
|
|
|
}
|
|
|
|
accumulate_max(availableWidth, 0);
|
|
|
|
if (availableWidth < _originalWidth) {
|
|
|
|
_text = _st.font->elided(_original, availableWidth);
|
|
|
|
_textWidth = _st.font->width(_text);
|
|
|
|
} else {
|
|
|
|
_text = _original;
|
|
|
|
_textWidth = _originalWidth;
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Profile
|
|
|
|
} // namespace Info
|