From 457301493f525c859e7692df9ac1e88c714248b0 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Sat, 10 Aug 2024 11:40:34 +0300 Subject: [PATCH] Moved out drawing of label for subscription rows to single place. --- .../info_statistics_list_controllers.cpp | 61 +++++++------------ .../settings/settings_credits_graphics.cpp | 45 ++++++++++++++ .../settings/settings_credits_graphics.h | 14 +++++ 3 files changed, 81 insertions(+), 39 deletions(-) diff --git a/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp b/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp index 9ca8384f5..fdc561491 100644 --- a/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp +++ b/Telegram/SourceFiles/info/statistics/info_statistics_list_controllers.cpp @@ -22,6 +22,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "main/main_session.h" #include "main/session/session_show.h" +#include "settings/settings_credits_graphics.h" // PaintSubscriptionRightLabelCallback #include "ui/effects/credits_graphics.h" #include "ui/effects/outline_segments.h" // Ui::UnreadStoryOutlineGradient. #include "ui/effects/toggle_arrow.h" @@ -758,6 +759,7 @@ private: const int _rowHeight; PaintRoundImageCallback _paintUserpicCallback; + std::optional _rightLabel; QString _title; QString _name; @@ -782,6 +784,14 @@ CreditsRow::CreditsRow( &_guard, [this, update = descriptor.updateCallback] { update(this); })); } + if (!_subscription.cancelled + && !_subscription.expired + && _subscription.subscription) { + _rightLabel = Settings::PaintSubscriptionRightLabelCallback( + &peer->session(), + st::boostsListBox.item, + _subscription.subscription.credits); + } init(); } @@ -843,14 +853,6 @@ void CreditsRow::init() { .append(manager.creditsEmoji()), kMarkupTextOptions, _context); - } else if (_subscription.subscription.credits && !isSpecial) { - const auto peer = PeerListRow::peer(); - _rightText.setMarkedText( - st::semiboldTextStyle, - manager.creditsEmoji().append( - Lang::FormatCountDecimal(_subscription.subscription.credits)), - kMarkupTextOptions, - _context); } if (!_paintUserpicCallback) { _paintUserpicCallback = !isSpecial @@ -876,7 +878,9 @@ PaintRoundImageCallback CreditsRow::generatePaintUserpicCallback(bool force) { } QSize CreditsRow::rightActionSize() const { - if (_subscription.cancelled || _subscription.expired) { + if (_rightLabel) { + return _rightLabel->size; + } else if (_subscription.cancelled || _subscription.expired) { const auto text = _subscription.cancelled ? tr::lng_credits_subscription_status_off_right(tr::now) : tr::lng_credits_subscription_status_none_right(tr::now); @@ -910,41 +914,20 @@ void CreditsRow::rightActionPaint( bool actionSelected) { const auto &font = _rightText.style()->font; const auto rightSkip = st::boxRowPadding.right(); - if (_subscription) { + if (_rightLabel) { + return _rightLabel->draw(p, x, y, _rowHeight); + } else if (_subscription.cancelled || _subscription.expired) { const auto &statusFont = st::contactsStatusFont; - const auto &st = st::boostsListBox.item; - const auto textHeight = font->height + statusFont->height; - const auto skip = (_rowHeight - textHeight) / 2; - if (_subscription.cancelled || _subscription.expired) { - y += _rowHeight / 2; - p.setFont(statusFont); - p.setPen(st::attentionButtonFg); - p.drawTextRight( - rightSkip, - y - statusFont->height / 2, - outerWidth, - _subscription.expired - ? tr::lng_credits_subscription_status_none_right(tr::now) - : tr::lng_credits_subscription_status_off_right(tr::now)); - return; - } - - p.setPen(st.statusFg); + y += _rowHeight / 2; p.setFont(statusFont); + p.setPen(st::attentionButtonFg); p.drawTextRight( rightSkip, - y + _rowHeight - skip - statusFont->height, + y - statusFont->height / 2, outerWidth, - tr::lng_group_invite_joined_right(tr::now)); - - p.setPen(st.nameFg); - _rightText.draw(p, Ui::Text::PaintContext{ - .position = QPoint( - outerWidth - _rightText.maxWidth() - rightSkip, - y + skip), - .outerWidth = outerWidth, - .availableWidth = outerWidth, - }); + _subscription.expired + ? tr::lng_credits_subscription_status_none_right(tr::now) + : tr::lng_credits_subscription_status_off_right(tr::now)); return; } y += _rowHeight / 2; diff --git a/Telegram/SourceFiles/settings/settings_credits_graphics.cpp b/Telegram/SourceFiles/settings/settings_credits_graphics.cpp index 07be46a92..0a32d3f9a 100644 --- a/Telegram/SourceFiles/settings/settings_credits_graphics.cpp +++ b/Telegram/SourceFiles/settings/settings_credits_graphics.cpp @@ -233,6 +233,51 @@ void AddViewMediaHandler( } // namespace +SubscriptionRightLabel PaintSubscriptionRightLabelCallback( + not_null session, + const style::PeerListItem &st, + int amount) { + const auto text = std::make_shared(); + text->setMarkedText( + st::semiboldTextStyle, + TextWithEntities() + .append(session->data().customEmojiManager().creditsEmoji()) + .append(QChar::Space) + .append(Lang::FormatCountDecimal(amount)), + kMarkupTextOptions, + Core::MarkedTextContext{ + .session = session, + .customEmojiRepaint = [] {}, + }); + const auto &font = text->style()->font; + const auto &statusFont = st::contactsStatusFont; + const auto status = tr::lng_group_invite_joined_right(tr::now); + const auto rightSkip = st::boxRowPadding.right(); + const auto statusWidth = statusFont->width(status); + const auto size = QSize( + std::max(text->maxWidth(), statusWidth) + rightSkip, + font->height + statusFont->height); + const auto statusX = size.width() - statusWidth; + auto draw = [=](QPainter &p, int x, int y, int h) { + p.setPen(st.statusFg); + p.setFont(statusFont); + const auto skip = y + (h - size.height()) / 2; + p.drawText( + x + statusX, + font->height + statusFont->ascent + skip, + status); + + p.setPen(st.nameFg); + const auto textWidth = text->maxWidth(); + text->draw(p, Ui::Text::PaintContext{ + .position = QPoint(x + size.width() - textWidth, skip), + .outerWidth = textWidth, + .availableWidth = textWidth, + }); + }; + return { std::move(draw), size }; +} + void FillCreditOptions( std::shared_ptr show, not_null container, diff --git a/Telegram/SourceFiles/settings/settings_credits_graphics.h b/Telegram/SourceFiles/settings/settings_credits_graphics.h index 785a7635f..2978a87aa 100644 --- a/Telegram/SourceFiles/settings/settings_credits_graphics.h +++ b/Telegram/SourceFiles/settings/settings_credits_graphics.h @@ -18,6 +18,7 @@ struct SubscriptionEntry; } // namespace Data namespace Main { +class Session; class SessionShow; } // namespace Main @@ -25,6 +26,10 @@ namespace Window { class SessionController; } // namespace Window +namespace style { +struct PeerListItem; +} // namespace style + namespace Ui { class GenericBox; class RpWidget; @@ -33,6 +38,15 @@ class VerticalLayout; namespace Settings { +struct SubscriptionRightLabel { + Fn draw; + QSize size; +}; +SubscriptionRightLabel PaintSubscriptionRightLabelCallback( + not_null session, + const style::PeerListItem &st, + int amount); + void FillCreditOptions( std::shared_ptr show, not_null container,