From 6fe380ff9fca9d7aa449e380808a8ee25e05cac8 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sat, 2 Jan 2016 09:28:11 +0800 Subject: [PATCH] checking inline bot query on text change, not on cursor position change --- Telegram/SourceFiles/gui/flattextarea.cpp | 11 ++- Telegram/SourceFiles/gui/flattextarea.h | 3 +- Telegram/SourceFiles/historywidget.cpp | 96 ++++++++++++----------- Telegram/SourceFiles/historywidget.h | 1 + Telegram/SourceFiles/structs.cpp | 12 ++- 5 files changed, 72 insertions(+), 51 deletions(-) diff --git a/Telegram/SourceFiles/gui/flattextarea.cpp b/Telegram/SourceFiles/gui/flattextarea.cpp index 92b2f4f3b..4f27183e7 100644 --- a/Telegram/SourceFiles/gui/flattextarea.cpp +++ b/Telegram/SourceFiles/gui/flattextarea.cpp @@ -270,11 +270,9 @@ EmojiPtr FlatTextarea::getSingleEmoji() const { return 0; } -QString FlatTextarea::getMentionHashtagBotCommandPart(bool &start, UserData *&inlineBot, QString &inlineBotUsername) const { - start = false; - - // check inline bot query +QString FlatTextarea::getInlineBotQuery(UserData *&inlineBot, QString &inlineBotUsername) const { const QString &text(getLastText()); + int32 inlineUsernameStart = 1, inlineUsernameLength = 0, size = text.size(); if (size > 2 && text.at(0) == '@' && text.at(1).isLetter()) { inlineUsernameLength = 1; @@ -318,6 +316,11 @@ QString FlatTextarea::getMentionHashtagBotCommandPart(bool &start, UserData *&in inlineBot = 0; inlineBotUsername = QString(); } + return QString(); +} + +QString FlatTextarea::getMentionHashtagBotCommandPart(bool &start) const { + start = false; int32 pos = textCursor().position(); if (textCursor().anchor() != pos) return QString(); diff --git a/Telegram/SourceFiles/gui/flattextarea.h b/Telegram/SourceFiles/gui/flattextarea.h index 66460afc7..14b9fa10d 100644 --- a/Telegram/SourceFiles/gui/flattextarea.h +++ b/Telegram/SourceFiles/gui/flattextarea.h @@ -64,7 +64,8 @@ public: QSize minimumSizeHint() const; EmojiPtr getSingleEmoji() const; - QString getMentionHashtagBotCommandPart(bool &start, UserData *&contextBot, QString &contextBotUsername) const; + QString getMentionHashtagBotCommandPart(bool &start) const; + QString getInlineBotQuery(UserData *&contextBot, QString &contextBotUsername) const; void removeSingleEmoji(); bool hasText() const; diff --git a/Telegram/SourceFiles/historywidget.cpp b/Telegram/SourceFiles/historywidget.cpp index 086e88b98..28ca78f93 100644 --- a/Telegram/SourceFiles/historywidget.cpp +++ b/Telegram/SourceFiles/historywidget.cpp @@ -2820,9 +2820,53 @@ void HistoryWidget::onMentionHashtagOrBotCommandInsert(QString str) { } } +void HistoryWidget::updateInlineBotQuery() { + UserData *bot = _inlineBot; + bool start = false; + QString inlineBotUsername(_inlineBotUsername); + QString query = _field.getInlineBotQuery(_inlineBot, _inlineBotUsername); + if (inlineBotUsername != _inlineBotUsername) { + if (_inlineBotResolveRequestId) { + Notify::inlineBotRequesting(false); + MTP::cancel(_inlineBotResolveRequestId); + _inlineBotResolveRequestId = 0; + } + if (_inlineBot == InlineBotLookingUpData) { + Notify::inlineBotRequesting(true); + _inlineBotResolveRequestId = MTP::send(MTPcontacts_ResolveUsername(MTP_string(_inlineBotUsername)), rpcDone(&HistoryWidget::inlineBotResolveDone), rpcFail(&HistoryWidget::inlineBotResolveFail, _inlineBotUsername)); + return; + } + } else if (_inlineBot == InlineBotLookingUpData) { + return; + } + + if (_inlineBot) { + if (_inlineBot != bot) { + updateFieldPlaceholder(); + } + if (_inlineBot->username == (cTestMode() ? qstr("contextbot") : qstr("gif")) && query.isEmpty()) { + _emojiPan.clearInlineBot(); + } else { + _emojiPan.queryInlineBot(_inlineBot, query); + } + if (!_attachMention.isHidden()) { + _attachMention.hideStart(); + } + } else { + if (_inlineBot != bot) { + updateFieldPlaceholder(); + _field.finishPlaceholder(); + } + _emojiPan.clearInlineBot(); + onCheckMentionDropdown(); + } +} + void HistoryWidget::onTextChange() { + updateInlineBotQuery(); + if (_peer && (!_peer->isChannel() || _peer->isMegagroup() || !_peer->asChannel()->canPublish() || (!_peer->asChannel()->isBroadcast() && !_broadcast.checked()))) { - if (!_inlineBot && !_inlineBotResolveRequestId && (_textUpdateEventsFlags & TextUpdateEventsSendTyping)) { + if (!_inlineBot && (_textUpdateEventsFlags & TextUpdateEventsSendTyping)) { updateSendAction(_history, SendActionTyping); } } @@ -5347,52 +5391,16 @@ void HistoryWidget::onFieldFocused() { } void HistoryWidget::onCheckMentionDropdown() { - if (!_history || _a_show.animating()) return; + if (!_history || _a_show.animating() || _inlineBot) return; - UserData *bot = _inlineBot; bool start = false; - QString inlineBotUsername(_inlineBotUsername); - QString query = _field.getMentionHashtagBotCommandPart(start, _inlineBot, _inlineBotUsername); - if (inlineBotUsername != _inlineBotUsername) { - if (_inlineBotResolveRequestId) { - Notify::inlineBotRequesting(false); - MTP::cancel(_inlineBotResolveRequestId); - _inlineBotResolveRequestId = 0; - } - if (_inlineBot == InlineBotLookingUpData) { - Notify::inlineBotRequesting(true); - _inlineBotResolveRequestId = MTP::send(MTPcontacts_ResolveUsername(MTP_string(_inlineBotUsername)), rpcDone(&HistoryWidget::inlineBotResolveDone), rpcFail(&HistoryWidget::inlineBotResolveFail, _inlineBotUsername)); - return; - } - } else if (_inlineBot == InlineBotLookingUpData) { - return; - } - - if (_inlineBot) { - if (_inlineBot != bot) { - updateFieldPlaceholder(); - } - if (_inlineBot->username == (cTestMode() ? qstr("contextbot") : qstr("gif")) && query.isEmpty()) { - _emojiPan.clearInlineBot(); - } else { - _emojiPan.queryInlineBot(_inlineBot, query); - } - if (!_attachMention.isHidden()) { - _attachMention.hideStart(); - } - } else { - if (_inlineBot != bot) { - updateFieldPlaceholder(); - _field.finishPlaceholder(); - } - _emojiPan.clearInlineBot(); - if (!query.isEmpty()) { - if (query.at(0) == '#' && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) Local::readRecentHashtagsAndBots(); - if (query.at(0) == '@' && cRecentInlineBots().isEmpty()) Local::readRecentHashtagsAndBots(); - if (query.at(0) == '/' && _peer->isUser() && !_peer->asUser()->botInfo) return; - } - _attachMention.showFiltered(_peer, query, start); + QString query = _field.getMentionHashtagBotCommandPart(start); + if (!query.isEmpty()) { + if (query.at(0) == '#' && cRecentWriteHashtags().isEmpty() && cRecentSearchHashtags().isEmpty()) Local::readRecentHashtagsAndBots(); + if (query.at(0) == '@' && cRecentInlineBots().isEmpty()) Local::readRecentHashtagsAndBots(); + if (query.at(0) == '/' && _peer->isUser() && !_peer->asUser()->botInfo) return; } + _attachMention.showFiltered(_peer, query, start); } void HistoryWidget::updateFieldPlaceholder() { diff --git a/Telegram/SourceFiles/historywidget.h b/Telegram/SourceFiles/historywidget.h index 7c235162e..826c84556 100644 --- a/Telegram/SourceFiles/historywidget.h +++ b/Telegram/SourceFiles/historywidget.h @@ -451,6 +451,7 @@ public: void destroyData(); void updateFieldPlaceholder(); + void updateInlineBotQuery(); void uploadImage(const QImage &img, PrepareMediaType type, FileLoadForceConfirmType confirm = FileLoadNoForceConfirm, const QString &source = QString(), bool withText = false); void uploadFile(const QString &file, PrepareMediaType type, FileLoadForceConfirmType confirm = FileLoadNoForceConfirm, bool withText = false); // with confirmation diff --git a/Telegram/SourceFiles/structs.cpp b/Telegram/SourceFiles/structs.cpp index 1339c635c..34bd47fba 100644 --- a/Telegram/SourceFiles/structs.cpp +++ b/Telegram/SourceFiles/structs.cpp @@ -1364,7 +1364,11 @@ void DocumentOpenLink::doOpen(DocumentData *data, ActionOnLoad action) { } } else if (location.accessEnable()) { if ((App::hoveredLinkItem() || App::contextItem()) && (data->isAnimation() || QImageReader(location.name()).canRead())) { - App::wnd()->showDocument(data, item); + if (action == ActionOnLoadPlayInline) { + item->getMedia()->playInline(item); + } else { + App::wnd()->showDocument(data, item); + } } else { psOpenFile(location.name()); } @@ -1634,7 +1638,11 @@ void DocumentData::performActionOnLoad() { } else if (_actionOnLoad == ActionOnLoadOpen || _actionOnLoad == ActionOnLoadPlayInline) { if (loc.accessEnable()) { if (showImage && QImageReader(loc.name()).canRead()) { - App::wnd()->showDocument(this, item); + if (_actionOnLoad == ActionOnLoadPlayInline) { + item->getMedia()->playInline(item); + } else { + App::wnd()->showDocument(this, item); + } } else { psOpenFile(already); }