Refactored handle of last editable message on Up arrow in sections.

This commit is contained in:
23rd 2021-01-31 07:22:46 +03:00 committed by John Preston
parent b13e5ddce9
commit 062c451c27
10 changed files with 41 additions and 83 deletions

View file

@ -7,7 +7,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/data_replies_list.h"
#include "base/unixtime.h"
#include "history/history.h"
#include "history/history_item.h"
#include "history/history_service.h"
@ -627,22 +626,4 @@ bool RepliesList::processMessagesIsEmpty(const MTPmessages_Messages &result) {
return (list.size() == skipped);
}
HistoryItem *RepliesList::lastEditableMessage() {
const auto message = [&](MsgId msgId) {
return _history->owner().message(_history->channelId(), msgId);
};
const auto now = base::unixtime::now();
auto proj = [&](MsgId msgId) {
if (const auto item = message(msgId)) {
return item->allowsEdit(now);
}
return false;
};
const auto it = ranges::find_if(_list, std::move(proj));
return (it == end(_list))
? nullptr
: _history->owner().groups().findItemToEdit(message(*it)).get();
}
} // namespace Data

View file

@ -31,8 +31,6 @@ public:
[[nodiscard]] rpl::producer<int> fullCount() const;
[[nodiscard]] HistoryItem *lastEditableMessage();
private:
struct Viewer;

View file

@ -546,26 +546,4 @@ int32 ScheduledMessages::countListHash(const List &list) const {
return HashFinalize(hash);
}
HistoryItem *ScheduledMessages::lastEditableMessage(
not_null<History*> history) {
const auto i = _data.find(history);
if (i == end(_data)) {
return nullptr;
}
auto &list = i->second;
sort(list);
const auto items = ranges::view::reverse(list.items);
const auto now = base::unixtime::now();
auto proj = [&](const OwnedItem &item) {
return item->allowsEdit(now);
};
const auto it = ranges::find_if(items, std::move(proj));
return (it == end(items))
? nullptr
: history->owner().groups().findItemToEdit((*it).get()).get();
}
} // namespace Data

View file

@ -32,8 +32,6 @@ public:
[[nodiscard]] HistoryItem *lookupItem(PeerId peer, MsgId msg) const;
[[nodiscard]] HistoryItem *lookupItem(FullMsgId itemId) const;
[[nodiscard]] int count(not_null<History*> history) const;
[[nodiscard]] HistoryItem *lastEditableMessage(
not_null<History*> history);
void checkEntitiesAndUpdate(const MTPDmessage &data);
void apply(const MTPDupdateNewScheduledMessage &update);

View file

