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) {
_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<HistoryMessageReplyMarkup>()) {
_placeholder = markup->placeholder;
} else {
_placeholder = QString();
}
_impl = nullptr;
if (auto markup = to->Get<HistoryMessageReplyMarkup>()) {
if (!markup->rows.empty()) {

View File

@ -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<Main::Session*> _session;
FullMsgId _wasForMsgId;
QString _placeholder;
int _height = 0;
int _maxOuterHeight = 0;
bool _maximizeSize = false;

View File

@ -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();

View File

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

View File

@ -217,6 +217,9 @@ HistoryWidget::HistoryWidget(
Ui::InputField::Mode::MultiLine,
tr::lng_message_ph())
, _kbScroll(this, st::botKbScroll)
, _keyboard(_kbScroll->setOwnedWidget(object_ptr<BotKeyboard>(
&session(),
this)))
, _membersDropdownShowTimer([=] { showMembersDropdown(); })
, _scrollTimer([=] { scrollByTimer(); })
, _saveDraftTimer([=] { saveDraft(); })
@ -300,10 +303,6 @@ HistoryWidget::HistoryWidget(
_topBar->hide();
_scroll->hide();
_keyboard = _kbScroll->setOwnedWidget(object_ptr<BotKeyboard>(
&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();
}

View File

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