2017-03-27 18:11:51 +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-03-27 18:11:51 +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-03-27 18:11:51 +00:00
|
|
|
*/
|
|
|
|
#include "inline_bots/inline_results_widget.h"
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
#include "inline_bots/inline_bot_result.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "inline_bots/inline_results_inner.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-06-06 10:21:40 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "ui/image/image_prepare.h"
|
2020-10-13 15:11:53 +00:00
|
|
|
#include "ui/cached_round_corners.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
namespace InlineBots {
|
|
|
|
namespace Layout {
|
2017-03-31 15:50:02 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kInlineBotRequestDelay = 400;
|
|
|
|
|
|
|
|
} // namespace
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
Widget::Widget(
|
|
|
|
QWidget *parent,
|
2019-06-06 10:21:40 +00:00
|
|
|
not_null<Window::SessionController*> controller)
|
2019-04-05 10:13:54 +00:00
|
|
|
: RpWidget(parent)
|
2017-04-07 18:10:49 +00:00
|
|
|
, _controller(controller)
|
2020-06-17 09:36:25 +00:00
|
|
|
, _api(&_controller->session().mtp())
|
2017-03-27 18:11:51 +00:00
|
|
|
, _contentMaxHeight(st::emojiPanMaxHeight)
|
|
|
|
, _contentHeight(_contentMaxHeight)
|
2020-08-09 16:06:18 +00:00
|
|
|
, _scroll(this, st::inlineBotsScroll)
|
|
|
|
, _inlineRequestTimer([=] { onInlineRequest(); }) {
|
2017-03-27 18:11:51 +00:00
|
|
|
resize(QRect(0, 0, st::emojiPanWidth, _contentHeight).marginsAdded(innerPadding()).size());
|
|
|
|
_width = width();
|
|
|
|
_height = height();
|
|
|
|
|
2020-12-08 07:19:23 +00:00
|
|
|
_scroll->resize(st::emojiPanWidth - st::roundRadiusSmall, _contentHeight);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
_scroll->move(verticalRect().topLeft());
|
2020-11-12 15:46:17 +00:00
|
|
|
_inner = _scroll->setOwnedWidget(object_ptr<Inner>(this, controller));
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
_inner->moveToLeft(0, 0, _scroll->width());
|
|
|
|
|
2020-08-09 16:06:18 +00:00
|
|
|
connect(
|
|
|
|
_scroll,
|
|
|
|
&Ui::ScrollArea::scrolled,
|
|
|
|
this,
|
|
|
|
&InlineBots::Layout::Widget::onScroll);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2020-08-09 16:06:18 +00:00
|
|
|
_inner->inlineRowsCleared(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
hideAnimated();
|
|
|
|
_inner->clearInlineRowsPanel();
|
|
|
|
}, lifetime());
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2019-04-05 10:13:54 +00:00
|
|
|
macWindowDeactivateEvents(
|
|
|
|
) | rpl::filter([=] {
|
|
|
|
return !isHidden();
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
leaveEvent(nullptr);
|
|
|
|
}, lifetime());
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
// Inner widget has OpaquePaintEvent attribute so it doesn't repaint on scroll.
|
|
|
|
// But we should force it to repaint so that GIFs will continue to animate without update() calls.
|
|
|
|
// We do that by creating a transparent widget above our _inner.
|
|
|
|
auto forceRepaintOnScroll = object_ptr<TWidget>(this);
|
2020-12-08 07:19:23 +00:00
|
|
|
forceRepaintOnScroll->setGeometry(innerRect().x() + st::roundRadiusSmall, innerRect().y() + st::roundRadiusSmall, st::roundRadiusSmall, st::roundRadiusSmall);
|
2017-03-27 18:11:51 +00:00
|
|
|
forceRepaintOnScroll->setAttribute(Qt::WA_TransparentForMouseEvents);
|
|
|
|
forceRepaintOnScroll->show();
|
|
|
|
|
|
|
|
setMouseTracking(true);
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::moveBottom(int bottom) {
|
|
|
|
_bottom = bottom;
|
|
|
|
updateContentHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::updateContentHeight() {
|
2017-04-03 16:46:24 +00:00
|
|
|
auto addedHeight = innerPadding().top() + innerPadding().bottom();
|
|
|
|
auto wantedContentHeight = qRound(st::emojiPanHeightRatio * _bottom) - addedHeight;
|
2021-01-23 03:29:50 +00:00
|
|
|
auto contentHeight = std::clamp(
|
|
|
|
wantedContentHeight,
|
|
|
|
st::inlineResultsMinHeight,
|
|
|
|
st::inlineResultsMaxHeight);
|
2017-04-03 16:46:24 +00:00
|
|
|
accumulate_min(contentHeight, _bottom - addedHeight);
|
|
|
|
accumulate_min(contentHeight, _contentMaxHeight);
|
|
|
|
auto resultTop = _bottom - addedHeight - contentHeight;
|
2017-03-27 18:11:51 +00:00
|
|
|
if (contentHeight == _contentHeight) {
|
|
|
|
move(x(), resultTop);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto was = _contentHeight;
|
|
|
|
_contentHeight = contentHeight;
|
|
|
|
|
|
|
|
resize(QRect(0, 0, innerRect().width(), _contentHeight).marginsAdded(innerPadding()).size());
|
|
|
|
_height = height();
|
2017-04-03 16:46:24 +00:00
|
|
|
moveToLeft(0, resultTop);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
if (was > _contentHeight) {
|
|
|
|
_scroll->resize(_scroll->width(), _contentHeight);
|
2017-04-03 16:46:24 +00:00
|
|
|
auto scrollTop = _scroll->scrollTop();
|
|
|
|
_inner->setVisibleTopBottom(scrollTop, scrollTop + _contentHeight);
|
2017-03-27 18:11:51 +00:00
|
|
|
} else {
|
2017-04-03 16:46:24 +00:00
|
|
|
auto scrollTop = _scroll->scrollTop();
|
|
|
|
_inner->setVisibleTopBottom(scrollTop, scrollTop + _contentHeight);
|
2017-03-27 18:11:51 +00:00
|
|
|
_scroll->resize(_scroll->width(), _contentHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto opacityAnimating = _a_opacity.animating();
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2019-04-02 09:13:30 +00:00
|
|
|
auto showAnimating = _a_show.animating();
|
2017-03-27 18:11:51 +00:00
|
|
|
if (_showAnimation && !showAnimating) {
|
|
|
|
_showAnimation.reset();
|
|
|
|
if (!opacityAnimating) {
|
|
|
|
showChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (showAnimating) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_showAnimation != nullptr);
|
2019-04-02 09:13:30 +00:00
|
|
|
if (auto opacity = _a_opacity.value(_hiding ? 0. : 1.)) {
|
|
|
|
_showAnimation->paintFrame(p, 0, 0, width(), _a_show.value(1.), opacity);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
} else if (opacityAnimating) {
|
2019-04-02 09:13:30 +00:00
|
|
|
p.setOpacity(_a_opacity.value(_hiding ? 0. : 1.));
|
2017-03-27 18:11:51 +00:00
|
|
|
p.drawPixmap(0, 0, _cache);
|
|
|
|
} else if (_hiding || isHidden()) {
|
|
|
|
hideFinished();
|
|
|
|
} else {
|
|
|
|
if (!_cache.isNull()) _cache = QPixmap();
|
|
|
|
if (!_inPanelGrab) Ui::Shadow::paint(p, innerRect(), width(), st::emojiPanAnimation.shadow);
|
|
|
|
paintContent(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::paintContent(Painter &p) {
|
|
|
|
auto inner = innerRect();
|
2020-10-13 15:11:53 +00:00
|
|
|
Ui::FillRoundRect(p, inner, st::emojiPanBg, ImageRoundRadius::Small, RectPart::FullTop | RectPart::FullBottom);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
auto horizontal = horizontalRect();
|
|
|
|
auto sidesTop = horizontal.y();
|
|
|
|
auto sidesHeight = horizontal.height();
|
|
|
|
p.fillRect(myrtlrect(inner.x() + inner.width() - st::emojiScroll.width, sidesTop, st::emojiScroll.width, sidesHeight), st::emojiPanBg);
|
2020-12-08 07:19:23 +00:00
|
|
|
p.fillRect(myrtlrect(inner.x(), sidesTop, st::roundRadiusSmall, sidesHeight), st::emojiPanBg);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::moveByBottom() {
|
|
|
|
updateContentHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::hideFast() {
|
|
|
|
if (isHidden()) return;
|
|
|
|
|
|
|
|
_hiding = false;
|
2019-04-02 09:13:30 +00:00
|
|
|
_a_opacity.stop();
|
2017-03-27 18:11:51 +00:00
|
|
|
hideFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::opacityAnimationCallback() {
|
|
|
|
update();
|
|
|
|
if (!_a_opacity.animating()) {
|
|
|
|
if (_hiding) {
|
|
|
|
_hiding = false;
|
|
|
|
hideFinished();
|
|
|
|
} else if (!_a_show.animating()) {
|
|
|
|
showChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::prepareCache() {
|
|
|
|
if (_a_opacity.animating()) return;
|
|
|
|
|
|
|
|
auto showAnimation = base::take(_a_show);
|
|
|
|
auto showAnimationData = base::take(_showAnimation);
|
|
|
|
showChildren();
|
2017-12-26 12:41:48 +00:00
|
|
|
_cache = Ui::GrabWidget(this);
|
2017-03-27 18:11:51 +00:00
|
|
|
_showAnimation = base::take(showAnimationData);
|
|
|
|
_a_show = base::take(showAnimation);
|
|
|
|
if (_a_show.animating()) {
|
|
|
|
hideChildren();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::startOpacityAnimation(bool hiding) {
|
|
|
|
_hiding = false;
|
|
|
|
prepareCache();
|
|
|
|
_hiding = hiding;
|
|
|
|
hideChildren();
|
|
|
|
_a_opacity.start([this] { opacityAnimationCallback(); }, _hiding ? 1. : 0., _hiding ? 0. : 1., st::emojiPanDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::startShowAnimation() {
|
|
|
|
if (!_a_show.animating()) {
|
|
|
|
auto cache = base::take(_cache);
|
|
|
|
auto opacityAnimation = base::take(_a_opacity);
|
|
|
|
showChildren();
|
|
|
|
auto image = grabForPanelAnimation();
|
|
|
|
_a_opacity = base::take(opacityAnimation);
|
|
|
|
_cache = base::take(_cache);
|
|
|
|
|
|
|
|
_showAnimation = std::make_unique<Ui::PanelAnimation>(st::emojiPanAnimation, Ui::PanelAnimation::Origin::BottomLeft);
|
|
|
|
auto inner = rect().marginsRemoved(st::emojiPanMargins);
|
|
|
|
_showAnimation->setFinalImage(std::move(image), QRect(inner.topLeft() * cIntRetinaFactor(), inner.size() * cIntRetinaFactor()));
|
2019-09-16 11:14:06 +00:00
|
|
|
_showAnimation->setCornerMasks(Images::CornersMask(ImageRoundRadius::Small));
|
2017-03-27 18:11:51 +00:00
|
|
|
_showAnimation->start();
|
|
|
|
}
|
|
|
|
hideChildren();
|
|
|
|
_a_show.start([this] { update(); }, 0., 1., st::emojiPanShowDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage Widget::grabForPanelAnimation() {
|
2017-12-26 12:41:48 +00:00
|
|
|
Ui::SendPendingMoveResizeEvents(this);
|
2017-03-27 18:11:51 +00:00
|
|
|
auto result = QImage(size() * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
_inPanelGrab = true;
|
|
|
|
render(&result);
|
|
|
|
_inPanelGrab = false;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:46:17 +00:00
|
|
|
void Widget::setResultSelectedCallback(Fn<void(ResultSelected)> callback) {
|
|
|
|
_inner->setResultSelectedCallback(std::move(callback));
|
|
|
|
}
|
|
|
|
|
2020-11-19 02:58:23 +00:00
|
|
|
void Widget::setSendMenuType(Fn<SendMenu::Type()> &&callback) {
|
|
|
|
_inner->setSendMenuType(std::move(callback));
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:46:17 +00:00
|
|
|
void Widget::setCurrentDialogsEntryState(Dialogs::EntryState state) {
|
|
|
|
_inner->setCurrentDialogsEntryState(state);
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:11:51 +00:00
|
|
|
void Widget::hideAnimated() {
|
|
|
|
if (isHidden()) return;
|
|
|
|
if (_hiding) return;
|
|
|
|
|
|
|
|
startOpacityAnimation(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget::~Widget() = default;
|
|
|
|
|
|
|
|
void Widget::hideFinished() {
|
|
|
|
hide();
|
2019-06-28 11:33:47 +00:00
|
|
|
_controller->disableGifPauseReason(
|
|
|
|
Window::GifPauseReason::InlineResults);
|
2017-04-03 14:13:55 +00:00
|
|
|
|
2020-05-22 09:58:46 +00:00
|
|
|
_inner->hideFinished();
|
2019-04-02 09:13:30 +00:00
|
|
|
_a_show.stop();
|
2017-03-27 18:11:51 +00:00
|
|
|
_showAnimation.reset();
|
|
|
|
_cache = QPixmap();
|
|
|
|
_horizontal = false;
|
|
|
|
_hiding = false;
|
|
|
|
|
|
|
|
_scroll->scrollToY(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::showAnimated() {
|
|
|
|
showStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::showStarted() {
|
|
|
|
if (isHidden()) {
|
|
|
|
recountContentMaxHeight();
|
|
|
|
_inner->preloadImages();
|
|
|
|
show();
|
2019-06-28 11:33:47 +00:00
|
|
|
_controller->enableGifPauseReason(
|
|
|
|
Window::GifPauseReason::InlineResults);
|
2017-03-27 18:11:51 +00:00
|
|
|
startShowAnimation();
|
|
|
|
} else if (_hiding) {
|
|
|
|
startOpacityAnimation(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onScroll() {
|
|
|
|
auto st = _scroll->scrollTop();
|
|
|
|
if (st + _scroll->height() > _scroll->scrollTopMax()) {
|
|
|
|
onInlineRequest();
|
|
|
|
}
|
|
|
|
_inner->setVisibleTopBottom(st, st + _scroll->height());
|
|
|
|
}
|
|
|
|
|
|
|
|
style::margins Widget::innerPadding() const {
|
|
|
|
return st::emojiPanMargins;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Widget::innerRect() const {
|
|
|
|
return rect().marginsRemoved(innerPadding());
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Widget::horizontalRect() const {
|
2020-12-08 07:19:23 +00:00
|
|
|
return innerRect().marginsRemoved(style::margins(0, st::roundRadiusSmall, 0, st::roundRadiusSmall));
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QRect Widget::verticalRect() const {
|
2020-12-08 07:19:23 +00:00
|
|
|
return innerRect().marginsRemoved(style::margins(st::roundRadiusSmall, 0, st::roundRadiusSmall, 0));
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::clearInlineBot() {
|
|
|
|
inlineBotChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::overlaps(const QRect &globalRect) const {
|
|
|
|
if (isHidden() || !_cache.isNull()) return false;
|
|
|
|
|
|
|
|
auto testRect = QRect(mapFromGlobal(globalRect.topLeft()), globalRect.size());
|
|
|
|
auto inner = rect().marginsRemoved(st::emojiPanMargins);
|
2020-12-08 07:19:23 +00:00
|
|
|
return inner.marginsRemoved(QMargins(st::roundRadiusSmall, 0, st::roundRadiusSmall, 0)).contains(testRect)
|
|
|
|
|| inner.marginsRemoved(QMargins(0, st::roundRadiusSmall, 0, st::roundRadiusSmall)).contains(testRect);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::inlineBotChanged() {
|
2020-06-10 18:08:17 +00:00
|
|
|
if (!_inlineBot) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
if (!isHidden() && !_hiding) {
|
|
|
|
hideAnimated();
|
|
|
|
}
|
|
|
|
|
2020-06-11 16:09:46 +00:00
|
|
|
_api.request(base::take(_inlineRequestId)).cancel();
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineQuery = _inlineNextQuery = _inlineNextOffset = QString();
|
|
|
|
_inlineBot = nullptr;
|
|
|
|
_inlineCache.clear();
|
|
|
|
_inner->inlineBotChanged();
|
|
|
|
_inner->hideInlineRowsPanel();
|
|
|
|
|
2020-06-10 18:08:17 +00:00
|
|
|
_requesting.fire(false);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::inlineResultsDone(const MTPmessages_BotResults &result) {
|
|
|
|
_inlineRequestId = 0;
|
2020-06-10 18:08:17 +00:00
|
|
|
_requesting.fire(false);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
auto it = _inlineCache.find(_inlineQuery);
|
|
|
|
auto adding = (it != _inlineCache.cend());
|
|
|
|
if (result.type() == mtpc_messages_botResults) {
|
|
|
|
auto &d = result.c_messages_botResults();
|
2020-05-28 13:51:18 +00:00
|
|
|
_controller->session().data().processUsers(d.vusers());
|
2018-01-04 19:54:35 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
auto &v = d.vresults().v;
|
|
|
|
auto queryId = d.vquery_id().v;
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
if (it == _inlineCache.cend()) {
|
2020-05-28 13:51:18 +00:00
|
|
|
it = _inlineCache.emplace(
|
|
|
|
_inlineQuery,
|
2020-11-12 15:46:17 +00:00
|
|
|
std::make_unique<CacheEntry>()).first;
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
auto entry = it->second.get();
|
2019-07-05 13:38:38 +00:00
|
|
|
entry->nextOffset = qs(d.vnext_offset().value_or_empty());
|
|
|
|
if (const auto switchPm = d.vswitch_pm()) {
|
|
|
|
switchPm->match([&](const MTPDinlineBotSwitchPM &data) {
|
|
|
|
entry->switchPmText = qs(data.vtext());
|
|
|
|
entry->switchPmStartToken = qs(data.vstart_param());
|
|
|
|
});
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (auto count = v.size()) {
|
|
|
|
entry->results.reserve(entry->results.size() + count);
|
|
|
|
}
|
|
|
|
auto added = 0;
|
2020-05-28 13:51:18 +00:00
|
|
|
for (const auto &res : v) {
|
|
|
|
auto result = InlineBots::Result::Create(
|
|
|
|
&_controller->session(),
|
|
|
|
queryId,
|
|
|
|
res);
|
|
|
|
if (result) {
|
2017-03-27 18:11:51 +00:00
|
|
|
++added;
|
|
|
|
entry->results.push_back(std::move(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!added) {
|
|
|
|
entry->nextOffset = QString();
|
|
|
|
}
|
|
|
|
} else if (adding) {
|
|
|
|
it->second->nextOffset = QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!showInlineRows(!adding)) {
|
|
|
|
it->second->nextOffset = QString();
|
|
|
|
}
|
|
|
|
onScroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::queryInlineBot(UserData *bot, PeerData *peer, QString query) {
|
|
|
|
bool force = false;
|
|
|
|
_inlineQueryPeer = peer;
|
|
|
|
if (bot != _inlineBot) {
|
|
|
|
inlineBotChanged();
|
|
|
|
_inlineBot = bot;
|
|
|
|
force = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_inlineQuery != query || force) {
|
|
|
|
if (_inlineRequestId) {
|
2020-06-11 16:09:46 +00:00
|
|
|
_api.request(_inlineRequestId).cancel();
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineRequestId = 0;
|
2020-06-10 18:08:17 +00:00
|
|
|
_requesting.fire(false);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
if (_inlineCache.find(query) != _inlineCache.cend()) {
|
2020-08-09 16:06:18 +00:00
|
|
|
_inlineRequestTimer.cancel();
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineQuery = _inlineNextQuery = query;
|
|
|
|
showInlineRows(true);
|
|
|
|
} else {
|
|
|
|
_inlineNextQuery = query;
|
2020-11-12 15:46:17 +00:00
|
|
|
_inlineRequestTimer.callOnce(kInlineBotRequestDelay);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onInlineRequest() {
|
|
|
|
if (_inlineRequestId || !_inlineBot || !_inlineQueryPeer) return;
|
|
|
|
_inlineQuery = _inlineNextQuery;
|
|
|
|
|
|
|
|
QString nextOffset;
|
|
|
|
auto it = _inlineCache.find(_inlineQuery);
|
|
|
|
if (it != _inlineCache.cend()) {
|
|
|
|
nextOffset = it->second->nextOffset;
|
2018-03-04 20:04:13 +00:00
|
|
|
if (nextOffset.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
2020-06-10 18:08:17 +00:00
|
|
|
_requesting.fire(true);
|
2019-11-27 08:02:56 +00:00
|
|
|
_inlineRequestId = _api.request(MTPmessages_GetInlineBotResults(
|
|
|
|
MTP_flags(0),
|
|
|
|
_inlineBot->inputUser,
|
|
|
|
_inlineQueryPeer->input,
|
|
|
|
MTPInputGeoPoint(),
|
|
|
|
MTP_string(_inlineQuery),
|
|
|
|
MTP_string(nextOffset)
|
|
|
|
)).done([=](const MTPmessages_BotResults &result) {
|
2017-03-27 18:11:51 +00:00
|
|
|
inlineResultsDone(result);
|
2019-11-27 08:02:56 +00:00
|
|
|
}).fail([=](const RPCError &error) {
|
2017-03-27 18:11:51 +00:00
|
|
|
// show error?
|
2020-06-10 18:08:17 +00:00
|
|
|
_requesting.fire(false);
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineRequestId = 0;
|
|
|
|
}).handleAllErrors().send();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::refreshInlineRows(int *added) {
|
|
|
|
auto it = _inlineCache.find(_inlineQuery);
|
2020-11-12 15:46:17 +00:00
|
|
|
const CacheEntry *entry = nullptr;
|
2017-03-27 18:11:51 +00:00
|
|
|
if (it != _inlineCache.cend()) {
|
|
|
|
if (!it->second->results.empty() || !it->second->switchPmText.isEmpty()) {
|
|
|
|
entry = it->second.get();
|
|
|
|
}
|
|
|
|
_inlineNextOffset = it->second->nextOffset;
|
|
|
|
}
|
|
|
|
if (!entry) prepareCache();
|
2017-06-11 18:33:20 +00:00
|
|
|
auto result = _inner->refreshInlineRows(_inlineQueryPeer, _inlineBot, entry, false);
|
2017-03-27 18:11:51 +00:00
|
|
|
if (added) *added = result;
|
|
|
|
return (entry != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Widget::showInlineRows(bool newResults) {
|
|
|
|
auto added = 0;
|
|
|
|
auto clear = !refreshInlineRows(&added);
|
|
|
|
if (newResults) {
|
|
|
|
_scroll->scrollToY(0);
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:46:24 +00:00
|
|
|
auto hidden = isHidden();
|
2017-03-27 18:11:51 +00:00
|
|
|
if (!hidden && !clear) {
|
|
|
|
recountContentMaxHeight();
|
|
|
|
}
|
|
|
|
if (clear) {
|
|
|
|
if (!hidden) {
|
|
|
|
hideAnimated();
|
|
|
|
} else if (!_hiding) {
|
|
|
|
_cache = QPixmap(); // clear after refreshInlineRows()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (hidden || _hiding) {
|
|
|
|
showAnimated();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::recountContentMaxHeight() {
|
2017-04-03 16:46:24 +00:00
|
|
|
_contentMaxHeight = _inner->countHeight();
|
2017-03-27 18:11:51 +00:00
|
|
|
updateContentHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Layout
|
|
|
|
} // namespace InlineBots
|