2020-11-12 15:46:17 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "inline_bots/inline_results_inner.h"
|
|
|
|
|
|
|
|
#include "api/api_common.h"
|
2021-03-13 09:52:15 +00:00
|
|
|
#include "chat_helpers/gifs_list_widget.h" // ChatHelpers::AddGifAction
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "chat_helpers/send_context_menu.h" // SendMenu::FillSendMenu
|
2021-07-26 16:27:08 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "data/data_file_origin.h"
|
|
|
|
#include "data/data_user.h"
|
|
|
|
#include "data/data_changes.h"
|
|
|
|
#include "inline_bots/inline_bot_result.h"
|
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
|
|
|
#include "lang/lang_keys.h"
|
2021-07-25 22:20:42 +00:00
|
|
|
#include "layout/layout_position.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "facades.h"
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "window/window_session_controller.h"
|
|
|
|
#include "ui/widgets/popup_menu.h"
|
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/labels.h"
|
2021-07-02 15:29:13 +00:00
|
|
|
#include "ui/effects/path_shift_gradient.h"
|
2020-11-12 15:46:17 +00:00
|
|
|
#include "history/view/history_view_cursor_state.h"
|
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
|
|
|
namespace InlineBots {
|
|
|
|
namespace Layout {
|
|
|
|
|
|
|
|
Inner::Inner(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: RpWidget(parent)
|
|
|
|
, _controller(controller)
|
2021-07-02 15:29:13 +00:00
|
|
|
, _pathGradient(std::make_unique<Ui::PathShiftGradient>(
|
|
|
|
st::windowBgRipple,
|
|
|
|
st::windowBgOver,
|
|
|
|
[=] { update(); }))
|
2020-11-12 15:46:17 +00:00
|
|
|
, _updateInlineItems([=] { updateInlineItems(); })
|
2021-07-21 22:47:56 +00:00
|
|
|
, _mosaic(st::emojiPanWidth - st::emojiScroll.width - st::inlineResultsLeft)
|
2020-11-12 15:46:17 +00:00
|
|
|
, _previewTimer([=] { showPreview(); }) {
|
2020-12-08 07:19:23 +00:00
|
|
|
resize(st::emojiPanWidth - st::emojiScroll.width - st::roundRadiusSmall, st::inlineResultsMinHeight);
|
2020-11-12 15:46:17 +00:00
|
|
|
|
|
|
|
setMouseTracking(true);
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
|
|
|
|
_controller->session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
update();
|
|
|
|
}, lifetime());
|
|
|
|
|
2021-05-25 10:35:38 +00:00
|
|
|
controller->gifPauseLevelChanged(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
if (!_controller->isGifPausedAtLeastFor(
|
|
|
|
Window::GifPauseReason::InlineResults)) {
|
2020-11-12 15:46:17 +00:00
|
|
|
update();
|
|
|
|
}
|
2021-05-25 10:35:38 +00:00
|
|
|
}, lifetime());
|
2020-11-12 15:46:17 +00:00
|
|
|
|
|
|
|
_controller->session().changes().peerUpdates(
|
|
|
|
Data::PeerUpdate::Flag::Rights
|
|
|
|
) | rpl::filter([=](const Data::PeerUpdate &update) {
|
|
|
|
return (update.peer.get() == _inlineQueryPeer);
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
auto isRestricted = (_restrictedLabel != nullptr);
|
|
|
|
if (isRestricted != isRestrictedView()) {
|
|
|
|
auto h = countHeight();
|
|
|
|
if (h != height()) resize(width(), h);
|
|
|
|
}
|
|
|
|
}, lifetime());
|
2021-07-21 22:47:56 +00:00
|
|
|
|
|
|
|
sizeValue(
|
|
|
|
) | rpl::start_with_next([=](const QSize &s) {
|
|
|
|
_mosaic.setFullWidth(s.width());
|
|
|
|
}, lifetime());
|
2021-07-26 00:25:33 +00:00
|
|
|
|
|
|
|
_mosaic.setRightSkip(st::inlineResultsSkip);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
|
|
|
_visibleBottom = visibleBottom;
|
|
|
|
if (_visibleTop != visibleTop) {
|
|
|
|
_visibleTop = visibleTop;
|
|
|
|
_lastScrolled = crl::now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::checkRestrictedPeer() {
|
|
|
|
if (_inlineQueryPeer) {
|
|
|
|
const auto error = Data::RestrictionError(
|
|
|
|
_inlineQueryPeer,
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatRestriction::SendInline);
|
2020-11-12 15:46:17 +00:00
|
|
|
if (error) {
|
|
|
|
if (!_restrictedLabel) {
|
|
|
|
_restrictedLabel.create(this, *error, st::stickersRestrictedLabel);
|
|
|
|
_restrictedLabel->show();
|
2020-12-08 07:19:23 +00:00
|
|
|
_restrictedLabel->move(st::inlineResultsLeft - st::roundRadiusSmall, st::stickerPanPadding);
|
|
|
|
_restrictedLabel->resizeToNaturalWidth(width() - (st::inlineResultsLeft - st::roundRadiusSmall) * 2);
|
2020-11-12 15:46:17 +00:00
|
|
|
if (_switchPmButton) {
|
|
|
|
_switchPmButton->hide();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_restrictedLabel) {
|
|
|
|
_restrictedLabel.destroy();
|
|
|
|
if (_switchPmButton) {
|
|
|
|
_switchPmButton->show();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::isRestrictedView() {
|
|
|
|
checkRestrictedPeer();
|
|
|
|
return (_restrictedLabel != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::countHeight() {
|
|
|
|
if (isRestrictedView()) {
|
|
|
|
return st::stickerPanPadding + _restrictedLabel->height() + st::stickerPanPadding;
|
2021-07-21 22:47:56 +00:00
|
|
|
} else if (_mosaic.empty() && !_switchPmButton) {
|
2020-11-12 15:46:17 +00:00
|
|
|
return st::stickerPanPadding + st::normalFont->height + st::stickerPanPadding;
|
|
|
|
}
|
|
|
|
auto result = st::stickerPanPadding;
|
|
|
|
if (_switchPmButton) {
|
|
|
|
result += _switchPmButton->height() + st::inlineResultsSkip;
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
for (auto i = 0, l = _mosaic.rowsCount(); i < l; ++i) {
|
|
|
|
result += _mosaic.rowHeightAt(i);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
return result + st::stickerPanPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Inner::tooltipText() const {
|
|
|
|
if (const auto lnk = ClickHandler::getActive()) {
|
|
|
|
return lnk->tooltip();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint Inner::tooltipPos() const {
|
|
|
|
return _lastMousePos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::tooltipWindowActive() const {
|
|
|
|
return Ui::AppInFocus() && Ui::InFocusChain(window());
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Inner::inlineRowsCleared() const {
|
|
|
|
return _inlineRowsCleared.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
Inner::~Inner() = default;
|
|
|
|
|
|
|
|
void Inner::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
QRect r = e ? e->rect() : rect();
|
|
|
|
if (r != rect()) {
|
|
|
|
p.setClipRect(r);
|
|
|
|
}
|
|
|
|
p.fillRect(r, st::emojiPanBg);
|
|
|
|
|
|
|
|
paintInlineItems(p, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::paintInlineItems(Painter &p, const QRect &r) {
|
|
|
|
if (_restrictedLabel) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
if (_mosaic.empty() && !_switchPmButton) {
|
2020-11-12 15:46:17 +00:00
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::noContactsColor);
|
|
|
|
p.drawText(QRect(0, 0, width(), (height() / 3) * 2 + st::normalFont->height), tr::lng_inline_bot_no_results(tr::now), style::al_center);
|
|
|
|
return;
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto gifPaused = _controller->isGifPausedAtLeastFor(
|
|
|
|
Window::GifPauseReason::InlineResults);
|
|
|
|
using namespace InlineBots::Layout;
|
|
|
|
PaintContext context(crl::now(), false, gifPaused, false);
|
2021-07-02 15:29:13 +00:00
|
|
|
context.pathGradient = _pathGradient.get();
|
|
|
|
context.pathGradient->startFrame(0, width(), width() / 2);
|
2020-11-12 15:46:17 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
auto paintItem = [&](not_null<const ItemBase*> item, QPoint point) {
|
2021-07-26 00:25:33 +00:00
|
|
|
p.translate(point.x(), point.y());
|
|
|
|
item->paint(
|
|
|
|
p,
|
|
|
|
r.translated(-point),
|
|
|
|
&context);
|
|
|
|
p.translate(-point.x(), -point.y());
|
|
|
|
};
|
|
|
|
_mosaic.paint(std::move(paintItem), r);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (e->button() != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
_pressed = _selected;
|
|
|
|
ClickHandler::pressed();
|
|
|
|
_previewTimer.callOnce(QApplication::startDragTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseReleaseEvent(QMouseEvent *e) {
|
|
|
|
_previewTimer.cancel();
|
|
|
|
|
|
|
|
auto pressed = std::exchange(_pressed, -1);
|
|
|
|
auto activated = ClickHandler::unpressed();
|
|
|
|
|
|
|
|
if (_previewShown) {
|
|
|
|
_previewShown = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
if (_selected < 0 || _selected != pressed || !activated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 06:21:56 +00:00
|
|
|
using namespace InlineBots::Layout;
|
|
|
|
const auto open = dynamic_cast<OpenFileClickHandler*>(activated.get());
|
|
|
|
if (dynamic_cast<SendClickHandler*>(activated.get()) || open) {
|
2021-07-21 22:47:56 +00:00
|
|
|
selectInlineResult(_selected, {}, !!open);
|
2020-11-12 15:46:17 +00:00
|
|
|
} else {
|
2021-07-26 16:27:08 +00:00
|
|
|
ActivateClickHandler(window(), activated, {
|
|
|
|
e->button(),
|
|
|
|
QVariant::fromValue(ClickHandlerContext{
|
|
|
|
.sessionWindow = base::make_weak(_controller.get()),
|
|
|
|
})
|
|
|
|
});
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::selectInlineResult(
|
2021-07-21 22:47:56 +00:00
|
|
|
int index,
|
2021-06-14 06:21:56 +00:00
|
|
|
Api::SendOptions options,
|
|
|
|
bool open) {
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto item = _mosaic.maybeItemAt(index);
|
|
|
|
if (!item) {
|
2020-11-12 15:46:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto inlineResult = item->getResult()) {
|
|
|
|
if (inlineResult->onChoose(item)) {
|
|
|
|
_resultSelectedCallback({
|
|
|
|
.result = inlineResult,
|
|
|
|
.bot = _inlineBot,
|
2021-06-14 06:21:56 +00:00
|
|
|
.options = std::move(options),
|
|
|
|
.open = open,
|
2020-11-12 15:46:17 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::leaveEventHook(QEvent *e) {
|
|
|
|
clearSelection();
|
|
|
|
Ui::Tooltip::Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::leaveToChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::enterFromChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::contextMenuEvent(QContextMenuEvent *e) {
|
|
|
|
if (_selected < 0 || _pressed >= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-19 02:58:23 +00:00
|
|
|
const auto type = _sendMenuType
|
|
|
|
? _sendMenuType()
|
|
|
|
: SendMenu::Type::Disabled;
|
2020-11-12 15:46:17 +00:00
|
|
|
|
|
|
|
_menu = base::make_unique_q<Ui::PopupMenu>(this);
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto send = [=, selected = _selected](Api::SendOptions options) {
|
|
|
|
selectInlineResult(selected, options, false);
|
2020-11-12 15:46:17 +00:00
|
|
|
};
|
|
|
|
SendMenu::FillSendMenu(
|
|
|
|
_menu,
|
2020-11-19 03:18:45 +00:00
|
|
|
type,
|
2020-11-12 15:46:17 +00:00
|
|
|
SendMenu::DefaultSilentCallback(send),
|
|
|
|
SendMenu::DefaultScheduleCallback(this, type, send));
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto item = _mosaic.itemAt(_selected);
|
2021-03-13 09:52:15 +00:00
|
|
|
if (const auto previewDocument = item->getPreviewDocument()) {
|
|
|
|
auto callback = [&](const QString &text, Fn<void()> &&done) {
|
|
|
|
_menu->addAction(text, std::move(done));
|
|
|
|
};
|
|
|
|
ChatHelpers::AddGifAction(std::move(callback), previewDocument);
|
|
|
|
}
|
|
|
|
|
2021-01-13 01:10:25 +00:00
|
|
|
if (!_menu->empty()) {
|
2020-11-12 15:46:17 +00:00
|
|
|
_menu->popup(QCursor::pos());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearSelection() {
|
|
|
|
if (_selected >= 0) {
|
2021-07-21 22:47:56 +00:00
|
|
|
ClickHandler::clearActive(_mosaic.itemAt(_selected));
|
2020-11-12 15:46:17 +00:00
|
|
|
setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
_selected = _pressed = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::hideFinished() {
|
|
|
|
clearHeavyData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearHeavyData() {
|
|
|
|
clearInlineRows(false);
|
|
|
|
for (const auto &[result, layout] : _inlineLayouts) {
|
|
|
|
layout->unloadHeavyPart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineBotChanged() {
|
|
|
|
refreshInlineRows(nullptr, nullptr, nullptr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearInlineRows(bool resultsDeleted) {
|
|
|
|
if (resultsDeleted) {
|
|
|
|
_selected = _pressed = -1;
|
|
|
|
} else {
|
|
|
|
clearSelection();
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
_mosaic.clearRows(resultsDeleted);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
ItemBase *Inner::layoutPrepareInlineResult(Result *result) {
|
2020-11-12 15:46:17 +00:00
|
|
|
auto it = _inlineLayouts.find(result);
|
|
|
|
if (it == _inlineLayouts.cend()) {
|
|
|
|
if (auto layout = ItemBase::createLayout(this, result, _inlineWithThumb)) {
|
|
|
|
it = _inlineLayouts.emplace(result, std::move(layout)).first;
|
|
|
|
it->second->initDimensions();
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!it->second->maxWidth()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::deleteUnusedInlineLayouts() {
|
2021-07-21 22:47:56 +00:00
|
|
|
if (_mosaic.empty()) { // delete all
|
2020-11-12 15:46:17 +00:00
|
|
|
_inlineLayouts.clear();
|
|
|
|
} else {
|
|
|
|
for (auto i = _inlineLayouts.begin(); i != _inlineLayouts.cend();) {
|
|
|
|
if (i->second->position() < 0) {
|
|
|
|
i = _inlineLayouts.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::preloadImages() {
|
2021-07-28 14:00:46 +00:00
|
|
|
_mosaic.forEach([](not_null<const ItemBase*> item) {
|
2021-07-26 00:25:33 +00:00
|
|
|
item->preload();
|
|
|
|
});
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::hideInlineRowsPanel() {
|
|
|
|
clearInlineRows(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearInlineRowsPanel() {
|
|
|
|
clearInlineRows(false);
|
|
|
|
}
|
|
|
|
|
2021-07-26 00:25:33 +00:00
|
|
|
void Inner::refreshMosaicOffset() {
|
|
|
|
const auto top = st::stickerPanPadding
|
|
|
|
+ (_switchPmButton
|
|
|
|
? _switchPmButton->height() + st::inlineResultsSkip
|
|
|
|
: 0);
|
|
|
|
_mosaic.setOffset(
|
|
|
|
st::inlineResultsLeft - st::roundRadiusSmall,
|
|
|
|
top);
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:46:17 +00:00
|
|
|
void Inner::refreshSwitchPmButton(const CacheEntry *entry) {
|
|
|
|
if (!entry || entry->switchPmText.isEmpty()) {
|
|
|
|
_switchPmButton.destroy();
|
|
|
|
_switchPmStartToken.clear();
|
|
|
|
} else {
|
|
|
|
if (!_switchPmButton) {
|
|
|
|
_switchPmButton.create(this, nullptr, st::switchPmButton);
|
|
|
|
_switchPmButton->show();
|
|
|
|
_switchPmButton->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
|
|
|
|
_switchPmButton->addClickHandler([=] { switchPm(); });
|
|
|
|
}
|
|
|
|
_switchPmButton->setText(rpl::single(entry->switchPmText));
|
|
|
|
_switchPmStartToken = entry->switchPmStartToken;
|
|
|
|
const auto buttonTop = st::stickerPanPadding;
|
2020-12-08 07:19:23 +00:00
|
|
|
_switchPmButton->move(st::inlineResultsLeft - st::roundRadiusSmall, buttonTop);
|
2020-11-12 15:46:17 +00:00
|
|
|
if (isRestrictedView()) {
|
|
|
|
_switchPmButton->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::refreshInlineRows(PeerData *queryPeer, UserData *bot, const CacheEntry *entry, bool resultsDeleted) {
|
|
|
|
_inlineBot = bot;
|
|
|
|
_inlineQueryPeer = queryPeer;
|
|
|
|
refreshSwitchPmButton(entry);
|
2021-07-26 00:25:33 +00:00
|
|
|
refreshMosaicOffset();
|
2020-11-12 15:46:17 +00:00
|
|
|
auto clearResults = [&] {
|
|
|
|
if (!entry) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (entry->results.empty() && entry->switchPmText.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
auto clearResultsResult = clearResults(); // Clang workaround.
|
|
|
|
if (clearResultsResult) {
|
|
|
|
if (resultsDeleted) {
|
|
|
|
clearInlineRows(true);
|
|
|
|
deleteUnusedInlineLayouts();
|
|
|
|
}
|
|
|
|
_inlineRowsCleared.fire({});
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
|
|
|
|
Assert(_inlineBot != 0);
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto count = int(entry->results.size());
|
|
|
|
const auto from = validateExistingInlineRows(entry->results);
|
2020-11-12 15:46:17 +00:00
|
|
|
auto added = 0;
|
|
|
|
|
|
|
|
if (count) {
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto resultItems = entry->results | ranges::views::slice(
|
|
|
|
from,
|
|
|
|
count
|
|
|
|
) | ranges::views::transform([&](const std::unique_ptr<Result> &r) {
|
|
|
|
return layoutPrepareInlineResult(r.get());
|
|
|
|
}) | ranges::views::filter([](const ItemBase *item) {
|
|
|
|
return item != nullptr;
|
2021-07-26 00:25:33 +00:00
|
|
|
}) | ranges::to<std::vector<not_null<ItemBase*>>>;
|
2021-07-21 22:47:56 +00:00
|
|
|
|
|
|
|
_mosaic.addItems(resultItems);
|
|
|
|
added = resultItems.size();
|
2021-07-26 00:25:33 +00:00
|
|
|
preloadImages();
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto h = countHeight();
|
|
|
|
if (h != height()) resize(width(), h);
|
|
|
|
update();
|
|
|
|
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::validateExistingInlineRows(const Results &results) {
|
2021-07-26 00:25:33 +00:00
|
|
|
const auto until = _mosaic.validateExistingRows([&](
|
2021-07-28 14:00:46 +00:00
|
|
|
not_null<const ItemBase*> item,
|
2021-07-26 00:25:33 +00:00
|
|
|
int untilIndex) {
|
|
|
|
return item->getResult() != results[untilIndex].get();
|
|
|
|
}, results.size());
|
2020-11-12 15:46:17 +00:00
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
if (_mosaic.empty()) {
|
2020-11-12 15:46:17 +00:00
|
|
|
_inlineWithThumb = false;
|
2021-07-21 22:47:56 +00:00
|
|
|
for (int i = until; i < results.size(); ++i) {
|
2020-11-12 15:46:17 +00:00
|
|
|
if (results.at(i)->hasThumbDisplay()) {
|
|
|
|
_inlineWithThumb = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return until;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineItemLayoutChanged(const ItemBase *layout) {
|
|
|
|
if (_selected < 0 || !isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
if (const auto item = _mosaic.maybeItemAt(_selected)) {
|
|
|
|
if (layout == item) {
|
2020-11-12 15:46:17 +00:00
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineItemRepaint(const ItemBase *layout) {
|
|
|
|
auto ms = crl::now();
|
|
|
|
if (_lastScrolled + 100 <= ms) {
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
_updateInlineItems.callOnce(_lastScrolled + 100 - ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::inlineItemVisible(const ItemBase *layout) {
|
|
|
|
int32 position = layout->position();
|
|
|
|
if (position < 0 || !isVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto &[row, column] = ::Layout::IndexToPosition(position);
|
2020-11-12 15:46:17 +00:00
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
auto top = st::stickerPanPadding;
|
|
|
|
for (auto i = 0; i != row; ++i) {
|
|
|
|
top += _mosaic.rowHeightAt(i);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
return (top < _visibleBottom)
|
|
|
|
&& (top + _mosaic.itemAt(row, column)->height() > _visibleTop);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Data::FileOrigin Inner::inlineItemFileOrigin() {
|
|
|
|
return Data::FileOrigin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::updateSelected() {
|
|
|
|
if (_pressed >= 0 && !_previewShown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
const auto p = mapFromGlobal(_lastMousePos);
|
2021-07-26 00:25:33 +00:00
|
|
|
const auto sx = rtl() ? (width() - p.x()) : p.x();
|
|
|
|
const auto sy = p.y();
|
|
|
|
const auto &[index, exact, relative] = _mosaic.findByPoint({ sx, sy });
|
|
|
|
const auto selected = exact ? index : -1;
|
2021-07-28 12:59:42 +00:00
|
|
|
const auto item = exact ? _mosaic.itemAt(selected).get() : nullptr;
|
2021-07-26 00:25:33 +00:00
|
|
|
const auto link = exact ? item->getState(relative, {}).link : nullptr;
|
2021-07-21 22:47:56 +00:00
|
|
|
|
|
|
|
if (_selected != selected) {
|
|
|
|
if (const auto s = _mosaic.maybeItemAt(_selected)) {
|
|
|
|
s->update();
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
_selected = selected;
|
|
|
|
if (item) {
|
|
|
|
item->update();
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
if (_previewShown && _selected >= 0 && _pressed != _selected) {
|
|
|
|
_pressed = _selected;
|
2021-07-21 22:47:56 +00:00
|
|
|
if (item) {
|
|
|
|
if (const auto preview = item->getPreviewDocument()) {
|
2021-02-03 03:31:11 +00:00
|
|
|
_controller->widget()->showMediaPreview(
|
|
|
|
Data::FileOrigin(),
|
2021-07-21 22:47:56 +00:00
|
|
|
preview);
|
|
|
|
} else if (const auto preview = item->getPreviewPhoto()) {
|
2021-02-03 03:31:11 +00:00
|
|
|
_controller->widget()->showMediaPreview(
|
|
|
|
Data::FileOrigin(),
|
2021-07-21 22:47:56 +00:00
|
|
|
preview);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
if (ClickHandler::setActive(link, item)) {
|
|
|
|
setCursor(link ? style::cur_pointer : style::cur_default);
|
2020-11-12 15:46:17 +00:00
|
|
|
Ui::Tooltip::Hide();
|
|
|
|
}
|
2021-07-21 22:47:56 +00:00
|
|
|
if (link) {
|
2020-11-12 15:46:17 +00:00
|
|
|
Ui::Tooltip::Show(1000, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::showPreview() {
|
2021-07-21 22:47:56 +00:00
|
|
|
if (_pressed < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-12 15:46:17 +00:00
|
|
|
|
2021-07-21 22:47:56 +00:00
|
|
|
if (const auto layout = _mosaic.maybeItemAt(_pressed)) {
|
2021-02-03 03:31:11 +00:00
|
|
|
if (const auto previewDocument = layout->getPreviewDocument()) {
|
|
|
|
_previewShown = _controller->widget()->showMediaPreview(
|
|
|
|
Data::FileOrigin(),
|
|
|
|
previewDocument);
|
|
|
|
} else if (const auto previewPhoto = layout->getPreviewPhoto()) {
|
|
|
|
_previewShown = _controller->widget()->showMediaPreview(
|
|
|
|
Data::FileOrigin(),
|
|
|
|
previewPhoto);
|
2020-11-12 15:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::updateInlineItems() {
|
|
|
|
auto ms = crl::now();
|
|
|
|
if (_lastScrolled + 100 <= ms) {
|
|
|
|
update();
|
|
|
|
} else {
|
|
|
|
_updateInlineItems.callOnce(_lastScrolled + 100 - ms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::switchPm() {
|
|
|
|
if (_inlineBot && _inlineBot->isBot()) {
|
|
|
|
_inlineBot->botInfo->startToken = _switchPmStartToken;
|
|
|
|
_inlineBot->botInfo->inlineReturnTo = _currentDialogsEntryState;
|
|
|
|
Ui::showPeerHistory(_inlineBot, ShowAndStartBotMsgId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 02:58:23 +00:00
|
|
|
void Inner::setSendMenuType(Fn<SendMenu::Type()> &&callback) {
|
|
|
|
_sendMenuType = std::move(callback);
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:46:17 +00:00
|
|
|
} // namespace Layout
|
|
|
|
} // namespace InlineBots
|