panel_animation.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "styles/style_widgets.h"
  9. namespace Ui {
  10. class RoundShadowAnimation {
  11. public:
  12. void setCornerMasks(const std::array<QImage, 4> &corners);
  13. protected:
  14. void start(int frameWidth, int frameHeight, float64 devicePixelRatio);
  15. void setShadow(const style::Shadow &st);
  16. bool started() const {
  17. return !_frame.isNull();
  18. }
  19. struct Corner {
  20. QImage image;
  21. int width = 0;
  22. int height = 0;
  23. const uchar *bytes = nullptr;
  24. int bytesPerPixel = 0;
  25. int bytesPerLineAdded = 0;
  26. bool valid() const {
  27. return !image.isNull();
  28. }
  29. };
  30. void setCornerMask(Corner &corner, const QImage &image);
  31. void paintCorner(Corner &corner, int left, int top);
  32. struct Shadow {
  33. style::margins extend;
  34. QImage left, topLeft, top, topRight, right, bottomRight, bottom, bottomLeft;
  35. bool valid() const {
  36. return !left.isNull();
  37. }
  38. };
  39. QImage cloneImage(const style::icon &source);
  40. void paintShadow(int left, int top, int right, int bottom);
  41. void paintShadowCorner(int left, int top, const QImage &image);
  42. void paintShadowVertical(int left, int top, int bottom, const QImage &image);
  43. void paintShadowHorizontal(int left, int right, int top, const QImage &image);
  44. Shadow _shadow;
  45. Corner _topLeft;
  46. Corner _topRight;
  47. Corner _bottomLeft;
  48. Corner _bottomRight;
  49. QImage _frame;
  50. uint32 *_frameInts = nullptr;
  51. int _frameWidth = 0;
  52. int _frameHeight = 0;
  53. int _frameAlpha = 0; // recounted each getFrame()
  54. int _frameIntsPerLine = 0;
  55. int _frameIntsPerLineAdded = 0;
  56. };
  57. class PanelAnimation : public RoundShadowAnimation {
  58. public:
  59. enum class Origin {
  60. TopLeft,
  61. TopRight,
  62. BottomLeft,
  63. BottomRight,
  64. };
  65. PanelAnimation(const style::PanelAnimation &st, Origin origin) : _st(st), _origin(origin) {
  66. }
  67. struct PaintState {
  68. float64 opacity = 0.;
  69. float64 widthProgress = 0.;
  70. float64 heightProgress = 0.;
  71. float64 fade = 0.;
  72. int width = 0;
  73. int height = 0;
  74. };
  75. void setFinalImage(QImage &&finalImage, QRect inner);
  76. void setSkipShadow(bool skipShadow);
  77. void start();
  78. [[nodiscard]] PaintState computeState(float64 dt, float64 opacity) const;
  79. PaintState paintFrame(
  80. QPainter &p,
  81. int x,
  82. int y,
  83. int outerWidth,
  84. float64 dt,
  85. float64 opacity);
  86. private:
  87. void setStartWidth();
  88. void setStartHeight();
  89. void setStartAlpha();
  90. void setStartFadeTop();
  91. void createFadeMask();
  92. void setWidthDuration();
  93. void setHeightDuration();
  94. void setAlphaDuration();
  95. const style::PanelAnimation &_st;
  96. Origin _origin = Origin::TopLeft;
  97. QPixmap _finalImage;
  98. int _finalWidth = 0;
  99. int _finalHeight = 0;
  100. int _finalInnerLeft = 0;
  101. int _finalInnerTop = 0;
  102. int _finalInnerRight = 0;
  103. int _finalInnerBottom = 0;
  104. int _finalInnerWidth = 0;
  105. int _finalInnerHeight = 0;
  106. bool _skipShadow = false;
  107. int _startWidth = -1;
  108. int _startHeight = -1;
  109. int _startAlpha = 0;
  110. int _startFadeTop = 0;
  111. QPixmap _fadeMask;
  112. int _fadeHeight = 0;
  113. QBrush _fadeFirst, _fadeLast;
  114. float64 _widthDuration = 1.;
  115. float64 _heightDuration = 1.;
  116. float64 _alphaDuration = 1.;
  117. };
  118. } // namespace Ui