Added bot button to bar of pinned messages.

This commit is contained in:
23rd 2022-04-22 21:45:54 +03:00 committed by John Preston
parent 1710890886
commit 31cd841b75
5 changed files with 108 additions and 18 deletions

View file

@ -6389,23 +6389,27 @@ void HistoryWidget::checkPinnedBarState() {
&session(),
_pinnedTracker->shownMessageId());
_pinnedBar = std::make_unique<Ui::PinnedBar>(this);
_pinnedBar->setContent(std::move(barContent));
Info::Profile::SharedMediaCountValue(
_peer,
nullptr,
Storage::SharedMediaType::Pinned
) | rpl::distinct_until_changed(
) | rpl::map([=](int count) {
if (_pinnedClickedId) {
_pinnedClickedId = FullMsgId();
_minPinnedId = std::nullopt;
updatePinnedViewer();
}
return (count > 1);
}) | rpl::distinct_until_changed(
) | rpl::start_with_next([=](bool many) {
refreshPinnedBarButton(many);
rpl::combine(
Info::Profile::SharedMediaCountValue(
_peer,
nullptr,
Storage::SharedMediaType::Pinned
) | rpl::distinct_until_changed(
) | rpl::map([=](int count) {
if (_pinnedClickedId) {
_pinnedClickedId = FullMsgId();
_minPinnedId = std::nullopt;
updatePinnedViewer();
}
return (count > 1);
}) | rpl::distinct_until_changed(),
HistoryView::PinnedBarItemWithReplyMarkup(
&session(),
_pinnedTracker->shownMessageId())
) | rpl::start_with_next([=](bool many, HistoryItem *item) {
refreshPinnedBarButton(many, item);
}, _pinnedBar->lifetime());
_pinnedBar->setContent(std::move(barContent));
controller()->adaptive().oneColumnValue(
) | rpl::start_with_next([=](bool one) {
@ -6492,7 +6496,30 @@ void HistoryWidget::setChooseReportMessagesDetails(
}
}
void HistoryWidget::refreshPinnedBarButton(bool many) {
void HistoryWidget::refreshPinnedBarButton(bool many, HistoryItem *item) {
if (const auto replyMarkup = item ? item->inlineReplyMarkup() : nullptr) {
const auto &rows = replyMarkup->data.rows;
if ((rows.size() == 1) && (rows.front().size() == 1)) {
const auto text = rows.front().front().text;
if (!text.isEmpty()) {
auto button = object_ptr<Ui::RoundButton>(
this,
rpl::single(text),
st::historyPinnedBotButton);
button->setTextTransform(
Ui::RoundButton::TextTransform::NoTransform);
button->setFullRadius(true);
button->setClickedCallback([=] {
App::activateBotCommand(controller(), item, 0, 0);
});
if (button->width() > st::historyPinnedBotButtonMaxWidth) {
button->setFullWidth(st::historyPinnedBotButtonMaxWidth);
}
_pinnedBar->setRightButton(std::move(button));
return;
}
}
}
const auto close = !many;
auto button = object_ptr<Ui::IconButton>(
this,

View file

@ -512,7 +512,7 @@ private:
void updatePinnedViewer();
void setupPinnedTracker();
void checkPinnedBarState();
void refreshPinnedBarButton(bool many);
void refreshPinnedBarButton(bool many, HistoryItem *item);
void checkLastPinnedClickedIdReset(
int wasScrollTop,
int nowScrollTop);

View file

@ -154,4 +154,56 @@ rpl::producer<Ui::MessageBarContent> PinnedBarContent(
}) | rpl::flatten_latest();
}
rpl::producer<HistoryItem*> PinnedBarItemWithReplyMarkup(
not_null<Main::Session*> session,
rpl::producer<PinnedId> id) {
return rpl::make_producer<HistoryItem*>([=,
id = std::move(id)](auto consumer) {
auto lifetime = rpl::lifetime();
consumer.put_next(nullptr);
struct State {
HistoryMessageReplyMarkup *previousReplyMarkup = nullptr;
rpl::lifetime lifetime;
};
const auto state = lifetime.make_state<State>();
const auto pushUnique = [=](not_null<HistoryItem*> item) {
const auto replyMarkup = item->inlineReplyMarkup();
if (state->previousReplyMarkup == replyMarkup) {
return;
}
consumer.put_next(item.get());
state->previousReplyMarkup = replyMarkup;
};
rpl::duplicate(
id
) | rpl::start_with_next([=](PinnedId current) {
const auto fullId = current.message;
if (!fullId) {
return;
}
const auto messageFlag = [=](not_null<HistoryItem*> item) {
using Update = Data::MessageUpdate;
session->changes().messageFlagsValue(
item,
Update::Flag::ReplyMarkup
) | rpl::start_with_next([=](const Update &update) {
pushUnique(update.item);
}, state->lifetime);
};
if (const auto item = session->data().message(fullId)) {
messageFlag(item);
} else {
session->api().requestMessageData(
session->data().peer(fullId.peer),
fullId.msg,
[=] { messageFlag(session->data().message(fullId)); });
}
}, lifetime);
return lifetime;
});
}
} // namespace HistoryView

View file

@ -49,4 +49,8 @@ struct PinnedId {
not_null<Main::Session*> session,
rpl::producer<PinnedId> id);
[[nodiscard]] rpl::producer<HistoryItem*> PinnedBarItemWithReplyMarkup(
not_null<Main::Session*> session,
rpl::producer<PinnedId> id);
} // namespace HistoryView

View file

@ -477,6 +477,13 @@ historyPinnedShowAll: IconButton(historyReplyCancel) {
icon: icon {{ "pinned_show_all", historyReplyCancelFg }};
iconOver: icon {{ "pinned_show_all", historyReplyCancelFgOver }};
}
historyPinnedBotButton: RoundButton(defaultActiveButton) {
width: -34px;
height: 30px;
textTop: 6px;
padding: margins(2px, 10px, 10px, 9px);
}
historyPinnedBotButtonMaxWidth: 150px;
msgBotKbDuration: 200;
msgBotKbFont: semiboldFont;