slide_animation.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #include "ui/effects/slide_animation.h"
  8. #include <QtGui/QPainter>
  9. namespace Ui {
  10. void SlideAnimation::setSnapshots(
  11. QPixmap leftSnapshot,
  12. QPixmap rightSnapshot) {
  13. Expects(!leftSnapshot.isNull());
  14. Expects(!rightSnapshot.isNull());
  15. _leftSnapshot = std::move(leftSnapshot);
  16. _rightSnapshot = std::move(rightSnapshot);
  17. _leftSnapshot.setDevicePixelRatio(style::DevicePixelRatio());
  18. _rightSnapshot.setDevicePixelRatio(style::DevicePixelRatio());
  19. }
  20. void SlideAnimation::paintFrame(QPainter &p, int x, int y, int outerWidth) {
  21. if (!animating()) {
  22. return;
  23. }
  24. const auto pixelRatio = style::DevicePixelRatio();
  25. const auto state = this->state();
  26. const auto leftCoord = _slideLeft
  27. ? anim::interpolate(-_leftSnapshotWidth, 0, state.leftProgress)
  28. : anim::interpolate(0, -_leftSnapshotWidth, state.leftProgress);
  29. const auto rightCoord = _slideLeft
  30. ? anim::interpolate(0, _rightSnapshotWidth, state.rightProgress)
  31. : anim::interpolate(_rightSnapshotWidth, 0, state.rightProgress);
  32. if (_overflowHidden) {
  33. const auto leftWidth = (_leftSnapshotWidth + leftCoord);
  34. if (leftWidth > 0) {
  35. p.setOpacity(state.leftAlpha);
  36. p.drawPixmap(
  37. x,
  38. y,
  39. leftWidth,
  40. _leftSnapshotHeight,
  41. _leftSnapshot,
  42. (_leftSnapshot.width() - leftWidth * pixelRatio),
  43. 0,
  44. leftWidth * pixelRatio,
  45. _leftSnapshot.height());
  46. }
  47. const auto rightWidth = _rightSnapshotWidth - rightCoord;
  48. if (rightWidth > 0) {
  49. p.setOpacity(state.rightAlpha);
  50. p.drawPixmap(
  51. x + rightCoord,
  52. y,
  53. _rightSnapshot,
  54. 0,
  55. 0,
  56. rightWidth * pixelRatio,
  57. _rightSnapshot.height());
  58. }
  59. } else {
  60. p.setOpacity(state.leftAlpha);
  61. p.drawPixmap(x + leftCoord, y, _leftSnapshot);
  62. p.setOpacity(state.rightAlpha);
  63. p.drawPixmap(x + rightCoord, y, _rightSnapshot);
  64. }
  65. }
  66. SlideAnimation::State SlideAnimation::state() const {
  67. const auto dt = _animation.value(1.);
  68. const auto easeOut = anim::easeOutCirc(1., dt);
  69. const auto easeIn = anim::easeInCirc(1., dt);
  70. const auto arrivingAlpha = easeIn;
  71. const auto departingAlpha = 1. - easeOut;
  72. auto result = State();
  73. result.leftProgress = _slideLeft ? easeOut : easeIn;
  74. result.leftAlpha = _slideLeft ? arrivingAlpha : departingAlpha;
  75. result.rightProgress = _slideLeft ? easeIn : easeOut;
  76. result.rightAlpha = _slideLeft ? departingAlpha : arrivingAlpha;
  77. return result;
  78. }
  79. } // namespace Ui