| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- /*
- This file is part of Telegram Desktop,
- the official desktop application for the Telegram messaging service.
- For license and copyright information please follow this link:
- https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
- */
- #include "payments/ui/payments_reaction_box.h"
- #include "base/qt/qt_compare.h"
- #include "lang/lang_keys.h"
- #include "ui/boxes/boost_box.h" // MakeBoostFeaturesBadge.
- #include "ui/controls/who_reacted_context_action.h"
- #include "ui/effects/premium_bubble.h"
- #include "ui/layers/generic_box.h"
- #include "ui/text/text_utilities.h"
- #include "ui/widgets/buttons.h"
- #include "ui/widgets/checkbox.h"
- #include "ui/widgets/continuous_sliders.h"
- #include "ui/widgets/popup_menu.h"
- #include "ui/wrap/slide_wrap.h"
- #include "ui/dynamic_image.h"
- #include "ui/painter.h"
- #include "ui/vertical_list.h"
- #include "styles/style_chat.h"
- #include "styles/style_chat_helpers.h"
- #include "styles/style_credits.h"
- #include "styles/style_layers.h"
- #include "styles/style_media_player.h"
- #include "styles/style_premium.h"
- #include "styles/style_settings.h"
- namespace Settings {
- [[nodiscard]] not_null<Ui::RpWidget*> AddBalanceWidget(
- not_null<Ui::RpWidget*> parent,
- rpl::producer<StarsAmount> balanceValue,
- bool rightAlign,
- rpl::producer<float64> opacityValue = nullptr);
- } // namespace Settings
- namespace Ui {
- namespace {
- constexpr auto kMaxTopPaidShown = 3;
- struct TopReactorKey {
- std::shared_ptr<DynamicImage> photo;
- int count = 0;
- QString name;
- friend inline auto operator<=>(
- const TopReactorKey &,
- const TopReactorKey &) = default;
- friend inline bool operator==(
- const TopReactorKey &,
- const TopReactorKey &) = default;
- };
- struct Discreter {
- Fn<int(float64)> ratioToValue;
- Fn<float64(int)> valueToRatio;
- };
- [[nodiscard]] Discreter DiscreterForMax(int max) {
- Expects(max >= 2);
- // 1/8 of width is 1..10
- // 1/3 of width is 1..100
- // 2/3 of width is 1..1000
- auto thresholds = base::flat_map<float64, int>();
- thresholds.emplace(0., 1);
- if (max <= 40) {
- thresholds.emplace(1., max);
- } else if (max <= 300) {
- thresholds.emplace(1. / 4, 10);
- thresholds.emplace(1., max);
- } else if (max <= 600) {
- thresholds.emplace(1. / 8, 10);
- thresholds.emplace(1. / 2, 100);
- thresholds.emplace(1., max);
- } else if (max <= 1900) {
- thresholds.emplace(1. / 8, 10);
- thresholds.emplace(1. / 3, 100);
- thresholds.emplace(1., max);
- } else {
- thresholds.emplace(1. / 8, 10);
- thresholds.emplace(1. / 3, 100);
- thresholds.emplace(2. / 3, 1000);
- thresholds.emplace(1., max);
- }
- const auto ratioToValue = [=](float64 ratio) {
- ratio = std::clamp(ratio, 0., 1.);
- const auto j = thresholds.lower_bound(ratio);
- if (j == begin(thresholds)) {
- return 1;
- }
- const auto i = j - 1;
- const auto progress = (ratio - i->first) / (j->first - i->first);
- const auto value = i->second + (j->second - i->second) * progress;
- return int(base::SafeRound(value));
- };
- const auto valueToRatio = [=](int value) {
- value = std::clamp(value, 1, max);
- auto i = begin(thresholds);
- auto j = i + 1;
- while (j->second < value) {
- i = j++;
- }
- const auto progress = (value - i->second)
- / float64(j->second - i->second);
- return i->first + (j->first - i->first) * progress;
- };
- return {
- .ratioToValue = ratioToValue,
- .valueToRatio = valueToRatio,
- };
- }
- void PaidReactionSlider(
- not_null<VerticalLayout*> container,
- int current,
- int max,
- Fn<void(int)> changed) {
- Expects(current >= 1 && current <= max);
- const auto slider = container->add(
- object_ptr<MediaSlider>(container, st::paidReactSlider),
- st::boxRowPadding + QMargins(0, st::paidReactSliderTop, 0, 0));
- slider->resize(slider->width(), st::paidReactSlider.seekSize.height());
- const auto discreter = DiscreterForMax(max);
- slider->setAlwaysDisplayMarker(true);
- slider->setDirection(ContinuousSlider::Direction::Horizontal);
- slider->setValue(discreter.valueToRatio(current));
- slider->setAdjustCallback([=](float64 ratio) {
- return discreter.valueToRatio(discreter.ratioToValue(ratio));
- });
- const auto ratioToValue = discreter.ratioToValue;
- slider->setChangeProgressCallback([=](float64 value) {
- changed(ratioToValue(value));
- });
- slider->setChangeFinishedCallback([=](float64 value) {
- changed(ratioToValue(value));
- });
- }
- [[nodiscard]] QImage GenerateBadgeImage(int count) {
- return GenerateSmallBadgeImage(
- Lang::FormatCountDecimal(count),
- st::paidReactTopStarIcon,
- st::creditsBg3->c,
- st::premiumButtonFg->c);
- }
- void AddArrowDown(not_null<RpWidget*> widget) {
- const auto arrow = CreateChild<RpWidget>(widget);
- const auto icon = &st::paidReactChannelArrow;
- const auto skip = st::lineWidth * 4;
- const auto size = icon->width() + skip * 2;
- arrow->resize(size, size);
- widget->widthValue() | rpl::start_with_next([=](int width) {
- const auto left = (width - st::paidReactTopUserpic) / 2;
- arrow->moveToRight(left - skip, -st::lineWidth, width);
- }, widget->lifetime());
- arrow->paintRequest() | rpl::start_with_next([=] {
- Painter p(arrow);
- auto hq = PainterHighQualityEnabler(p);
- p.setBrush(st::activeButtonBg);
- p.setPen(st::activeButtonFg);
- const auto rect = arrow->rect();
- const auto line = st::lineWidth;
- p.drawEllipse(rect.marginsRemoved({ line, line, line, line }));
- icon->paint(p, skip, (size - icon->height()) / 2 + line, size);
- }, widget->lifetime());
- arrow->setAttribute(Qt::WA_TransparentForMouseEvents);
- arrow->show();
- }
- [[nodiscard]] not_null<RpWidget*> MakeTopReactor(
- not_null<QWidget*> parent,
- const PaidReactionTop &data,
- Fn<void()> selectShownPeer) {
- const auto result = CreateChild<AbstractButton>(parent);
- result->show();
- if (data.click && !data.my) {
- result->setClickedCallback(data.click);
- } else if (data.click && selectShownPeer) {
- result->setClickedCallback(selectShownPeer);
- AddArrowDown(result);
- } else {
- result->setAttribute(Qt::WA_TransparentForMouseEvents);
- }
- struct State {
- QImage badge;
- Text::String name;
- };
- const auto state = result->lifetime().make_state<State>();
- state->name.setText(st::defaultTextStyle, data.name);
- const auto count = data.count;
- const auto photo = data.photo->clone();
- photo->subscribeToUpdates([=] {
- result->update();
- });
- style::PaletteChanged(
- ) | rpl::start_with_next([=] {
- state->badge = QImage();
- }, result->lifetime());
- result->paintRequest() | rpl::start_with_next([=] {
- auto p = Painter(result);
- const auto left = (result->width() - st::paidReactTopUserpic) / 2;
- p.drawImage(left, 0, photo->image(st::paidReactTopUserpic));
- if (state->badge.isNull()) {
- state->badge = GenerateBadgeImage(count);
- }
- const auto bwidth = state->badge.width()
- / state->badge.devicePixelRatio();
- p.drawImage(
- (result->width() - bwidth) / 2,
- st::paidReactTopBadgeSkip,
- state->badge);
- p.setPen(st::windowFg);
- const auto skip = st::normalFont->spacew;
- const auto nameTop = st::paidReactTopNameSkip;
- const auto available = result->width() - skip * 2;
- state->name.draw(p, skip, nameTop, available, style::al_top);
- }, result->lifetime());
- return result;
- }
- void SelectShownPeer(
- std::shared_ptr<QPointer<PopupMenu>> menu,
- not_null<QWidget*> parent,
- const std::vector<PaidReactionTop> &mine,
- uint64 selected,
- Fn<void(uint64)> callback) {
- if (*menu) {
- (*menu)->hideMenu();
- }
- (*menu) = CreateChild<PopupMenu>(
- parent,
- st::paidReactChannelMenu);
- struct Entry {
- not_null<Ui::WhoReactedEntryAction*> action;
- std::shared_ptr<Ui::DynamicImage> userpic;
- };
- auto actions = std::make_shared<std::vector<Entry>>();
- actions->reserve(mine.size());
- for (const auto &entry : mine) {
- auto action = base::make_unique_q<WhoReactedEntryAction>(
- (*menu)->menu(),
- nullptr,
- (*menu)->menu()->st(),
- Ui::WhoReactedEntryData());
- const auto index = int(actions->size());
- actions->push_back({ action.get(), entry.photo->clone() });
- const auto id = entry.barePeerId;
- const auto updateUserpic = [=] {
- const auto size = st::defaultWhoRead.photoSize;
- actions->at(index).action->setData({
- .text = entry.name,
- .type = ((id == selected)
- ? Ui::WhoReactedType::RefRecipientNow
- : Ui::WhoReactedType::RefRecipient),
- .userpic = actions->at(index).userpic->image(size),
- .callback = [=] { callback(id); },
- });
- };
- actions->back().userpic->subscribeToUpdates(updateUserpic);
- (*menu)->addAction(std::move(action));
- updateUserpic();
- }
- (*menu)->popup(QCursor::pos());
- }
- void FillTopReactors(
- not_null<VerticalLayout*> container,
- std::vector<PaidReactionTop> top,
- rpl::producer<int> chosen,
- rpl::producer<uint64> shownPeer,
- Fn<void(uint64)> changeShownPeer) {
- container->add(
- MakeBoostFeaturesBadge(
- container,
- tr::lng_paid_react_top_title(),
- [](QRect) { return st::creditsBg3->b; }),
- st::boxRowPadding + st::paidReactTopTitleMargin);
- const auto height = st::paidReactTopNameSkip + st::normalFont->height;
- const auto wrap = container->add(
- object_ptr<SlideWrap<FixedHeightWidget>>(
- container,
- object_ptr<FixedHeightWidget>(container, height),
- st::paidReactTopMargin));
- const auto parent = wrap->entity();
- using Key = TopReactorKey;
- struct State {
- base::flat_map<Key, not_null<RpWidget*>> cache;
- std::vector<not_null<RpWidget*>> widgets;
- rpl::event_stream<> updated;
- std::optional<int> initialChosen;
- bool chosenChanged = false;
- };
- const auto state = wrap->lifetime().make_state<State>();
- const auto menu = std::make_shared<QPointer<Ui::PopupMenu>>();
- rpl::combine(
- std::move(chosen),
- std::move(shownPeer)
- ) | rpl::start_with_next([=](int chosen, uint64 barePeerId) {
- if (!state->initialChosen) {
- state->initialChosen = chosen;
- } else if (*state->initialChosen != chosen) {
- state->chosenChanged = true;
- }
- auto mine = std::vector<PaidReactionTop>();
- auto list = std::vector<PaidReactionTop>();
- list.reserve(kMaxTopPaidShown + 1);
- for (const auto &entry : top) {
- if (!entry.my) {
- list.push_back(entry);
- } else if (entry.barePeerId == barePeerId) {
- auto copy = entry;
- if (state->chosenChanged) {
- copy.count += chosen;
- }
- list.push_back(copy);
- }
- if (entry.my && entry.barePeerId) {
- mine.push_back(entry);
- }
- }
- ranges::stable_sort(
- list,
- ranges::greater(),
- &PaidReactionTop::count);
- while (list.size() > kMaxTopPaidShown
- || (!list.empty() && !list.back().count)) {
- list.pop_back();
- }
- auto selectShownPeer = (mine.size() < 2)
- ? Fn<void()>()
- : [=] { SelectShownPeer(
- menu,
- parent,
- mine,
- barePeerId,
- changeShownPeer); };
- if (list.empty()) {
- wrap->hide(anim::type::normal);
- } else {
- for (const auto &widget : state->widgets) {
- widget->hide();
- }
- state->widgets.clear();
- for (const auto &entry : list) {
- const auto key = Key{
- .photo = entry.photo,
- .count = entry.count,
- .name = entry.name,
- };
- const auto i = state->cache.find(key);
- const auto widget = (i != end(state->cache))
- ? i->second
- : MakeTopReactor(parent, entry, selectShownPeer);
- state->widgets.push_back(widget);
- widget->show();
- }
- for (const auto &[k, widget] : state->cache) {
- if (widget->isHidden()) {
- delete widget;
- }
- }
- wrap->show(anim::type::normal);
- }
- state->updated.fire({});
- }, wrap->lifetime());
- wrap->finishAnimating();
- rpl::combine(
- state->updated.events_starting_with({}),
- wrap->widthValue()
- ) | rpl::start_with_next([=](auto, int width) {
- const auto single = width / 4;
- if (single <= st::paidReactTopUserpic) {
- return;
- }
- const auto count = int(state->widgets.size());
- auto left = (width - single * count) / 2;
- for (const auto &widget : state->widgets) {
- widget->setGeometry(left, 0, single, height);
- left += single;
- }
- }, wrap->lifetime());
- }
- } // namespace
- void PaidReactionsBox(
- not_null<GenericBox*> box,
- PaidReactionBoxArgs &&args) {
- Expects(!args.top.empty());
- args.max = std::max(args.max, 2);
- args.chosen = std::clamp(args.chosen, 1, args.max);
- box->setWidth(st::boxWideWidth);
- box->setStyle(st::paidReactBox);
- box->setNoContentMargin(true);
- struct State {
- rpl::variable<int> chosen;
- rpl::variable<uint64> shownPeer;
- uint64 savedShownPeer = 0;
- };
- const auto state = box->lifetime().make_state<State>();
- state->chosen = args.chosen;
- const auto changed = [=](int count) {
- state->chosen = count;
- };
- const auto initialShownPeer = ranges::find(
- args.top,
- true,
- &PaidReactionTop::my
- )->barePeerId;
- state->shownPeer = initialShownPeer;
- state->savedShownPeer = ranges::find_if(args.top, [](
- const PaidReactionTop &entry) {
- return entry.my && entry.barePeerId != 0;
- })->barePeerId;
- const auto content = box->verticalLayout();
- AddSkip(content, st::boxTitleClose.height + st::paidReactBubbleTop);
- const auto valueToRatio = DiscreterForMax(args.max).valueToRatio;
- auto bubbleRowState = state->chosen.value() | rpl::map([=](int value) {
- const auto full = st::boxWideWidth
- - st::boxRowPadding.left()
- - st::boxRowPadding.right();
- const auto marker = st::paidReactSlider.seekSize.width();
- const auto start = marker / 2;
- const auto inner = full - marker;
- const auto correct = start + inner * valueToRatio(value);
- return Premium::BubbleRowState{
- .counter = value,
- .ratio = correct / full,
- };
- });
- Premium::AddBubbleRow(
- content,
- st::boostBubble,
- BoxShowFinishes(box),
- std::move(bubbleRowState),
- Premium::BubbleType::Credits,
- nullptr,
- &st::paidReactBubbleIcon,
- st::boxRowPadding);
- const auto already = ranges::find(
- args.top,
- true,
- &PaidReactionTop::my)->count;
- PaidReactionSlider(content, args.chosen, args.max, changed);
- box->addTopButton(st::boxTitleClose, [=] { box->closeBox(); });
- box->addRow(
- object_ptr<FlatLabel>(
- box,
- tr::lng_paid_react_title(),
- st::boostCenteredTitle),
- st::boxRowPadding + QMargins(0, st::paidReactTitleSkip, 0, 0));
- const auto labelWrap = box->addRow(
- object_ptr<RpWidget>(box),
- (st::boxRowPadding
- + QMargins(0, st::lineWidth, 0, st::boostBottomSkip)));
- const auto label = CreateChild<FlatLabel>(
- labelWrap,
- (already
- ? tr::lng_paid_react_already(
- lt_count,
- rpl::single(already) | tr::to_count(),
- Text::RichLangValue)
- : tr::lng_paid_react_about(
- lt_channel,
- rpl::single(Text::Bold(args.channel)),
- Text::RichLangValue)),
- st::boostText);
- labelWrap->widthValue() | rpl::start_with_next([=](int width) {
- label->resizeToWidth(width);
- }, label->lifetime());
- label->heightValue() | rpl::start_with_next([=](int height) {
- const auto min = 2 * st::normalFont->height;
- const auto skip = std::max((min - height) / 2, 0);
- labelWrap->resize(labelWrap->width(), 2 * skip + height);
- label->moveToLeft(0, skip);
- }, label->lifetime());
- FillTopReactors(
- content,
- std::move(args.top),
- state->chosen.value(),
- state->shownPeer.value(),
- [=](uint64 barePeerId) {
- state->shownPeer = state->savedShownPeer = barePeerId;
- });
- const auto named = box->addRow(object_ptr<CenterWrap<Checkbox>>(
- box,
- object_ptr<Checkbox>(
- box,
- tr::lng_paid_react_show_in_top(tr::now),
- state->shownPeer.current() != 0)));
- named->entity()->checkedValue(
- ) | rpl::start_with_next([=](bool show) {
- state->shownPeer = show ? state->savedShownPeer : 0;
- }, named->lifetime());
- const auto button = box->addButton(rpl::single(QString()), [=] {
- args.send(state->chosen.current(), state->shownPeer.current());
- });
- box->boxClosing() | rpl::filter([=] {
- return state->shownPeer.current() != initialShownPeer;
- }) | rpl::start_with_next([=] {
- args.send(0, state->shownPeer.current());
- }, box->lifetime());
- {
- const auto buttonLabel = CreateChild<FlatLabel>(
- button,
- rpl::single(QString()),
- st::creditsBoxButtonLabel);
- args.submit(
- state->chosen.value()
- ) | rpl::start_with_next([=](const TextWithContext &text) {
- buttonLabel->setMarkedText(
- text.text,
- text.context);
- }, buttonLabel->lifetime());
- buttonLabel->setTextColorOverride(
- box->getDelegate()->style().button.textFg->c);
- button->sizeValue(
- ) | rpl::start_with_next([=](const QSize &size) {
- buttonLabel->moveToLeft(
- (size.width() - buttonLabel->width()) / 2,
- (size.height() - buttonLabel->height()) / 2);
- }, buttonLabel->lifetime());
- buttonLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
- }
- box->widthValue(
- ) | rpl::start_with_next([=](int width) {
- const auto &padding = st::paidReactBox.buttonPadding;
- button->resizeToWidth(width
- - padding.left()
- - padding.right());
- button->moveToLeft(padding.left(), button->y());
- }, button->lifetime());
- {
- const auto balance = Settings::AddBalanceWidget(
- content,
- std::move(args.balanceValue),
- false);
- rpl::combine(
- balance->sizeValue(),
- box->widthValue()
- ) | rpl::start_with_next([=] {
- balance->moveToLeft(
- st::creditsHistoryRightSkip * 2,
- st::creditsHistoryRightSkip);
- balance->update();
- }, balance->lifetime());
- }
- }
- object_ptr<BoxContent> MakePaidReactionBox(PaidReactionBoxArgs &&args) {
- return Box(PaidReactionsBox, std::move(args));
- }
- QImage GenerateSmallBadgeImage(
- QString text,
- const style::icon &icon,
- QColor bg,
- QColor fg,
- const style::RoundCheckbox *borderSt) {
- const auto length = st::chatSimilarBadgeFont->width(text);
- const auto contents = st::chatSimilarLockedIconPosition.x()
- + icon.width()
- + st::paidReactTopStarSkip
- + length;
- const auto badge = QRect(
- st::chatSimilarBadgePadding.left(),
- st::chatSimilarBadgePadding.top(),
- contents,
- st::chatSimilarBadgeFont->height);
- const auto rect = badge.marginsAdded(st::chatSimilarBadgePadding);
- const auto add = borderSt ? borderSt->width : 0;
- const auto ratio = style::DevicePixelRatio();
- auto result = QImage(
- (rect + QMargins(add, add, add, add)).size() * ratio,
- QImage::Format_ARGB32_Premultiplied);
- result.setDevicePixelRatio(ratio);
- result.fill(Qt::transparent);
- auto q = QPainter(&result);
- const auto &font = st::chatSimilarBadgeFont;
- const auto textTop = badge.y() + font->ascent;
- const auto position = st::chatSimilarLockedIconPosition;
- auto hq = PainterHighQualityEnabler(q);
- q.translate(add, add);
- q.setBrush(bg);
- if (borderSt) {
- q.setPen(QPen(borderSt->border->c, borderSt->width));
- } else {
- q.setPen(Qt::NoPen);
- }
- const auto radius = rect.height() / 2.;
- const auto shift = add / 2.;
- q.drawRoundedRect(
- QRectF(rect) + QMarginsF(shift, shift, shift, shift),
- radius,
- radius);
- auto textLeft = 0;
- icon.paint(
- q,
- badge.x() + position.x(),
- badge.y() + position.y(),
- rect.width());
- textLeft += position.x() + icon.width() + st::paidReactTopStarSkip;
- q.setFont(font);
- q.setPen(fg);
- q.drawText(textLeft, textTop, text);
- q.end();
- return result;
- }
- } // namespace Ui
|