From aaefeed3f13a106527c0f9e75db3e1535efed662 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 5 Jul 2021 20:59:23 +0300 Subject: [PATCH] Support custom keyboard placeholders. --- .../SourceFiles/chat_helpers/bot_keyboard.cpp | 7 +++++++ Telegram/SourceFiles/chat_helpers/bot_keyboard.h | 15 ++++++++++----- .../history/history_item_components.cpp | 5 +++++ .../history/history_item_components.h | 1 + Telegram/SourceFiles/history/history_widget.cpp | 16 +++++++++++----- Telegram/SourceFiles/history/history_widget.h | 2 +- 6 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp b/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp index f8416d5ac6..4e5ec451ed 100644 --- a/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp +++ b/Telegram/SourceFiles/chat_helpers/bot_keyboard.cpp @@ -201,6 +201,7 @@ bool BotKeyboard::updateMarkup(HistoryItem *to, bool force) { if (_wasForMsgId.msg) { _maximizeSize = _singleUse = _forceReply = false; _wasForMsgId = FullMsgId(); + _placeholder = QString(); _impl = nullptr; return true; } @@ -218,6 +219,12 @@ bool BotKeyboard::updateMarkup(HistoryItem *to, bool force) { _maximizeSize = !(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_resize); _singleUse = _forceReply || (markupFlags & MTPDreplyKeyboardMarkup::Flag::f_single_use); + if (const auto markup = to->Get()) { + _placeholder = markup->placeholder; + } else { + _placeholder = QString(); + } + _impl = nullptr; if (auto markup = to->Get()) { if (!markup->rows.empty()) { diff --git a/Telegram/SourceFiles/chat_helpers/bot_keyboard.h b/Telegram/SourceFiles/chat_helpers/bot_keyboard.h index a87867b3b1..8a15d2c630 100644 --- a/Telegram/SourceFiles/chat_helpers/bot_keyboard.h +++ b/Telegram/SourceFiles/chat_helpers/bot_keyboard.h @@ -31,8 +31,12 @@ public: // With force=true the markup is updated even if it is // already shown for the passed history item. bool updateMarkup(HistoryItem *last, bool force = false); - bool hasMarkup() const; - bool forceReply() const; + [[nodiscard]] bool hasMarkup() const; + [[nodiscard]] bool forceReply() const; + + [[nodiscard]] QString placeholder() const { + return _placeholder; + } void step_selected(crl::time ms, bool timer); void resizeToWidth(int newWidth, int maxOuterHeight) { @@ -40,10 +44,10 @@ public: return TWidget::resizeToWidth(newWidth); } - bool maximizeSize() const; - bool singleUse() const; + [[nodiscard]] bool maximizeSize() const; + [[nodiscard]] bool singleUse() const; - FullMsgId forMsgId() const { + [[nodiscard]] FullMsgId forMsgId() const { return _wasForMsgId; } @@ -76,6 +80,7 @@ private: const not_null _session; FullMsgId _wasForMsgId; + QString _placeholder; int _height = 0; int _maxOuterHeight = 0; bool _maximizeSize = false; diff --git a/Telegram/SourceFiles/history/history_item_components.cpp b/Telegram/SourceFiles/history/history_item_components.cpp index f8502321f3..243edb69ff 100644 --- a/Telegram/SourceFiles/history/history_item_components.cpp +++ b/Telegram/SourceFiles/history/history_item_components.cpp @@ -934,6 +934,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) { case mtpc_replyKeyboardMarkup: { auto &d = markup.c_replyKeyboardMarkup(); flags = d.vflags().v; + placeholder = d.vplaceholder() ? qs(*d.vplaceholder()) : QString(); createFromButtonRows(d.vrows().v); } break; @@ -941,6 +942,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) { case mtpc_replyInlineMarkup: { auto &d = markup.c_replyInlineMarkup(); flags = MTPDreplyKeyboardMarkup::Flags(0) | MTPDreplyKeyboardMarkup_ClientFlag::f_inline; + placeholder = QString(); createFromButtonRows(d.vrows().v); } break; @@ -948,11 +950,13 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) { case mtpc_replyKeyboardHide: { auto &d = markup.c_replyKeyboardHide(); flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_zero; + placeholder = QString(); } break; case mtpc_replyKeyboardForceReply: { auto &d = markup.c_replyKeyboardForceReply(); flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_force_reply; + placeholder = d.vplaceholder() ? qs(*d.vplaceholder()) : QString(); } break; } } @@ -960,6 +964,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) { void HistoryMessageReplyMarkup::create( const HistoryMessageReplyMarkup &markup) { flags = markup.flags; + placeholder = markup.placeholder; inlineKeyboard = nullptr; rows.clear(); diff --git a/Telegram/SourceFiles/history/history_item_components.h b/Telegram/SourceFiles/history/history_item_components.h index b60a8d0512..d8076a1896 100644 --- a/Telegram/SourceFiles/history/history_item_components.h +++ b/Telegram/SourceFiles/history/history_item_components.h @@ -237,6 +237,7 @@ struct HistoryMessageReplyMarkup : public RuntimeComponent> rows; MTPDreplyKeyboardMarkup::Flags flags = 0; + QString placeholder; std::unique_ptr inlineKeyboard; diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index a2b7946a61..218950c59f 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -217,6 +217,9 @@ HistoryWidget::HistoryWidget( Ui::InputField::Mode::MultiLine, tr::lng_message_ph()) , _kbScroll(this, st::botKbScroll) +, _keyboard(_kbScroll->setOwnedWidget(object_ptr( + &session(), + this))) , _membersDropdownShowTimer([=] { showMembersDropdown(); }) , _scrollTimer([=] { scrollByTimer(); }) , _saveDraftTimer([=] { saveDraft(); }) @@ -300,10 +303,6 @@ HistoryWidget::HistoryWidget( _topBar->hide(); _scroll->hide(); - - _keyboard = _kbScroll->setOwnedWidget(object_ptr( - &session(), - this)); _kbScroll->hide(); updateScrollColors(); @@ -3980,6 +3979,7 @@ void HistoryWidget::toggleKeyboard(bool manual) { } } updateControlsGeometry(); + updateFieldPlaceholder(); if (_botKeyboardHide->isHidden() && canWriteMessage() && !_a_show.animating()) { _tabbedSelectorToggle->show(); } else { @@ -4261,6 +4261,9 @@ void HistoryWidget::updateFieldPlaceholder() { return tr::lng_edit_message_text(); } else if (!_history) { return tr::lng_message_ph(); + } else if ((_kbShown || _keyboard->forceReply()) + && !_keyboard->placeholder().isEmpty()) { + return rpl::single(_keyboard->placeholder()); } else if (const auto channel = _history->peer->asChannel()) { if (channel->isBroadcast()) { return session().data().notifySilentPosts(channel) @@ -4960,7 +4963,9 @@ void HistoryWidget::updateBotKeyboard(History *h, bool force) { changed = _keyboard->updateMarkup(keyboardItem, force); } updateCmdStartShown(); - if (!changed) return; + if (!changed) { + return; + } bool hasMarkup = _keyboard->hasMarkup(), forceReply = _keyboard->forceReply() && (!_replyToId || !_replyEditMsg); if (hasMarkup || forceReply) { @@ -5025,6 +5030,7 @@ void HistoryWidget::updateBotKeyboard(History *h, bool force) { } } refreshTopBarActiveChat(); + updateFieldPlaceholder(); updateControlsGeometry(); update(); } diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index a2d6991101..50a7f88c10 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -722,7 +722,7 @@ private: bool _kbShown = false; HistoryItem *_kbReplyTo = nullptr; object_ptr _kbScroll; - QPointer _keyboard; + const not_null _keyboard; object_ptr _membersDropdown = { nullptr }; base::Timer _membersDropdownShowTimer;