media_player_button.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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/animations.h"
  9. #include "ui/widgets/buttons.h"
  10. #include "ui/rect_part.h"
  11. #include <QtGui/QFontMetrics>
  12. namespace style {
  13. struct MediaPlayerButton;
  14. struct MediaSpeedButton;
  15. } // namespace style
  16. namespace Media::Player {
  17. class PlayButtonLayout {
  18. public:
  19. enum class State {
  20. Play,
  21. Pause,
  22. Cancel,
  23. };
  24. PlayButtonLayout(const style::MediaPlayerButton &st, Fn<void()> callback);
  25. void setState(State state);
  26. void finishTransform();
  27. void paint(QPainter &p, const QBrush &brush);
  28. private:
  29. void animationCallback();
  30. void startTransform(float64 from, float64 to);
  31. void paintPlay(QPainter &p, const QBrush &brush);
  32. void paintPlayToPause(QPainter &p, const QBrush &brush, float64 progress);
  33. void paintPlayToCancel(QPainter &p, const QBrush &brush, float64 progress);
  34. void paintPauseToCancel(QPainter &p, const QBrush &brush, float64 progress);
  35. const style::MediaPlayerButton &_st;
  36. State _state = State::Play;
  37. State _oldState = State::Play;
  38. State _nextState = State::Play;
  39. Ui::Animations::Simple _transformProgress;
  40. bool _transformBackward = false;
  41. Fn<void()> _callback;
  42. };
  43. class SpeedButtonLayout {
  44. public:
  45. SpeedButtonLayout(
  46. const style::MediaSpeedButton &st,
  47. Fn<void()> callback,
  48. float64 speed);
  49. void setSpeed(float64 speed);
  50. void paint(QPainter &p, bool over, bool active);
  51. private:
  52. const style::MediaSpeedButton &_st;
  53. float64 _speed = 1.;
  54. QFontMetricsF _metrics;
  55. float64 _adjustedAscent = 0.;
  56. float64 _adjustedHeight = 0.;
  57. QString _text;
  58. float64 _textWidth = 0;
  59. Fn<void()> _callback;
  60. };
  61. class SpeedButton final : public Ui::RippleButton {
  62. public:
  63. SpeedButton(QWidget *parent, const style::MediaSpeedButton &st);
  64. [[nodiscard]] const style::MediaSpeedButton &st() const {
  65. return _st;
  66. }
  67. void setSpeed(float64 speed);
  68. private:
  69. void paintEvent(QPaintEvent *e) override;
  70. QPoint prepareRippleStartPosition() const override;
  71. QImage prepareRippleMask() const override;
  72. const style::MediaSpeedButton &_st;
  73. SpeedButtonLayout _layout;
  74. bool _isDefault = false;
  75. };
  76. class SettingsButton final : public Ui::RippleButton {
  77. public:
  78. SettingsButton(QWidget *parent, const style::MediaSpeedButton &st);
  79. [[nodiscard]] const style::MediaSpeedButton &st() const {
  80. return _st;
  81. }
  82. void setSpeed(float64 speed);
  83. void setQuality(int quality);
  84. void setActive(bool active);
  85. private:
  86. void paintEvent(QPaintEvent *e) override;
  87. QPoint prepareRippleStartPosition() const override;
  88. QImage prepareRippleMask() const override;
  89. void onStateChanged(State was, StateChangeSource source) override;
  90. void paintBadge(
  91. QPainter &p,
  92. const QString &text,
  93. RectPart origin,
  94. QColor color);
  95. void prepareFrame();
  96. const style::MediaSpeedButton &_st;
  97. Ui::Animations::Simple _activeAnimation;
  98. Ui::Animations::Simple _overAnimation;
  99. QImage _frameCache;
  100. float _speed = 1.;
  101. int _quality = 0;
  102. bool _isDefaultSpeed = false;
  103. bool _active = false;
  104. };
  105. } // namespace Media::Player