premium_bubble.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/effects/numbers_animation.h"
  9. #include "ui/rp_widget.h"
  10. enum lngtag_count : int;
  11. namespace tr {
  12. template <typename ...Tags>
  13. struct phrase;
  14. } // namespace tr
  15. namespace style {
  16. struct PremiumBubble;
  17. } // namespace style
  18. namespace Ui {
  19. class VerticalLayout;
  20. } // namespace Ui
  21. namespace Ui::Premium {
  22. using TextFactory = Fn<QString(int)>;
  23. [[nodiscard]] TextFactory ProcessTextFactory(
  24. std::optional<tr::phrase<lngtag_count>> phrase);
  25. class Bubble final {
  26. public:
  27. using EdgeProgress = float64;
  28. Bubble(
  29. const style::PremiumBubble &st,
  30. Fn<void()> updateCallback,
  31. TextFactory textFactory,
  32. const style::icon *icon,
  33. bool hasTail);
  34. [[nodiscard]] static crl::time SlideNoDeflectionDuration();
  35. [[nodiscard]] int counter() const;
  36. [[nodiscard]] int height() const;
  37. [[nodiscard]] int width() const;
  38. [[nodiscard]] int bubbleRadius() const;
  39. [[nodiscard]] int countMaxWidth(int maxPossibleCounter) const;
  40. void setCounter(int value);
  41. void setTailEdge(EdgeProgress edge);
  42. void setFlipHorizontal(bool value);
  43. void paintBubble(QPainter &p, const QRect &r, const QBrush &brush);
  44. [[nodiscard]] rpl::producer<> widthChanges() const;
  45. private:
  46. [[nodiscard]] int filledWidth() const;
  47. const style::PremiumBubble &_st;
  48. const Fn<void()> _updateCallback;
  49. const TextFactory _textFactory;
  50. const style::icon *_icon;
  51. NumbersAnimation _numberAnimation;
  52. const int _height;
  53. const int _textTop;
  54. const bool _hasTail;
  55. int _counter = -1;
  56. EdgeProgress _tailEdge = 0.;
  57. bool _flipHorizontal = false;
  58. rpl::event_stream<> _widthChanges;
  59. };
  60. struct BubbleRowState {
  61. int counter = 0;
  62. float64 ratio = 0.;
  63. bool animateFromZero = false;
  64. bool dynamic = false;
  65. };
  66. enum class BubbleType : uchar {
  67. NoPremium,
  68. Premium,
  69. Credits,
  70. };
  71. class BubbleWidget final : public Ui::RpWidget {
  72. public:
  73. BubbleWidget(
  74. not_null<Ui::RpWidget*> parent,
  75. const style::PremiumBubble &st,
  76. TextFactory textFactory,
  77. rpl::producer<BubbleRowState> state,
  78. BubbleType type,
  79. rpl::producer<> showFinishes,
  80. const style::icon *icon,
  81. const style::margins &outerPadding);
  82. protected:
  83. void paintEvent(QPaintEvent *e) override;
  84. private:
  85. struct GradientParams {
  86. int left = 0;
  87. int width = 0;
  88. int outer = 0;
  89. friend inline constexpr bool operator==(
  90. GradientParams,
  91. GradientParams) = default;
  92. };
  93. void animateTo(BubbleRowState state);
  94. const style::PremiumBubble &_st;
  95. BubbleRowState _animatingFrom;
  96. float64 _animatingFromResultRatio = 0.;
  97. float64 _animatingFromBubbleEdge = 0.;
  98. rpl::variable<BubbleRowState> _state;
  99. Bubble _bubble;
  100. int _maxBubbleWidth = 0;
  101. const BubbleType _type;
  102. const style::margins _outerPadding;
  103. Ui::Animations::Simple _appearanceAnimation;
  104. QSize _spaceForDeflection;
  105. QLinearGradient _cachedGradient;
  106. std::optional<GradientParams> _cachedGradientParams;
  107. float64 _deflection;
  108. bool _ignoreDeflection = false;
  109. float64 _stepBeforeDeflection;
  110. float64 _stepAfterDeflection;
  111. };
  112. void AddBubbleRow(
  113. not_null<Ui::VerticalLayout*> parent,
  114. const style::PremiumBubble &st,
  115. rpl::producer<> showFinishes,
  116. int min,
  117. int current,
  118. int max,
  119. BubbleType type,
  120. std::optional<tr::phrase<lngtag_count>> phrase,
  121. const style::icon *icon);
  122. void AddBubbleRow(
  123. not_null<Ui::VerticalLayout*> parent,
  124. const style::PremiumBubble &st,
  125. rpl::producer<> showFinishes,
  126. rpl::producer<BubbleRowState> state,
  127. BubbleType type,
  128. Fn<QString(int)> text,
  129. const style::icon *icon,
  130. const style::margins &outerPadding);
  131. } // namespace Ui::Premium