slide_animation.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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/effects/animations.h"
  9. namespace Ui {
  10. class SlideAnimation {
  11. public:
  12. struct State {
  13. float64 leftProgress = 0.;
  14. float64 leftAlpha = 0.;
  15. float64 rightProgress = 0.;
  16. float64 rightAlpha = 0.;
  17. };
  18. void setSnapshots(QPixmap leftSnapshot, QPixmap rightSnapshot);
  19. void setOverflowHidden(bool hidden) {
  20. _overflowHidden = hidden;
  21. }
  22. template <typename Lambda>
  23. void start(bool slideLeft, Lambda &&updateCallback, float64 duration);
  24. void paintFrame(QPainter &p, int x, int y, int outerWidth);
  25. [[nodiscard]] State state() const;
  26. [[nodiscard]] bool animating() const {
  27. return _animation.animating();
  28. }
  29. private:
  30. Ui::Animations::Simple _animation;
  31. QPixmap _leftSnapshot;
  32. QPixmap _rightSnapshot;
  33. bool _slideLeft = false;
  34. bool _overflowHidden = true;
  35. int _leftSnapshotWidth = 0;
  36. int _leftSnapshotHeight = 0;
  37. int _rightSnapshotWidth = 0;
  38. };
  39. template <typename Lambda>
  40. void SlideAnimation::start(
  41. bool slideLeft,
  42. Lambda &&updateCallback,
  43. float64 duration) {
  44. _slideLeft = slideLeft;
  45. if (_slideLeft) {
  46. std::swap(_leftSnapshot, _rightSnapshot);
  47. }
  48. const auto pixelRatio = style::DevicePixelRatio();
  49. _leftSnapshotWidth = _leftSnapshot.width() / pixelRatio;
  50. _leftSnapshotHeight = _leftSnapshot.height() / pixelRatio;
  51. _rightSnapshotWidth = _rightSnapshot.width() / pixelRatio;
  52. _animation.start(std::forward<Lambda>(updateCallback), 0., 1., duration);
  53. }
  54. } // namespace Ui