From bdce2d5e25835324b30d766a58ab4b6094dd23ff Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 16 Sep 2020 17:09:04 +0300 Subject: [PATCH] Added ability to mark as read chats from folder from context menu. Fixed #7507. Fixed #6004. --- Telegram/Resources/langs/lang.strings | 1 + .../window/window_filters_menu.cpp | 24 +++++++-- .../SourceFiles/window/window_main_menu.cpp | 42 +++++++++------ .../SourceFiles/window/window_peer_menu.cpp | 51 +++++++++++++++++++ .../SourceFiles/window/window_peer_menu.h | 10 ++++ 5 files changed, 109 insertions(+), 19 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index fb96812e06..cebdc4f086 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1457,6 +1457,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_context_unpin_from_top" = "Unpin from top"; "lng_context_mark_unread" = "Mark as unread"; "lng_context_mark_read" = "Mark as read"; +"lng_context_mark_read_sure" = "Are you sure you want to mark all chats from this folder as read?"; "lng_context_archive_expand" = "Expand"; "lng_context_archive_collapse" = "Collapse"; "lng_context_archive_to_menu" = "Move to main menu"; diff --git a/Telegram/SourceFiles/window/window_filters_menu.cpp b/Telegram/SourceFiles/window/window_filters_menu.cpp index d12ddc5101..bb9c31e704 100644 --- a/Telegram/SourceFiles/window/window_filters_menu.cpp +++ b/Telegram/SourceFiles/window/window_filters_menu.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "window/window_controller.h" #include "window/window_main_menu.h" +#include "window/window_peer_menu.h" #include "main/main_session.h" #include "data/data_session.h" #include "data/data_chat_filters.h" @@ -307,12 +308,27 @@ void FiltersMenu::showMenu(QPoint position, FilterId id) { return; } _popupMenu = base::make_unique_q(i->second.get()); - _popupMenu->addAction( + const auto addAction = [&](const QString &text, Fn callback) { + return _popupMenu->addAction( + text, + crl::guard(&_outer, std::move(callback))); + }; + + addAction( tr::lng_filters_context_edit(tr::now), - crl::guard(&_outer, [=] { showEditBox(id); })); - _popupMenu->addAction( + [=] { showEditBox(id); }); + + auto filteredChats = [=] { + return _session->session().data().chatsFilters().chatsList(id); + }; + Window::MenuAddMarkAsReadChatListAction( + &_session->session().data(), + std::move(filteredChats), + addAction); + + addAction( tr::lng_filters_context_remove(tr::now), - crl::guard(&_outer, [=] { showRemoveBox(id); })); + [=] { showRemoveBox(id); }); _popupMenu->popup(position); } diff --git a/Telegram/SourceFiles/window/window_main_menu.cpp b/Telegram/SourceFiles/window/window_main_menu.cpp index f8800de981..ee13f9d173 100644 --- a/Telegram/SourceFiles/window/window_main_menu.cpp +++ b/Telegram/SourceFiles/window/window_main_menu.cpp @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_main_menu.h" #include "window/themes/window_theme.h" +#include "window/window_peer_menu.h" #include "window/window_session_controller.h" #include "ui/widgets/buttons.h" #include "ui/widgets/labels.h" @@ -599,20 +600,21 @@ MainMenu::MainMenu( } void MainMenu::setupArchiveButton() { + const auto controller = _controller; + const auto folder = [=] { + return controller->session().data().folderLoaded(Data::Folder::kId); + }; const auto showArchive = [=] { - const auto folder = _controller->session().data().folderLoaded( - Data::Folder::kId); - if (folder) { - _controller->openFolder(folder); + if (const auto f = folder()) { + controller->openFolder(f); Ui::hideSettingsAndLayer(); } }; const auto checkArchive = [=] { - const auto folder = _controller->session().data().folderLoaded( - Data::Folder::kId); - return folder - && !folder->chatsList()->empty() - && _controller->session().settings().archiveInMainMenu(); + const auto f = folder(); + return f + && !f->chatsList()->empty() + && controller->session().settings().archiveInMainMenu(); }; _archiveButton->setVisible(checkArchive()); _archiveButton->setAcceptBoth(true); @@ -625,16 +627,26 @@ void MainMenu::setupArchiveButton() { return; } _contextMenu = base::make_unique_q(this); - _contextMenu->addAction( - tr::lng_context_archive_to_list(tr::now), [=] { - _controller->session().settings().setArchiveInMainMenu(false); - _controller->session().saveSettingsDelayed(); + const auto addAction = [&](const QString &text, Fn callback) { + return _contextMenu->addAction(text, std::move(callback)); + }; + + const auto hide = [=] { + controller->session().settings().setArchiveInMainMenu(false); + controller->session().saveSettingsDelayed(); Ui::hideSettingsAndLayer(); - }); + }; + addAction(tr::lng_context_archive_to_list(tr::now), std::move(hide)); + + MenuAddMarkAsReadChatListAction( + &controller->session().data(), + [f = folder()] { return f->chatsList(); }, + addAction); + _contextMenu->popup(QCursor::pos()); }, _archiveButton->lifetime()); - _controller->session().data().chatsListChanges( + controller->session().data().chatsListChanges( ) | rpl::filter([](Data::Folder *folder) { return folder && (folder->id() == Data::Folder::kId); }) | rpl::start_with_next([=](Data::Folder *folder) { diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp index 3df53dc55f..ad5f8d74a8 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.cpp +++ b/Telegram/SourceFiles/window/window_peer_menu.cpp @@ -65,6 +65,7 @@ namespace Window { namespace { constexpr auto kArchivedToastDuration = crl::time(5000); +constexpr auto kMaxUnreadWithoutConfirmation = 10000; class Filler { public: @@ -710,6 +711,11 @@ void FolderFiller::addTogglesForArchive() { !controller->session().settings().archiveInMainMenu()); controller->session().saveSettingsDelayed(); }); + + MenuAddMarkAsReadChatListAction( + &controller->session().data(), + [folder = _folder] { return folder->chatsList(); }, + _addAction); } // //void FolderFiller::addInfo() { @@ -1130,6 +1136,51 @@ void PeerMenuAddMuteAction( muteAction->setText(muteText(!enabled)); }, *lifetime); } + +void MenuAddMarkAsReadChatListAction( + not_null data, + Fn()> list, + const PeerMenuCallback &addAction) { + const auto owner = data; + const auto unreadState = list()->unreadState(); + if (unreadState.empty()) { + return; + } + + const auto read = [=](not_null history) { + if ((history->chatListUnreadCount() > 0) + || history->chatListUnreadMark()) { + owner->histories().readInbox(history); + } + }; + const auto markAsRead = [=] { + for (const auto row : list()->indexed()->all()) { + if (const auto history = row->history()) { + read(history); + if (const auto migrated = history->migrateSibling()) { + read(migrated); + } + } + } + }; + + auto callback = [=] { + if (unreadState.messages > kMaxUnreadWithoutConfirmation) { + auto boxCallback = [=](Fn &&close) { + markAsRead(); + close(); + }; + Ui::show(Box( + tr::lng_context_mark_read_sure(tr::now), + std::move(boxCallback))); + } else { + markAsRead(); + } + }; + addAction( + tr::lng_context_mark_read(tr::now), + std::move(callback)); +} // #feed //void PeerMenuUngroupFeed(not_null feed) { // Ui::show(Box( diff --git a/Telegram/SourceFiles/window/window_peer_menu.h b/Telegram/SourceFiles/window/window_peer_menu.h index f4b7a34152..87f601edcf 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.h +++ b/Telegram/SourceFiles/window/window_peer_menu.h @@ -19,8 +19,13 @@ class GenericBox; namespace Data { class Folder; +class Session; } // namespace Data +namespace Dialogs { +class MainList; +} // namespace Dialogs + namespace Window { class Controller; @@ -54,6 +59,11 @@ void PeerMenuAddMuteAction( not_null peer, const PeerMenuCallback &addAction); +void MenuAddMarkAsReadChatListAction( + not_null data, + Fn()> list, + const PeerMenuCallback &addAction); + void PeerMenuExportChat(not_null peer); void PeerMenuDeleteContact(not_null user); void PeerMenuShareContactBox(