From 18de2f724ce419fd70dd0f99e6d2123d0d2a5b90 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 2 Jan 2016 11:45:05 +0800 Subject: [PATCH] fixed dropdown behaviour for @gif search from saved gifs panel --- Telegram/SourceFiles/dropdown.cpp | 117 +++++++++++++++---------- Telegram/SourceFiles/dropdown.h | 4 + Telegram/SourceFiles/historywidget.cpp | 4 +- Telegram/SourceFiles/settings.h | 3 + 4 files changed, 81 insertions(+), 47 deletions(-) diff --git a/Telegram/SourceFiles/dropdown.cpp b/Telegram/SourceFiles/dropdown.cpp index 65fa08556..96fb37ac8 100644 --- a/Telegram/SourceFiles/dropdown.cpp +++ b/Telegram/SourceFiles/dropdown.cpp @@ -1303,6 +1303,12 @@ void StickerPanInner::paintEvent(QPaintEvent *e) { } void StickerPanInner::paintInlineItems(Painter &p, const QRect &r) { + if (_inlineRows.isEmpty()) { + p.setFont(st::normalFont); + p.setPen(st::noContactsColor); + p.drawText(QRect(0, 0, width(), (height() / 3) * 2 + st::normalFont->height), lang(lng_inline_bot_no_results), style::al_center); + return; + } InlinePaintContext context(getms(), false, Ui::isLayerShown() || Ui::isMediaViewShown() || _previewShown, false); int32 top = st::emojiPanHeader; @@ -1890,9 +1896,13 @@ void StickerPanInner::hideInlineRowsPanel() { } } +void StickerPanInner::clearInlineRowsPanel() { + clearInlineRows(false); +} + void StickerPanInner::refreshInlineRows(UserData *bot, const InlineResults &results, bool resultsDeleted) { - int32 count = results.size(), until = 0, untilrow = 0, untilcol = 0; - if (!count) { + _inlineBot = bot; + if (results.isEmpty() && (!_inlineBot || _inlineBot->username != cInlineGifBotUsername())) { if (resultsDeleted) { clearInlineRows(true); } @@ -1904,12 +1914,38 @@ void StickerPanInner::refreshInlineRows(UserData *bot, const InlineResults &resu clearSelection(true); } - t_assert(bot != 0); - _inlineBot = bot; - _inlineBotTitle = lng_inline_bot_results(lt_inline_bot, bot->username.isEmpty() ? bot->name : ('@' + bot->username)); + t_assert(_inlineBot != 0); + _inlineBotTitle = lng_inline_bot_results(lt_inline_bot, _inlineBot->username.isEmpty() ? _inlineBot->name : ('@' + _inlineBot->username)); _showingInlineItems = true; _showingSavedGifs = false; + + int32 count = results.size(), from = validateExistingInlineRows(results); + + if (count) { + _inlineRows.reserve(count); + InlineRow row; + row.items.reserve(SavedGifsMaxPerRow); + int32 sumWidth = 0; + for (int32 i = from; i < count; ++i) { + inlineRowsAddItem(0, results.at(i), row, sumWidth); + } + inlineRowFinalize(row, sumWidth, true); + } + + int32 h = countHeight(); + if (h != height()) resize(width(), h); + update(); + + emit refreshIcons(); + if (_showingInlineItems) { + _lastMousePos = QCursor::pos(); + updateSelected(); + } +} + +int32 StickerPanInner::validateExistingInlineRows(const InlineResults &results) { + int32 count = results.size(), until = 0, untilrow = 0, untilcol = 0; for (; until < count;) { if (untilrow >= _inlineRows.size() || _inlineRows.at(untilrow).items.at(untilcol)->result() != results.at(until)) { break; @@ -1922,7 +1958,7 @@ void StickerPanInner::refreshInlineRows(UserData *bot, const InlineResults &resu } if (until == count) { // all items are layed out if (untilrow == _inlineRows.size()) { // nothing changed - return; + return until; } for (int32 i = untilrow, l = _inlineRows.size(), skip = untilcol; i < l; ++i) { @@ -1936,12 +1972,12 @@ void StickerPanInner::refreshInlineRows(UserData *bot, const InlineResults &resu } if (!untilcol) { // all good rows are filled _inlineRows.resize(untilrow); - return; + return until; } _inlineRows.resize(untilrow + 1); _inlineRows[untilrow].items.resize(untilcol); _inlineRows[untilrow] = layoutInlineRow(_inlineRows[untilrow]); - return; + return until; } if (untilrow && !untilcol) { // remove last row, maybe it is not full --untilrow; @@ -1965,25 +2001,7 @@ void StickerPanInner::refreshInlineRows(UserData *bot, const InlineResults &resu } } } - - _inlineRows.reserve(count); - InlineRow row; - row.items.reserve(SavedGifsMaxPerRow); - int32 sumWidth = 0; - for (int32 i = until; i < count; ++i) { - inlineRowsAddItem(0, results.at(i), row, sumWidth); - } - inlineRowFinalize(row, sumWidth, true); - - int32 h = countHeight(); - if (h != height()) resize(width(), h); - update(); - - emit refreshIcons(); - if (_showingInlineItems) { - _lastMousePos = QCursor::pos(); - updateSelected(); - } + return until; } void StickerPanInner::ui_repaintInlineItem(const LayoutInlineItem *layout) { @@ -2426,7 +2444,7 @@ void StickerPanInner::updateShowingSavedGifs() { void StickerPanInner::showFinish() { if (_showingInlineItems && _showingSavedGifs) { - _setGifCommand = App::insertBotCommand((cTestMode() ? qstr("@contextbot") : qstr("@gif")), true); + _setGifCommand = App::insertBotCommand('@' + cInlineGifBotUsername(), true); } } @@ -3197,9 +3215,7 @@ void EmojiPan::hideStart() { hideAnimated(); } -void EmojiPan::hideAnimated() { - if (_hiding) return; - +void EmojiPan::prepareShowHideCache() { if (_cache.isNull()) { QPixmap from = _fromCache, to = _toCache; _fromCache = _toCache = QPixmap(); @@ -3207,6 +3223,12 @@ void EmojiPan::hideAnimated() { _cache = myGrab(this, rect().marginsRemoved(st::dropdownDef.padding)); _fromCache = from; _toCache = to; } +} + +void EmojiPan::hideAnimated() { + if (_hiding) return; + + prepareShowHideCache(); hideAll(); _hiding = true; a_opacity.start(0); @@ -3254,6 +3276,7 @@ void EmojiPan::showStart() { s_inner.refreshRecent(); _stickersShown = false; _shownFromInlineQuery = false; + _cache = QPixmap(); // clear after refreshInlineRows() } recountContentMaxHeight(); s_inner.preloadImages(); @@ -3265,13 +3288,7 @@ void EmojiPan::showStart() { onSwitch(); } } - if (_cache.isNull()) { - QPixmap from = _fromCache, to = _toCache; - _fromCache = _toCache = QPixmap(); - showAll(); - _cache = myGrab(this, rect().marginsRemoved(st::dropdownDef.padding)); - _fromCache = from; _toCache = to; - } + prepareShowHideCache(); hideAll(); _hiding = false; show(); @@ -3574,11 +3591,15 @@ void EmojiPan::clearInlineBot() { e_switch.moveToRight(0, 0, st::emojiPanWidth); } +bool EmojiPan::hideOnNoInlineResults() { + return _inlineBot && _stickersShown && s_inner.inlineResultsShown() && (_shownFromInlineQuery || _inlineBot->username != cInlineGifBotUsername()); +} + void EmojiPan::inlineBotChanged() { if (!_inlineBot) return; if (!isHidden() && !_hiding) { - if (_stickersShown || !rect().contains(mapFromGlobal(QCursor::pos()))) { + if (hideOnNoInlineResults() || !rect().contains(mapFromGlobal(QCursor::pos()))) { hideAnimated(); } } @@ -3740,10 +3761,12 @@ void EmojiPan::onInlineRequest() { } void EmojiPan::onEmptyInlineRows() { - if (_shownFromInlineQuery || _stickersShown) { + if (_shownFromInlineQuery || hideOnNoInlineResults()) { hideAnimated(); - } else { + } else if (!_inlineBot) { s_inner.hideInlineRowsPanel(); + } else { + s_inner.clearInlineRowsPanel(); } } @@ -3754,7 +3777,7 @@ bool EmojiPan::refreshInlineRows() { clear = i.value()->results.isEmpty(); _inlineNextOffset = i.value()->nextOffset; } - + if (clear) prepareShowHideCache(); s_inner.refreshInlineRows(_inlineBot, clear ? InlineResults() : i.value()->results, false); return !clear; } @@ -3770,9 +3793,13 @@ void EmojiPan::showInlineRows(bool newResults) { if (!hidden && !clear) { recountContentMaxHeight(); } - if (clear && !hidden && _stickersShown) { - hideAnimated(); - } else if (!clear) { + if (clear) { + if (!hidden && hideOnNoInlineResults()) { + hideAnimated(); + } else { + _cache = QPixmap(); // clear after refreshInlineRows() + } + } else { _hideTimer.stop(); if (hidden || _hiding) { showStart(); diff --git a/Telegram/SourceFiles/dropdown.h b/Telegram/SourceFiles/dropdown.h index d4b4df93a..9cbc0eb6b 100644 --- a/Telegram/SourceFiles/dropdown.h +++ b/Telegram/SourceFiles/dropdown.h @@ -348,6 +348,7 @@ public: void refreshRecent(); void inlineBotChanged(); void hideInlineRowsPanel(); + void clearInlineRowsPanel(); void fillIcons(QList &icons); void fillPanels(QVector &panels); @@ -465,6 +466,7 @@ private: void deleteUnusedInlineLayouts(); + int32 validateExistingInlineRows(const InlineResults &results); int32 _selected, _pressedSel; QPoint _lastMousePos; TextLinkPtr _linkOver, _linkDown; @@ -631,6 +633,7 @@ private: void leaveToChildEvent(QEvent *e); void hideAnimated(); + void prepareShowHideCache(); void updateSelected(); void updateIcons(); @@ -707,6 +710,7 @@ private: void inlineBotChanged(); void showInlineRows(bool newResults); + bool hideOnNoInlineResults(); void recountContentMaxHeight(); bool refreshInlineRows(); UserData *_inlineBot; diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp index 2d991af30..631c129ae 100644 --- a/Telegram/SourceFiles/historywidget.cpp +++ b/Telegram/SourceFiles/historywidget.cpp @@ -2844,7 +2844,7 @@ void HistoryWidget::updateInlineBotQuery() { if (_inlineBot != bot) { updateFieldPlaceholder(); } - if (_inlineBot->username == (cTestMode() ? qstr("contextbot") : qstr("gif")) && query.isEmpty()) { + if (_inlineBot->username == cInlineGifBotUsername() && query.isEmpty()) { _emojiPan.clearInlineBot(); } else { _emojiPan.queryInlineBot(_inlineBot, query); @@ -4910,7 +4910,7 @@ bool HistoryWidget::insertBotCommand(const QString &cmd, bool specialGif) { if (toInsert.at(0) != '@') { QString text = _field.getLastText(); if (specialGif) { - if (text.trimmed() == (cTestMode() ? qstr("@contextbot") : qstr("@gif")) && text.at(0) == '@') { + if (text.trimmed() == '@' + cInlineGifBotUsername() && text.at(0) == '@') { setFieldText(QString(), TextUpdateEventsSaveDraft, false); } } else { diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 25b34f573..b84578a44 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -73,6 +73,9 @@ DeclareSetting(uint64, RealBetaVersion); DeclareSetting(QByteArray, BetaPrivateKey); DeclareSetting(bool, TestMode); +inline QString cInlineGifBotUsername() { + return cTestMode() ? qstr("contextbot") : qstr("gif"); +} DeclareSetting(QString, LoggedPhoneNumber); DeclareReadSetting(uint32, ConnectionsInSession); DeclareSetting(bool, AutoStart);