From 081817f62a885d54cb54a06d804854288332f087 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sun, 17 Dec 2023 12:59:19 +0300 Subject: [PATCH] Moved out making of premium summary to separated function. --- .../SourceFiles/settings/settings_premium.cpp | 316 +++++++++--------- .../SourceFiles/settings/settings_premium.h | 6 + 2 files changed, 168 insertions(+), 154 deletions(-) diff --git a/Telegram/SourceFiles/settings/settings_premium.cpp b/Telegram/SourceFiles/settings/settings_premium.cpp index fd6c2322b..6c1cb52cd 100644 --- a/Telegram/SourceFiles/settings/settings_premium.cpp +++ b/Telegram/SourceFiles/settings/settings_premium.cpp @@ -919,164 +919,15 @@ void Premium::setupSubscriptionOptions( void Premium::setupContent() { const auto content = Ui::CreateChild(this); - const auto &stDefault = st::settingsButton; - const auto &stLabel = st::defaultFlatLabel; - const auto iconSize = st::settingsPremiumIconDouble.size(); - const auto &titlePadding = st::settingsPremiumRowTitlePadding; - const auto &descriptionPadding = st::settingsPremiumRowAboutPadding; - setupSubscriptionOptions(content); - auto entryMap = EntryMap(); - auto iconContainers = std::vector(); - iconContainers.reserve(int(entryMap.size())); + auto buttonCallback = [=](PremiumPreview section) { + _setPaused(true); + const auto hidden = crl::guard(this, [=] { _setPaused(false); }); - const auto addRow = [&](Entry &entry) { - const auto labelAscent = stLabel.style.font->ascent; - const auto button = Ui::CreateChild( - content, - rpl::single(QString())); - - const auto label = content->add( - object_ptr( - content, - std::move(entry.title) | rpl::map(Ui::Text::Bold), - stLabel), - titlePadding); - label->setAttribute(Qt::WA_TransparentForMouseEvents); - const auto description = content->add( - object_ptr( - content, - std::move(entry.description), - st::boxDividerLabel), - descriptionPadding); - description->setAttribute(Qt::WA_TransparentForMouseEvents); - - if (entry.newBadge) { - Ui::NewBadge::AddAfterLabel(content, label); - } - const auto dummy = Ui::CreateChild(content); - dummy->setAttribute(Qt::WA_TransparentForMouseEvents); - - content->sizeValue( - ) | rpl::start_with_next([=](const QSize &s) { - dummy->resize(s.width(), iconSize.height()); - }, dummy->lifetime()); - - label->geometryValue( - ) | rpl::start_with_next([=](const QRect &r) { - dummy->moveToLeft(0, r.y() + (r.height() - labelAscent)); - }, dummy->lifetime()); - - rpl::combine( - content->widthValue(), - label->heightValue(), - description->heightValue() - ) | rpl::start_with_next([=, - topPadding = titlePadding, - bottomPadding = descriptionPadding]( - int width, - int topHeight, - int bottomHeight) { - button->resize( - width, - topPadding.top() - + topHeight - + topPadding.bottom() - + bottomPadding.top() - + bottomHeight - + bottomPadding.bottom()); - }, button->lifetime()); - label->topValue( - ) | rpl::start_with_next([=, padding = titlePadding.top()](int top) { - button->moveToLeft(0, top - padding); - }, button->lifetime()); - const auto arrow = Ui::CreateChild( - button, - st::backButton); - arrow->setIconOverride( - &st::settingsPremiumArrow, - &st::settingsPremiumArrowOver); - arrow->setAttribute(Qt::WA_TransparentForMouseEvents); - button->sizeValue( - ) | rpl::start_with_next([=](const QSize &s) { - const auto &point = st::settingsPremiumArrowShift; - arrow->moveToRight( - -point.x(), - point.y() + (s.height() - arrow->height()) / 2); - }, arrow->lifetime()); - - const auto section = entry.section; - button->setClickedCallback([=, controller = _controller] { - _setPaused(true); - const auto hidden = crl::guard(this, [=] { - _setPaused(false); - }); - - ShowPremiumPreviewToBuy(controller, section, hidden); - }); - - iconContainers.push_back(dummy); + ShowPremiumPreviewToBuy(_controller, section, hidden); }; - - auto icons = std::vector(); - icons.reserve(int(entryMap.size())); - { - const auto &account = _controller->session().account(); - const auto mtpOrder = account.appConfig().get( - "premium_promo_order", - FallbackOrder()); - const auto processEntry = [&](Entry &entry) { - icons.push_back(entry.icon); - addRow(entry); - }; - - for (const auto &key : mtpOrder) { - auto it = entryMap.find(key); - if (it == end(entryMap)) { - continue; - } - processEntry(it->second); - } - - SendScreenShow(_controller, mtpOrder, _ref); - } - - content->resizeToWidth(content->height()); - - // Icons. - Assert(iconContainers.size() > 2); - const auto from = iconContainers.front()->y(); - const auto to = iconContainers.back()->y() + iconSize.height(); - auto gradient = QLinearGradient(0, 0, 0, to - from); - gradient.setStops(Ui::Premium::FullHeightGradientStops()); - for (auto i = 0; i < int(icons.size()); i++) { - const auto &iconContainer = iconContainers[i]; - - const auto pointTop = iconContainer->y() - from; - const auto pointBottom = pointTop + iconContainer->height(); - const auto ratioTop = pointTop / float64(to - from); - const auto ratioBottom = pointBottom / float64(to - from); - - auto resultGradient = QLinearGradient( - QPointF(), - QPointF(0, pointBottom - pointTop)); - - resultGradient.setColorAt( - .0, - anim::gradient_color_at(gradient, ratioTop)); - resultGradient.setColorAt( - .1, - anim::gradient_color_at(gradient, ratioBottom)); - - const auto brush = QBrush(resultGradient); - AddButtonIcon( - iconContainer, - stDefault, - { .icon = icons[i], .backgroundBrush = brush }); - } - - Ui::AddSkip(content, descriptionPadding.bottom()); + AddSummaryPremium(content, _controller, _ref, std::move(buttonCallback)); #if 0 Ui::AddSkip(content); Ui::AddDivider(content); @@ -1657,4 +1508,161 @@ not_null CreateSubscribeButton( }) | ranges::to_vector; } +void AddSummaryPremium( + not_null content, + not_null controller, + const QString &ref, + Fn buttonCallback) { + const auto &stDefault = st::settingsButton; + const auto &stLabel = st::defaultFlatLabel; + const auto iconSize = st::settingsPremiumIconDouble.size(); + const auto &titlePadding = st::settingsPremiumRowTitlePadding; + const auto &descriptionPadding = st::settingsPremiumRowAboutPadding; + + auto entryMap = EntryMap(); + auto iconContainers = std::vector(); + iconContainers.reserve(int(entryMap.size())); + + const auto addRow = [&](Entry &entry) { + const auto labelAscent = stLabel.style.font->ascent; + const auto button = Ui::CreateChild( + content.get(), + rpl::single(QString())); + + const auto label = content->add( + object_ptr( + content, + std::move(entry.title) | rpl::map(Ui::Text::Bold), + stLabel), + titlePadding); + label->setAttribute(Qt::WA_TransparentForMouseEvents); + const auto description = content->add( + object_ptr( + content, + std::move(entry.description), + st::boxDividerLabel), + descriptionPadding); + description->setAttribute(Qt::WA_TransparentForMouseEvents); + + if (entry.newBadge) { + Ui::NewBadge::AddAfterLabel(content, label); + } + const auto dummy = Ui::CreateChild(content.get()); + dummy->setAttribute(Qt::WA_TransparentForMouseEvents); + + content->sizeValue( + ) | rpl::start_with_next([=](const QSize &s) { + dummy->resize(s.width(), iconSize.height()); + }, dummy->lifetime()); + + label->geometryValue( + ) | rpl::start_with_next([=](const QRect &r) { + dummy->moveToLeft(0, r.y() + (r.height() - labelAscent)); + }, dummy->lifetime()); + + rpl::combine( + content->widthValue(), + label->heightValue(), + description->heightValue() + ) | rpl::start_with_next([=, + topPadding = titlePadding, + bottomPadding = descriptionPadding]( + int width, + int topHeight, + int bottomHeight) { + button->resize( + width, + topPadding.top() + + topHeight + + topPadding.bottom() + + bottomPadding.top() + + bottomHeight + + bottomPadding.bottom()); + }, button->lifetime()); + label->topValue( + ) | rpl::start_with_next([=, padding = titlePadding.top()](int top) { + button->moveToLeft(0, top - padding); + }, button->lifetime()); + const auto arrow = Ui::CreateChild( + button, + st::backButton); + arrow->setIconOverride( + &st::settingsPremiumArrow, + &st::settingsPremiumArrowOver); + arrow->setAttribute(Qt::WA_TransparentForMouseEvents); + button->sizeValue( + ) | rpl::start_with_next([=](const QSize &s) { + const auto &point = st::settingsPremiumArrowShift; + arrow->moveToRight( + -point.x(), + point.y() + (s.height() - arrow->height()) / 2); + }, arrow->lifetime()); + + const auto section = entry.section; + button->setClickedCallback([=] { buttonCallback(section); }); + + iconContainers.push_back(dummy); + }; + + auto icons = std::vector(); + icons.reserve(int(entryMap.size())); + { + const auto &account = controller->session().account(); + const auto mtpOrder = account.appConfig().get( + "premium_promo_order", + FallbackOrder()); + const auto processEntry = [&](Entry &entry) { + icons.push_back(entry.icon); + addRow(entry); + }; + + for (const auto &key : mtpOrder) { + auto it = entryMap.find(key); + if (it == end(entryMap)) { + continue; + } + processEntry(it->second); + } + + SendScreenShow(controller, mtpOrder, ref); + } + + content->resizeToWidth(content->height()); + + // Icons. + Assert(iconContainers.size() > 2); + const auto from = iconContainers.front()->y(); + const auto to = iconContainers.back()->y() + iconSize.height(); + auto gradient = QLinearGradient(0, 0, 0, to - from); + gradient.setStops(Ui::Premium::FullHeightGradientStops()); + for (auto i = 0; i < int(icons.size()); i++) { + const auto &iconContainer = iconContainers[i]; + + const auto pointTop = iconContainer->y() - from; + const auto pointBottom = pointTop + iconContainer->height(); + const auto ratioTop = pointTop / float64(to - from); + const auto ratioBottom = pointBottom / float64(to - from); + + auto resultGradient = QLinearGradient( + QPointF(), + QPointF(0, pointBottom - pointTop)); + + resultGradient.setColorAt( + .0, + anim::gradient_color_at(gradient, ratioTop)); + resultGradient.setColorAt( + .1, + anim::gradient_color_at(gradient, ratioBottom)); + + const auto brush = QBrush(resultGradient); + AddButtonIcon( + iconContainer, + stDefault, + { .icon = icons[i], .backgroundBrush = brush }); + } + + Ui::AddSkip(content, descriptionPadding.bottom()); + +} + } // namespace Settings diff --git a/Telegram/SourceFiles/settings/settings_premium.h b/Telegram/SourceFiles/settings/settings_premium.h index 44d9c8ea4..52a9e2b9e 100644 --- a/Telegram/SourceFiles/settings/settings_premium.h +++ b/Telegram/SourceFiles/settings/settings_premium.h @@ -23,6 +23,7 @@ namespace Ui { class RpWidget; class RoundButton; class GradientButton; +class VerticalLayout; } // namespace Ui namespace Main { @@ -84,6 +85,11 @@ struct SubscribeButtonArgs final { [[nodiscard]] std::vector PremiumPreviewOrder( not_null<::Main::Session*> session); +void AddSummaryPremium( + not_null content, + not_null controller, + const QString &ref, + Fn buttonCallback); } // namespace Settings