Support custom keyboard placeholders.

This commit is contained in:
John Preston 2021-07-05 20:59:23 +03:00
parent 7f00065bd8
commit aaefeed3f1
6 changed files with 35 additions and 11 deletions

View File

@ -201,6 +201,7 @@ bool BotKeyboard::updateMarkup(HistoryItem *to, bool force) {
if (_wasForMsgId.msg) { if (_wasForMsgId.msg) {
_maximizeSize = _singleUse = _forceReply = false; _maximizeSize = _singleUse = _forceReply = false;
_wasForMsgId = FullMsgId(); _wasForMsgId = FullMsgId();
_placeholder = QString();
_impl = nullptr; _impl = nullptr;
return true; return true;
} }
@ -218,6 +219,12 @@ bool BotKeyboard::updateMarkup(HistoryItem *to, bool force) {
_maximizeSize = !(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_resize); _maximizeSize = !(markupFlags & MTPDreplyKeyboardMarkup::Flag::f_resize);
_singleUse = _forceReply || (markupFlags & MTPDreplyKeyboardMarkup::Flag::f_single_use); _singleUse = _forceReply || (markupFlags & MTPDreplyKeyboardMarkup::Flag::f_single_use);
if (const auto markup = to->Get<HistoryMessageReplyMarkup>()) {
_placeholder = markup->placeholder;
} else {
_placeholder = QString();
}
_impl = nullptr; _impl = nullptr;
if (auto markup = to->Get<HistoryMessageReplyMarkup>()) { if (auto markup = to->Get<HistoryMessageReplyMarkup>()) {
if (!markup->rows.empty()) { if (!markup->rows.empty()) {

View File

@ -31,8 +31,12 @@ public:
// With force=true the markup is updated even if it is // With force=true the markup is updated even if it is
// already shown for the passed history item. // already shown for the passed history item.
bool updateMarkup(HistoryItem *last, bool force = false); bool updateMarkup(HistoryItem *last, bool force = false);
bool hasMarkup() const; [[nodiscard]] bool hasMarkup() const;
bool forceReply() const; [[nodiscard]] bool forceReply() const;
[[nodiscard]] QString placeholder() const {
return _placeholder;
}
void step_selected(crl::time ms, bool timer); void step_selected(crl::time ms, bool timer);
void resizeToWidth(int newWidth, int maxOuterHeight) { void resizeToWidth(int newWidth, int maxOuterHeight) {
@ -40,10 +44,10 @@ public:
return TWidget::resizeToWidth(newWidth); return TWidget::resizeToWidth(newWidth);
} }
bool maximizeSize() const; [[nodiscard]] bool maximizeSize() const;
bool singleUse() const; [[nodiscard]] bool singleUse() const;
FullMsgId forMsgId() const { [[nodiscard]] FullMsgId forMsgId() const {
return _wasForMsgId; return _wasForMsgId;
} }
@ -76,6 +80,7 @@ private:
const not_null<Main::Session*> _session; const not_null<Main::Session*> _session;
FullMsgId _wasForMsgId; FullMsgId _wasForMsgId;
QString _placeholder;
int _height = 0; int _height = 0;
int _maxOuterHeight = 0; int _maxOuterHeight = 0;
bool _maximizeSize = false; bool _maximizeSize = false;

View File

@ -934,6 +934,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) {
case mtpc_replyKeyboardMarkup: { case mtpc_replyKeyboardMarkup: {
auto &d = markup.c_replyKeyboardMarkup(); auto &d = markup.c_replyKeyboardMarkup();
flags = d.vflags().v; flags = d.vflags().v;
placeholder = d.vplaceholder() ? qs(*d.vplaceholder()) : QString();
createFromButtonRows(d.vrows().v); createFromButtonRows(d.vrows().v);
} break; } break;
@ -941,6 +942,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) {
case mtpc_replyInlineMarkup: { case mtpc_replyInlineMarkup: {
auto &d = markup.c_replyInlineMarkup(); auto &d = markup.c_replyInlineMarkup();
flags = MTPDreplyKeyboardMarkup::Flags(0) | MTPDreplyKeyboardMarkup_ClientFlag::f_inline; flags = MTPDreplyKeyboardMarkup::Flags(0) | MTPDreplyKeyboardMarkup_ClientFlag::f_inline;
placeholder = QString();
createFromButtonRows(d.vrows().v); createFromButtonRows(d.vrows().v);
} break; } break;
@ -948,11 +950,13 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) {
case mtpc_replyKeyboardHide: { case mtpc_replyKeyboardHide: {
auto &d = markup.c_replyKeyboardHide(); auto &d = markup.c_replyKeyboardHide();
flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_zero; flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_zero;
placeholder = QString();
} break; } break;
case mtpc_replyKeyboardForceReply: { case mtpc_replyKeyboardForceReply: {
auto &d = markup.c_replyKeyboardForceReply(); auto &d = markup.c_replyKeyboardForceReply();
flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_force_reply; flags = mtpCastFlags(d.vflags()) | MTPDreplyKeyboardMarkup_ClientFlag::f_force_reply;
placeholder = d.vplaceholder() ? qs(*d.vplaceholder()) : QString();
} break; } break;
} }
} }
@ -960,6 +964,7 @@ void HistoryMessageReplyMarkup::create(const MTPReplyMarkup &markup) {
void HistoryMessageReplyMarkup::create( void HistoryMessageReplyMarkup::create(
const HistoryMessageReplyMarkup &markup) { const HistoryMessageReplyMarkup &markup) {
flags = markup.flags; flags = markup.flags;
placeholder = markup.placeholder;
inlineKeyboard = nullptr; inlineKeyboard = nullptr;
rows.clear(); rows.clear();

View File

@ -237,6 +237,7 @@ struct HistoryMessageReplyMarkup : public RuntimeComponent<HistoryMessageReplyMa
std::vector<std::vector<Button>> rows; std::vector<std::vector<Button>> rows;
MTPDreplyKeyboardMarkup::Flags flags = 0; MTPDreplyKeyboardMarkup::Flags flags = 0;
QString placeholder;
std::unique_ptr<ReplyKeyboard> inlineKeyboard; std::unique_ptr<ReplyKeyboard> inlineKeyboard;

View File

@ -217,6 +217,9 @@ HistoryWidget::HistoryWidget(
Ui::InputField::Mode::MultiLine, Ui::InputField::Mode::MultiLine,
tr::lng_message_ph()) tr::lng_message_ph())
, _kbScroll(this, st::botKbScroll) , _kbScroll(this, st::botKbScroll)
, _keyboard(_kbScroll->setOwnedWidget(object_ptr<BotKeyboard>(
&session(),
this)))
, _membersDropdownShowTimer([=] { showMembersDropdown(); }) , _membersDropdownShowTimer([=] { showMembersDropdown(); })
, _scrollTimer([=] { scrollByTimer(); }) , _scrollTimer([=] { scrollByTimer(); })
, _saveDraftTimer([=] { saveDraft(); }) , _saveDraftTimer([=] { saveDraft(); })
@ -300,10 +303,6 @@ HistoryWidget::HistoryWidget(
_topBar->hide(); _topBar->hide();
_scroll->hide(); _scroll->hide();
_keyboard = _kbScroll->setOwnedWidget(object_ptr<BotKeyboard>(
&session(),
this));
_kbScroll->hide(); _kbScroll->hide();
updateScrollColors(); updateScrollColors();
@ -3980,6 +3979,7 @@ void HistoryWidget::toggleKeyboard(bool manual) {
} }
} }
updateControlsGeometry(); updateControlsGeometry();
updateFieldPlaceholder();
if (_botKeyboardHide->isHidden() && canWriteMessage() && !_a_show.animating()) { if (_botKeyboardHide->isHidden() && canWriteMessage() && !_a_show.animating()) {
_tabbedSelectorToggle->show(); _tabbedSelectorToggle->show();
} else { } else {
@ -4261,6 +4261,9 @@ void HistoryWidget::updateFieldPlaceholder() {
return tr::lng_edit_message_text(); return tr::lng_edit_message_text();
} else if (!_history) { } else if (!_history) {
return tr::lng_message_ph(); 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()) { } else if (const auto channel = _history->peer->asChannel()) {
if (channel->isBroadcast()) { if (channel->isBroadcast()) {
return session().data().notifySilentPosts(channel) return session().data().notifySilentPosts(channel)
@ -4960,7 +4963,9 @@ void HistoryWidget::updateBotKeyboard(History *h, bool force) {
changed = _keyboard->updateMarkup(keyboardItem, force); changed = _keyboard->updateMarkup(keyboardItem, force);
} }
updateCmdStartShown(); updateCmdStartShown();
if (!changed) return; if (!changed) {
return;
}
bool hasMarkup = _keyboard->hasMarkup(), forceReply = _keyboard->forceReply() && (!_replyToId || !_replyEditMsg); bool hasMarkup = _keyboard->hasMarkup(), forceReply = _keyboard->forceReply() && (!_replyToId || !_replyEditMsg);
if (hasMarkup || forceReply) { if (hasMarkup || forceReply) {
@ -5025,6 +5030,7 @@ void HistoryWidget::updateBotKeyboard(History *h, bool force) {
} }
} }
refreshTopBarActiveChat(); refreshTopBarActiveChat();
updateFieldPlaceholder();
updateControlsGeometry(); updateControlsGeometry();
update(); update();
} }

View File

@ -722,7 +722,7 @@ private:
bool _kbShown = false; bool _kbShown = false;
HistoryItem *_kbReplyTo = nullptr; HistoryItem *_kbReplyTo = nullptr;
object_ptr<Ui::ScrollArea> _kbScroll; object_ptr<Ui::ScrollArea> _kbScroll;
QPointer<BotKeyboard> _keyboard; const not_null<BotKeyboard*> _keyboard;
object_ptr<Ui::InnerDropdown> _membersDropdown = { nullptr }; object_ptr<Ui::InnerDropdown> _membersDropdown = { nullptr };
base::Timer _membersDropdownShowTimer; base::Timer _membersDropdownShowTimer;