2017-04-08 13:27:53 +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-04-08 13:27:53 +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-04-08 13:27:53 +00:00
|
|
|
*/
|
|
|
|
#include "chat_helpers/bot_keyboard.h"
|
|
|
|
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-04-25 12:45:15 +00:00
|
|
|
#include "data/data_session.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-10-13 15:11:53 +00:00
|
|
|
#include "ui/cached_round_corners.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "styles/style_widgets.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "styles/style_chat.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class Style : public ReplyKeyboard::Style {
|
|
|
|
public:
|
|
|
|
Style(
|
|
|
|
not_null<BotKeyboard*> parent,
|
|
|
|
const style::BotKeyboardButton &st);
|
|
|
|
|
|
|
|
int buttonRadius() const override;
|
|
|
|
|
|
|
|
void startPaint(Painter &p) const override;
|
|
|
|
const style::TextStyle &textStyle() const override;
|
|
|
|
void repaint(not_null<const HistoryItem*> item) const override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintButtonBg(
|
|
|
|
Painter &p,
|
|
|
|
const QRect &rect,
|
|
|
|
float64 howMuchOver) const override;
|
|
|
|
void paintButtonIcon(
|
|
|
|
Painter &p,
|
|
|
|
const QRect &rect,
|
|
|
|
int outerWidth,
|
|
|
|
HistoryMessageMarkupButton::Type type) const override;
|
|
|
|
void paintButtonLoading(Painter &p, const QRect &rect) const override;
|
|
|
|
int minButtonWidth(HistoryMessageMarkupButton::Type type) const override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
not_null<BotKeyboard*> _parent;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Style::Style(
|
|
|
|
not_null<BotKeyboard*> parent,
|
|
|
|
const style::BotKeyboardButton &st)
|
|
|
|
: ReplyKeyboard::Style(st), _parent(parent) {
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
void Style::startPaint(Painter &p) const {
|
2017-04-08 13:27:53 +00:00
|
|
|
p.setPen(st::botKbColor);
|
|
|
|
p.setFont(st::botKbStyle.font);
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
const style::TextStyle &Style::textStyle() const {
|
2017-04-08 13:27:53 +00:00
|
|
|
return st::botKbStyle;
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
void Style::repaint(not_null<const HistoryItem*> item) const {
|
2017-04-08 13:27:53 +00:00
|
|
|
_parent->update();
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
int Style::buttonRadius() const {
|
2020-12-08 07:19:23 +00:00
|
|
|
return st::roundRadiusSmall;
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
void Style::paintButtonBg(
|
|
|
|
Painter &p,
|
|
|
|
const QRect &rect,
|
|
|
|
float64 howMuchOver) const {
|
2020-10-13 15:11:53 +00:00
|
|
|
Ui::FillRoundRect(p, rect, st::botKbBg, Ui::BotKeyboardCorners);
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
void Style::paintButtonIcon(
|
|
|
|
Painter &p,
|
|
|
|
const QRect &rect,
|
|
|
|
int outerWidth,
|
|
|
|
HistoryMessageMarkupButton::Type type) const {
|
2017-04-08 13:27:53 +00:00
|
|
|
// Buttons with icons should not appear here.
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
void Style::paintButtonLoading(Painter &p, const QRect &rect) const {
|
2017-04-08 13:27:53 +00:00
|
|
|
// Buttons with loading progress should not appear here.
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
int Style::minButtonWidth(HistoryMessageMarkupButton::Type type) const {
|
2017-04-08 13:27:53 +00:00
|
|
|
int result = 2 * buttonPadding();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-18 15:44:50 +00:00
|
|
|
} // namespace
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
BotKeyboard::BotKeyboard(not_null<Main::Session*> session, QWidget *parent)
|
|
|
|
: TWidget(parent)
|
|
|
|
, _session(session)
|
2017-12-18 15:44:50 +00:00
|
|
|
, _st(&st::botKbButton) {
|
|
|
|
setGeometry(0, 0, _st->margin, st::botKbScroll.deltat);
|
|
|
|
_height = st::botKbScroll.deltat;
|
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
|
|
|
auto clip = e->rect();
|
|
|
|
p.fillRect(clip, st::historyComposeAreaBg);
|
|
|
|
|
|
|
|
if (_impl) {
|
|
|
|
int x = rtl() ? st::botKbScroll.width : _st->margin;
|
|
|
|
p.translate(x, st::botKbScroll.deltat);
|
2019-04-02 09:13:30 +00:00
|
|
|
_impl->paint(p, width(), clip.translated(-x, -st::botKbScroll.deltat));
|
2017-12-18 15:44:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
void BotKeyboard::mousePressEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
ClickHandler::pressed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
if (ClickHandlerPtr activated = ClickHandler::unpressed()) {
|
2019-09-16 11:14:06 +00:00
|
|
|
ActivateClickHandler(window(), activated, e->button());
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::enterEventHook(QEvent *e) {
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::leaveEventHook(QEvent *e) {
|
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::moderateKeyActivate(int key) {
|
2020-06-08 09:06:50 +00:00
|
|
|
if (const auto item = _session->data().message(_wasForMsgId)) {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (const auto markup = item->Get<HistoryMessageReplyMarkup>()) {
|
2021-04-27 08:47:58 +00:00
|
|
|
if (key >= Qt::Key_1 && key <= Qt::Key_2) {
|
2017-12-18 10:38:14 +00:00
|
|
|
const auto index = int(key - Qt::Key_1);
|
|
|
|
if (!markup->rows.empty()
|
|
|
|
&& index >= 0
|
|
|
|
&& index < int(markup->rows.front().size())) {
|
2017-04-08 13:27:53 +00:00
|
|
|
App::activateBotCommand(item, 0, index);
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-01 18:19:33 +00:00
|
|
|
} else if (const auto user = item->history()->peer->asUser()) {
|
|
|
|
if (user->isBot() && item->from() == user) {
|
2021-04-26 00:23:53 +00:00
|
|
|
if (key == Qt::Key_Q || key == Qt::Key_6) {
|
2017-04-08 13:27:53 +00:00
|
|
|
App::sendBotCommand(user, user, qsl("/translate"));
|
2021-04-26 00:23:53 +00:00
|
|
|
} else if (key == Qt::Key_W || key == Qt::Key_5) {
|
2020-07-01 18:19:33 +00:00
|
|
|
App::sendBotCommand(user, user, qsl("/eng"));
|
2021-02-27 23:19:56 +00:00
|
|
|
} else if (key == Qt::Key_3) {
|
|
|
|
App::sendBotCommand(user, user, qsl("/pattern"));
|
|
|
|
} else if (key == Qt::Key_4) {
|
|
|
|
App::sendBotCommand(user, user, qsl("/abuse"));
|
2021-07-08 22:48:22 +00:00
|
|
|
} else if (key == Qt::Key_0 || key == Qt::Key_E || key == Qt::Key_9) {
|
2021-04-26 00:23:53 +00:00
|
|
|
App::sendBotCommand(user, user, qsl("/undo"));
|
2021-07-08 22:48:22 +00:00
|
|
|
} else if (key == Qt::Key_Plus || key == Qt::Key_QuoteLeft || key == Qt::Key_7) {
|
2021-04-26 00:23:53 +00:00
|
|
|
App::sendBotCommand(user, user, qsl("/next"));
|
2021-07-08 22:48:22 +00:00
|
|
|
} else if (key == Qt::Key_Period || key == Qt::Key_S || key == Qt::Key_8) {
|
2021-04-26 00:23:53 +00:00
|
|
|
App::sendBotCommand(user, user, qsl("/stats"));
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
2020-07-01 18:19:33 +00:00
|
|
|
return true;
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::clickHandlerActiveChanged(const ClickHandlerPtr &p, bool active) {
|
|
|
|
if (!_impl) return;
|
|
|
|
_impl->clickHandlerActiveChanged(p, active);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::clickHandlerPressedChanged(const ClickHandlerPtr &p, bool pressed) {
|
|
|
|
if (!_impl) return;
|
|
|
|
_impl->clickHandlerPressedChanged(p, pressed);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::updateMarkup(HistoryItem *to, bool force) {
|
|
|
|
if (!to || !to->definesReplyKeyboard()) {
|
|
|
|
if (_wasForMsgId.msg) {
|
|
|
|
_maximizeSize = _singleUse = _forceReply = false;
|
|
|
|
_wasForMsgId = FullMsgId();
|
2021-07-05 17:59:23 +00:00
|
|
|
_placeholder = QString();
|
2017-04-08 13:27:53 +00:00
|
|
|
_impl = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_wasForMsgId == FullMsgId(to->channelId(), to->id) && !force) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_wasForMsgId = FullMsgId(to->channelId(), to->id);
|
|
|
|
|
|
|
|
auto markupFlags = to->replyKeyboardFlags();
|
|
|
|
_forceReply = markupFlags & MTPDreplyKeyboardMarkup_ClientFlag::f_force_reply;
|
|
|
|
_maximizeSize = !(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_resize);
|
|
|
|
_singleUse = _forceReply || (markupFlags & MTPDreplyKeyboardMarkup::Flag::f_single_use);
|
|
|
|
|
2021-07-05 17:59:23 +00:00
|
|
|
if (const auto markup = to->Get<HistoryMessageReplyMarkup>()) {
|
|
|
|
_placeholder = markup->placeholder;
|
|
|
|
} else {
|
|
|
|
_placeholder = QString();
|
|
|
|
}
|
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
_impl = nullptr;
|
|
|
|
if (auto markup = to->Get<HistoryMessageReplyMarkup>()) {
|
2017-12-18 10:38:14 +00:00
|
|
|
if (!markup->rows.empty()) {
|
|
|
|
_impl = std::make_unique<ReplyKeyboard>(
|
|
|
|
to,
|
|
|
|
std::make_unique<Style>(this, *_st));
|
2017-04-08 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resizeToWidth(width(), _maxOuterHeight);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::hasMarkup() const {
|
|
|
|
return _impl != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::forceReply() const {
|
|
|
|
return _forceReply;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BotKeyboard::resizeGetHeight(int newWidth) {
|
|
|
|
updateStyle(newWidth);
|
|
|
|
_height = st::botKbScroll.deltat + st::botKbScroll.deltab + (_impl ? _impl->naturalHeight() : 0);
|
|
|
|
if (_maximizeSize) {
|
|
|
|
accumulate_max(_height, _maxOuterHeight);
|
|
|
|
}
|
|
|
|
if (_impl) {
|
|
|
|
int implWidth = newWidth - _st->margin - st::botKbScroll.width;
|
|
|
|
int implHeight = _height - (st::botKbScroll.deltat + st::botKbScroll.deltab);
|
|
|
|
_impl->resize(implWidth, implHeight);
|
|
|
|
}
|
|
|
|
return _height;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::maximizeSize() const {
|
|
|
|
return _maximizeSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BotKeyboard::singleUse() const {
|
|
|
|
return _singleUse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::updateStyle(int newWidth) {
|
|
|
|
if (!_impl) return;
|
|
|
|
|
|
|
|
int implWidth = newWidth - st::botKbButton.margin - st::botKbScroll.width;
|
|
|
|
_st = _impl->isEnoughSpace(implWidth, st::botKbButton) ? &st::botKbButton : &st::botKbTinyButton;
|
|
|
|
|
|
|
|
_impl->setStyle(std::make_unique<Style>(this, *_st));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::clearSelection() {
|
|
|
|
if (_impl) {
|
|
|
|
if (ClickHandler::setActive(ClickHandlerPtr(), this)) {
|
|
|
|
Ui::Tooltip::Hide();
|
|
|
|
setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint BotKeyboard::tooltipPos() const {
|
|
|
|
return _lastMousePos;
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
bool BotKeyboard::tooltipWindowActive() const {
|
2019-12-31 13:48:44 +00:00
|
|
|
return Ui::AppInFocus() && Ui::InFocusChain(window());
|
2019-09-16 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
2017-04-08 13:27:53 +00:00
|
|
|
QString BotKeyboard::tooltipText() const {
|
|
|
|
if (ClickHandlerPtr lnk = ClickHandler::getActive()) {
|
|
|
|
return lnk->tooltip();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BotKeyboard::updateSelected() {
|
|
|
|
Ui::Tooltip::Show(1000, this);
|
|
|
|
|
|
|
|
if (!_impl) return;
|
|
|
|
|
2017-06-21 21:38:31 +00:00
|
|
|
auto p = mapFromGlobal(_lastMousePos);
|
|
|
|
auto x = rtl() ? st::botKbScroll.width : _st->margin;
|
2017-04-08 13:27:53 +00:00
|
|
|
|
2018-01-27 13:59:24 +00:00
|
|
|
auto link = _impl->getLink(p - QPoint(x, _st->margin));
|
2017-04-08 13:27:53 +00:00
|
|
|
if (ClickHandler::setActive(link, this)) {
|
|
|
|
Ui::Tooltip::Hide();
|
|
|
|
setCursor(link ? style::cur_pointer : style::cur_default);
|
|
|
|
}
|
|
|
|
}
|
2017-12-18 15:44:50 +00:00
|
|
|
|
|
|
|
BotKeyboard::~BotKeyboard() = default;
|