diff --git a/Telegram/SourceFiles/api/api_suggest_post.cpp b/Telegram/SourceFiles/api/api_suggest_post.cpp index e3490be581..7c5a31b314 100644 --- a/Telegram/SourceFiles/api/api_suggest_post.cpp +++ b/Telegram/SourceFiles/api/api_suggest_post.cpp @@ -29,6 +29,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/boxes/choose_date_time.h" #include "ui/layers/generic_box.h" #include "ui/boxes/confirm_box.h" +#include "ui/boxes/emoji_stake_box.h" // InsufficientTonBox #include "ui/text/text_utilities.h" #include "ui/widgets/fields/input_field.h" #include "ui/widgets/popup_menu.h" @@ -101,9 +102,9 @@ void ConfirmApproval( credits->tonLoad(); return; } else if (price > credits->tonBalance()) { - const auto peer = item->history()->peer; + const auto session = &item->history()->session(); show->show( - Box(HistoryView::InsufficientTonBox, peer, price)); + Box(Ui::InsufficientTonBox, session, price)); return; } } else { diff --git a/Telegram/SourceFiles/boxes/star_gift_box.cpp b/Telegram/SourceFiles/boxes/star_gift_box.cpp index adfa05b13b..5c82109b2b 100644 --- a/Telegram/SourceFiles/boxes/star_gift_box.cpp +++ b/Telegram/SourceFiles/boxes/star_gift_box.cpp @@ -78,6 +78,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_premium.h" #include "ui/boxes/boost_box.h" #include "ui/boxes/confirm_box.h" +#include "ui/boxes/emoji_stake_box.h" // InsufficientTonBox #include "ui/chat/chat_style.h" #include "ui/chat/chat_theme.h" #include "ui/controls/button_labels.h" @@ -5623,7 +5624,7 @@ void SubmitTonForm( state->lifetime.destroy(); if (session->credits().tonBalance() < ton) { - show->show(Box(InsufficientTonBox, session->user(), ton)); + show->show(Box(Ui::InsufficientTonBox, session, ton)); } else { ready(); } diff --git a/Telegram/SourceFiles/history/history_item_helpers.cpp b/Telegram/SourceFiles/history/history_item_helpers.cpp index b9f213a0b9..5316462b2e 100644 --- a/Telegram/SourceFiles/history/history_item_helpers.cpp +++ b/Telegram/SourceFiles/history/history_item_helpers.cpp @@ -42,6 +42,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "settings/settings_credits_graphics.h" #include "storage/storage_account.h" #include "ui/boxes/confirm_box.h" +#include "ui/boxes/emoji_stake_box.h" // InsufficientTonBox #include "ui/text/format_values.h" #include "ui/text/text_utilities.h" #include "ui/toast/toast.h" @@ -515,10 +516,12 @@ bool SendPaymentHelper::check( done); return false; } + const auto session = &peer->session(); if (checkSuggestPriceTon - && checkSuggestPriceTon > peer->session().credits().tonBalance()) { + && checkSuggestPriceTon > session->credits().tonBalance()) { using namespace HistoryView; - show->show(Box(InsufficientTonBox, peer, checkSuggestPriceTon)); + show->show( + Box(Ui::InsufficientTonBox, session, checkSuggestPriceTon)); return false; } return true; diff --git a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp index 71c0d033e5..72a565978e 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.cpp @@ -665,7 +665,8 @@ void ChooseSuggestPriceBox( state->savePending = true; return; } else if (credits->tonBalance() < value) { - box->uiShow()->show(Box(InsufficientTonBox, usePeer, value)); + box->uiShow()->show( + Box(Ui::InsufficientTonBox, session, value)); return; } } @@ -836,60 +837,6 @@ QString FormatAfterCommissionPercent( return QString::number(mul / 10.) + '%'; } -void InsufficientTonBox( - not_null box, - not_null peer, - CreditsAmount required) { - box->setStyle(st::suggestPriceBox); - box->addTopButton(st::boxTitleClose, [=] { - box->closeBox(); - }); - - auto icon = Settings::CreateLottieIcon( - box->verticalLayout(), - { - .name = u"diamond"_q, - .sizeOverride = st::normalBoxLottieSize, - }, - {}); - box->setShowFinishedCallback([animate = std::move(icon.animate)] { - animate(anim::repeat::loop); - }); - box->addRow(std::move(icon.widget), st::lowTonIconPadding); - const auto add = required - peer->session().credits().tonBalance(); - const auto nano = add.whole() * Ui::kNanosInOne + add.nano(); - const auto amount = Ui::FormatTonAmount(nano).full; - box->addRow( - object_ptr( - box, - tr::lng_suggest_low_ton_title(tr::now, lt_amount, amount), - st::boxTitle), - st::boxRowPadding + st::lowTonTitlePadding, - style::al_top); - const auto label = box->addRow( - object_ptr( - box, - tr::lng_suggest_low_ton_text(tr::rich), - st::lowTonText), - st::boxRowPadding + st::lowTonTextPadding, - style::al_top); - label->setTryMakeSimilarLines(true); - label->resizeToWidth( - st::boxWidth - st::boxRowPadding.left() - st::boxRowPadding.right()); - - const auto url = tr::lng_suggest_low_ton_fragment_url(tr::now); - const auto button = box->addButton( - tr::lng_suggest_low_ton_fragment(), - [=] { UrlClickHandler::Open(url); }); - const auto buttonWidth = st::boxWidth - - rect::m::sum::h(st::suggestPriceBox.buttonPadding); - button->widthValue() | rpl::filter([=] { - return (button->widthNoMargins() != buttonWidth); - }) | rpl::on_next([=] { - button->resizeToWidth(buttonWidth); - }, button->lifetime()); -} - SuggestOptionsBar::SuggestOptionsBar( std::shared_ptr show, not_null peer, diff --git a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.h b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.h index 80f523bf5d..fb5fd4fea0 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.h +++ b/Telegram/SourceFiles/history/view/controls/history_view_suggest_options.h @@ -100,11 +100,6 @@ void ChooseSuggestPriceBox( not_null session, CreditsAmount price); -void InsufficientTonBox( - not_null box, - not_null peer, - CreditsAmount required); - class SuggestOptionsBar final { public: SuggestOptionsBar( diff --git a/Telegram/SourceFiles/ui/boxes/emoji_stake_box.cpp b/Telegram/SourceFiles/ui/boxes/emoji_stake_box.cpp index 8db9e85fab..d0a9c88ef0 100644 --- a/Telegram/SourceFiles/ui/boxes/emoji_stake_box.cpp +++ b/Telegram/SourceFiles/ui/boxes/emoji_stake_box.cpp @@ -32,8 +32,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "ui/widgets/labels.h" #include "ui/wrap/padding_wrap.h" #include "ui/wrap/vertical_layout.h" +#include "ui/basic_click_handlers.h" // UrlClickHandler #include "ui/painter.h" +#include "ui/rect.h" #include "ui/vertical_list.h" +#include "styles/style_boxes.h" #include "styles/style_calls.h" // confcallJoinBox #include "styles/style_chat.h" #include "styles/style_chat_helpers.h" @@ -291,6 +294,60 @@ void AddApproximateUsd( usd->widthValue() | rpl::on_next(move, usd->lifetime()); } +void InsufficientTonBox( + not_null box, + not_null session, + CreditsAmount required) { + box->setStyle(st::suggestPriceBox); + box->addTopButton(st::boxTitleClose, [=] { + box->closeBox(); + }); + + auto icon = Settings::CreateLottieIcon( + box->verticalLayout(), + { + .name = u"diamond"_q, + .sizeOverride = st::normalBoxLottieSize, + }, + {}); + box->setShowFinishedCallback([animate = std::move(icon.animate)] { + animate(anim::repeat::loop); + }); + box->addRow(std::move(icon.widget), st::lowTonIconPadding); + const auto add = required - session->credits().tonBalance(); + const auto nano = add.whole() * Ui::kNanosInOne + add.nano(); + const auto amount = Ui::FormatTonAmount(nano).full; + box->addRow( + object_ptr( + box, + tr::lng_suggest_low_ton_title(tr::now, lt_amount, amount), + st::boxTitle), + st::boxRowPadding + st::lowTonTitlePadding, + style::al_top); + const auto label = box->addRow( + object_ptr( + box, + tr::lng_suggest_low_ton_text(tr::rich), + st::lowTonText), + st::boxRowPadding + st::lowTonTextPadding, + style::al_top); + label->setTryMakeSimilarLines(true); + label->resizeToWidth( + st::boxWidth - st::boxRowPadding.left() - st::boxRowPadding.right()); + + const auto url = tr::lng_suggest_low_ton_fragment_url(tr::now); + const auto button = box->addButton( + tr::lng_suggest_low_ton_fragment(), + [=] { UrlClickHandler::Open(url); }); + const auto buttonWidth = st::boxWidth + - rect::m::sum::h(st::suggestPriceBox.buttonPadding); + button->widthValue() | rpl::filter([=] { + return (button->widthNoMargins() != buttonWidth); + }) | rpl::on_next([=] { + button->resizeToWidth(buttonWidth); + }, button->lifetime()); +} + void EmojiGameStakeBox( not_null box, EmojiGameStakeArgs &&args) { @@ -393,35 +450,39 @@ void EmojiGameStakeBox( field->setFocusFast(); }); - const auto price = field->lifetime().make_state>( - args.currentStake); - auto priceValue = price->value() | rpl::map([=](int64 amount) { + const auto fromNanoTon = [](int64 nanoton) { return CreditsAmount( - amount / Ui::kNanosInOne, - amount % Ui::kNanosInOne, + nanoton / Ui::kNanosInOne, + nanoton % Ui::kNanosInOne, CreditsType::Ton); - }); + }; + const auto price = field->lifetime().make_state< + rpl::variable + >(rpl::single( + args.currentStake + ) | rpl::then(field->changes() | rpl::map([=] { + return Ui::ParseTonAmountString(field->getLastText()).value_or(0); + })) | rpl::map(fromNanoTon)); AddApproximateUsd( field, args.session, - std::move(priceValue)); - - field->changes() | rpl::on_next([=] { - *price = Ui::ParseTonAmountString(field->getLastText()).value_or(0); - }, field->lifetime()); + price->value()); const auto submit = args.submit; const auto callback = [=] { const auto text = field->getLastText(); - const auto now = Ui::ParseTonAmountString(text).value_or(0); + const auto now = price->current(); + const auto credits = &args.session->credits(); const auto appConfig = &args.session->appConfig(); - const auto min = appConfig->stakeDiceNanoTonMin(); - const auto max = appConfig->stakeDiceNanoTonMax(); + const auto min = fromNanoTon(appConfig->stakeDiceNanoTonMin()); + const auto max = fromNanoTon(appConfig->stakeDiceNanoTonMax()); if (!now || now < min || now > max) { field->showError(); + } else if (credits->tonBalance() < now) { + box->uiShow()->show(Box(InsufficientTonBox, args.session, now)); } else { box->closeBox(); - submit(now); + submit(now.whole() * Ui::kNanosInOne + now.nano()); } }; field->submits() | rpl::on_next(callback, field->lifetime()); diff --git a/Telegram/SourceFiles/ui/boxes/emoji_stake_box.h b/Telegram/SourceFiles/ui/boxes/emoji_stake_box.h index 3a84abf8ed..7077fffc28 100644 --- a/Telegram/SourceFiles/ui/boxes/emoji_stake_box.h +++ b/Telegram/SourceFiles/ui/boxes/emoji_stake_box.h @@ -39,6 +39,11 @@ void AddApproximateUsd( not_null session, rpl::producer price); +void InsufficientTonBox( + not_null box, + not_null session, + CreditsAmount required); + struct EmojiGameStakeArgs { not_null session; int64 currentStake = 0;