From edceed28d71e9f1c2d011587e7ec95bda2a3d554 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 9 Aug 2020 14:28:39 +0300 Subject: [PATCH] Moved code for send context menu to separate file. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/boxes/create_poll_box.cpp | 1 + Telegram/SourceFiles/boxes/send_files_box.cpp | 1 + Telegram/SourceFiles/boxes/share_box.cpp | 1 + .../chat_helpers/gifs_list_widget.cpp | 2 +- .../chat_helpers/message_field.cpp | 94 --------------- .../SourceFiles/chat_helpers/message_field.h | 20 ---- .../chat_helpers/send_context_menu.cpp | 111 ++++++++++++++++++ .../chat_helpers/send_context_menu.h | 39 ++++++ .../chat_helpers/stickers_list_widget.cpp | 2 +- .../chat_helpers/tabbed_selector.cpp | 2 +- .../SourceFiles/history/history_widget.cpp | 1 + .../view/history_view_context_menu.cpp | 2 +- .../view/history_view_schedule_box.cpp | 2 +- .../view/history_view_scheduled_section.cpp | 2 +- .../inline_bots/inline_results_widget.cpp | 2 +- 16 files changed, 163 insertions(+), 121 deletions(-) create mode 100644 Telegram/SourceFiles/chat_helpers/send_context_menu.cpp create mode 100644 Telegram/SourceFiles/chat_helpers/send_context_menu.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index f0bafb1b1..a26881c4d 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -340,6 +340,8 @@ PRIVATE chat_helpers/gifs_list_widget.h chat_helpers/message_field.cpp chat_helpers/message_field.h + chat_helpers/send_context_menu.cpp + chat_helpers/send_context_menu.h chat_helpers/spellchecker_common.cpp chat_helpers/spellchecker_common.h chat_helpers/stickers_emoji_image_loader.cpp diff --git a/Telegram/SourceFiles/boxes/create_poll_box.cpp b/Telegram/SourceFiles/boxes/create_poll_box.cpp index 97fb5d1ae..37d66216a 100644 --- a/Telegram/SourceFiles/boxes/create_poll_box.cpp +++ b/Telegram/SourceFiles/boxes/create_poll_box.cpp @@ -24,6 +24,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "core/core_settings.h" #include "chat_helpers/emoji_suggestions_widget.h" #include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "history/view/history_view_schedule_box.h" #include "settings/settings_common.h" #include "base/unique_qptr.h" diff --git a/Telegram/SourceFiles/boxes/send_files_box.cpp b/Telegram/SourceFiles/boxes/send_files_box.cpp index 73b05d706..443d77d7e 100644 --- a/Telegram/SourceFiles/boxes/send_files_box.cpp +++ b/Telegram/SourceFiles/boxes/send_files_box.cpp @@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "mtproto/mtproto_config.h" #include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "chat_helpers/emoji_suggestions_widget.h" #include "chat_helpers/tabbed_panel.h" #include "chat_helpers/tabbed_selector.h" diff --git a/Telegram/SourceFiles/boxes/share_box.cpp b/Telegram/SourceFiles/boxes/share_box.cpp index 006a8307d..1691502f7 100644 --- a/Telegram/SourceFiles/boxes/share_box.cpp +++ b/Telegram/SourceFiles/boxes/share_box.cpp @@ -23,6 +23,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/wrap/slide_wrap.h" #include "ui/text_options.h" #include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "history/history.h" #include "history/history_message.h" #include "history/view/history_view_schedule_box.h" diff --git a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp index 3d32f233c..f0971a362 100644 --- a/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/gifs_list_widget.cpp @@ -17,7 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_photo_media.h" #include "data/data_document_media.h" #include "data/stickers/data_stickers.h" -#include "chat_helpers/message_field.h" // FillSendMenu +#include "chat_helpers/send_context_menu.h" // FillSendMenu #include "ui/widgets/buttons.h" #include "ui/widgets/input_fields.h" #include "ui/widgets/popup_menu.h" diff --git a/Telegram/SourceFiles/chat_helpers/message_field.cpp b/Telegram/SourceFiles/chat_helpers/message_field.cpp index d48addd0d..347cfb9a5 100644 --- a/Telegram/SourceFiles/chat_helpers/message_field.cpp +++ b/Telegram/SourceFiles/chat_helpers/message_field.cpp @@ -685,97 +685,3 @@ void MessageLinksParser::apply( } _list = std::move(parsed); } - -bool FillSendMenu( - not_null menu, - Fn type, - Fn silent, - Fn schedule) { - if (!silent && !schedule) { - return false; - } - const auto now = type(); - if (now == SendMenuType::Disabled - || (!silent && now == SendMenuType::SilentOnly)) { - return false; - } - - if (silent && now != SendMenuType::Reminder) { - menu->addAction(tr::lng_send_silent_message(tr::now), silent); - } - if (schedule && now != SendMenuType::SilentOnly) { - menu->addAction( - (now == SendMenuType::Reminder - ? tr::lng_reminder_message(tr::now) - : tr::lng_schedule_message(tr::now)), - schedule); - } - return true; -} - -void SetupSendMenuAndShortcuts( - not_null button, - Fn type, - Fn silent, - Fn schedule) { - if (!silent && !schedule) { - return; - } - const auto menu = std::make_shared>(); - const auto showMenu = [=] { - *menu = base::make_unique_q(button); - const auto success = FillSendMenu(*menu, type, silent, schedule); - if (success) { - (*menu)->popup(QCursor::pos()); - } - return success; - }; - base::install_event_filter(button, [=](not_null e) { - if (e->type() == QEvent::ContextMenu && showMenu()) { - return base::EventFilterResult::Cancel; - } - return base::EventFilterResult::Continue; - }); - - Shortcuts::Requests( - ) | rpl::start_with_next([=](not_null request) { - using Command = Shortcuts::Command; - - const auto now = type(); - if (now == SendMenuType::Disabled - || (!silent && now == SendMenuType::SilentOnly)) { - return; - } - (silent - && (now != SendMenuType::Reminder) - && request->check(Command::SendSilentMessage) - && request->handle([=] { - silent(); - return true; - })) - || - (schedule - && (now != SendMenuType::SilentOnly) - && request->check(Command::ScheduleMessage) - && request->handle([=] { - schedule(); - return true; - })) - || - (request->check(Command::JustSendMessage) && request->handle([=] { - const auto post = [&](QEvent::Type type) { - QApplication::postEvent( - button, - new QMouseEvent( - type, - QPointF(0, 0), - Qt::LeftButton, - Qt::LeftButton, - Qt::NoModifier)); - }; - post(QEvent::MouseButtonPress); - post(QEvent::MouseButtonRelease); - return true; - })); - }, button->lifetime()); -} diff --git a/Telegram/SourceFiles/chat_helpers/message_field.h b/Telegram/SourceFiles/chat_helpers/message_field.h index 035d3709e..fd7e53aad 100644 --- a/Telegram/SourceFiles/chat_helpers/message_field.h +++ b/Telegram/SourceFiles/chat_helpers/message_field.h @@ -101,23 +101,3 @@ private: base::qt_connection _connection; }; - -enum class SendMenuType { - Disabled, - SilentOnly, - Scheduled, - ScheduledToUser, // For "Send when online". - Reminder, -}; - -bool FillSendMenu( - not_null menu, - Fn type, - Fn silent, - Fn schedule); - -void SetupSendMenuAndShortcuts( - not_null button, - Fn type, - Fn silent, - Fn schedule); diff --git a/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp b/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp new file mode 100644 index 000000000..4add9f213 --- /dev/null +++ b/Telegram/SourceFiles/chat_helpers/send_context_menu.cpp @@ -0,0 +1,111 @@ +/* +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 "chat_helpers/send_context_menu.h" + +#include "api/api_common.h" +#include "base/event_filter.h" +#include "core/shortcuts.h" +#include "lang/lang_keys.h" +#include "ui/widgets/popup_menu.h" + +#include + +FillMenuResult FillSendMenu( + not_null menu, + Fn type, + Fn silent, + Fn schedule) { + if (!silent && !schedule) { + return FillMenuResult::None; + } + const auto now = type(); + if (now == SendMenuType::Disabled + || (!silent && now == SendMenuType::SilentOnly)) { + return FillMenuResult::None; + } + + if (silent && now != SendMenuType::Reminder) { + menu->addAction(tr::lng_send_silent_message(tr::now), silent); + } + if (schedule && now != SendMenuType::SilentOnly) { + menu->addAction( + (now == SendMenuType::Reminder + ? tr::lng_reminder_message(tr::now) + : tr::lng_schedule_message(tr::now)), + schedule); + } + return FillMenuResult::Success; +} + +void SetupSendMenuAndShortcuts( + not_null button, + Fn type, + Fn silent, + Fn schedule) { + if (!silent && !schedule) { + return; + } + const auto menu = std::make_shared>(); + const auto showMenu = [=] { + *menu = base::make_unique_q(button); + const auto result = FillSendMenu(*menu, type, silent, schedule); + const auto success = (result == FillMenuResult::Success); + if (success) { + (*menu)->popup(QCursor::pos()); + } + return success; + }; + base::install_event_filter(button, [=](not_null e) { + if (e->type() == QEvent::ContextMenu && showMenu()) { + return base::EventFilterResult::Cancel; + } + return base::EventFilterResult::Continue; + }); + + Shortcuts::Requests( + ) | rpl::start_with_next([=](not_null request) { + using Command = Shortcuts::Command; + + const auto now = type(); + if (now == SendMenuType::Disabled + || (!silent && now == SendMenuType::SilentOnly)) { + return; + } + (silent + && (now != SendMenuType::Reminder) + && request->check(Command::SendSilentMessage) + && request->handle([=] { + silent(); + return true; + })) + || + (schedule + && (now != SendMenuType::SilentOnly) + && request->check(Command::ScheduleMessage) + && request->handle([=] { + schedule(); + return true; + })) + || + (request->check(Command::JustSendMessage) && request->handle([=] { + const auto post = [&](QEvent::Type type) { + QApplication::postEvent( + button, + new QMouseEvent( + type, + QPointF(0, 0), + Qt::LeftButton, + Qt::LeftButton, + Qt::NoModifier)); + }; + post(QEvent::MouseButtonPress); + post(QEvent::MouseButtonRelease); + return true; + })); + }, button->lifetime()); +} diff --git a/Telegram/SourceFiles/chat_helpers/send_context_menu.h b/Telegram/SourceFiles/chat_helpers/send_context_menu.h new file mode 100644 index 000000000..cd221f1a3 --- /dev/null +++ b/Telegram/SourceFiles/chat_helpers/send_context_menu.h @@ -0,0 +1,39 @@ +/* +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 "ui/rp_widget.h" + +namespace Ui { +class PopupMenu; +} // namespace Ui + +enum class SendMenuType { + Disabled, + SilentOnly, + Scheduled, + ScheduledToUser, // For "Send when online". + Reminder, +}; + +enum class FillMenuResult { + Success, + None, +}; + +FillMenuResult FillSendMenu( + not_null menu, + Fn type, + Fn silent, + Fn schedule); + +void SetupSendMenuAndShortcuts( + not_null button, + Fn type, + Fn silent, + Fn schedule); diff --git a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp index 29fcce2e5..92bb0b1b1 100644 --- a/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp +++ b/Telegram/SourceFiles/chat_helpers/stickers_list_widget.cpp @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_cloud_file.h" #include "data/data_changes.h" -#include "chat_helpers/message_field.h" // FillSendMenu +#include "chat_helpers/send_context_menu.h" // FillSendMenu #include "chat_helpers/stickers_lottie.h" #include "ui/widgets/buttons.h" #include "ui/widgets/popup_menu.h" diff --git a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp index 0802ace53..92323c9ca 100644 --- a/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp +++ b/Telegram/SourceFiles/chat_helpers/tabbed_selector.cpp @@ -7,10 +7,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "chat_helpers/tabbed_selector.h" -#include "chat_helpers/message_field.h" #include "chat_helpers/emoji_list_widget.h" #include "chat_helpers/stickers_list_widget.h" #include "chat_helpers/gifs_list_widget.h" +#include "chat_helpers/send_context_menu.h" #include "ui/widgets/buttons.h" #include "ui/widgets/labels.h" #include "ui/widgets/shadow.h" diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 2f3ac4ee7..13df5ea95 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -69,6 +69,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "chat_helpers/tabbed_section.h" #include "chat_helpers/bot_keyboard.h" #include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "platform/platform_specific.h" #include "mtproto/mtproto_config.h" #include "lang/lang_keys.h" diff --git a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp index 1a4585ecd..140a06593 100644 --- a/Telegram/SourceFiles/history/view/history_view_context_menu.cpp +++ b/Telegram/SourceFiles/history/view/history_view_context_menu.cpp @@ -22,7 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/image/image.h" #include "ui/toast/toast.h" #include "ui/ui_utility.h" -#include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "boxes/confirm_box.h" #include "boxes/sticker_set_box.h" #include "data/data_photo.h" diff --git a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp index 075e8be29..1b27cdee2 100644 --- a/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp +++ b/Telegram/SourceFiles/history/view/history_view_schedule_box.cpp @@ -20,7 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/buttons.h" #include "ui/widgets/popup_menu.h" #include "ui/wrap/padding_wrap.h" -#include "chat_helpers/message_field.h" +#include "chat_helpers/send_context_menu.h" #include "styles/style_info.h" #include "styles/style_layers.h" #include "styles/style_history.h" diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index 8d0a2fdc6..850d89b05 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -14,7 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history.h" #include "history/history_drag_area.h" #include "history/history_item.h" -#include "chat_helpers/message_field.h" // SendMenuType. +#include "chat_helpers/send_context_menu.h" // SendMenuType. #include "ui/widgets/scroll_area.h" #include "ui/widgets/shadow.h" #include "ui/layers/generic_box.h" diff --git a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp index 828f00e7d..6fb589b80 100644 --- a/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp +++ b/Telegram/SourceFiles/inline_bots/inline_results_widget.cpp @@ -8,7 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "inline_bots/inline_results_widget.h" #include "api/api_common.h" -#include "chat_helpers/message_field.h" // FillSendMenu +#include "chat_helpers/send_context_menu.h" // FillSendMenu #include "data/data_photo.h" #include "data/data_document.h" #include "data/data_channel.h"