From 030d35ea7ea043216f82b7932a28745f2fd464df Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 21 May 2024 05:19:32 +0300 Subject: [PATCH] Added initial implementation of balance in credits settings. --- .../SourceFiles/settings/settings_credits.cpp | 69 ++++++++++++++++++- Telegram/SourceFiles/ui/effects/credits.style | 1 + .../ui/effects/premium_top_bar.cpp | 7 +- 3 files changed, 71 insertions(+), 6 deletions(-) diff --git a/Telegram/SourceFiles/settings/settings_credits.cpp b/Telegram/SourceFiles/settings/settings_credits.cpp index 2b6064abb..4ed6e8ac2 100644 --- a/Telegram/SourceFiles/settings/settings_credits.cpp +++ b/Telegram/SourceFiles/settings/settings_credits.cpp @@ -139,6 +139,7 @@ private: const not_null _controller; QImage _star; + QImage _balanceStar; base::unique_qptr> _back; base::unique_qptr _close; @@ -157,7 +158,8 @@ Credits::Credits( not_null controller) : Section(parent) , _controller(controller) -, _star(GenerateStars(st::creditsTopupButton.height, 1)) { +, _star(GenerateStars(st::creditsTopupButton.height, 1)) +, _balanceStar(GenerateStars(st::creditsBalanceStarHeight, 1)) { setupContent(); } @@ -514,6 +516,71 @@ 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 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()); + }; + api->request({}, [=](Data::CreditsStatusSlice slice) { + count->setText( + st::semiboldTextStyle, + Lang::FormatCountToShort(slice.balance).string); + resize(); + }); + 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() + ) | rpl::start_with_next([=](const QSize &, const QSize &) { + balance->moveToRight( + (_close + ? _close->width() + st::creditsHistoryRightSkip + : st::creditsHistoryRightSkip * 2), + st::creditsHistoryRightSkip); + balance->update(); + }, balance->lifetime()); + } + _wrap.value( ) | rpl::start_with_next([=](Info::Wrap wrap) { const auto isLayer = (wrap == Info::Wrap::Layer); diff --git a/Telegram/SourceFiles/ui/effects/credits.style b/Telegram/SourceFiles/ui/effects/credits.style index 355d0fc5b..8a1afec19 100644 --- a/Telegram/SourceFiles/ui/effects/credits.style +++ b/Telegram/SourceFiles/ui/effects/credits.style @@ -24,3 +24,4 @@ creditsTopupPrice: FlatLabel(defaultFlatLabel) { } creditsHistoryRightSkip: 10px; +creditsBalanceStarHeight: 20px; diff --git a/Telegram/SourceFiles/ui/effects/premium_top_bar.cpp b/Telegram/SourceFiles/ui/effects/premium_top_bar.cpp index fd3d11950..999a077d4 100644 --- a/Telegram/SourceFiles/ui/effects/premium_top_bar.cpp +++ b/Telegram/SourceFiles/ui/effects/premium_top_bar.cpp @@ -256,11 +256,8 @@ void TopBar::resizeEvent(QResizeEvent *e) { const auto progress = (max > min) ? ((e->size().height() - min) / float64(max - min)) : 1.; - _progress.top = 1. - - std::clamp( - (1. - progress) / kBodyAnimationPart, - 0., - 1.); + _progress.top = 1. + - std::clamp((1. - progress) / kBodyAnimationPart, 0., 1.); _progress.body = _progress.top; _progress.title = 1. - progress; _progress.scaleTitle = 1. + kTitleAdditionalScale * progress;