animated_icon.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/style/style_core_types.h"
  9. #include "ui/effects/animations.h"
  10. #include "base/weak_ptr.h"
  11. #include <crl/crl_time.h>
  12. #include <QtCore/QByteArray>
  13. #include <optional>
  14. namespace Ui {
  15. class FrameGenerator;
  16. struct AnimatedIconDescriptor {
  17. FnMut<std::unique_ptr<FrameGenerator>()> generator;
  18. QSize sizeOverride;
  19. bool colorized = false;
  20. };
  21. class AnimatedIcon final : public base::has_weak_ptr {
  22. public:
  23. explicit AnimatedIcon(AnimatedIconDescriptor &&descriptor);
  24. AnimatedIcon(const AnimatedIcon &other) = delete;
  25. AnimatedIcon &operator=(const AnimatedIcon &other) = delete;
  26. AnimatedIcon(AnimatedIcon &&other) = delete; // _animation captures this.
  27. AnimatedIcon &operator=(AnimatedIcon &&other) = delete;
  28. [[nodiscard]] bool valid() const;
  29. [[nodiscard]] int frameIndex() const;
  30. [[nodiscard]] int framesCount() const;
  31. [[nodiscard]] double frameRate() const;
  32. [[nodiscard]] QImage frame(const QColor &textColor) const;
  33. [[nodiscard]] QImage notColorizedFrame() const;
  34. [[nodiscard]] int width() const;
  35. [[nodiscard]] int height() const;
  36. [[nodiscard]] QSize size() const;
  37. struct ResizedFrame {
  38. QImage image;
  39. bool scaled = false;
  40. };
  41. [[nodiscard]] ResizedFrame frame(
  42. const QColor &textColor,
  43. QSize desiredSize,
  44. Fn<void()> updateWithPerfect) const;
  45. [[nodiscard]] ResizedFrame notColorizedFrame(
  46. QSize desiredSize,
  47. Fn<void()> updateWithPerfect) const;
  48. void animate(Fn<void()> update);
  49. void jumpToStart(Fn<void()> update);
  50. void paint(QPainter &p, int x, int y);
  51. void paintInCenter(QPainter &p, QRect rect);
  52. [[nodiscard]] bool animating() const;
  53. private:
  54. struct Frame;
  55. class Impl;
  56. friend class Impl;
  57. void wait() const;
  58. [[nodiscard]] int wantedFrameIndex(
  59. crl::time now,
  60. const Frame *resolvedCurrent = nullptr) const;
  61. void preloadNextFrame(
  62. crl::time now,
  63. const Frame *resolvedCurrent = nullptr,
  64. QSize updatedDesiredSize = QSize()) const;
  65. void frameJumpFinished();
  66. void continueAnimation(crl::time now);
  67. std::shared_ptr<Impl> _impl;
  68. crl::time _animationStartTime = 0;
  69. crl::time _animationStarted = 0;
  70. mutable Animations::Simple _animation;
  71. mutable Fn<void()> _repaint;
  72. mutable crl::time _animationDuration = 0;
  73. mutable crl::time _animationCurrentStart = 0;
  74. mutable crl::time _animationNextStart = 0;
  75. mutable int _animationCurrentIndex = 0;
  76. bool _colorized = false;
  77. };
  78. [[nodiscard]] std::unique_ptr<AnimatedIcon> MakeAnimatedIcon(
  79. AnimatedIconDescriptor &&descriptor);
  80. } // namespace Ui