From 53a2d4704d67cf8bb8cb0504d8f3aac9ba8337d9 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 16 Mar 2026 15:59:50 +0400 Subject: [PATCH] Improve giveaway prices display. --- Telegram/SourceFiles/api/api_premium.cpp | 3 ++ .../SourceFiles/api/api_premium_option.cpp | 22 +++++++------ Telegram/SourceFiles/api/api_premium_option.h | 6 ++-- .../ui/effects/premium_graphics.cpp | 31 +++++++++++-------- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Telegram/SourceFiles/api/api_premium.cpp b/Telegram/SourceFiles/api/api_premium.cpp index f628d46e88..215c9640f5 100644 --- a/Telegram/SourceFiles/api/api_premium.cpp +++ b/Telegram/SourceFiles/api/api_premium.cpp @@ -56,6 +56,9 @@ namespace { + QChar(0x00D7) + ' ' + QString::number(tlOption.vusers().v); + options[i].total = Ui::FillAmountAndCurrency( + tlOption.vamount().v, + currency); options[i].currency = currency; } return options; diff --git a/Telegram/SourceFiles/api/api_premium_option.cpp b/Telegram/SourceFiles/api/api_premium_option.cpp index 5cc9c9a125..3acd9aca2b 100644 --- a/Telegram/SourceFiles/api/api_premium_option.cpp +++ b/Telegram/SourceFiles/api/api_premium_option.cpp @@ -15,29 +15,33 @@ constexpr auto kDiscountDivider = 1.; Data::PremiumSubscriptionOption CreateSubscriptionOption( int months, - int monthlyAmount, + float64 monthlyAmount, int64 amount, const QString ¤cy, const QString &botUrl) { + const auto baselineAmount = monthlyAmount * months; const auto discount = [&] { - const auto percent = 1. - float64(amount) / (monthlyAmount * months); - return std::round(percent * 100. / kDiscountDivider) + const auto percent = 1. - float64(amount) / baselineAmount; + return base::SafeRound(percent * 100. / kDiscountDivider) * kDiscountDivider; }(); + const auto hasDiscount = (discount > 0); return { .months = months, .duration = Ui::FormatTTL(months * 86400 * 31), - .discount = (discount > 0) + .discount = hasDiscount ? QString::fromUtf8("\xe2\x88\x92%1%").arg(discount) : QString(), .costPerMonth = Ui::FillAmountAndCurrency( - amount / float64(months), - currency), - .costNoDiscount = Ui::FillAmountAndCurrency( - monthlyAmount * months, + int64(base::SafeRound(amount / float64(months))), currency), + .costNoDiscount = hasDiscount + ? Ui::FillAmountAndCurrency( + int64(base::SafeRound(baselineAmount)), + currency) + : QString(), .costPerYear = Ui::FillAmountAndCurrency( - amount / float64(months / 12.), + int64(base::SafeRound(amount / float64(months / 12.))), currency), .botUrl = botUrl, }; diff --git a/Telegram/SourceFiles/api/api_premium_option.h b/Telegram/SourceFiles/api/api_premium_option.h index 2648a7f9c7..4b265f1fb3 100644 --- a/Telegram/SourceFiles/api/api_premium_option.h +++ b/Telegram/SourceFiles/api/api_premium_option.h @@ -13,7 +13,7 @@ namespace Api { [[nodiscard]] Data::PremiumSubscriptionOption CreateSubscriptionOption( int months, - int monthlyAmount, + float64 monthlyAmount, int64 amount, const QString ¤cy, const QString &botUrl); @@ -24,9 +24,9 @@ template if (tlOpts.isEmpty()) { return {}; } - auto monthlyAmountPerCurrency = base::flat_map(); + auto monthlyAmountPerCurrency = base::flat_map(); auto result = Data::PremiumSubscriptionOptions(); - const auto monthlyAmount = [&](const QString ¤cy) -> int { + const auto monthlyAmount = [&](const QString ¤cy) -> float64 { const auto it = monthlyAmountPerCurrency.find(currency); if (it != end(monthlyAmountPerCurrency)) { return it->second; diff --git a/Telegram/SourceFiles/ui/effects/premium_graphics.cpp b/Telegram/SourceFiles/ui/effects/premium_graphics.cpp index ae99fe0a55..cb967aab2c 100644 --- a/Telegram/SourceFiles/ui/effects/premium_graphics.cpp +++ b/Telegram/SourceFiles/ui/effects/premium_graphics.cpp @@ -938,24 +938,30 @@ void AddGiftOptions( const auto costPerMonthIcon = info.costPerMonth.startsWith(kStar) ? GenerateStars(costPerMonthFont->height, 1) : QImage(); + auto leftText = TextWithEntities(); + if (!info.costNoDiscount.isEmpty()) { + leftText.append(Ui::Text::Wrapped( + TextWithEntities{ info.costNoDiscount }, + EntityType::StrikeOut)); + leftText.append(' '); + } + leftText.append(costPerMonthIcon.isNull() + ? info.costPerMonth + : removedStar(info.costPerMonth)); const auto costPerMonthLabel = row->lifetime().make_state(); costPerMonthLabel->setMarkedText( st::shareBoxListItem.nameStyle, - TextWithEntities() - .append(Ui::Text::Wrapped( - TextWithEntities{ info.costNoDiscount }, - EntityType::StrikeOut)) - .append(' ') - .append(costPerMonthIcon.isNull() - ? info.costPerMonth - : removedStar(info.costPerMonth))); + std::move(leftText)); + const auto rightText = info.total.isEmpty() + ? info.costPerYear + : info.total; const auto costPerYearEntry = [&] { - if (!info.costPerYear.startsWith(kStar)) { + if (!rightText.startsWith(kStar)) { return QImage(); } - const auto text = removedStar(info.costPerYear); + const auto text = removedStar(rightText); const auto icon = GenerateStars(costPerYearFont->height, 1); auto result = QImage( QSize(costPerYearFont->spacew + costPerYearFont->width(text), 0) @@ -969,7 +975,6 @@ void AddGiftOptions( p.drawImage(0, 0, icon); p.setPen(st::windowSubTextFg); p.setFont(costPerYearFont); - auto copy = info.costPerYear; p.drawText( Rect(result.size() / style::DevicePixelRatio()), text, @@ -1078,7 +1083,7 @@ void AddGiftOptions( + costPerMonthIcon.width() / style::DevicePixelRatio()); const auto costPerYearWidth = costPerYearFont->width( - info.costPerYear); + rightText); const auto pos = perRect.translated(left, 0).topLeft(); const auto availableWidth = row->width() - pos.x() @@ -1096,7 +1101,7 @@ void AddGiftOptions( - QMargins(0, 0, st.rowMargins.right(), 0); if (costPerYearEntry.isNull()) { p.setFont(costPerYearFont); - p.drawText(totalRect, info.costPerYear, style::al_right); + p.drawText(totalRect, rightText, style::al_right); } else { const auto size = costPerYearEntry.size() / style::DevicePixelRatio();