From 67d8a7e106a35db586b2e6fb909b5e7109067e72 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 31 Mar 2022 13:37:17 +0300 Subject: [PATCH] Moved out peer menu callback and its creation to separated files. --- .../dialogs/dialogs_inner_widget.cpp | 18 +-------- .../view/history_view_top_bar_widget.cpp | 28 ++++---------- .../view/history_view_top_bar_widget.h | 2 +- .../SourceFiles/info/info_wrap_widget.cpp | 19 +--------- .../SourceFiles/menu/add_action_callback.cpp | 27 +++++++++++++ .../SourceFiles/menu/add_action_callback.h | 38 +++++++++++++++++++ .../menu/add_action_callback_factory.cpp | 34 +++++++++++++++++ .../menu/add_action_callback_factory.h | 23 +++++++++++ .../SourceFiles/window/window_peer_menu.h | 29 +------------- Telegram/cmake/td_ui.cmake | 4 ++ 10 files changed, 140 insertions(+), 82 deletions(-) create mode 100644 Telegram/SourceFiles/menu/add_action_callback.cpp create mode 100644 Telegram/SourceFiles/menu/add_action_callback.h create mode 100644 Telegram/SourceFiles/menu/add_action_callback_factory.cpp create mode 100644 Telegram/SourceFiles/menu/add_action_callback_factory.h diff --git a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp index c1fdab017..692844743 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_inner_widget.cpp @@ -37,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "mainwindow.h" #include "mainwidget.h" +#include "menu/add_action_callback_factory.h" #include "storage/storage_account.h" #include "apiwrap.h" #include "main/main_session.h" @@ -1771,22 +1772,7 @@ void InnerWidget::contextMenuEvent(QContextMenuEvent *e) { fillArchiveSearchMenu(_menu.get()); } } else { - const auto addAction = Window::PeerMenuCallback([&]( - Window::PeerMenuCallback::Args &&a) { - if (a.fillSubmenu) { - const auto action = _menu->addAction( - a.text, - std::move(a.handler), - a.icon); - // Dummy menu. - action->setMenu(Ui::CreateChild(_menu->menu().get())); - a.fillSubmenu(_menu->ensureSubmenu(action)); - return action; - } else if (a.isSeparator) { - return _menu->addSeparator(); - } - return _menu->addAction(a.text, std::move(a.handler), a.icon); - }); + const auto addAction = Menu::CreateAddActionCallback(_menu); Window::FillDialogsEntryMenu( _controller, Dialogs::EntryState{ diff --git a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp index 4cc81207e..3c592b0b7 100644 --- a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mainwidget.h" #include "mainwindow.h" #include "main/main_session.h" +#include "menu/add_action_callback_factory.h" #include "mtproto/mtproto_config.h" #include "lang/lang_keys.h" #include "core/shortcuts.h" @@ -283,14 +284,14 @@ bool TopBarWidget::createMenu(not_null button) { if (!_activeChat.key || _menu) { return false; } - _menu.create(this, st::popupMenuExpandedSeparator); + _menu = base::make_unique_q( + this, + st::popupMenuExpandedSeparator); _menu->setDestroyedCallback([ weak = Ui::MakeWeak(this), weakButton = Ui::MakeWeak(button), - menu = _menu.data()] { - menu->deleteLater(); + menu = _menu.get()] { if (weak && weak->_menu == menu) { - weak->_menu = nullptr; if (weakButton) { weakButton->setForceRippled(false); } @@ -305,25 +306,10 @@ void TopBarWidget::showPeerMenu() { if (!created) { return; } - const auto addAction = Window::PeerMenuCallback([&]( - Window::PeerMenuCallback::Args a) { - if (a.fillSubmenu) { - const auto action = _menu->addAction( - a.text, - std::move(a.handler), - a.icon); - // Dummy menu. - action->setMenu(Ui::CreateChild(_menu->menu().get())); - a.fillSubmenu(_menu->ensureSubmenu(action)); - return action; - } else if (a.isSeparator) { - return _menu->addSeparator(); - } - return _menu->addAction(a.text, std::move(a.handler), a.icon); - }); + const auto addAction = Menu::CreateAddActionCallback(_menu); Window::FillDialogsEntryMenu(_controller, _activeChat, addAction); if (_menu->empty()) { - _menu.destroy(); + _menu = nullptr; } else { _menu->setForcedOrigin(Ui::PanelAnimation::Origin::TopRight); _menu->popup(mapToGlobal(QPoint( diff --git a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h index adcabdce9..1e825b970 100644 --- a/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_top_bar_widget.h @@ -179,7 +179,7 @@ private: object_ptr _search; object_ptr _infoToggle; object_ptr _menuToggle; - object_ptr _menu = { nullptr }; + base::unique_qptr _menu; object_ptr _membersShowArea = { nullptr }; rpl::event_stream _membersShowAreaActive; diff --git a/Telegram/SourceFiles/info/info_wrap_widget.cpp b/Telegram/SourceFiles/info/info_wrap_widget.cpp index f38ff4f00..f2cb903bf 100644 --- a/Telegram/SourceFiles/info/info_wrap_widget.cpp +++ b/Telegram/SourceFiles/info/info_wrap_widget.cpp @@ -31,6 +31,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peer_list_box.h" #include "ui/boxes/confirm_box.h" #include "main/main_session.h" +#include "menu/add_action_callback_factory.h" #include "mtproto/mtproto_config.h" #include "data/data_download_manager.h" #include "data/data_session.h" @@ -505,23 +506,7 @@ void WrapWidget::showTopBarMenu() { }); _topBarMenuToggle->setForceRippled(true); - const auto addAction = Window::PeerMenuCallback([=]( - Window::PeerMenuCallback::Args a) { - if (a.isSeparator) { - return _topBarMenu->addSeparator(); - } else if (a.fillSubmenu) { - const auto action = _topBarMenu->addAction( - a.text, - std::move(a.handler), - a.icon); - // Dummy menu. - action->setMenu( - Ui::CreateChild(_topBarMenu->menu().get())); - a.fillSubmenu(_topBarMenu->ensureSubmenu(action)); - return action; - } - return _topBarMenu->addAction(a.text, std::move(a.handler), a.icon); - }); + const auto addAction = Menu::CreateAddActionCallback(_topBarMenu); if (key().isDownloads()) { addAction( tr::lng_context_delete_all_files(tr::now), diff --git a/Telegram/SourceFiles/menu/add_action_callback.cpp b/Telegram/SourceFiles/menu/add_action_callback.cpp new file mode 100644 index 000000000..1f36c84f1 --- /dev/null +++ b/Telegram/SourceFiles/menu/add_action_callback.cpp @@ -0,0 +1,27 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "menu/add_action_callback.h" + +namespace Menu { + +MenuCallback::MenuCallback(MenuCallback::Callback callback) +: _callback(std::move(callback)) { +} + +QAction *MenuCallback::operator()(Args &&args) const { + return _callback(std::move(args)); +} + +QAction *MenuCallback::operator()( + const QString &text, + Fn handler, + const style::icon *icon) const { + return _callback({ text, std::move(handler), icon, nullptr }); +} + +} // namespace Menu diff --git a/Telegram/SourceFiles/menu/add_action_callback.h b/Telegram/SourceFiles/menu/add_action_callback.h new file mode 100644 index 000000000..4e9dd190a --- /dev/null +++ b/Telegram/SourceFiles/menu/add_action_callback.h @@ -0,0 +1,38 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Ui { +class PopupMenu; +} // namespace Ui + +namespace Menu { + +struct MenuCallback final { +public: + struct Args { + QString text; + Fn handler; + const style::icon *icon; + Fn)> fillSubmenu; + bool isSeparator = false; + }; + using Callback = Fn; + + explicit MenuCallback(Callback callback); + + QAction *operator()(Args &&args) const; + QAction *operator()( + const QString &text, + Fn handler, + const style::icon *icon) const; +private: + Callback _callback; +}; + +} // namespace Menu diff --git a/Telegram/SourceFiles/menu/add_action_callback_factory.cpp b/Telegram/SourceFiles/menu/add_action_callback_factory.cpp new file mode 100644 index 000000000..2146a225b --- /dev/null +++ b/Telegram/SourceFiles/menu/add_action_callback_factory.cpp @@ -0,0 +1,34 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "menu/add_action_callback_factory.h" + +#include "menu/add_action_callback.h" +#include "ui/widgets/popup_menu.h" + +namespace Menu { + +MenuCallback CreateAddActionCallback( + const base::unique_qptr &menu) { + return MenuCallback([&](MenuCallback::Args a) { + if (a.fillSubmenu) { + const auto action = menu->addAction( + a.text, + std::move(a.handler), + a.icon); + // Dummy menu. + action->setMenu(Ui::CreateChild(menu->menu().get())); + a.fillSubmenu(menu->ensureSubmenu(action)); + return action; + } else if (a.isSeparator) { + return menu->addSeparator(); + } + return menu->addAction(a.text, std::move(a.handler), a.icon); + }); +} + +} // namespace Menu diff --git a/Telegram/SourceFiles/menu/add_action_callback_factory.h b/Telegram/SourceFiles/menu/add_action_callback_factory.h new file mode 100644 index 000000000..dae8c338c --- /dev/null +++ b/Telegram/SourceFiles/menu/add_action_callback_factory.h @@ -0,0 +1,23 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +#include "base/unique_qptr.h" + +namespace Ui { +class PopupMenu; +} // namespace Ui + +namespace Menu { + +struct MenuCallback; + +[[nodiscard]] MenuCallback CreateAddActionCallback( + const base::unique_qptr &menu); + +} // namespace Menu diff --git a/Telegram/SourceFiles/window/window_peer_menu.h b/Telegram/SourceFiles/window/window_peer_menu.h index a4032358e..f84f377a4 100644 --- a/Telegram/SourceFiles/window/window_peer_menu.h +++ b/Telegram/SourceFiles/window/window_peer_menu.h @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "api/api_common.h" #include "menu/menu_send.h" #include "data/data_poll.h" +#include "menu/add_action_callback.h" class History; @@ -38,33 +39,7 @@ class SessionNavigation; extern const char kOptionViewProfileInChatsListContextMenu[]; -struct PeerMenuCallback { -public: - struct Args { - QString text; - Fn handler; - const style::icon *icon; - Fn)> fillSubmenu; - bool isSeparator = false; - }; - using Callback = Fn; - - explicit PeerMenuCallback(Callback callback) - : callback(std::move(callback)) { - } - - QAction *operator()(Args &&args) const { - return callback(std::move(args)); - } - QAction *operator()( - const QString &text, - Fn handler, - const style::icon *icon) const { - return callback({ text, std::move(handler), icon, nullptr }); - } -private: - Callback callback; -}; +using PeerMenuCallback = Menu::MenuCallback; void FillDialogsEntryMenu( not_null controller, diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index d1e9fb658..a6ab0269a 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -102,6 +102,10 @@ PRIVATE media/player/media_player_dropdown.cpp media/player/media_player_dropdown.h + menu/add_action_callback.cpp + menu/add_action_callback.h + menu/add_action_callback_factory.cpp + menu/add_action_callback_factory.h menu/menu_check_item.cpp menu/menu_check_item.h menu/menu_ttl.cpp