From 1d8a7f8fd3b0a910ad4ff7bdadc292c402aa8af3 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Thu, 30 Jan 2025 13:12:18 +0300 Subject: [PATCH] Removed redundant template from Info::Media::AddCountedButton. --- .../info/media/info_media_buttons.cpp | 239 ++++++++++++++- .../info/media/info_media_buttons.h | 273 ++++-------------- .../info/media/info_media_inner_widget.cpp | 2 + .../profile/info_profile_inner_widget.cpp | 7 + .../info/saved/info_saved_sublists_widget.cpp | 1 + .../stories/info_stories_inner_widget.cpp | 1 + 6 files changed, 294 insertions(+), 229 deletions(-) diff --git a/Telegram/SourceFiles/info/media/info_media_buttons.cpp b/Telegram/SourceFiles/info/media/info_media_buttons.cpp index 92dd44e16..bc0859881 100644 --- a/Telegram/SourceFiles/info/media/info_media_buttons.cpp +++ b/Telegram/SourceFiles/info/media/info_media_buttons.cpp @@ -7,14 +7,28 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/media/info_media_buttons.h" - #include "base/call_delayed.h" +#include "base/qt/qt_key_modifiers.h" +#include "data/data_channel.h" +#include "data/data_saved_messages.h" +#include "data/data_session.h" +#include "data/data_stories_ids.h" +#include "data/data_user.h" +#include "history/view/history_view_sublist_section.h" +#include "info/info_controller.h" +#include "info/info_memento.h" +#include "info/profile/info_profile_values.h" +#include "info/stories/info_stories_widget.h" +#include "ui/widgets/buttons.h" #include "ui/widgets/popup_menu.h" +#include "ui/wrap/slide_wrap.h" +#include "ui/wrap/vertical_layout.h" #include "window/window_separate_id.h" +#include "window/window_session_controller.h" +#include "styles/style_info.h" #include "styles/style_menu_icons.h" -namespace Info { -namespace Media { +namespace Info::Media { namespace { Window::SeparateSharedMediaType ToSeparateType( @@ -38,8 +52,6 @@ Window::SeparateSharedMediaType ToSeparateType( : SeparatedType::None; } -} // namespace - Fn SeparateWindowFactory( not_null controller, not_null peer, @@ -83,5 +95,218 @@ void AddContextMenuToButton( }); } -} // namespace Media -} // namespace Info +} // namespace + +tr::phrase MediaTextPhrase(Type type) { + switch (type) { + case Type::Photo: return tr::lng_profile_photos; + case Type::GIF: return tr::lng_profile_gifs; + case Type::Video: return tr::lng_profile_videos; + case Type::File: return tr::lng_profile_files; + case Type::MusicFile: return tr::lng_profile_songs; + case Type::Link: return tr::lng_profile_shared_links; + case Type::RoundVoiceFile: return tr::lng_profile_audios; + } + Unexpected("Type in MediaTextPhrase()"); +}; + +Fn MediaText(Type type) { + return [phrase = MediaTextPhrase(type)](int count) { + return phrase(tr::now, lt_count, count); + }; +} + +not_null*> AddCountedButton( + Ui::VerticalLayout *parent, + rpl::producer &&count, + Fn &&textFromCount, + Ui::MultiSlideTracker &tracker) { + using namespace ::Settings; + auto forked = std::move(count) + | start_spawning(parent->lifetime()); + auto text = rpl::duplicate( + forked + ) | rpl::map([textFromCount](int count) { + return (count > 0) + ? textFromCount(count) + : QString(); + }); + auto button = parent->add(object_ptr>( + parent, + object_ptr( + parent, + std::move(text), + st::infoSharedMediaButton)) + )->setDuration( + st::infoSlideDuration + )->toggleOn( + rpl::duplicate(forked) | rpl::map(rpl::mappers::_1 > 0) + ); + tracker.track(button); + return button; +}; + +Fn SeparateWindowFactory( + not_null controller, + not_null peer, + MsgId topicRootId, + Type type); + +void AddContextMenuToButton( + not_null button, + Fn openInWindow); + +not_null AddButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + MsgId topicRootId, + PeerData *migrated, + Type type, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::SharedMediaCountValue(peer, topicRootId, migrated, type), + MediaText(type), + tracker)->entity(); + const auto openInWindow = SeparateWindowFactory( + navigation->parentController(), + peer, + topicRootId, + type); + AddContextMenuToButton(result, openInWindow); + result->addClickHandler([=](Qt::MouseButton mouse) { + if (mouse == Qt::RightButton) { + return; + } + if (openInWindow + && (base::IsCtrlPressed() || mouse == Qt::MiddleButton)) { + return openInWindow(); + } + const auto topic = topicRootId + ? peer->forumTopicFor(topicRootId) + : nullptr; + if (topicRootId && !topic) { + return; + } + navigation->showSection(topicRootId + ? std::make_shared(topic, Section(type)) + : std::make_shared(peer, Section(type))); + }); + return result; +}; + +not_null AddCommonGroupsButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null user, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::CommonGroupsCountValue(user), + [](int count) { + return tr::lng_profile_common_groups(tr::now, lt_count, count); + }, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection( + std::make_shared( + user, + Section::Type::CommonGroups)); + }); + return result; +} + +not_null AddSimilarPeersButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::SimilarPeersCountValue(peer), + [=](int count) { + return peer->isBroadcast() + ? tr::lng_profile_similar_channels(tr::now, lt_count, count) + : tr::lng_profile_similar_bots(tr::now, lt_count, count); + }, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection( + std::make_shared( + peer, + Section::Type::SimilarPeers)); + }); + return result; +} + +not_null AddStoriesButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker) { + auto count = rpl::single(0) | rpl::then(Data::SavedStoriesIds( + peer, + ServerMaxStoryId - 1, + 0 + ) | rpl::map([](const Data::StoriesIdsSlice &slice) { + return slice.fullCount().value_or(0); + })); + const auto phrase = peer->isChannel() ? (+[](int count) { + return tr::lng_profile_posts(tr::now, lt_count, count); + }) : (+[](int count) { + return tr::lng_profile_saved_stories(tr::now, lt_count, count); + }); + auto result = AddCountedButton( + parent, + std::move(count), + phrase, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection(Info::Stories::Make(peer)); + }); + return result; +} + +not_null AddSavedSublistButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::SavedSublistCountValue(peer), + [](int count) { + return tr::lng_profile_saved_messages(tr::now, lt_count, count); + }, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection( + std::make_shared( + peer->owner().savedMessages().sublist(peer))); + }); + return result; +} + +not_null AddPeerGiftsButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::PeerGiftsCountValue(peer), + [](int count) { + return tr::lng_profile_peer_gifts(tr::now, lt_count, count); + }, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection( + std::make_shared( + peer, + Section::Type::PeerGifts)); + }); + return result; +} + +} // namespace Info::Media diff --git a/Telegram/SourceFiles/info/media/info_media_buttons.h b/Telegram/SourceFiles/info/media/info_media_buttons.h index 7023868d6..e8927c097 100644 --- a/Telegram/SourceFiles/info/media/info_media_buttons.h +++ b/Telegram/SourceFiles/info/media/info_media_buttons.h @@ -7,244 +7,73 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include -#include #include "lang/lang_keys.h" -#include "base/qt/qt_key_modifiers.h" -#include "data/data_saved_messages.h" -#include "data/data_session.h" -#include "data/data_stories_ids.h" #include "storage/storage_shared_media.h" -#include "history/view/history_view_sublist_section.h" -#include "info/info_memento.h" -#include "info/info_controller.h" -#include "info/profile/info_profile_values.h" -#include "info/stories/info_stories_widget.h" -#include "ui/wrap/slide_wrap.h" -#include "ui/wrap/vertical_layout.h" -#include "ui/widgets/buttons.h" -#include "window/window_session_controller.h" -#include "data/data_channel.h" -#include "data/data_user.h" -#include "styles/style_info.h" + +namespace Ui { +class AbstractButton; +class MultiSlideTracker; +class SettingsButton; +class VerticalLayout; +template +class SlideWrap; +} // namespace Ui + +namespace Window { +class SessionNavigation; +} // namespace Window namespace Info::Media { using Type = Storage::SharedMediaType; -inline tr::phrase MediaTextPhrase(Type type) { - switch (type) { - case Type::Photo: return tr::lng_profile_photos; - case Type::GIF: return tr::lng_profile_gifs; - case Type::Video: return tr::lng_profile_videos; - case Type::File: return tr::lng_profile_files; - case Type::MusicFile: return tr::lng_profile_songs; - case Type::Link: return tr::lng_profile_shared_links; - case Type::RoundVoiceFile: return tr::lng_profile_audios; - } - Unexpected("Type in MediaTextPhrase()"); -}; +[[nodiscard]] tr::phrase MediaTextPhrase(Type type); -inline auto MediaText(Type type) { - return [phrase = MediaTextPhrase(type)](int count) { - return phrase(tr::now, lt_count, count); - }; -} +[[nodiscard]] Fn MediaText(Type type); -template -inline auto AddCountedButton( - Ui::VerticalLayout *parent, - Count &&count, - Text &&textFromCount, - Ui::MultiSlideTracker &tracker) { - using namespace rpl::mappers; +[[nodiscard]] not_null*> AddCountedButton( + Ui::VerticalLayout *parent, + rpl::producer &&count, + Fn &&textFromCount, + Ui::MultiSlideTracker &tracker); - using namespace ::Settings; - auto forked = std::move(count) - | start_spawning(parent->lifetime()); - auto text = rpl::duplicate( - forked - ) | rpl::map([textFromCount](int count) { - return (count > 0) - ? textFromCount(count) - : QString(); - }); - auto button = parent->add(object_ptr>( - parent, - object_ptr( - parent, - std::move(text), - st::infoSharedMediaButton)) - )->setDuration( - st::infoSlideDuration - )->toggleOn( - rpl::duplicate(forked) | rpl::map(_1 > 0) - ); - tracker.track(button); - return button; -}; - -Fn SeparateWindowFactory( - not_null controller, +[[nodiscard]] not_null AddButton( + Ui::VerticalLayout *parent, + not_null navigation, not_null peer, MsgId topicRootId, - Type type); + PeerData *migrated, + Type type, + Ui::MultiSlideTracker &tracker); -void AddContextMenuToButton( - not_null button, - Fn openInWindow); +[[nodiscard]] not_null AddCommonGroupsButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null user, + Ui::MultiSlideTracker &tracker); -inline auto AddButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null peer, - MsgId topicRootId, - PeerData *migrated, - Type type, - Ui::MultiSlideTracker &tracker) { - auto result = AddCountedButton( - parent, - Profile::SharedMediaCountValue(peer, topicRootId, migrated, type), - MediaText(type), - tracker)->entity(); - const auto openInWindow = SeparateWindowFactory( - navigation->parentController(), - peer, - topicRootId, - type); - AddContextMenuToButton(result, openInWindow); - result->addClickHandler([=](Qt::MouseButton mouse) { - if (mouse == Qt::RightButton) { - return; - } - if (openInWindow - && (base::IsCtrlPressed() || mouse == Qt::MiddleButton)) { - return openInWindow(); - } - const auto topic = topicRootId - ? peer->forumTopicFor(topicRootId) - : nullptr; - if (topicRootId && !topic) { - return; - } - navigation->showSection(topicRootId - ? std::make_shared(topic, Section(type)) - : std::make_shared(peer, Section(type))); - }); - return result; -}; +[[nodiscard]] not_null AddSimilarPeersButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker); -inline auto AddCommonGroupsButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null user, - Ui::MultiSlideTracker &tracker) { - auto result = AddCountedButton( - parent, - Profile::CommonGroupsCountValue(user), - [](int count) { - return tr::lng_profile_common_groups(tr::now, lt_count, count); - }, - tracker)->entity(); - result->addClickHandler([=] { - navigation->showSection( - std::make_shared( - user, - Section::Type::CommonGroups)); - }); - return result; -} +[[nodiscard]] not_null AddStoriesButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker); -inline auto AddSimilarPeersButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null peer, - Ui::MultiSlideTracker &tracker) { - auto result = AddCountedButton( - parent, - Profile::SimilarPeersCountValue(peer), - [=](int count) { - return peer->isBroadcast() - ? tr::lng_profile_similar_channels(tr::now, lt_count, count) - : tr::lng_profile_similar_bots(tr::now, lt_count, count); - }, - tracker)->entity(); - result->addClickHandler([=] { - navigation->showSection( - std::make_shared( - peer, - Section::Type::SimilarPeers)); - }); - return result; -} +[[nodiscard]] not_null AddSavedSublistButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker); -inline auto AddStoriesButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null peer, - Ui::MultiSlideTracker &tracker) { - auto count = rpl::single(0) | rpl::then(Data::SavedStoriesIds( - peer, - ServerMaxStoryId - 1, - 0 - ) | rpl::map([](const Data::StoriesIdsSlice &slice) { - return slice.fullCount().value_or(0); - })); - const auto phrase = peer->isChannel() ? (+[](int count) { - return tr::lng_profile_posts(tr::now, lt_count, count); - }) : (+[](int count) { - return tr::lng_profile_saved_stories(tr::now, lt_count, count); - }); - auto result = AddCountedButton( - parent, - std::move(count), - phrase, - tracker)->entity(); - result->addClickHandler([=] { - navigation->showSection(Info::Stories::Make(peer)); - }); - return result; -} - -inline auto AddSavedSublistButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null peer, - Ui::MultiSlideTracker &tracker) { - auto result = AddCountedButton( - parent, - Profile::SavedSublistCountValue(peer), - [](int count) { - return tr::lng_profile_saved_messages(tr::now, lt_count, count); - }, - tracker)->entity(); - result->addClickHandler([=] { - navigation->showSection( - std::make_shared( - peer->owner().savedMessages().sublist(peer))); - }); - return result; -} - -inline auto AddPeerGiftsButton( - Ui::VerticalLayout *parent, - not_null navigation, - not_null peer, - Ui::MultiSlideTracker &tracker) { - auto result = AddCountedButton( - parent, - Profile::PeerGiftsCountValue(peer), - [](int count) { - return tr::lng_profile_peer_gifts(tr::now, lt_count, count); - }, - tracker)->entity(); - result->addClickHandler([=] { - navigation->showSection( - std::make_shared( - peer, - Section::Type::PeerGifts)); - }); - return result; -} +[[nodiscard]] not_null AddPeerGiftsButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null peer, + Ui::MultiSlideTracker &tracker); } // namespace Info::Media diff --git a/Telegram/SourceFiles/info/media/info_media_inner_widget.cpp b/Telegram/SourceFiles/info/media/info_media_inner_widget.cpp index eed93256b..6f2bedac4 100644 --- a/Telegram/SourceFiles/info/media/info_media_inner_widget.cpp +++ b/Telegram/SourceFiles/info/media/info_media_inner_widget.cpp @@ -15,10 +15,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/profile/info_profile_icon.h" #include "info/info_controller.h" #include "data/data_forum_topic.h" +#include "data/data_peer.h" #include "ui/widgets/discrete_sliders.h" #include "ui/widgets/shadow.h" #include "ui/widgets/buttons.h" #include "ui/widgets/box_content_divider.h" +#include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" #include "ui/search_field_controller.h" #include "styles/style_info.h" diff --git a/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp b/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp index 3171fa194..42594dd01 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp @@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/profile/info_profile_inner_widget.h" +#include "info/info_controller.h" #include "info/profile/info_profile_widget.h" #include "info/profile/info_profile_cover.h" #include "info/profile/info_profile_icon.h" @@ -14,9 +15,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/profile/info_profile_actions.h" #include "info/media/info_media_buttons.h" #include "data/data_changes.h" +#include "data/data_channel.h" #include "data/data_forum_topic.h" +#include "data/data_peer.h" #include "data/data_photo.h" #include "data/data_file_origin.h" +#include "data/data_user.h" #include "main/main_session.h" #include "apiwrap.h" #include "api/api_peer_photo.h" @@ -25,7 +29,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/checkbox.h" #include "ui/widgets/scroll_area.h" #include "ui/widgets/shadow.h" +#include "ui/wrap/vertical_layout.h" +#include "ui/wrap/slide_wrap.h" #include "ui/ui_utility.h" +#include "styles/style_info.h" namespace Info { namespace Profile { diff --git a/Telegram/SourceFiles/info/saved/info_saved_sublists_widget.cpp b/Telegram/SourceFiles/info/saved/info_saved_sublists_widget.cpp index 3a3ff46ad..9a070889a 100644 --- a/Telegram/SourceFiles/info/saved/info_saved_sublists_widget.cpp +++ b/Telegram/SourceFiles/info/saved/info_saved_sublists_widget.cpp @@ -19,6 +19,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "lang/lang_keys.h" #include "ui/widgets/box_content_divider.h" +#include "ui/widgets/buttons.h" #include "ui/wrap/slide_wrap.h" #include "ui/wrap/vertical_layout.h" #include "ui/ui_utility.h" diff --git a/Telegram/SourceFiles/info/stories/info_stories_inner_widget.cpp b/Telegram/SourceFiles/info/stories/info_stories_inner_widget.cpp index c83795cab..d0ab12cb5 100644 --- a/Telegram/SourceFiles/info/stories/info_stories_inner_widget.cpp +++ b/Telegram/SourceFiles/info/stories/info_stories_inner_widget.cpp @@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/media/info_media_list_widget.h" #include "info/profile/info_profile_actions.h" #include "info/profile/info_profile_icon.h" +#include "info/profile/info_profile_values.h" #include "info/profile/info_profile_widget.h" #include "info/stories/info_stories_widget.h" #include "info/info_controller.h"