numbers_animation.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. #include "ui/effects/animations.h"
  10. namespace style {
  11. struct FlatLabel;
  12. } // namespace style
  13. namespace Ui {
  14. class NumbersAnimation {
  15. public:
  16. NumbersAnimation(
  17. const style::font &font,
  18. Fn<void()> animationCallback);
  19. void setWidthChangedCallback(Fn<void()> callback) {
  20. _widthChangedCallback = std::move(callback);
  21. }
  22. void setText(const QString &text, int value);
  23. void setDuration(int duration);
  24. void setDisabledMonospace(bool value);
  25. void finishAnimating();
  26. void paint(QPainter &p, int x, int y, int outerWidth);
  27. int countWidth() const;
  28. int maxWidth() const;
  29. private:
  30. struct Digit {
  31. QChar from = QChar(0);
  32. QChar to = QChar(0);
  33. int fromWidth = 0;
  34. int toWidth = 0;
  35. };
  36. void animationCallback();
  37. void realSetText(QString text, int value);
  38. const style::font &_font;
  39. int _duration;
  40. QList<Digit> _digits;
  41. int _digitWidth = 0;
  42. int _fromWidth = 0;
  43. int _toWidth = 0;
  44. int _bothWidth = 0;
  45. Ui::Animations::Simple _a_ready;
  46. QString _delayedText;
  47. int _delayedValue = 0;
  48. int _value = 0;
  49. bool _growing = false;
  50. bool _disabledMonospace = false;
  51. Fn<void()> _animationCallback;
  52. Fn<void()> _widthChangedCallback;
  53. };
  54. struct StringWithNumbers {
  55. static StringWithNumbers FromString(const QString &text) {
  56. return { text };
  57. }
  58. QString text;
  59. int offset = -1;
  60. int length = 0;
  61. };
  62. class LabelWithNumbers : public Ui::RpWidget {
  63. public:
  64. LabelWithNumbers(
  65. QWidget *parent,
  66. const style::FlatLabel &st,
  67. int textTop,
  68. const StringWithNumbers &value);
  69. void setValue(const StringWithNumbers &value);
  70. void finishAnimating();
  71. int naturalWidth() const override {
  72. return _beforeWidth + _numbers.maxWidth() + _afterWidth;
  73. }
  74. protected:
  75. void paintEvent(QPaintEvent *e) override;
  76. private:
  77. static QString GetBefore(const StringWithNumbers &value);
  78. static QString GetAfter(const StringWithNumbers &value);
  79. static QString GetNumbers(const StringWithNumbers &value);
  80. const style::FlatLabel &_st;
  81. int _textTop;
  82. QString _before;
  83. QString _after;
  84. NumbersAnimation _numbers;
  85. int _beforeWidth = 0;
  86. int _afterWidth = 0;
  87. Ui::Animations::Simple _beforeWidthAnimation;
  88. };
  89. } // namespace Ui