diff --git a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp index a9b892ae6..3ad60a265 100644 --- a/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp +++ b/Telegram/SourceFiles/boxes/peers/edit_peer_info_box.cpp @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "api/api_credits.h" #include "api/api_peer_photo.h" +#include "api/api_statistics.h" #include "api/api_user_names.h" #include "main/main_session.h" #include "ui/boxes/confirm_box.h" @@ -46,7 +47,9 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/admin_log/history_admin_log_section.h" #include "info/bot/earn/info_bot_earn_widget.h" #include "info/channel_statistics/boosts/info_boosts_widget.h" +#include "info/channel_statistics/earn/earn_format.h" #include "info/channel_statistics/earn/earn_icons.h" +#include "info/channel_statistics/earn/info_channel_earn_widget.h" #include "info/profile/info_profile_values.h" #include "info/info_memento.h" #include "lang/lang_keys.h" @@ -353,7 +356,8 @@ private: void fillPendingRequestsButton(); void fillBotUsernamesButton(); - void fillBotBalanceButton(); + void fillBotCurrencyButton(); + void fillBotCreditsButton(); void fillBotEditIntroButton(); void fillBotEditCommandsButton(); void fillBotEditSettingsButton(); @@ -1175,7 +1179,8 @@ void Controller::fillManageSection() { ::AddSkip(container, 0); fillBotUsernamesButton(); - fillBotBalanceButton(); + fillBotCurrencyButton(); + fillBotCreditsButton(); fillBotEditIntroButton(); fillBotEditCommandsButton(); fillBotEditSettingsButton(); @@ -1584,7 +1589,72 @@ void Controller::fillBotUsernamesButton() { { &st::menuIconLinks }); } -void Controller::fillBotBalanceButton() { +void Controller::fillBotCurrencyButton() { + Expects(_isBot); + + struct State final { + rpl::variable balance; + }; + + auto &lifetime = _controls.buttonsLayout->lifetime(); + const auto state = lifetime.make_state(); + const auto format = [=](uint64 balance) { + return Info::ChannelEarn::MajorPart(balance) + + Info::ChannelEarn::MinorPart(balance); + }; + const auto was = _peer->session().credits().balanceCurrency( + _peer->id); + if (was) { + state->balance = format(was); + } + + const auto wrap = _controls.buttonsLayout->add( + object_ptr>( + _controls.buttonsLayout, + EditPeerInfoBox::CreateButton( + _controls.buttonsLayout, + tr::lng_manage_peer_bot_balance_currency(), + state->balance.value(), + [controller = _navigation->parentController(), peer = _peer] { + controller->showSection(Info::ChannelEarn::Make(peer)); + }, + st::manageGroupButton, + {}))); + wrap->toggle(!state->balance.current().isEmpty(), anim::type::instant); + + const auto button = wrap->entity(); + { + const auto currencyLoad + = button->lifetime().make_state(_peer); + currencyLoad->request( + ) | rpl::start_with_error_done([=](const QString &error) { + }, [=] { + const auto balance = currencyLoad->data().currentBalance; + if (balance) { + wrap->toggle(true, anim::type::normal); + } + state->balance = format(balance); + }, button->lifetime()); + } + { + const auto icon = Ui::CreateChild(button); + icon->resize(st::menuIconLinks.size()); + const auto image = Ui::Earn::MenuIconCurrency(icon->size()); + icon->paintRequest() | rpl::start_with_next([=] { + auto p = QPainter(icon); + p.drawImage(0, 0, image); + }, icon->lifetime()); + + button->sizeValue( + ) | rpl::start_with_next([=](const QSize &size) { + icon->moveToLeft( + button->st().iconLeft, + (size.height() - icon->height()) / 2); + }, icon->lifetime()); + } +} + +void Controller::fillBotCreditsButton() { Expects(_isBot); struct State final { @@ -1602,7 +1672,7 @@ void Controller::fillBotBalanceButton() { _controls.buttonsLayout, EditPeerInfoBox::CreateButton( _controls.buttonsLayout, - tr::lng_manage_peer_bot_balance(), + tr::lng_manage_peer_bot_balance_credits(), state->balance.value(), [controller = _navigation->parentController(), peer = _peer] { controller->showSection(Info::BotEarn::Make(peer)); diff --git a/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.cpp b/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.cpp index cc32bf31f..0a57f9288 100644 --- a/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.cpp +++ b/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/rect.h" #include "styles/style_menu_icons.h" #include "styles/style_widgets.h" +#include "styles/style_info.h" // infoIconReport. #include #include @@ -62,6 +63,37 @@ QImage IconCurrencyColored( return image; } +QImage MenuIconCurrency(const QSize &size) { + auto image = QImage( + size * style::DevicePixelRatio(), + QImage::Format_ARGB32_Premultiplied); + image.setDevicePixelRatio(style::DevicePixelRatio()); + image.fill(Qt::transparent); + auto p = QPainter(&image); + st::infoIconReport.paintInCenter( + p, + Rect(size), + st::infoIconFg->c); + p.setCompositionMode(QPainter::CompositionMode_Clear); + const auto w = st::lineWidth * 6; + p.fillRect( + QRect( + rect::center(Rect(size)).x() - w / 2, + rect::center(Rect(size)).y() - w, + w, + w * 2), + Qt::white); + p.setCompositionMode(QPainter::CompositionMode_SourceOver); + const auto i = IconCurrencyColored( + st::inviteLinkSubscribeBoxTerms.style.font, + st::infoIconFg->c); + p.drawImage( + (size.width() - i.width() / style::DevicePixelRatio()) / 2, + (size.height() - i.height() / style::DevicePixelRatio()) / 2, + i); + return image; +} + QImage MenuIconCredits() { constexpr auto kStrokeWidth = 5; const auto sizeShift = st::lineWidth * 1.5; diff --git a/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.h b/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.h index 72817de9d..ccc2ca7f8 100644 --- a/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.h +++ b/Telegram/SourceFiles/info/channel_statistics/earn/earn_icons.h @@ -13,6 +13,7 @@ namespace Ui::Earn { const style::font &font, const QColor &c); +[[nodiscard]] QImage MenuIconCurrency(const QSize &size); [[nodiscard]] QImage MenuIconCredits(); } // namespace Ui::Earn diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 6ba286fe7..5a44d9916 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -795,39 +795,9 @@ rpl::producer AddCurrencyAction( const auto button = wrapButton->entity(); const auto icon = Ui::CreateChild(button); icon->resize(st::infoIconReport.size()); - const auto image = [&] { - auto image = QImage( - icon->size() * style::DevicePixelRatio(), - QImage::Format_ARGB32_Premultiplied); - image.setDevicePixelRatio(style::DevicePixelRatio()); - image.fill(Qt::transparent); - auto p = QPainter(&image); - st::infoIconReport.paintInCenter( - p, - icon->rect(), - st::infoIconFg->c); - p.setCompositionMode(QPainter::CompositionMode_Clear); - const auto w = st::lineWidth * 6; - p.fillRect( - QRect( - rect::center(icon->rect()).x() - w / 2, - rect::center(icon->rect()).y() - w, - w, - w * 2), - Qt::white); - p.setCompositionMode(QPainter::CompositionMode_SourceOver); - const auto i = Ui::Earn::IconCurrencyColored( - st::inviteLinkSubscribeBoxTerms.style.font, - st::infoIconFg->c); - p.drawImage( - (icon->width() - i.width() / style::DevicePixelRatio()) / 2, - (icon->height() - i.height() / style::DevicePixelRatio()) / 2, - i); - return image; - }(); + const auto image = Ui::Earn::MenuIconCurrency(icon->size()); icon->paintRequest() | rpl::start_with_next([=] { auto p = QPainter(icon); - auto hq = PainterHighQualityEnabler(p); p.drawImage(0, 0, image); }, icon->lifetime());