From a2a27e115cf9b315dd1c93f5b42831349c9e5080 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 May 2024 21:55:19 +0300 Subject: [PATCH] Moved out box for credits history entries to single place. --- .../SourceFiles/settings/settings_credits.cpp | 235 +++++++++--------- .../SourceFiles/settings/settings_credits.h | 17 ++ 2 files changed, 139 insertions(+), 113 deletions(-) diff --git a/Telegram/SourceFiles/settings/settings_credits.cpp b/Telegram/SourceFiles/settings/settings_credits.cpp index 1b9efbe073..c028600451 100644 --- a/Telegram/SourceFiles/settings/settings_credits.cpp +++ b/Telegram/SourceFiles/settings/settings_credits.cpp @@ -465,120 +465,12 @@ void Credits::setupHistory(not_null container) { }, inner->lifetime()); const auto controller = _controller->parentController(); - const auto entryBox = [=]( - not_null box, - const Data::CreditsHistoryEntry &e) { - box->setStyle(st::giveawayGiftCodeBox); - box->setNoContentMargin(true); - - const auto content = box->verticalLayout(); - Ui::AddSkip(content); - Ui::AddSkip(content); - Ui::AddSkip(content); - - using Type = Data::CreditsHistoryEntry::PeerType; - - const auto &stUser = st::boostReplaceUserpic; - const auto peer = (e.peerType == Type::PremiumBot) - ? premiumBot.get() - : e.bareId - ? _controller->session().data().peer(PeerId(e.bareId)).get() - : nullptr; - if (peer) { - content->add(object_ptr>( - content, - object_ptr(content, peer, stUser))); - } else { - const auto widget = content->add( - object_ptr>( - content, - object_ptr(content)))->entity(); - using Draw = Fn; - const auto draw = widget->lifetime().make_state( - Ui::GenerateCreditsPaintUserpicCallback(e)); - widget->resize(Size(stUser.photoSize)); - widget->paintRequest( - ) | rpl::start_with_next([=] { - auto p = Painter(widget); - (*draw)(p, 0, 0, stUser.photoSize, stUser.photoSize); - }, widget->lifetime()); - } - - Ui::AddSkip(content); - Ui::AddSkip(content); - - - box->addRow(object_ptr>( - box, - object_ptr( - box, - rpl::single(peer - ? peer->name() - : Ui::GenerateEntryName(e).text), - st::creditsBoxAboutTitle))); - - Ui::AddSkip(content); - - { - constexpr auto kMinus = QChar(0x2212); - auto &lifetime = content->lifetime(); - const auto text = lifetime.make_state( - st::semiboldTextStyle, - (!e.bareId ? QChar('+') : kMinus) - + Lang::FormatCountDecimal(e.credits)); - - const auto amount = content->add( - object_ptr( - content, - _star.height() / style::DevicePixelRatio())); - const auto font = text->style()->font; - amount->paintRequest( - ) | rpl::start_with_next([=] { - auto p = Painter(amount); - const auto starWidth = _star.width() - / style::DevicePixelRatio(); - const auto fullWidth = text->maxWidth() - + font->spacew * 2 - + starWidth; - p.setPen(!e.bareId - ? st::boxTextFgGood - : st::menuIconAttentionColor); - const auto x = (amount->width() - fullWidth) / 2; - text->draw(p, Ui::Text::PaintContext{ - .position = QPoint( - x, - (amount->height() - font->height) / 2), - .outerWidth = amount->width(), - .availableWidth = amount->width(), - });; - p.drawImage( - x + fullWidth - starWidth, - 0, - _star); - }, amount->lifetime()); - } - - Ui::AddSkip(content); - Ui::AddSkip(content); - - AddCreditsHistoryEntryTable( - controller, - box->verticalLayout(), - e); - - const auto button = box->addButton(tr::lng_box_ok(), [=] { - box->closeBox(); - }); - const auto buttonWidth = st::boxWidth - - rect::m::sum::h(st::giveawayGiftCodeBox.buttonPadding); - button->widthValue() | rpl::filter([=] { - return (button->widthNoMargins() != buttonWidth); - }) | rpl::start_with_next([=] { - button->resizeToWidth(buttonWidth); - }, button->lifetime()); - }; const auto entryClicked = [=](const Data::CreditsHistoryEntry &e) { - controller->uiShow()->show(Box(entryBox, e)); + controller->uiShow()->show(Box( + ReceiptCreditsBox, + controller, + premiumBot.get(), + e)); }; Info::Statistics::AddCreditsHistoryList( @@ -847,4 +739,121 @@ not_null AddBalanceWidget( return balance; } +void ReceiptCreditsBox( + not_null box, + not_null controller, + PeerData *premiumBot, + const Data::CreditsHistoryEntry &e) { + box->setStyle(st::giveawayGiftCodeBox); + box->setNoContentMargin(true); + + const auto star = GenerateStars(st::creditsTopupButton.height, 1); + + const auto content = box->verticalLayout(); + Ui::AddSkip(content); + Ui::AddSkip(content); + Ui::AddSkip(content); + + using Type = Data::CreditsHistoryEntry::PeerType; + + const auto &stUser = st::boostReplaceUserpic; + const auto peer = (e.peerType == Type::PremiumBot) + ? premiumBot + : e.bareId + ? controller->session().data().peer(PeerId(e.bareId)).get() + : nullptr; + if (peer) { + content->add(object_ptr>( + content, + object_ptr(content, peer, stUser))); + } else { + const auto widget = content->add( + object_ptr>( + content, + object_ptr(content)))->entity(); + using Draw = Fn; + const auto draw = widget->lifetime().make_state( + Ui::GenerateCreditsPaintUserpicCallback(e)); + widget->resize(Size(stUser.photoSize)); + widget->paintRequest( + ) | rpl::start_with_next([=] { + auto p = Painter(widget); + (*draw)(p, 0, 0, stUser.photoSize, stUser.photoSize); + }, widget->lifetime()); + } + + Ui::AddSkip(content); + Ui::AddSkip(content); + + + box->addRow(object_ptr>( + box, + object_ptr( + box, + rpl::single(peer + ? peer->name() + : Ui::GenerateEntryName(e).text), + st::creditsBoxAboutTitle))); + + Ui::AddSkip(content); + + { + constexpr auto kMinus = QChar(0x2212); + auto &lifetime = content->lifetime(); + const auto text = lifetime.make_state( + st::semiboldTextStyle, + (!e.bareId ? QChar('+') : kMinus) + + Lang::FormatCountDecimal(e.credits)); + + const auto amount = content->add( + object_ptr( + content, + star.height() / style::DevicePixelRatio())); + const auto font = text->style()->font; + amount->paintRequest( + ) | rpl::start_with_next([=] { + auto p = Painter(amount); + const auto starWidth = star.width() + / style::DevicePixelRatio(); + const auto fullWidth = text->maxWidth() + + font->spacew * 2 + + starWidth; + p.setPen(!e.bareId + ? st::boxTextFgGood + : st::menuIconAttentionColor); + const auto x = (amount->width() - fullWidth) / 2; + text->draw(p, Ui::Text::PaintContext{ + .position = QPoint( + x, + (amount->height() - font->height) / 2), + .outerWidth = amount->width(), + .availableWidth = amount->width(), + });; + p.drawImage( + x + fullWidth - starWidth, + 0, + star); + }, amount->lifetime()); + } + + Ui::AddSkip(content); + Ui::AddSkip(content); + + AddCreditsHistoryEntryTable( + controller, + box->verticalLayout(), + e); + + const auto button = box->addButton(tr::lng_box_ok(), [=] { + box->closeBox(); + }); + const auto buttonWidth = st::boxWidth + - rect::m::sum::h(st::giveawayGiftCodeBox.buttonPadding); + button->widthValue() | rpl::filter([=] { + return (button->widthNoMargins() != buttonWidth); + }) | rpl::start_with_next([=] { + button->resizeToWidth(buttonWidth); + }, button->lifetime()); +} + } // namespace Settings diff --git a/Telegram/SourceFiles/settings/settings_credits.h b/Telegram/SourceFiles/settings/settings_credits.h index 2267a0cd56..9f0d97ee04 100644 --- a/Telegram/SourceFiles/settings/settings_credits.h +++ b/Telegram/SourceFiles/settings/settings_credits.h @@ -9,7 +9,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_type.h" +class PeerData; + +namespace Data { +struct CreditsHistoryEntry; +} // namespace Data + +namespace Window { +class SessionController; +} // namespace Window + namespace Ui { +class GenericBox; class RpWidget; } // namespace Ui @@ -22,5 +33,11 @@ namespace Settings { rpl::producer balanceValue, bool rightAlign); +void ReceiptCreditsBox( + not_null box, + not_null controller, + PeerData *premiumBot, + const Data::CreditsHistoryEntry &e); + } // namespace Settings