@ -712,20 +712,16 @@ rpl::producer<> ComposeControls::cancelRequests() const {
return _cancelRequests.events();
}
rpl::producer<not_null<QKeyEvent*>> ComposeControls::keyEvents() const {
return _wrap->events(
) | rpl::map([=](not_null<QEvent*> e) -> not_null<QKeyEvent*> {
return static_cast<QKeyEvent*>(e.get());
}) | rpl::filter([=](not_null<QEvent*> event) {
return (event->type() == QEvent::KeyPress);
});
}
auto ComposeControls::scrollKeyEvents() const
-> rpl::producer<not_null<QKeyEvent*>> {
return _scrollKeyEvents.events();
}
auto ComposeControls::editLastMessageRequests() const
-> rpl::producer<not_null<QKeyEvent*>> {
return _editLastMessageRequests.events();
}
auto ComposeControls::sendContentRequests(SendRequestType requestType) const {
auto filter = rpl::filter([=] {
const auto type = (_mode == Mode::Normal)
@ -1061,6 +1057,12 @@ void ComposeControls::initKeyHandler() {
}) | rpl::start_with_next([=](not_null<QEvent*> e) {
auto keyEvent = static_cast<QKeyEvent*>(e.get());
const auto key = keyEvent->key();
if (key == Qt::Key_Up) {
if (!isEditingMessage()) {
_editLastMessageRequests.fire(std::move(keyEvent));
return;
}
}
if ((key == Qt::Key_Up)
|| (key == Qt::Key_Down)
|| (key == Qt::Key_PageUp)

View file

@ -118,12 +118,13 @@ public:
[[nodiscard]] rpl::producer<FileChosen> fileChosen() const;
[[nodiscard]] rpl::producer<PhotoChosen> photoChosen() const;
[[nodiscard]] rpl::producer<Data::MessagePosition> scrollRequests() const;
[[nodiscard]] rpl::producer<not_null<QKeyEvent*>> keyEvents() const;
[[nodiscard]] rpl::producer<InlineChosen> inlineResultChosen() const;
[[nodiscard]] rpl::producer<SendActionUpdate> sendActionUpdates() const;
[[nodiscard]] rpl::producer<not_null<QEvent*>> viewportEvents() const;
[[nodiscard]] auto scrollKeyEvents() const
-> rpl::producer<not_null<QKeyEvent*>>;
[[nodiscard]] auto editLastMessageRequests() const
-> rpl::producer<not_null<QKeyEvent*>>;
using MimeDataHook = Fn<bool(
not_null<const QMimeData*> data,
@ -294,6 +295,7 @@ private:
rpl::event_stream<SendActionUpdate> _sendActionUpdates;
rpl::event_stream<QString> _sendCommandRequests;
rpl::event_stream<not_null<QKeyEvent*>> _scrollKeyEvents;
rpl::event_stream<not_null<QKeyEvent*>> _editLastMessageRequests;
TextUpdateEvents _textUpdateEvents = TextUpdateEvents()
| TextUpdateEvent::SaveDraft

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "history/view/history_view_list_widget.h"
#include "base/unixtime.h"
#include "history/history_message.h"
#include "history/history_item_components.h"
#include "history/history_item_text.h"
@ -2725,10 +2726,27 @@ rpl::producer<FullMsgId> ListWidget::editMessageRequested() const {
return _requestedToEditMessage.events();
}
void ListWidget::editMessageRequestNotify(FullMsgId item) {
void ListWidget::editMessageRequestNotify(FullMsgId item) const {
_requestedToEditMessage.fire(std::move(item));
}
bool ListWidget::lastMessageEditRequestNotify() const {
const auto now = base::unixtime::now();
auto proj = [&](not_null<Element*> view) {
return view->data()->allowsEdit(now);
};
const auto &list = ranges::view::reverse(_items);
const auto it = ranges::find_if(list, std::move(proj));
if (it == end(list)) {
return false;
} else {
const auto item =
session().data().groups().findItemToEdit((*it)->data()).get();
editMessageRequestNotify(item->fullId());
return true;
}
}
rpl::producer<FullMsgId> ListWidget::replyToMessageRequested() const {
return _requestedToReplyToMessage.events();
}

View file

@ -206,7 +206,8 @@ public:
bool tooltipWindowActive() const override;
[[nodiscard]] rpl::producer<FullMsgId> editMessageRequested() const;
void editMessageRequestNotify(FullMsgId item);
void editMessageRequestNotify(FullMsgId item) const;
[[nodiscard]] bool lastMessageEditRequestNotify() const;
[[nodiscard]] rpl::producer<FullMsgId> replyToMessageRequested() const;
void replyToMessageRequestNotify(FullMsgId item);
[[nodiscard]] rpl::producer<FullMsgId> readMessageRequested() const;

View file

@ -493,19 +493,10 @@ void RepliesWidget::setupComposeControls() {
_scroll->keyPressEvent(e);
}, lifetime());
_composeControls->keyEvents(
_composeControls->editLastMessageRequests(
) | rpl::start_with_next([=](not_null<QKeyEvent*> e) {
if (e->key() == Qt::Key_Up) {
if (!_composeControls->isEditingMessage()) {
if (const auto item = _replies->lastEditableMessage()) {
_inner->editMessageRequestNotify(item->fullId());
} else {
_scroll->keyPressEvent(e);
}
} else {
_scroll->keyPressEvent(e);
}
e->accept();
if (!_inner->lastMessageEditRequestNotify()) {
_scroll->keyPressEvent(e);
}
}, lifetime());

View file

@ -236,21 +236,10 @@ void ScheduledWidget::setupComposeControls() {
_scroll->keyPressEvent(e);
}, lifetime());
_composeControls->keyEvents(
_composeControls->editLastMessageRequests(
) | rpl::start_with_next([=](not_null<QKeyEvent*> e) {
if (e->key() == Qt::Key_Up) {
if (!_composeControls->isEditingMessage()) {
const auto item = session().data().scheduledMessages()
.lastEditableMessage(_history);
if (item) {
_inner->editMessageRequestNotify(item->fullId());
} else {
_scroll->keyPressEvent(e);
}
} else {
_scroll->keyPressEvent(e);
}
e->accept();
if (!_inner->lastMessageEditRequestNotify()) {
_scroll->keyPressEvent(e);
}
}, lifetime());