From 69c48e2b5b8ad096e238ad473c66306e752004ca Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 22 May 2024 21:47:36 +0300 Subject: [PATCH] Moved out credits balance widget to single place. --- .../SourceFiles/settings/settings_credits.cpp | 116 ++++++++++-------- .../SourceFiles/settings/settings_credits.h | 28 +---- 2 files changed, 70 insertions(+), 74 deletions(-) diff --git a/Telegram/SourceFiles/settings/settings_credits.cpp b/Telegram/SourceFiles/settings/settings_credits.cpp index 1793d07ace..a1fde01f49 100644 --- a/Telegram/SourceFiles/settings/settings_credits.cpp +++ b/Telegram/SourceFiles/settings/settings_credits.cpp @@ -562,63 +562,16 @@ QPointer Credits::createPinnedToTop( } }, content->lifetime()); - const auto balance = Ui::CreateChild(content); { - const auto starSize = _balanceStar.size() / style::DevicePixelRatio(); - const auto label = balance->lifetime().make_state( - st::defaultTextStyle, - tr::lng_credits_summary_balance(tr::now)); - const auto count = balance->lifetime().make_state( - st::semiboldTextStyle, - tr::lng_contacts_loading(tr::now)); + const auto balance = AddBalanceWidget( + content, + _controller->session().creditsValue(), + true); const auto api = balance->lifetime().make_state( _controller->session().user()); - const auto diffBetweenStarAndCount = count->style()->font->spacew; - const auto resize = [=] { - balance->resize( - std::max( - label->maxWidth(), - count->maxWidth() - + starSize.width() - + diffBetweenStarAndCount), - label->style()->font->height + starSize.height()); - }; - _controller->session().creditsValue( - ) | rpl::start_with_next([=](uint64 value) { - count->setText( - st::semiboldTextStyle, - Lang::FormatCountToShort(value).string); - balance->setBalance(value); - resize(); - }, balance->lifetime()); api->request({}, [=](Data::CreditsStatusSlice slice) { _controller->session().setCredits(slice.balance); }); - balance->paintRequest( - ) | rpl::start_with_next([=] { - auto p = QPainter(balance); - - p.setPen(st::boxTextFg); - - label->draw(p, { - .position = QPoint(balance->width() - label->maxWidth(), 0), - .availableWidth = balance->width(), - }); - count->draw(p, { - .position = QPoint( - balance->width() - count->maxWidth(), - label->minHeight() - + (starSize.height() - count->minHeight()) / 2), - .availableWidth = balance->width(), - }); - p.drawImage( - balance->width() - - count->maxWidth() - - starSize.width() - - diffBetweenStarAndCount, - label->minHeight(), - _balanceStar); - }, balance->lifetime()); rpl::combine( balance->sizeValue(), content->sizeValue() @@ -706,4 +659,65 @@ Type CreditsId() { return Credits::Id(); } +not_null AddBalanceWidget( + not_null parent, + rpl::producer balanceValue, + bool rightAlign) { + const auto balance = Ui::CreateChild(parent); + const auto balanceStar = balance->lifetime().make_state( + GenerateStars(st::creditsBalanceStarHeight, 1)); + const auto starSize = balanceStar->size() / style::DevicePixelRatio(); + const auto label = balance->lifetime().make_state( + st::defaultTextStyle, + tr::lng_credits_summary_balance(tr::now)); + const auto count = balance->lifetime().make_state( + st::semiboldTextStyle, + tr::lng_contacts_loading(tr::now)); + const auto diffBetweenStarAndCount = count->style()->font->spacew; + const auto resize = [=] { + balance->resize( + std::max( + label->maxWidth(), + count->maxWidth() + + starSize.width() + + diffBetweenStarAndCount), + label->style()->font->height + starSize.height()); + }; + std::move(balanceValue) | rpl::start_with_next([=](uint64 value) { + count->setText( + st::semiboldTextStyle, + Lang::FormatCountToShort(value).string); + balance->setBalance(value); + resize(); + }, balance->lifetime()); + balance->paintRequest( + ) | rpl::start_with_next([=] { + auto p = QPainter(balance); + + p.setPen(st::boxTextFg); + + label->draw(p, { + .position = QPoint( + rightAlign ? (balance->width() - label->maxWidth()) : 0, + 0), + .availableWidth = balance->width(), + }); + count->draw(p, { + .position = QPoint( + balance->width() - count->maxWidth(), + label->minHeight() + + (starSize.height() - count->minHeight()) / 2), + .availableWidth = balance->width(), + }); + p.drawImage( + balance->width() + - count->maxWidth() + - starSize.width() + - diffBetweenStarAndCount, + label->minHeight(), + *balanceStar); + }, balance->lifetime()); + return balance; +} + } // namespace Settings diff --git a/Telegram/SourceFiles/settings/settings_credits.h b/Telegram/SourceFiles/settings/settings_credits.h index 7484fa6373..2267a0cd56 100644 --- a/Telegram/SourceFiles/settings/settings_credits.h +++ b/Telegram/SourceFiles/settings/settings_credits.h @@ -9,36 +9,18 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_type.h" -enum class PremiumFeature; - -namespace style { -struct RoundButton; -} // namespace style - -namespace ChatHelpers { -class Show; -enum class WindowUsage; -} // namespace ChatHelpers - namespace Ui { class RpWidget; -class RoundButton; -class GradientButton; -class VerticalLayout; } // namespace Ui -namespace Main { -class Session; -class SessionShow; -} // namespace Main - -namespace Window { -class SessionController; -} // namespace Window - namespace Settings { [[nodiscard]] Type CreditsId(); +[[nodiscard]] not_null AddBalanceWidget( + not_null parent, + rpl::producer balanceValue, + bool rightAlign); + } // namespace Settings