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"
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
|
|
|
#include "data/data_user.h"
|
2019-01-18 12:27:37 +00:00
|
|
|
#include "data/data_session.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
#include "ui/widgets/buttons.h"
|
|
|
|
#include "ui/widgets/shadow.h"
|
|
|
|
#include "ui/effects/ripple_animation.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image_prepare.h"
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "boxes/confirm_box.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
#include "inline_bots/inline_bot_result.h"
|
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
|
|
|
#include "dialogs/dialogs_layout.h"
|
|
|
|
#include "storage/localstorage.h"
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "apiwrap.h"
|
|
|
|
#include "mainwidget.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"
|
2017-04-08 13:27:53 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2017-06-11 18:33:20 +00:00
|
|
|
#include "ui/widgets/labels.h"
|
|
|
|
#include "observer_peer.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "history/view/history_view_cursor_state.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2019-12-05 08:32:33 +00:00
|
|
|
#include "styles/style_chat_helpers.h"
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
2017-03-27 18:11:51 +00:00
|
|
|
namespace InlineBots {
|
|
|
|
namespace Layout {
|
|
|
|
namespace internal {
|
2017-03-31 15:50:02 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kInlineBotRequestDelay = 400;
|
|
|
|
|
|
|
|
} // namespace
|
2017-03-27 18:11:51 +00:00
|
|
|
|
2019-06-06 10:21:40 +00:00
|
|
|
Inner::Inner(
|
|
|
|
QWidget *parent,
|
|
|
|
not_null<Window::SessionController*> controller)
|
|
|
|
: TWidget(parent)
|
2018-11-21 18:14:48 +00:00
|
|
|
, _controller(controller)
|
|
|
|
, _updateInlineItems([=] { updateInlineItems(); })
|
|
|
|
, _previewTimer([=] { showPreview(); }) {
|
2018-11-22 12:48:50 +00:00
|
|
|
resize(st::emojiPanWidth - st::emojiScroll.width - st::buttonRadius, st::inlineResultsMinHeight);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
setMouseTracking(true);
|
|
|
|
setAttribute(Qt::WA_OpaquePaintEvent);
|
|
|
|
|
2017-08-04 14:54:32 +00:00
|
|
|
subscribe(Auth().downloaderTaskFinished(), [this] {
|
2017-03-27 18:11:51 +00:00
|
|
|
update();
|
|
|
|
});
|
2017-04-07 18:10:49 +00:00
|
|
|
subscribe(controller->gifPauseLevelChanged(), [this] {
|
|
|
|
if (!_controller->isGifPausedAtLeastFor(Window::GifPauseReason::InlineResults)) {
|
2017-04-03 14:13:55 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
});
|
2019-01-05 10:50:04 +00:00
|
|
|
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(Notify::PeerUpdate::Flag::RightsChanged, [this](const Notify::PeerUpdate &update) {
|
2017-06-11 18:33:20 +00:00
|
|
|
if (update.peer == _inlineQueryPeer) {
|
|
|
|
auto isRestricted = (_restrictedLabel != nullptr);
|
|
|
|
if (isRestricted != isRestrictedView()) {
|
|
|
|
auto h = countHeight();
|
|
|
|
if (h != height()) resize(width(), h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
void Inner::visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
2017-03-27 18:11:51 +00:00
|
|
|
_visibleBottom = visibleBottom;
|
|
|
|
if (_visibleTop != visibleTop) {
|
|
|
|
_visibleTop = visibleTop;
|
2019-02-19 06:57:53 +00:00
|
|
|
_lastScrolled = crl::now();
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 18:33:20 +00:00
|
|
|
void Inner::checkRestrictedPeer() {
|
2019-01-05 10:50:04 +00:00
|
|
|
if (_inlineQueryPeer) {
|
2019-06-19 15:09:03 +00:00
|
|
|
const auto error = Data::RestrictionError(
|
2019-01-14 17:43:06 +00:00
|
|
|
_inlineQueryPeer,
|
|
|
|
ChatRestriction::f_send_inline);
|
2019-06-19 15:09:03 +00:00
|
|
|
if (error) {
|
2017-06-11 18:33:20 +00:00
|
|
|
if (!_restrictedLabel) {
|
2019-06-19 15:09:03 +00:00
|
|
|
_restrictedLabel.create(this, *error, st::stickersRestrictedLabel);
|
2017-06-11 18:33:20 +00:00
|
|
|
_restrictedLabel->show();
|
|
|
|
_restrictedLabel->move(st::inlineResultsLeft - st::buttonRadius, st::stickerPanPadding);
|
2019-10-07 15:55:39 +00:00
|
|
|
_restrictedLabel->resizeToNaturalWidth(width() - (st::inlineResultsLeft - st::buttonRadius) * 2);
|
2017-06-11 18:33:20 +00:00
|
|
|
if (_switchPmButton) {
|
|
|
|
_switchPmButton->hide();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_restrictedLabel) {
|
|
|
|
_restrictedLabel.destroy();
|
|
|
|
if (_switchPmButton) {
|
|
|
|
_switchPmButton->show();
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::isRestrictedView() {
|
|
|
|
checkRestrictedPeer();
|
|
|
|
return (_restrictedLabel != nullptr);
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:46:24 +00:00
|
|
|
int Inner::countHeight() {
|
2017-06-11 18:33:20 +00:00
|
|
|
if (isRestrictedView()) {
|
|
|
|
return st::stickerPanPadding + _restrictedLabel->height() + st::stickerPanPadding;
|
2018-03-04 20:04:13 +00:00
|
|
|
} else if (_rows.isEmpty() && !_switchPmButton) {
|
|
|
|
return st::stickerPanPadding + st::normalFont->height + st::stickerPanPadding;
|
2017-06-11 18:33:20 +00:00
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
auto result = st::stickerPanPadding;
|
|
|
|
if (_switchPmButton) {
|
|
|
|
result += _switchPmButton->height() + st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
for (int i = 0, l = _rows.count(); i < l; ++i) {
|
|
|
|
result += _rows[i].height;
|
|
|
|
}
|
2017-04-03 16:46:24 +00:00
|
|
|
return result + st::stickerPanPadding;
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 09:23:39 +00:00
|
|
|
QString Inner::tooltipText() const {
|
|
|
|
if (const auto lnk = ClickHandler::getActive()) {
|
|
|
|
return lnk->tooltip();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPoint Inner::tooltipPos() const {
|
|
|
|
return _lastMousePos;
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:14:06 +00:00
|
|
|
bool Inner::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-03-27 18:11:51 +00:00
|
|
|
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) {
|
2017-06-11 18:33:20 +00:00
|
|
|
if (_restrictedLabel) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
if (_rows.isEmpty() && !_switchPmButton) {
|
|
|
|
p.setFont(st::normalFont);
|
|
|
|
p.setPen(st::noContactsColor);
|
2019-06-19 15:09:03 +00:00
|
|
|
p.drawText(QRect(0, 0, width(), (height() / 3) * 2 + st::normalFont->height), tr::lng_inline_bot_no_results(tr::now), style::al_center);
|
2017-03-27 18:11:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-07 18:10:49 +00:00
|
|
|
auto gifPaused = _controller->isGifPausedAtLeastFor(Window::GifPauseReason::InlineResults);
|
2019-02-19 06:57:53 +00:00
|
|
|
InlineBots::Layout::PaintContext context(crl::now(), false, gifPaused, false);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
auto top = st::stickerPanPadding;
|
|
|
|
if (_switchPmButton) {
|
|
|
|
top += _switchPmButton->height() + st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto fromx = rtl() ? (width() - r.x() - r.width()) : r.x();
|
|
|
|
auto tox = rtl() ? (width() - r.x()) : (r.x() + r.width());
|
|
|
|
for (auto row = 0, rows = _rows.size(); row != rows; ++row) {
|
|
|
|
auto &inlineRow = _rows[row];
|
|
|
|
if (top >= r.top() + r.height()) break;
|
|
|
|
if (top + inlineRow.height > r.top()) {
|
|
|
|
auto left = st::inlineResultsLeft - st::buttonRadius;
|
|
|
|
if (row == rows - 1) context.lastRow = true;
|
|
|
|
for (int col = 0, cols = inlineRow.items.size(); col < cols; ++col) {
|
|
|
|
if (left >= tox) break;
|
|
|
|
|
|
|
|
auto item = inlineRow.items.at(col);
|
|
|
|
auto w = item->width();
|
|
|
|
if (left + w > fromx) {
|
|
|
|
p.translate(left, top);
|
|
|
|
item->paint(p, r.translated(-left, -top), &context);
|
|
|
|
p.translate(-left, -top);
|
|
|
|
}
|
|
|
|
left += w;
|
|
|
|
if (item->hasRightSkip()) {
|
|
|
|
left += st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
top += inlineRow.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mousePressEvent(QMouseEvent *e) {
|
|
|
|
if (e->button() != Qt::LeftButton) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
_pressed = _selected;
|
|
|
|
ClickHandler::pressed();
|
2018-11-21 18:14:48 +00:00
|
|
|
_previewTimer.callOnce(QApplication::startDragTime());
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseReleaseEvent(QMouseEvent *e) {
|
2018-11-21 18:14:48 +00:00
|
|
|
_previewTimer.cancel();
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-18 09:07:18 +00:00
|
|
|
if (dynamic_cast<InlineBots::Layout::SendClickHandler*>(activated.get())) {
|
2017-03-27 18:11:51 +00:00
|
|
|
int row = _selected / MatrixRowShift, column = _selected % MatrixRowShift;
|
|
|
|
selectInlineResult(row, column);
|
|
|
|
} else {
|
2019-09-16 11:14:06 +00:00
|
|
|
ActivateClickHandler(window(), activated, e->button());
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::selectInlineResult(int row, int column) {
|
|
|
|
if (row >= _rows.size() || column >= _rows.at(row).items.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto item = _rows[row].items[column];
|
|
|
|
if (auto inlineResult = item->getResult()) {
|
|
|
|
if (inlineResult->onChoose(item)) {
|
|
|
|
_resultSelectedCallback(inlineResult, _inlineBot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::mouseMoveEvent(QMouseEvent *e) {
|
|
|
|
_lastMousePos = e->globalPos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::leaveEventHook(QEvent *e) {
|
|
|
|
clearSelection();
|
2019-07-04 09:23:39 +00:00
|
|
|
Ui::Tooltip::Hide();
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::leaveToChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
clearSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::enterFromChildEvent(QEvent *e, QWidget *child) {
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearSelection() {
|
|
|
|
if (_selected >= 0) {
|
|
|
|
int srow = _selected / MatrixRowShift, scol = _selected % MatrixRowShift;
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(srow >= 0 && srow < _rows.size() && scol >= 0 && scol < _rows.at(srow).items.size());
|
2017-03-27 18:11:51 +00:00
|
|
|
ClickHandler::clearActive(_rows.at(srow).items.at(scol));
|
|
|
|
setCursor(style::cur_default);
|
|
|
|
}
|
|
|
|
_selected = _pressed = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::hideFinish(bool completely) {
|
|
|
|
if (completely) {
|
2018-10-23 12:57:43 +00:00
|
|
|
const auto unload = [](const auto &item) {
|
|
|
|
if (const auto document = item->getDocument()) {
|
|
|
|
document->unload();
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
2018-10-23 12:57:43 +00:00
|
|
|
if (const auto photo = item->getPhoto()) {
|
|
|
|
photo->unload();
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
2018-10-23 12:57:43 +00:00
|
|
|
if (const auto result = item->getResult()) {
|
|
|
|
result->unload();
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
2020-03-16 13:38:36 +00:00
|
|
|
item->unloadAnimation();
|
2017-03-27 18:11:51 +00:00
|
|
|
};
|
|
|
|
clearInlineRows(false);
|
2018-10-23 12:57:43 +00:00
|
|
|
for (const auto &[result, layout] : _inlineLayouts) {
|
|
|
|
unload(layout);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::inlineRowsAddItem(Result *result, Row &row, int32 &sumWidth) {
|
|
|
|
auto layout = layoutPrepareInlineResult(result, (_rows.size() * MatrixRowShift) + row.items.size());
|
|
|
|
if (!layout) return false;
|
|
|
|
|
|
|
|
layout->preload();
|
|
|
|
if (inlineRowFinalize(row, sumWidth, layout->isFullLine())) {
|
|
|
|
layout->setPosition(_rows.size() * MatrixRowShift);
|
|
|
|
}
|
|
|
|
|
|
|
|
sumWidth += layout->maxWidth();
|
|
|
|
if (!row.items.isEmpty() && row.items.back()->hasRightSkip()) {
|
|
|
|
sumWidth += st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
|
|
|
|
row.items.push_back(layout);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::inlineRowFinalize(Row &row, int32 &sumWidth, bool force) {
|
|
|
|
if (row.items.isEmpty()) return false;
|
|
|
|
|
|
|
|
auto full = (row.items.size() >= kInlineItemsMaxPerRow);
|
|
|
|
auto big = (sumWidth >= st::emojiPanWidth - st::emojiScroll.width - st::inlineResultsLeft);
|
|
|
|
if (full || big || force) {
|
|
|
|
_rows.push_back(layoutInlineRow(row, (full || big) ? sumWidth : 0));
|
|
|
|
row = Row();
|
|
|
|
row.items.reserve(kInlineItemsMaxPerRow);
|
|
|
|
sumWidth = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineBotChanged() {
|
2017-06-11 18:33:20 +00:00
|
|
|
refreshInlineRows(nullptr, nullptr, nullptr, true);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearInlineRows(bool resultsDeleted) {
|
|
|
|
if (resultsDeleted) {
|
|
|
|
_selected = _pressed = -1;
|
|
|
|
} else {
|
|
|
|
clearSelection();
|
|
|
|
for_const (auto &row, _rows) {
|
|
|
|
for_const (auto &item, row.items) {
|
|
|
|
item->setPosition(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_rows.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemBase *Inner::layoutPrepareInlineResult(Result *result, int32 position) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
it->second->setPosition(position);
|
|
|
|
return it->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::deleteUnusedInlineLayouts() {
|
|
|
|
if (_rows.isEmpty()) { // delete all
|
|
|
|
_inlineLayouts.clear();
|
|
|
|
} else {
|
|
|
|
for (auto i = _inlineLayouts.begin(); i != _inlineLayouts.cend();) {
|
|
|
|
if (i->second->position() < 0) {
|
|
|
|
i = _inlineLayouts.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Inner::Row &Inner::layoutInlineRow(Row &row, int32 sumWidth) {
|
|
|
|
auto count = int(row.items.size());
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(count <= kInlineItemsMaxPerRow);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
// enumerate items in the order of growing maxWidth()
|
|
|
|
// for that sort item indices by maxWidth()
|
|
|
|
int indices[kInlineItemsMaxPerRow];
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
indices[i] = i;
|
|
|
|
}
|
|
|
|
std::sort(indices, indices + count, [&row](int a, int b) -> bool {
|
|
|
|
return row.items.at(a)->maxWidth() < row.items.at(b)->maxWidth();
|
|
|
|
});
|
|
|
|
|
|
|
|
row.height = 0;
|
|
|
|
int availw = width() - (st::inlineResultsLeft - st::buttonRadius);
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
int index = indices[i];
|
|
|
|
int w = sumWidth ? (row.items.at(index)->maxWidth() * availw / sumWidth) : row.items.at(index)->maxWidth();
|
|
|
|
int actualw = qMax(w, int(st::inlineResultsMinWidth));
|
|
|
|
row.height = qMax(row.height, row.items.at(index)->resizeGetHeight(actualw));
|
|
|
|
if (sumWidth) {
|
|
|
|
availw -= actualw;
|
|
|
|
sumWidth -= row.items.at(index)->maxWidth();
|
|
|
|
if (index > 0 && row.items.at(index - 1)->hasRightSkip()) {
|
|
|
|
availw -= st::inlineResultsSkip;
|
|
|
|
sumWidth -= st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::preloadImages() {
|
|
|
|
for (auto row = 0, rows = _rows.size(); row != rows; ++row) {
|
|
|
|
for (auto col = 0, cols = _rows[row].items.size(); col != cols; ++col) {
|
|
|
|
_rows[row].items[col]->preload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::hideInlineRowsPanel() {
|
|
|
|
clearInlineRows(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::clearInlineRowsPanel() {
|
|
|
|
clearInlineRows(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::refreshSwitchPmButton(const CacheEntry *entry) {
|
|
|
|
if (!entry || entry->switchPmText.isEmpty()) {
|
|
|
|
_switchPmButton.destroy();
|
|
|
|
_switchPmStartToken.clear();
|
|
|
|
} else {
|
|
|
|
if (!_switchPmButton) {
|
2019-06-18 16:53:27 +00:00
|
|
|
_switchPmButton.create(this, nullptr, st::switchPmButton);
|
2017-03-27 18:11:51 +00:00
|
|
|
_switchPmButton->show();
|
|
|
|
_switchPmButton->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
|
2019-09-13 16:45:48 +00:00
|
|
|
_switchPmButton->addClickHandler([=] { onSwitchPm(); });
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
2019-06-18 16:53:27 +00:00
|
|
|
_switchPmButton->setText(rpl::single(entry->switchPmText));
|
2017-03-27 18:11:51 +00:00
|
|
|
_switchPmStartToken = entry->switchPmStartToken;
|
2019-06-18 16:53:27 +00:00
|
|
|
const auto buttonTop = st::stickerPanPadding;
|
2017-03-27 18:11:51 +00:00
|
|
|
_switchPmButton->move(st::inlineResultsLeft - st::buttonRadius, buttonTop);
|
2017-06-11 18:33:20 +00:00
|
|
|
if (isRestrictedView()) {
|
|
|
|
_switchPmButton->hide();
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2017-06-11 18:33:20 +00:00
|
|
|
int Inner::refreshInlineRows(PeerData *queryPeer, UserData *bot, const CacheEntry *entry, bool resultsDeleted) {
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineBot = bot;
|
2017-06-11 18:33:20 +00:00
|
|
|
_inlineQueryPeer = queryPeer;
|
2017-03-27 18:11:51 +00:00
|
|
|
refreshSwitchPmButton(entry);
|
2018-04-07 08:47:08 +00:00
|
|
|
auto clearResults = [&] {
|
2017-03-27 18:11:51 +00:00
|
|
|
if (!entry) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (entry->results.empty() && entry->switchPmText.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
2017-04-01 08:10:13 +00:00
|
|
|
auto clearResultsResult = clearResults(); // Clang workaround.
|
|
|
|
if (clearResultsResult) {
|
2017-03-27 18:11:51 +00:00
|
|
|
if (resultsDeleted) {
|
|
|
|
clearInlineRows(true);
|
|
|
|
deleteUnusedInlineLayouts();
|
|
|
|
}
|
|
|
|
emit emptyInlineRows();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearSelection();
|
|
|
|
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(_inlineBot != 0);
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
auto count = int(entry->results.size());
|
|
|
|
auto from = validateExistingInlineRows(entry->results);
|
|
|
|
auto added = 0;
|
|
|
|
|
|
|
|
if (count) {
|
|
|
|
_rows.reserve(count);
|
|
|
|
auto row = Row();
|
|
|
|
row.items.reserve(kInlineItemsMaxPerRow);
|
|
|
|
auto sumWidth = 0;
|
|
|
|
for (auto i = from; i != count; ++i) {
|
|
|
|
if (inlineRowsAddItem(entry->results[i].get(), row, sumWidth)) {
|
|
|
|
++added;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inlineRowFinalize(row, sumWidth, true);
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:46:24 +00:00
|
|
|
auto h = countHeight();
|
2017-03-27 18:11:51 +00:00
|
|
|
if (h != height()) resize(width(), h);
|
|
|
|
update();
|
|
|
|
|
|
|
|
_lastMousePos = QCursor::pos();
|
|
|
|
updateSelected();
|
|
|
|
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Inner::validateExistingInlineRows(const Results &results) {
|
|
|
|
int count = results.size(), until = 0, untilrow = 0, untilcol = 0;
|
|
|
|
for (; until < count;) {
|
|
|
|
if (untilrow >= _rows.size() || _rows[untilrow].items[untilcol]->getResult() != results[until].get()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++until;
|
|
|
|
if (++untilcol == _rows[untilrow].items.size()) {
|
|
|
|
++untilrow;
|
|
|
|
untilcol = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (until == count) { // all items are layed out
|
|
|
|
if (untilrow == _rows.size()) { // nothing changed
|
|
|
|
return until;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = untilrow, l = _rows.size(), skip = untilcol; i < l; ++i) {
|
|
|
|
for (int j = 0, s = _rows[i].items.size(); j < s; ++j) {
|
|
|
|
if (skip) {
|
|
|
|
--skip;
|
|
|
|
} else {
|
|
|
|
_rows[i].items[j]->setPosition(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!untilcol) { // all good rows are filled
|
|
|
|
_rows.resize(untilrow);
|
|
|
|
return until;
|
|
|
|
}
|
|
|
|
_rows.resize(untilrow + 1);
|
|
|
|
_rows[untilrow].items.resize(untilcol);
|
|
|
|
_rows[untilrow] = layoutInlineRow(_rows[untilrow]);
|
|
|
|
return until;
|
|
|
|
}
|
|
|
|
if (untilrow && !untilcol) { // remove last row, maybe it is not full
|
|
|
|
--untilrow;
|
|
|
|
untilcol = _rows[untilrow].items.size();
|
|
|
|
}
|
|
|
|
until -= untilcol;
|
|
|
|
|
|
|
|
for (int i = untilrow, l = _rows.size(); i < l; ++i) {
|
|
|
|
for (int j = 0, s = _rows[i].items.size(); j < s; ++j) {
|
|
|
|
_rows[i].items[j]->setPosition(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_rows.resize(untilrow);
|
|
|
|
|
|
|
|
if (_rows.isEmpty()) {
|
|
|
|
_inlineWithThumb = false;
|
|
|
|
for (int i = until; i < count; ++i) {
|
|
|
|
if (results.at(i)->hasThumbDisplay()) {
|
|
|
|
_inlineWithThumb = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return until;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineItemLayoutChanged(const ItemBase *layout) {
|
|
|
|
if (_selected < 0 || !isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = _selected / MatrixRowShift, col = _selected % MatrixRowShift;
|
|
|
|
if (row < _rows.size() && col < _rows.at(row).items.size()) {
|
|
|
|
if (layout == _rows.at(row).items.at(col)) {
|
|
|
|
updateSelected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::inlineItemRepaint(const ItemBase *layout) {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-03-27 18:11:51 +00:00
|
|
|
if (_lastScrolled + 100 <= ms) {
|
|
|
|
update();
|
|
|
|
} else {
|
2018-11-21 18:14:48 +00:00
|
|
|
_updateInlineItems.callOnce(_lastScrolled + 100 - ms);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Inner::inlineItemVisible(const ItemBase *layout) {
|
|
|
|
int32 position = layout->position();
|
|
|
|
if (position < 0 || !isVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int row = position / MatrixRowShift, col = position % MatrixRowShift;
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert((row < _rows.size()) && (col < _rows[row].items.size()));
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
auto &inlineItems = _rows[row].items;
|
|
|
|
int top = st::stickerPanPadding;
|
|
|
|
for (int32 i = 0; i < row; ++i) {
|
|
|
|
top += _rows.at(i).height;
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:46:24 +00:00
|
|
|
return (top < _visibleBottom) && (top + _rows[row].items[col]->height() > _visibleTop);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
Data::FileOrigin Inner::inlineItemFileOrigin() {
|
|
|
|
return Data::FileOrigin();
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:11:51 +00:00
|
|
|
void Inner::updateSelected() {
|
|
|
|
if (_pressed >= 0 && !_previewShown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto newSelected = -1;
|
|
|
|
auto p = mapFromGlobal(_lastMousePos);
|
|
|
|
|
|
|
|
int sx = (rtl() ? width() - p.x() : p.x()) - (st::inlineResultsLeft - st::buttonRadius);
|
|
|
|
int sy = p.y() - st::stickerPanPadding;
|
|
|
|
if (_switchPmButton) {
|
|
|
|
sy -= _switchPmButton->height() + st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
int row = -1, col = -1, sel = -1;
|
|
|
|
ClickHandlerPtr lnk;
|
|
|
|
ClickHandlerHost *lnkhost = nullptr;
|
2018-01-27 13:59:24 +00:00
|
|
|
HistoryView::CursorState cursor = HistoryView::CursorState::None;
|
2017-03-27 18:11:51 +00:00
|
|
|
if (sy >= 0) {
|
|
|
|
row = 0;
|
|
|
|
for (int rows = _rows.size(); row < rows; ++row) {
|
2018-01-27 13:59:24 +00:00
|
|
|
if (sy < _rows[row].height) {
|
2017-03-27 18:11:51 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-27 13:59:24 +00:00
|
|
|
sy -= _rows[row].height;
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sx >= 0 && row >= 0 && row < _rows.size()) {
|
|
|
|
auto &inlineItems = _rows[row].items;
|
|
|
|
col = 0;
|
|
|
|
for (int cols = inlineItems.size(); col < cols; ++col) {
|
|
|
|
int width = inlineItems.at(col)->width();
|
|
|
|
if (sx < width) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sx -= width;
|
|
|
|
if (inlineItems.at(col)->hasRightSkip()) {
|
|
|
|
sx -= st::inlineResultsSkip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (col < inlineItems.size()) {
|
|
|
|
sel = row * MatrixRowShift + col;
|
2018-01-27 13:59:24 +00:00
|
|
|
auto result = inlineItems[col]->getState(
|
|
|
|
QPoint(sx, sy),
|
|
|
|
HistoryView::StateRequest());
|
2017-10-13 19:07:04 +00:00
|
|
|
lnk = result.link;
|
|
|
|
cursor = result.cursor;
|
|
|
|
lnkhost = inlineItems[col];
|
2017-03-27 18:11:51 +00:00
|
|
|
} else {
|
|
|
|
row = col = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
row = col = -1;
|
|
|
|
}
|
|
|
|
int srow = (_selected >= 0) ? (_selected / MatrixRowShift) : -1;
|
|
|
|
int scol = (_selected >= 0) ? (_selected % MatrixRowShift) : -1;
|
|
|
|
if (_selected != sel) {
|
|
|
|
if (srow >= 0 && scol >= 0) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(srow >= 0 && srow < _rows.size() && scol >= 0 && scol < _rows.at(srow).items.size());
|
2017-03-27 18:11:51 +00:00
|
|
|
_rows[srow].items[scol]->update();
|
|
|
|
}
|
|
|
|
_selected = sel;
|
|
|
|
if (row >= 0 && col >= 0) {
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(row >= 0 && row < _rows.size() && col >= 0 && col < _rows.at(row).items.size());
|
2017-03-27 18:11:51 +00:00
|
|
|
_rows[row].items[col]->update();
|
|
|
|
}
|
|
|
|
if (_previewShown && _selected >= 0 && _pressed != _selected) {
|
|
|
|
_pressed = _selected;
|
|
|
|
if (row >= 0 && col >= 0) {
|
|
|
|
auto layout = _rows.at(row).items.at(col);
|
2019-06-29 10:29:04 +00:00
|
|
|
if (const auto w = App::wnd()) {
|
|
|
|
if (const auto previewDocument = layout->getPreviewDocument()) {
|
|
|
|
w->showMediaPreview(
|
|
|
|
Data::FileOrigin(),
|
|
|
|
previewDocument);
|
|
|
|
} else if (auto previewPhoto = layout->getPreviewPhoto()) {
|
|
|
|
w->showMediaPreview(Data::FileOrigin(), previewPhoto);
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ClickHandler::setActive(lnk, lnkhost)) {
|
|
|
|
setCursor(lnk ? style::cur_pointer : style::cur_default);
|
2019-07-04 09:23:39 +00:00
|
|
|
Ui::Tooltip::Hide();
|
|
|
|
}
|
|
|
|
if (lnk) {
|
|
|
|
Ui::Tooltip::Show(1000, this);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 18:14:48 +00:00
|
|
|
void Inner::showPreview() {
|
2017-03-27 18:11:51 +00:00
|
|
|
if (_pressed < 0) return;
|
|
|
|
|
|
|
|
int row = _pressed / MatrixRowShift, col = _pressed % MatrixRowShift;
|
|
|
|
if (row < _rows.size() && col < _rows.at(row).items.size()) {
|
|
|
|
auto layout = _rows.at(row).items.at(col);
|
2019-06-29 10:29:04 +00:00
|
|
|
if (const auto w = App::wnd()) {
|
|
|
|
if (const auto previewDocument = layout->getPreviewDocument()) {
|
|
|
|
w->showMediaPreview(Data::FileOrigin(), previewDocument);
|
|
|
|
_previewShown = true;
|
|
|
|
} else if (const auto previewPhoto = layout->getPreviewPhoto()) {
|
|
|
|
w->showMediaPreview(Data::FileOrigin(), previewPhoto);
|
|
|
|
_previewShown = true;
|
|
|
|
}
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 18:14:48 +00:00
|
|
|
void Inner::updateInlineItems() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-03-27 18:11:51 +00:00
|
|
|
if (_lastScrolled + 100 <= ms) {
|
|
|
|
update();
|
|
|
|
} else {
|
2018-11-21 18:14:48 +00:00
|
|
|
_updateInlineItems.callOnce(_lastScrolled + 100 - ms);
|
2017-03-27 18:11:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Inner::onSwitchPm() {
|
2019-07-18 08:51:11 +00:00
|
|
|
if (_inlineBot && _inlineBot->isBot()) {
|
2017-03-27 18:11:51 +00:00
|
|
|
_inlineBot->botInfo->startToken = _switchPmStartToken;
|
|
|
|
Ui::showPeerHistory(_inlineBot, ShowAndStartBotMsgId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
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)
|
2019-11-27 08:02:56 +00:00
|
|
|
, _api(_controller->session().api().instance())
|
2017-03-27 18:11:51 +00:00
|
|
|
, _contentMaxHeight(st::emojiPanMaxHeight)
|
|
|
|
, _contentHeight(_contentMaxHeight)
|
|
|
|
, _scroll(this, st::inlineBotsScroll) {
|
|
|
|
resize(QRect(0, 0, st::emojiPanWidth, _contentHeight).marginsAdded(innerPadding()).size());
|
|
|
|
_width = width();
|
|
|
|
_height = height();
|
|
|
|
|
|
|
|
_scroll->resize(st::emojiPanWidth - st::buttonRadius, _contentHeight);
|
|
|
|
|
|
|
|
_scroll->move(verticalRect().topLeft());
|
2017-04-07 18:10:49 +00:00
|
|
|
_inner = _scroll->setOwnedWidget(object_ptr<internal::Inner>(this, controller));
|
2017-03-27 18:11:51 +00:00
|
|
|
|
|
|
|
_inner->moveToLeft(0, 0, _scroll->width());
|
|
|
|
|
|
|
|
connect(_scroll, SIGNAL(scrolled()), this, SLOT(onScroll()));
|
|
|
|
|
|
|
|
connect(_inner, SIGNAL(emptyInlineRows()), this, SLOT(onEmptyInlineRows()));
|
|
|
|
|
|
|
|
// inline bots
|
|
|
|
_inlineRequestTimer.setSingleShot(true);
|
|
|
|
connect(&_inlineRequestTimer, SIGNAL(timeout()), this, SLOT(onInlineRequest()));
|
|
|
|
|
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);
|
|
|
|
forceRepaintOnScroll->setGeometry(innerRect().x() + st::buttonRadius, innerRect().y() + st::buttonRadius, st::buttonRadius, st::buttonRadius);
|
|
|
|
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;
|
2018-11-22 12:48:50 +00:00
|
|
|
auto contentHeight = snap(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();
|
2017-05-24 12:20:50 +00:00
|
|
|
App::roundRect(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);
|
|
|
|
p.fillRect(myrtlrect(inner.x(), sidesTop, st::buttonRadius, sidesHeight), st::emojiPanBg);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-03-27 18:11:51 +00:00
|
|
|
_inner->hideFinish(true);
|
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 {
|
|
|
|
return innerRect().marginsRemoved(style::margins(0, st::buttonRadius, 0, st::buttonRadius));
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect Widget::verticalRect() const {
|
|
|
|
return innerRect().marginsRemoved(style::margins(st::buttonRadius, 0, st::buttonRadius, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return inner.marginsRemoved(QMargins(st::buttonRadius, 0, st::buttonRadius, 0)).contains(testRect)
|
|
|
|
|| inner.marginsRemoved(QMargins(0, st::buttonRadius, 0, st::buttonRadius)).contains(testRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::inlineBotChanged() {
|
|
|
|
if (!_inlineBot) return;
|
|
|
|
|
|
|
|
if (!isHidden() && !_hiding) {
|
|
|
|
hideAnimated();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_inlineRequestId) MTP::cancel(_inlineRequestId);
|
|
|
|
_inlineRequestId = 0;
|
|
|
|
_inlineQuery = _inlineNextQuery = _inlineNextOffset = QString();
|
|
|
|
_inlineBot = nullptr;
|
|
|
|
_inlineCache.clear();
|
|
|
|
_inner->inlineBotChanged();
|
|
|
|
_inner->hideInlineRowsPanel();
|
|
|
|
|
|
|
|
Notify::inlineBotRequesting(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::inlineResultsDone(const MTPmessages_BotResults &result) {
|
|
|
|
_inlineRequestId = 0;
|
|
|
|
Notify::inlineBotRequesting(false);
|
|
|
|
|
|
|
|
auto it = _inlineCache.find(_inlineQuery);
|
|
|
|
auto adding = (it != _inlineCache.cend());
|
|
|
|
if (result.type() == mtpc_messages_botResults) {
|
|
|
|
auto &d = result.c_messages_botResults();
|
2019-07-05 13:38:38 +00:00
|
|
|
Auth().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()) {
|
|
|
|
it = _inlineCache.emplace(_inlineQuery, std::make_unique<internal::CacheEntry>()).first;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
for_const (const auto &res, v) {
|
|
|
|
if (auto result = InlineBots::Result::create(queryId, res)) {
|
|
|
|
++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) {
|
|
|
|
MTP::cancel(_inlineRequestId);
|
|
|
|
_inlineRequestId = 0;
|
|
|
|
Notify::inlineBotRequesting(false);
|
|
|
|
}
|
|
|
|
if (_inlineCache.find(query) != _inlineCache.cend()) {
|
|
|
|
_inlineRequestTimer.stop();
|
|
|
|
_inlineQuery = _inlineNextQuery = query;
|
|
|
|
showInlineRows(true);
|
|
|
|
} else {
|
|
|
|
_inlineNextQuery = query;
|
2017-03-31 15:50:02 +00:00
|
|
|
_inlineRequestTimer.start(internal::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
|
|
|
}
|
|
|
|
Notify::inlineBotRequesting(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?
|
|
|
|
Notify::inlineBotRequesting(false);
|
|
|
|
_inlineRequestId = 0;
|
|
|
|
}).handleAllErrors().send();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Widget::onEmptyInlineRows() {
|
|
|
|
hideAnimated();
|
|
|
|
_inner->clearInlineRowsPanel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Widget::refreshInlineRows(int *added) {
|
|
|
|
auto it = _inlineCache.find(_inlineQuery);
|
|
|
|
const internal::CacheEntry *entry = nullptr;
|
|
|
|
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
|