diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index a4f02b1a4..acf322fc2 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -899,6 +899,8 @@ PRIVATE info/profile/info_profile_widget.h info/settings/info_settings_widget.cpp info/settings/info_settings_widget.h + info/similar_channels/info_similar_channels_widget.cpp + info/similar_channels/info_similar_channels_widget.h info/statistics/info_statistics_common.h info/statistics/info_statistics_inner_widget.cpp info/statistics/info_statistics_inner_widget.h diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 7217f5c87..f35d70511 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -1175,6 +1175,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_profile_bot_privacy" = "Bot Privacy Policy"; "lng_profile_common_groups#one" = "{count} group in common"; "lng_profile_common_groups#other" = "{count} groups in common"; +"lng_profile_similar_channels#one" = "{count} similar channel"; +"lng_profile_similar_channels#other" = "{count} similar channels"; "lng_profile_participants_section" = "Members"; "lng_profile_subscribers_section" = "Subscribers"; "lng_profile_add_contact" = "Add Contact"; @@ -1686,6 +1688,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_similar_channels_premium_all#one" = "Subscribe to {link} to unlock up to **{count}** similar channel."; "lng_similar_channels_premium_all#other" = "Subscribe to {link} to unlock up to **{count}** similar channels."; "lng_similar_channels_premium_all_link" = "Telegram Premium"; +"lng_similar_channels_show_more" = "Show more channels"; "lng_premium_gift_duration_months#one" = "for {count} month"; "lng_premium_gift_duration_months#other" = "for {count} months"; diff --git a/Telegram/SourceFiles/api/api_chat_participants.cpp b/Telegram/SourceFiles/api/api_chat_participants.cpp index 35e9e2055..583332790 100644 --- a/Telegram/SourceFiles/api/api_chat_participants.cpp +++ b/Telegram/SourceFiles/api/api_chat_participants.cpp @@ -227,7 +227,9 @@ void ApplyBotsList( } } if constexpr (MTPDmessages_chatsSlice::Is()) { - result.more = data.vcount().v - data.vchats().v.size(); + if (channel->session().premiumPossible()) { + result.more = data.vcount().v - data.vchats().v.size(); + } } }); return result; diff --git a/Telegram/SourceFiles/data/data_premium_limits.cpp b/Telegram/SourceFiles/data/data_premium_limits.cpp index 41397c383..0f0d495b8 100644 --- a/Telegram/SourceFiles/data/data_premium_limits.cpp +++ b/Telegram/SourceFiles/data/data_premium_limits.cpp @@ -29,6 +29,18 @@ int PremiumLimits::channelsCurrent() const { : channelsDefault(); } +int PremiumLimits::similarChannelsDefault() const { + return appConfigLimit("recommended_channels_limit_default", 10); +} +int PremiumLimits::similarChannelsPremium() const { + return appConfigLimit("recommended_channels_limit_premium", 100); +} +int PremiumLimits::similarChannelsCurrent() const { + return isPremium() + ? channelsPremium() + : channelsDefault(); +} + int PremiumLimits::gifsDefault() const { return appConfigLimit("saved_gifs_limit_default", 200); } diff --git a/Telegram/SourceFiles/data/data_premium_limits.h b/Telegram/SourceFiles/data/data_premium_limits.h index 17ffa1247..079f214e6 100644 --- a/Telegram/SourceFiles/data/data_premium_limits.h +++ b/Telegram/SourceFiles/data/data_premium_limits.h @@ -21,6 +21,10 @@ public: [[nodiscard]] int channelsPremium() const; [[nodiscard]] int channelsCurrent() const; + [[nodiscard]] int similarChannelsDefault() const; + [[nodiscard]] int similarChannelsPremium() const; + [[nodiscard]] int similarChannelsCurrent() const; + [[nodiscard]] int gifsDefault() const; [[nodiscard]] int gifsPremium() const; [[nodiscard]] int gifsCurrent() const; diff --git a/Telegram/SourceFiles/history/view/media/history_view_similar_channels.cpp b/Telegram/SourceFiles/history/view/media/history_view_similar_channels.cpp index db20e3ae6..2ab07ee66 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_similar_channels.cpp +++ b/Telegram/SourceFiles/history/view/media/history_view_similar_channels.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/peer_lists_box.h" #include "core/click_handler_types.h" #include "data/data_channel.h" +#include "data/data_premium_limits.h" #include "data/data_session.h" #include "dialogs/ui/dialogs_stories_content.h" #include "dialogs/ui/dialogs_stories_list.h" @@ -19,9 +20,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/view/history_view_cursor_state.h" #include "history/history.h" #include "history/history_item.h" +#include "info/similar_channels/info_similar_channels_widget.h" +#include "info/info_controller.h" +#include "info/info_memento.h" #include "lang/lang_keys.h" -#include "main/main_account.h" -#include "main/main_app_config.h" #include "main/main_session.h" #include "settings/settings_premium.h" #include "ui/chat/chat_style.h" @@ -37,73 +39,19 @@ namespace { using Channels = Api::ChatParticipants::Channels; -class SimilarChannelsController final : public PeerListController { -public: - SimilarChannelsController( - not_null controller, - Channels channels); - - void prepare() override; - void loadMoreRows() override; - void rowClicked(not_null row) override; - Main::Session &session() const override; - -private: - const not_null _controller; - const Channels _channels; - -}; - -SimilarChannelsController::SimilarChannelsController( - not_null controller, - Channels channels) -: _controller(controller) -, _channels(std::move(channels)) { -} - -void SimilarChannelsController::prepare() { - for (const auto &channel : _channels.list) { - auto row = std::make_unique(channel); - if (const auto count = channel->membersCount(); count > 1) { - row->setCustomStatus(tr::lng_chat_status_subscribers( - tr::now, - lt_count, - count)); - } - delegate()->peerListAppendRow(std::move(row)); - } - delegate()->peerListRefreshRows(); -} - -void SimilarChannelsController::loadMoreRows() { -} - -void SimilarChannelsController::rowClicked(not_null row) { - const auto other = ClickHandlerContext{ - .sessionWindow = _controller, - .show = _controller->uiShow(), - }; - row->peer()->openLink()->onClick({ - Qt::LeftButton, - QVariant::fromValue(other) - }); -} - -Main::Session &SimilarChannelsController::session() const { - return _channels.list.front()->session(); -} - -[[nodiscard]] object_ptr SimilarChannelsBox( - not_null controller, - const Channels &channels) { - const auto initBox = [=](not_null box) { - box->setTitle(tr::lng_similar_channels_title()); - box->addButton(tr::lng_close(), [=] { box->closeBox(); }); - }; - return Box( - std::make_unique(controller, channels), - initBox); -} +//void SimilarChannelsController::prepare() { +// for (const auto &channel : _channels.list) { +// auto row = std::make_unique(channel); +// if (const auto count = channel->membersCount(); count > 1) { +// row->setCustomStatus(tr::lng_chat_status_subscribers( +// tr::now, +// lt_count, +// count)); +// } +// delegate()->peerListAppendRow(std::move(row)); +// } +// delegate()->peerListRefreshRows(); +//} [[nodiscard]] ClickHandlerPtr MakeViewAllLink( not_null channel, @@ -113,10 +61,8 @@ Main::Session &SimilarChannelsController::session() const { if (const auto strong = my.sessionWindow.get()) { Assert(channel != nullptr); if (promoForNonPremium && !channel->session().premium()) { - const auto account = &channel->session().account(); - const auto upto = account->appConfig().get( - u"recommended_channels_limit_premium"_q, - 100); + const auto upto = Data::PremiumLimits( + &channel->session()).similarChannelsPremium(); Settings::ShowPremiumPromoToast( strong->uiShow(), tr::lng_similar_channels_premium_all( @@ -137,7 +83,10 @@ Main::Session &SimilarChannelsController::session() const { if (list.list.empty()) { return; } - strong->show(SimilarChannelsBox(strong, list)); + strong->showSection( + std::make_shared( + channel, + Info::Section::Type::SimilarChannels)); } }); } @@ -217,6 +166,7 @@ void SimilarChannels::draw(Painter &p, const PaintContext &context) const { const auto padding = st::chatSimilarChannelPadding; p.setClipRect(geometry); _hasHeavyPart = 1; + validateLastPremiumLock(); const auto drawOne = [&](const Channel &channel) { const auto geometry = channel.geometry.translated(-_scrollLeft, 0); const auto right = geometry.x() + geometry.width(); @@ -394,12 +344,68 @@ void SimilarChannels::draw(Painter &p, const PaintContext &context) const { p.setClipping(false); } +void SimilarChannels::validateLastPremiumLock() const { + if (_channels.empty()) { + return; + } + if (!_moreThumbnailsValid) { + _moreThumbnailsValid = 1; + fillMoreThumbnails(); + } + const auto &last = _channels.back(); + if (!last.more) { + return; + } + const auto premium = history()->session().premium(); + const auto locked = !premium && history()->session().premiumPossible(); + if (last.moreLocked == locked) { + return; + } + last.moreLocked = locked ? 1 : 0; + last.counterBgValid = 0; +} + +void SimilarChannels::fillMoreThumbnails() const { + const auto channel = parent()->history()->peer->asChannel(); + Assert(channel != nullptr); + + _moreThumbnails = {}; + const auto api = &channel->session().api(); + const auto &similar = api->chatParticipants().similar(channel); + for (auto i = 0, count = int(_moreThumbnails.size()); i != count; ++i) { + if (similar.list.size() <= _channels.size() + i) { + break; + } + _moreThumbnails[i] = Dialogs::Stories::MakeUserpicThumbnail( + similar.list[_channels.size() + i]); + } +} + void SimilarChannels::validateCounterBg(const Channel &channel) const { if (channel.counterBgValid) { return; } channel.counterBgValid = 1; + const auto photo = st::chatSimilarChannelPhoto; + const auto inner = QRect(0, 0, photo, photo); + const auto outer = inner.marginsAdded(st::chatSimilarChannelPadding); + const auto length = st::chatSimilarBadgeFont->width(channel.counter); + const auto contents = length + + (!channel.more + ? st::chatSimilarBadgeIcon.width() + : channel.moreLocked + ? st::chatSimilarLockedIcon.width() + : 0); + const auto delta = (outer.width() - contents) / 2; + const auto badge = QRect( + delta, + st::chatSimilarBadgeTop, + outer.width() - 2 * delta, + st::chatSimilarBadgeFont->height); + channel.counterRect = badge.marginsAdded( + st::chatSimilarBadgePadding); + const auto width = channel.counterRect.width(); const auto height = channel.counterRect.height(); const auto ratio = style::DevicePixelRatio(); @@ -517,8 +523,9 @@ QSize SimilarChannels::countOptimalSize() { const auto api = &channel->session().api(); api->chatParticipants().loadSimilarChannels(channel); const auto premium = channel->session().premium(); - const auto similar = api->chatParticipants().similar(channel); + const auto &similar = api->chatParticipants().similar(channel); _empty = similar.list.empty() ? 1 : 0; + _moreThumbnailsValid = 0; using Flag = ChannelDataFlag; _toggled = (channel->flags() & Flag::SimilarExpanded) ? 1 : 0; if (_empty || !_toggled) { @@ -532,9 +539,8 @@ QSize SimilarChannels::countOptimalSize() { const auto photo = st::chatSimilarChannelPhoto; const auto inner = QRect(0, 0, photo, photo); const auto outer = inner.marginsAdded(st::chatSimilarChannelPadding); - const auto limit = channel->session().account().appConfig().get( - u"recommended_channels_limit_default"_q, - 10); + const auto limit = Data::PremiumLimits( + &channel->session()).similarChannelsDefault(); const auto take = (similar.more > 0 || similar.list.size() > 2 * limit) ? limit : int(similar.list.size()); @@ -565,34 +571,11 @@ QSize SimilarChannels::countOptimalSize() { ? moreCounter : channel->membersCount(); if (moreCounter || counter > 1) { - const auto text = (moreCounter ? u"+"_q : QString()) + last.counter = (moreCounter ? u"+"_q : QString()) + Lang::FormatCountToShort(counter).string; - const auto length = st::chatSimilarBadgeFont->width(text); - const auto width = length - + (!moreCounter - ? st::chatSimilarBadgeIcon.width() - : !premium - ? st::chatSimilarLockedIcon.width() - : 0); - const auto delta = (outer.width() - width) / 2; - const auto badge = QRect( - delta, - st::chatSimilarBadgeTop, - outer.width() - 2 * delta, - st::chatSimilarBadgeFont->height); - last.counter = text; - last.counterRect = badge.marginsAdded( - st::chatSimilarBadgePadding); } x += outer.width() + skip; } - for (auto i = 0, count = int(_moreThumbnails.size()); i != count; ++i) { - if (similar.list.size() <= _channels.size() + i) { - break; - } - _moreThumbnails[i] = Dialogs::Stories::MakeUserpicThumbnail( - similar.list[_channels.size() + i]); - } _title = tr::lng_similar_channels_title(tr::now); _titleWidth = st::chatSimilarTitle->width(_title); _viewAll = tr::lng_similar_channels_view_all(tr::now); @@ -604,7 +587,7 @@ QSize SimilarChannels::countOptimalSize() { const auto bubble = full.marginsAdded(st::chatSimilarPadding); _fullWidth = bubble.width(); const auto titleSkip = st::chatSimilarTitlePosition.x(); - const auto min = _titleWidth + 2 * titleSkip; + const auto min = int(_titleWidth) + 2 * titleSkip; const auto limited = std::max( std::min(int(_fullWidth), st::chatSimilarWidthMax), min); diff --git a/Telegram/SourceFiles/history/view/media/history_view_similar_channels.h b/Telegram/SourceFiles/history/view/media/history_view_similar_channels.h index b77caff90..a7f30b2a7 100644 --- a/Telegram/SourceFiles/history/view/media/history_view_similar_channels.h +++ b/Telegram/SourceFiles/history/view/media/history_view_similar_channels.h @@ -65,16 +65,18 @@ private: std::shared_ptr thumbnail; ClickHandlerPtr link; QString counter; - QRect counterRect; + mutable QRect counterRect; mutable QImage counterBg; mutable std::unique_ptr ripple; uint32 more : 29 = 0; - uint32 moreLocked : 1 = 0; + mutable uint32 moreLocked : 1 = 0; mutable uint32 subscribed : 1 = 0; mutable uint32 counterBgValid : 1 = 0; }; void ensureCacheReady(QSize size) const; + void validateLastPremiumLock() const; + void fillMoreThumbnails() const; void validateCounterBg(const Channel &channel) const; [[nodiscard]] ClickHandlerPtr ensureToggleLink() const; @@ -85,7 +87,8 @@ private: mutable QImage _roundedCache; mutable std::array _roundedCorners; mutable QPoint _lastPoint; - int _titleWidth = 0; + uint32 _titleWidth : 15 = 0; + mutable uint32 _moreThumbnailsValid : 1 = 0; uint32 _viewAllWidth : 15 = 0; uint32 _fullWidth : 15 = 0; uint32 _empty : 1 = 0; @@ -96,7 +99,7 @@ private: mutable uint32 _hasHeavyPart : 1 = 0; std::vector _channels; - std::array, 2> _moreThumbnails; + mutable std::array, 2> _moreThumbnails; mutable ClickHandlerPtr _viewAllLink; mutable ClickHandlerPtr _toggleLink; diff --git a/Telegram/SourceFiles/info/info.style b/Telegram/SourceFiles/info/info.style index b88237792..0c7401964 100644 --- a/Telegram/SourceFiles/info/info.style +++ b/Telegram/SourceFiles/info/info.style @@ -395,6 +395,7 @@ infoIconMediaFile: icon {{ "info/info_media_file", infoIconFg }}; infoIconMediaAudio: icon {{ "info/info_media_audio", infoIconFg }}; infoIconMediaLink: icon {{ "info/info_media_link", infoIconFg }}; infoIconMediaGroup: icon {{ "info/info_common_groups", infoIconFg }}; +infoIconMediaChannel: icon {{ "menu/channel", infoIconFg, point(4px, 4px) }}; infoIconMediaVoice: icon {{ "info/info_media_voice", infoIconFg }}; infoIconMediaStories: icon {{ "info/info_media_stories", infoIconFg }}; infoIconMediaStoriesArchive: icon {{ "info/info_stories_archive", infoIconFg }}; @@ -530,7 +531,7 @@ infoMediaLeft: 3px; infoMediaMargin: margins(0px, 6px, 0px, 2px); infoMediaMinGridSize: 90px; -infoCommonGroupsMargin: margins(0px, 13px, 0px, 2px); +infoCommonGroupsMargin: margins(0px, 2px, 0px, 2px); infoCommonGroupsListItem: PeerListItem(defaultPeerListItem) { height: 52px; photoSize: 40px; @@ -952,3 +953,14 @@ shortInfoCover: ShortInfoCover { } permissionsExpandIcon: icon{{ "info/edit/expand_arrow_small", windowBoldFg }}; + +similarChannelsLockOverlap: 58px; +similarChannelsLockFade: 58px; +similarChannelsLock: defaultActiveButton; +similarChannelsLockPadding: margins(12px, 12px, 12px, 12px); +similarChannelsLockAbout: FlatLabel(defaultFlatLabel) { + textFg: windowSubTextFg; + align: align(top); + minWidth: 128px; +} +similarChannelsLockAboutPadding: margins(12px, 12px, 12px, 12px); diff --git a/Telegram/SourceFiles/info/info_controller.h b/Telegram/SourceFiles/info/info_controller.h index 33ef698f1..046635020 100644 --- a/Telegram/SourceFiles/info/info_controller.h +++ b/Telegram/SourceFiles/info/info_controller.h @@ -125,6 +125,7 @@ public: Profile, Media, CommonGroups, + SimilarChannels, Members, Settings, Downloads, diff --git a/Telegram/SourceFiles/info/info_memento.cpp b/Telegram/SourceFiles/info/info_memento.cpp index 24932a701..b2f78b029 100644 --- a/Telegram/SourceFiles/info/info_memento.cpp +++ b/Telegram/SourceFiles/info/info_memento.cpp @@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "info/members/info_members_widget.h" #include "info/common_groups/info_common_groups_widget.h" #include "info/settings/info_settings_widget.h" +#include "info/similar_channels/info_similar_channels_widget.h" #include "info/polls/info_polls_results_widget.h" #include "info/info_section_widget.h" #include "info/info_layer_widget.h" @@ -141,6 +142,9 @@ std::shared_ptr Memento::DefaultContent( section.mediaType()); case Section::Type::CommonGroups: return std::make_shared(peer->asUser()); + case Section::Type::SimilarChannels: + return std::make_shared( + peer->asChannel()); case Section::Type::Members: return std::make_shared( peer, diff --git a/Telegram/SourceFiles/info/media/info_media_buttons.h b/Telegram/SourceFiles/info/media/info_media_buttons.h index ef2537496..527f7a4db 100644 --- a/Telegram/SourceFiles/info/media/info_media_buttons.h +++ b/Telegram/SourceFiles/info/media/info_media_buttons.h @@ -20,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #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" @@ -120,7 +121,33 @@ inline auto AddCommonGroupsButton( tracker)->entity(); result->addClickHandler([=] { navigation->showSection( - std::make_shared(user, Section::Type::CommonGroups)); + std::make_shared( + user, + Section::Type::CommonGroups)); + }); + return result; +}; + +inline auto AddSimilarChannelsButton( + Ui::VerticalLayout *parent, + not_null navigation, + not_null channel, + Ui::MultiSlideTracker &tracker) { + auto result = AddCountedButton( + parent, + Profile::SimilarChannelsCountValue(channel), + [](int count) { + return tr::lng_profile_similar_channels( + tr::now, + lt_count, + count); + }, + tracker)->entity(); + result->addClickHandler([=] { + navigation->showSection( + std::make_shared( + channel, + Section::Type::SimilarChannels)); }); return result; }; diff --git a/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp b/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp index 24c59e8c2..e6056857f 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_inner_widget.cpp @@ -186,6 +186,19 @@ object_ptr InnerWidget::setupSharedMedia( icon, st::infoSharedMediaButtonIconPosition); }; + const auto addSimilarChannelsButton = [&]( + not_null channel, + const style::icon &icon) { + auto result = Media::AddSimilarChannelsButton( + content, + _controller, + channel, + tracker); + object_ptr( + result, + icon, + st::infoSharedMediaButtonIconPosition); + }; auto addStoriesButton = [&]( not_null peer, const style::icon &icon) { @@ -211,8 +224,10 @@ object_ptr InnerWidget::setupSharedMedia( addMediaButton(MediaType::Link, st::infoIconMediaLink); addMediaButton(MediaType::RoundVoiceFile, st::infoIconMediaVoice); addMediaButton(MediaType::GIF, st::infoIconMediaGif); - if (auto user = _peer->asUser()) { + if (const auto user = _peer->asUser()) { addCommonGroupsButton(user, st::infoIconMediaGroup); + } else if (const auto channel = _peer->asChannel()) { + addSimilarChannelsButton(channel, st::infoIconMediaChannel); } auto result = object_ptr>( diff --git a/Telegram/SourceFiles/info/profile/info_profile_values.cpp b/Telegram/SourceFiles/info/profile/info_profile_values.cpp index ee1cfa1d7..e5dc79379 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_values.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_values.cpp @@ -7,6 +7,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "info/profile/info_profile_values.h" +#include "api/api_chat_participants.h" +#include "apiwrap.h" #include "info/profile/info_profile_badge.h" #include "core/application.h" #include "core/click_handler_types.h" @@ -520,6 +522,20 @@ rpl::producer CommonGroupsCountValue(not_null user) { }); } +rpl::producer SimilarChannelsCountValue( + not_null channel) { + const auto participants = &channel->session().api().chatParticipants(); + participants->loadSimilarChannels(channel); + return rpl::single(channel) | rpl::then( + participants->similarLoaded() + ) | rpl::filter( + rpl::mappers::_1 == channel + ) | rpl::map([=] { + const auto &similar = participants->similar(channel); + return int(similar.list.size()) + similar.more; + }); +} + rpl::producer CanAddMemberValue(not_null peer) { if (const auto chat = peer->asChat()) { return peer->session().changes().peerFlagsValue( diff --git a/Telegram/SourceFiles/info/profile/info_profile_values.h b/Telegram/SourceFiles/info/profile/info_profile_values.h index b3a110fe4..f669a4535 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_values.h +++ b/Telegram/SourceFiles/info/profile/info_profile_values.h @@ -102,6 +102,8 @@ rpl::producer> MigratedOrMeValue( Storage::SharedMediaType type); [[nodiscard]] rpl::producer CommonGroupsCountValue( not_null user); +[[nodiscard]] rpl::producer SimilarChannelsCountValue( + not_null channel); [[nodiscard]] rpl::producer CanAddMemberValue( not_null peer); [[nodiscard]] rpl::producer FullReactionsCountValue( diff --git a/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.cpp b/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.cpp new file mode 100644 index 000000000..049ea9fdd --- /dev/null +++ b/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.cpp @@ -0,0 +1,515 @@ +/* +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 "info/similar_channels/info_similar_channels_widget.h" + +#include "api/api_chat_participants.h" +#include "apiwrap.h" +#include "boxes/peer_list_box.h" +#include "data/data_channel.h" +#include "data/data_peer_values.h" +#include "data/data_premium_limits.h" +#include "data/data_session.h" +#include "info/info_controller.h" +#include "main/main_session.h" +#include "ui/text/text_utilities.h" +#include "ui/widgets/buttons.h" +#include "ui/widgets/scroll_area.h" +#include "ui/widgets/tooltip.h" +#include "ui/ui_utility.h" +#include "lang/lang_keys.h" +#include "settings/settings_premium.h" +#include "window/window_session_controller.h" +#include "styles/style_info.h" +#include "styles/style_widgets.h" + +namespace Info::SimilarChannels { +namespace { + +class ListController final : public PeerListController { +public: + ListController( + not_null controller, + not_null channel); + + Main::Session &session() const override; + void prepare() override; + void rowClicked(not_null row) override; + void loadMoreRows() override; + + std::unique_ptr createRestoredRow( + not_null peer) override { + return createRow(peer); + } + + std::unique_ptr saveState() const override; + void restoreState(std::unique_ptr state) override; + + void setContentWidget(not_null widget); + [[nodiscard]] rpl::producer unlockHeightValue() const; + +private: + std::unique_ptr createRow(not_null peer); + void setupUnlock(); + void rebuild(); + + struct SavedState : SavedStateBase { + }; + const not_null _controller; + const not_null _channel; + Ui::RpWidget *_content = nullptr; + Ui::RpWidget *_unlock = nullptr; + rpl::variable _unlockHeight; + +}; + +ListController::ListController( + not_null controller, + not_null channel) +: PeerListController() +, _controller(controller) +, _channel(channel) { +} + +Main::Session &ListController::session() const { + return _channel->session(); +} + +std::unique_ptr ListController::createRow( + not_null peer) { + auto result = std::make_unique(peer); + if (const auto channel = peer->asChannel()) { + if (const auto count = channel->membersCount(); count > 1) { + result->setCustomStatus( + tr::lng_chat_status_subscribers(tr::now, lt_count, count)); + } + } + return result; +} + +void ListController::prepare() { + delegate()->peerListSetTitle(tr::lng_similar_channels_title()); + + const auto participants = &_channel->session().api().chatParticipants(); + + Data::AmPremiumValue( + &_channel->session() + ) | rpl::start_with_next([=] { + participants->loadSimilarChannels(_channel); + rebuild(); + }, lifetime()); + + participants->similarLoaded( + ) | rpl::filter( + rpl::mappers::_1 == _channel + ) | rpl::start_with_next([=] { + rebuild(); + }, lifetime()); +} + +void ListController::setContentWidget(not_null widget) { + _content = widget; +} + +rpl::producer ListController::unlockHeightValue() const { + return _unlockHeight.value(); +} + +void ListController::rebuild() { + const auto participants = &_channel->session().api().chatParticipants(); + const auto &list = participants->similar(_channel); + for (const auto channel : list.list) { + if (!delegate()->peerListFindRow(channel->id.value)) { + delegate()->peerListAppendRow(createRow(channel)); + } + } + if (!list.more + || _channel->session().premium() + || !_channel->session().premiumPossible()) { + delete base::take(_unlock); + _unlockHeight = 0; + } else if (!_unlock) { + setupUnlock(); + } + delegate()->peerListRefreshRows(); +} + +void ListController::setupUnlock() { + Expects(_content != nullptr); + + _unlock = Ui::CreateChild(_content); + _unlock->show(); + + const auto button = Ui::CreateChild( + _unlock, + tr::lng_similar_channels_show_more(), + st::similarChannelsLock); + button->setTextTransform(Ui::RoundButton::TextTransform::NoTransform); + button->setClickedCallback([=] { + const auto window = _controller->parentController(); + ::Settings::ShowPremium(window, u"similar_channels"_q); + }); + const auto upto = Data::PremiumLimits( + &_channel->session()).similarChannelsPremium(); + const auto about = Ui::CreateChild( + _unlock, + tr::lng_similar_channels_premium_all( + lt_count, + rpl::single(upto * 1.), + lt_link, + tr::lng_similar_channels_premium_all_link( + ) | Ui::Text::ToBold() | Ui::Text::ToLink(), + Ui::Text::RichLangValue), + st::similarChannelsLockAbout); + about->setClickHandlerFilter([=](const auto &...) { + const auto window = _controller->parentController(); + ::Settings::ShowPremium(window, u"similar_channels"_q); + return false; + }); + + rpl::combine( + _content->sizeValue(), + tr::lng_similar_channels_show_more() + ) | rpl::start_with_next([=](QSize size, const auto &) { + auto top = st::similarChannelsLockFade + + st::similarChannelsLockPadding.top(); + button->setGeometry( + st::similarChannelsLockPadding.left(), + top, + (size.width() + - st::similarChannelsLockPadding.left() + - st::similarChannelsLockPadding.right()), + button->height()); + top += button->height() + st::similarChannelsLockPadding.bottom(); + + const auto minWidth = st::similarChannelsLockAbout.minWidth; + const auto maxWidth = std::max( + minWidth + 1, + (size.width() + - st::similarChannelsLockAboutPadding.left() + - st::similarChannelsLockAboutPadding.right())); + const auto countAboutHeight = [&](int width) { + about->resizeToWidth(width); + return about->height(); + }; + const auto desired = Ui::FindNiceTooltipWidth( + minWidth, + maxWidth, + countAboutHeight); + about->resizeToWidth(desired); + about->move((size.width() - about->width()) / 2, top); + top += about->height() + + st::similarChannelsLockAboutPadding.bottom(); + _unlock->setGeometry(0, size.height() - top, size.width(), top); + }, _unlock->lifetime()); + + _unlockHeight = _unlock->heightValue(); + + _unlock->paintRequest( + ) | rpl::start_with_next([=] { + auto p = QPainter(_unlock); + const auto width = _unlock->width(); + const auto fade = st::similarChannelsLockFade; + auto gradient = QLinearGradient(0, 0, 0, fade); + gradient.setStops({ + { 0., QColor(255, 255, 255, 0) }, + { 1., st::windowBg->c }, + }); + p.fillRect(0, 0, width, fade, gradient); + p.fillRect(0, fade, width, _unlock->height() - fade, st::windowBg); + }, _unlock->lifetime()); +} + +void ListController::loadMoreRows() { +} + +std::unique_ptr ListController::saveState() const { + auto result = PeerListController::saveState(); + auto my = std::make_unique(); + result->controllerState = std::move(my); + return result; +} + +void ListController::restoreState( + std::unique_ptr state) { + auto typeErasedState = state + ? state->controllerState.get() + : nullptr; + if (auto my = dynamic_cast(typeErasedState)) { + PeerListController::restoreState(std::move(state)); + } +} + +void ListController::rowClicked(not_null row) { + _controller->parentController()->showPeerHistory( + row->peer(), + Window::SectionShow::Way::Forward); +} + +} // namespace + +class InnerWidget final + : public Ui::RpWidget + , private PeerListContentDelegate { +public: + InnerWidget( + QWidget *parent, + not_null controller, + not_null channel); + + [[nodiscard]] not_null channel() const { + return _channel; + } + + rpl::producer scrollToRequests() const; + + int desiredHeight() const; + + void saveState(not_null memento); + void restoreState(not_null memento); + +protected: + void visibleTopBottomUpdated( + int visibleTop, + int visibleBottom) override; + +private: + using ListWidget = PeerListContent; + + // PeerListContentDelegate interface. + void peerListSetTitle(rpl::producer title) override; + void peerListSetAdditionalTitle(rpl::producer title) override; + bool peerListIsRowChecked(not_null row) override; + int peerListSelectedRowsCount() override; + void peerListScrollToTop() override; + void peerListAddSelectedPeerInBunch( + not_null peer) override; + void peerListAddSelectedRowInBunch( + not_null row) override; + void peerListFinishSelectedRowsBunch() override; + void peerListSetDescription( + object_ptr description) override; + std::shared_ptr peerListUiShow() override; + + object_ptr setupList( + RpWidget *parent, + not_null controller); + + const std::shared_ptr _show; + not_null _controller; + const not_null _channel; + std::unique_ptr _listController; + object_ptr _list; + + rpl::event_stream _scrollToRequests; + +}; + +InnerWidget::InnerWidget( + QWidget *parent, + not_null controller, + not_null channel) +: RpWidget(parent) +, _show(controller->uiShow()) +, _controller(controller) +, _channel(channel) +, _listController(std::make_unique(controller, _channel)) +, _list(setupList(this, _listController.get())) { + setContent(_list.data()); + _listController->setDelegate(static_cast(this)); +} + +void InnerWidget::visibleTopBottomUpdated( + int visibleTop, + int visibleBottom) { + setChildVisibleTopBottom(_list, visibleTop, visibleBottom); +} + +void InnerWidget::saveState(not_null memento) { + memento->setListState(_listController->saveState()); +} + +void InnerWidget::restoreState(not_null memento) { + _listController->restoreState(memento->listState()); +} + +rpl::producer InnerWidget::scrollToRequests() const { + return _scrollToRequests.events(); +} + +int InnerWidget::desiredHeight() const { + auto desired = 0; + desired += _list->fullRowsCount() * st::infoMembersList.item.height; + return qMax(height(), desired); +} + +object_ptr InnerWidget::setupList( + RpWidget *parent, + not_null controller) { + controller->setStyleOverrides(&st::infoMembersList); + auto result = object_ptr( + parent, + controller); + controller->setContentWidget(this); + result->scrollToRequests( + ) | rpl::start_with_next([this](Ui::ScrollToRequest request) { + auto addmin = (request.ymin < 0) + ? 0 + : st::infoCommonGroupsMargin.top(); + auto addmax = (request.ymax < 0) + ? 0 + : st::infoCommonGroupsMargin.top(); + _scrollToRequests.fire({ + request.ymin + addmin, + request.ymax + addmax }); + }, result->lifetime()); + result->moveToLeft(0, st::infoCommonGroupsMargin.top()); + parent->widthValue( + ) | rpl::start_with_next([list = result.data()](int newWidth) { + list->resizeToWidth(newWidth); + }, result->lifetime()); + rpl::combine( + result->heightValue(), + controller->unlockHeightValue() + ) | rpl::start_with_next([=](int listHeight, int unlockHeight) { + auto newHeight = st::infoCommonGroupsMargin.top() + + listHeight + + (unlockHeight + ? (unlockHeight - st::similarChannelsLockOverlap) + : st::infoCommonGroupsMargin.bottom()); + parent->resize(parent->width(), std::max(newHeight, 0)); + }, result->lifetime()); + return result; +} + +void InnerWidget::peerListSetTitle(rpl::producer title) { +} + +void InnerWidget::peerListSetAdditionalTitle(rpl::producer title) { +} + +bool InnerWidget::peerListIsRowChecked(not_null row) { + return false; +} + +int InnerWidget::peerListSelectedRowsCount() { + return 0; +} + +void InnerWidget::peerListScrollToTop() { + _scrollToRequests.fire({ -1, -1 }); +} + +void InnerWidget::peerListAddSelectedPeerInBunch(not_null peer) { + Unexpected("Item selection in Info::Profile::Members."); +} + +void InnerWidget::peerListAddSelectedRowInBunch(not_null row) { + Unexpected("Item selection in Info::Profile::Members."); +} + +void InnerWidget::peerListFinishSelectedRowsBunch() { +} + +void InnerWidget::peerListSetDescription( + object_ptr description) { + description.destroy(); +} + +std::shared_ptr InnerWidget::peerListUiShow() { + return _show; +} + +Memento::Memento(not_null channel) +: ContentMemento(channel, nullptr, PeerId()) { +} + +Section Memento::section() const { + return Section(Section::Type::SimilarChannels); +} + +not_null Memento::channel() const { + return peer()->asChannel(); +} + +object_ptr Memento::createWidget( + QWidget *parent, + not_null controller, + const QRect &geometry) { + auto result = object_ptr(parent, controller, channel()); + result->setInternalState(geometry, this); + return result; +} + +void Memento::setListState(std::unique_ptr state) { + _listState = std::move(state); +} + +std::unique_ptr Memento::listState() { + return std::move(_listState); +} + +Memento::~Memento() = default; + +Widget::Widget( + QWidget *parent, + not_null controller, + not_null channel) +: ContentWidget(parent, controller) { + _inner = setInnerWidget(object_ptr( + this, + controller, + channel)); +} + +rpl::producer Widget::title() { + return tr::lng_similar_channels_title(); +} + +not_null Widget::channel() const { + return _inner->channel(); +} + +bool Widget::showInternal(not_null memento) { + if (!controller()->validateMementoPeer(memento)) { + return false; + } + if (auto similarMemento = dynamic_cast(memento.get())) { + if (similarMemento->channel() == channel()) { + restoreState(similarMemento); + return true; + } + } + return false; +} + +void Widget::setInternalState( + const QRect &geometry, + not_null memento) { + setGeometry(geometry); + Ui::SendPendingMoveResizeEvents(this); + restoreState(memento); +} + +std::shared_ptr Widget::doCreateMemento() { + auto result = std::make_shared(channel()); + saveState(result.get()); + return result; +} + +void Widget::saveState(not_null memento) { + memento->setScrollTop(scrollTopSave()); + _inner->saveState(memento); +} + +void Widget::restoreState(not_null memento) { + _inner->restoreState(memento); + scrollTopRestore(memento->scrollTop()); +} + +} // namespace Info::SimilarChannels + diff --git a/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.h b/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.h new file mode 100644 index 000000000..7c32a855c --- /dev/null +++ b/Telegram/SourceFiles/info/similar_channels/info_similar_channels_widget.h @@ -0,0 +1,71 @@ +/* +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 "info/info_content_widget.h" + +class ChannelData; +struct PeerListState; + +namespace Info::SimilarChannels { + +class InnerWidget; + +class Memento final : public ContentMemento { +public: + explicit Memento(not_null channel); + + object_ptr createWidget( + QWidget *parent, + not_null controller, + const QRect &geometry) override; + + Section section() const override; + + [[nodiscard]] not_null channel() const; + + void setListState(std::unique_ptr state); + std::unique_ptr listState(); + + ~Memento(); + +private: + std::unique_ptr _listState; + +}; + +class Widget final : public ContentWidget { +public: + Widget( + QWidget *parent, + not_null controller, + not_null channel); + + [[nodiscard]] not_null channel() const; + + bool showInternal( + not_null memento) override; + + void setInternalState( + const QRect &geometry, + not_null memento); + + rpl::producer title() override; + +private: + void saveState(not_null memento); + void restoreState(not_null memento); + + std::shared_ptr doCreateMemento() override; + + InnerWidget *_inner = nullptr; + +}; + +} // namespace Info::SimilarChannels + diff --git a/Telegram/SourceFiles/window/window_session_controller.h b/Telegram/SourceFiles/window/window_session_controller.h index cd07d4a7b..33b3c44a5 100644 --- a/Telegram/SourceFiles/window/window_session_controller.h +++ b/Telegram/SourceFiles/window/window_session_controller.h @@ -160,6 +160,7 @@ struct SectionShow { anim::activation activation = anim::activation::normal; bool thirdColumn = false; bool childColumn = false; + bool forbidLayer = false; bool reapplyLocalDraft = false; Origin origin; diff --git a/Telegram/lib_ui b/Telegram/lib_ui index 66ca90c1b..44161f183 160000 --- a/Telegram/lib_ui +++ b/Telegram/lib_ui @@ -1 +1 @@ -Subproject commit 66ca90c1b2c111fe8ae45453c7bf180af2593d8d +Subproject commit 44161f183c255dc0dac7ebe9558a1ca48f5e5258