2014-05-30 08:53:19 +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.
|
2014-05-30 08:53:19 +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
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2016-11-11 13:46:04 +00:00
|
|
|
#include "ui/abstract_button.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-10-01 09:39:07 +00:00
|
|
|
#include <rpl/filter.h>
|
|
|
|
#include <rpl/mappers.h>
|
2017-11-21 10:27:37 +00:00
|
|
|
#include "messenger.h"
|
2017-10-01 09:39:07 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
namespace Ui {
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-10-01 09:39:07 +00:00
|
|
|
AbstractButton::AbstractButton(QWidget *parent) : RpWidget(parent) {
|
|
|
|
setMouseTracking(true);
|
|
|
|
|
|
|
|
using namespace rpl::mappers;
|
|
|
|
shownValue()
|
2017-11-20 12:32:55 +00:00
|
|
|
| rpl::filter(_1 == false)
|
2017-10-01 09:39:07 +00:00
|
|
|
| rpl::start_with_next([this] { clearState(); }, lifetime());
|
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void AbstractButton::leaveEventHook(QEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_state & StateFlag::Down) return;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2017-02-11 11:24:37 +00:00
|
|
|
return TWidget::leaveEventHook(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:24:37 +00:00
|
|
|
void AbstractButton::enterEventHook(QEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
checkIfOver(mapFromGlobal(QCursor::pos()));
|
2017-02-11 11:24:37 +00:00
|
|
|
return TWidget::enterEventHook(e);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setAcceptBoth(bool acceptBoth) {
|
2014-05-30 08:53:19 +00:00
|
|
|
_acceptBoth = acceptBoth;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
void AbstractButton::checkIfOver(QPoint localPos) {
|
|
|
|
auto over = rect().marginsRemoved(getMargins()).contains(localPos);
|
|
|
|
setOver(over, StateChangeSource::ByHover);
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mousePressEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
checkIfOver(e->pos());
|
2016-12-02 19:16:35 +00:00
|
|
|
if (_acceptBoth || (e->buttons() & Qt::LeftButton)) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if ((_state & StateFlag::Over) && !(_state & StateFlag::Down)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state |= StateFlag::Down;
|
|
|
|
onStateChanged(was, StateChangeSource::ByPress);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
|
|
|
e->accept();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mouseMoveEvent(QMouseEvent *e) {
|
2016-12-02 19:16:35 +00:00
|
|
|
if (rect().marginsRemoved(getMargins()).contains(e->pos())) {
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(true, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-11-11 13:46:04 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::mouseReleaseEvent(QMouseEvent *e) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (_state & StateFlag::Down) {
|
2018-12-05 09:55:56 +00:00
|
|
|
const auto was = _state;
|
2016-12-05 11:01:08 +00:00
|
|
|
_state &= ~State(StateFlag::Down);
|
2018-12-05 09:55:56 +00:00
|
|
|
|
|
|
|
auto weak = make_weak(this);
|
2016-12-05 11:01:08 +00:00
|
|
|
onStateChanged(was, StateChangeSource::ByPress);
|
2018-12-05 09:55:56 +00:00
|
|
|
if (!weak) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-05 11:01:08 +00:00
|
|
|
if (was & StateFlag::Over) {
|
2014-12-20 21:33:08 +00:00
|
|
|
_modifiers = e->modifiers();
|
2018-11-26 11:55:02 +00:00
|
|
|
if (const auto callback = _clickedCallback) {
|
|
|
|
callback();
|
2016-11-24 19:28:23 +00:00
|
|
|
} else {
|
|
|
|
emit clicked();
|
2016-08-17 15:14:08 +00:00
|
|
|
}
|
2017-11-30 18:04:13 +00:00
|
|
|
if (weak) {
|
2018-09-13 20:09:26 +00:00
|
|
|
_clicks.fire(e->button());
|
2017-09-25 16:06:53 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
} else {
|
2016-12-05 11:01:08 +00:00
|
|
|
setOver(false, StateChangeSource::ByHover);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 13:34:41 +00:00
|
|
|
void AbstractButton::setPointerCursor(bool enablePointerCursor) {
|
|
|
|
if (_enablePointerCursor != enablePointerCursor) {
|
|
|
|
_enablePointerCursor = enablePointerCursor;
|
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setOver(bool over, StateChangeSource source) {
|
2016-12-05 11:01:08 +00:00
|
|
|
if (over && !(_state & StateFlag::Over)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state |= StateFlag::Over;
|
2017-11-21 10:27:37 +00:00
|
|
|
Messenger::Instance().registerLeaveSubscription(this);
|
2016-12-05 11:01:08 +00:00
|
|
|
onStateChanged(was, source);
|
|
|
|
} else if (!over && (_state & StateFlag::Over)) {
|
|
|
|
auto was = _state;
|
|
|
|
_state &= ~State(StateFlag::Over);
|
2017-11-21 10:27:37 +00:00
|
|
|
Messenger::Instance().unregisterLeaveSubscription(this);
|
2016-12-05 11:01:08 +00:00
|
|
|
onStateChanged(was, source);
|
2014-09-29 02:47:30 +00:00
|
|
|
}
|
2016-12-31 13:34:41 +00:00
|
|
|
updateCursor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractButton::updateCursor() {
|
|
|
|
auto pointerCursor = _enablePointerCursor && (_state & StateFlag::Over);
|
|
|
|
setCursor(pointerCursor ? style::cur_pointer : style::cur_default);
|
2014-09-29 02:47:30 +00:00
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::setDisabled(bool disabled) {
|
2016-12-05 11:01:08 +00:00
|
|
|
auto was = _state;
|
|
|
|
if (disabled && !(_state & StateFlag::Disabled)) {
|
|
|
|
_state |= StateFlag::Disabled;
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
|
|
|
} else if (!disabled && (_state & StateFlag::Disabled)) {
|
|
|
|
_state &= ~State(StateFlag::Disabled);
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 13:46:04 +00:00
|
|
|
void AbstractButton::clearState() {
|
2016-12-05 11:01:08 +00:00
|
|
|
auto was = _state;
|
|
|
|
_state = StateFlag::None;
|
|
|
|
onStateChanged(was, StateChangeSource::ByUser);
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2016-11-11 13:46:04 +00:00
|
|
|
|
|
|
|
} // namespace Ui
|