Slightly improved code style of Window::SlideAnimation.

This commit is contained in:
23rd 2022-05-19 10:33:13 +03:00
parent 51ed3af14c
commit e5ac8ccda3
2 changed files with 78 additions and 27 deletions

View file

@ -15,45 +15,90 @@ namespace Window {
void SlideAnimation::paintContents(Painter &p) const { void SlideAnimation::paintContents(Painter &p) const {
const auto retina = style::DevicePixelRatio(); const auto retina = style::DevicePixelRatio();
auto progress = _animation.value((_direction == SlideDirection::FromLeft) ? 0. : 1.); const auto slideLeft = (_direction == SlideDirection::FromLeft);
const auto progress = _animation.value(slideLeft ? 0. : 1.);
if (_withFade) { if (_withFade) {
auto slideLeft = (_direction == SlideDirection::FromLeft); const auto dt = slideLeft
auto dt = slideLeft
? (1. - progress) ? (1. - progress)
: progress; : progress;
auto easeOut = anim::easeOutCirc(1., dt); const auto easeOut = anim::easeOutCirc(1., dt);
auto easeIn = anim::easeInCirc(1., dt); const auto easeIn = anim::easeInCirc(1., dt);
auto arrivingAlpha = easeIn; const auto arrivingAlpha = easeIn;
auto departingAlpha = 1. - easeOut; const auto departingAlpha = 1. - easeOut;
auto leftWidthFull = _cacheUnder.width() / cIntRetinaFactor(); const auto leftWidthFull = _cacheUnder.width() / retina;
auto rightWidthFull = _cacheOver.width() / cIntRetinaFactor(); const auto rightWidthFull = _cacheOver.width() / retina;
auto leftCoord = (slideLeft ? anim::interpolate(-leftWidthFull, 0, easeOut) : anim::interpolate(0, -leftWidthFull, easeIn)); const auto leftCoord = slideLeft
auto leftAlpha = (slideLeft ? arrivingAlpha : departingAlpha); ? anim::interpolate(-leftWidthFull, 0, easeOut)
auto rightCoord = (slideLeft ? anim::interpolate(0, rightWidthFull, easeIn) : anim::interpolate(rightWidthFull, 0, easeOut)); : anim::interpolate(0, -leftWidthFull, easeIn);
auto rightAlpha = (slideLeft ? departingAlpha : arrivingAlpha); const auto leftAlpha = (slideLeft ? arrivingAlpha : departingAlpha);
const auto rightCoord = slideLeft
? anim::interpolate(0, rightWidthFull, easeIn)
: anim::interpolate(rightWidthFull, 0, easeOut);
const auto rightAlpha = (slideLeft ? departingAlpha : arrivingAlpha);
const auto leftWidth = (leftWidthFull + leftCoord);
const auto rightWidth = rightWidthFull - rightCoord;
auto leftWidth = (leftWidthFull + leftCoord);
if (leftWidth > 0) { if (leftWidth > 0) {
p.setOpacity(leftAlpha); p.setOpacity(leftAlpha);
p.drawPixmap(0, _topSkip, _cacheUnder, (_cacheUnder.width() - leftWidth * cIntRetinaFactor()), _topSkip * retina, leftWidth * cIntRetinaFactor(), _cacheUnder.height() - _topSkip * retina); p.drawPixmap(
0,
_topSkip,
_cacheUnder,
(_cacheUnder.width() - leftWidth * retina),
_topSkip * retina,
leftWidth * retina,
_cacheUnder.height() - _topSkip * retina);
} }
auto rightWidth = rightWidthFull - rightCoord;
if (rightWidth > 0) { if (rightWidth > 0) {
p.setOpacity(rightAlpha); p.setOpacity(rightAlpha);
p.drawPixmap(rightCoord, _topSkip, _cacheOver, 0, _topSkip * retina, rightWidth * cIntRetinaFactor(), _cacheOver.height() - _topSkip * retina); p.drawPixmap(
rightCoord,
_topSkip,
_cacheOver,
0,
_topSkip * retina,
rightWidth * retina,
_cacheOver.height() - _topSkip * retina);
} }
} else { } else {
auto coordUnder = anim::interpolate(0, -st::slideShift, progress); const auto coordUnder = anim::interpolate(
auto coordOver = anim::interpolate(_cacheOver.width() / cIntRetinaFactor(), 0, progress); 0,
-st::slideShift,
progress);
const auto coordOver = anim::interpolate(
_cacheOver.width() / retina,
0,
progress);
if (coordOver) { if (coordOver) {
p.drawPixmap(QRect(0, 0, coordOver, _cacheUnder.height() / retina), _cacheUnder, QRect(-coordUnder * retina, 0, coordOver * retina, _cacheUnder.height())); p.drawPixmap(
QRect(0, 0, coordOver, _cacheUnder.height() / retina),
_cacheUnder,
QRect(
-coordUnder * retina,
0,
coordOver * retina,
_cacheUnder.height()));
p.setOpacity(progress); p.setOpacity(progress);
p.fillRect(0, 0, coordOver, _cacheUnder.height() / retina, st::slideFadeOutBg); p.fillRect(
0,
0,
coordOver, _cacheUnder.height() / retina,
st::slideFadeOutBg);
p.setOpacity(1); p.setOpacity(1);
} }
p.drawPixmap(QRect(coordOver, 0, _cacheOver.width() / retina, _cacheOver.height() / retina), _cacheOver, QRect(0, 0, _cacheOver.width(), _cacheOver.height())); p.drawPixmap(
QRect(QPoint(coordOver, 0), _cacheOver.size() / retina),
_cacheOver,
QRect(QPoint(), _cacheOver.size()));
p.setOpacity(progress); p.setOpacity(progress);
st::slideShadow.fill(p, QRect(coordOver - st::slideShadow.width(), 0, st::slideShadow.width(), _cacheOver.height() / retina)); st::slideShadow.fill(
p,
QRect(
coordOver - st::slideShadow.width(),
0,
st::slideShadow.width(),
_cacheOver.height() / retina));
} }
} }
@ -61,7 +106,9 @@ void SlideAnimation::setDirection(SlideDirection direction) {
_direction = direction; _direction = direction;
} }
void SlideAnimation::setPixmaps(const QPixmap &oldContentCache, const QPixmap &newContentCache) { void SlideAnimation::setPixmaps(
const QPixmap &oldContentCache,
const QPixmap &newContentCache) {
_cacheUnder = oldContentCache; _cacheUnder = oldContentCache;
_cacheOver = newContentCache; _cacheOver = newContentCache;
} }
@ -87,8 +134,10 @@ void SlideAnimation::setFinishedCallback(FinishedCallback &&callback) {
} }
void SlideAnimation::start() { void SlideAnimation::start() {
auto fromLeft = (_direction == SlideDirection::FromLeft); const auto fromLeft = (_direction == SlideDirection::FromLeft);
if (fromLeft) std::swap(_cacheUnder, _cacheOver); if (fromLeft) {
std::swap(_cacheUnder, _cacheOver);
}
_animation.start( _animation.start(
[this] { animationCallback(); }, [this] { animationCallback(); },
fromLeft ? 1. : 0., fromLeft ? 1. : 0.,

View file

@ -21,7 +21,9 @@ public:
void paintContents(Painter &p) const; void paintContents(Painter &p) const;
void setDirection(SlideDirection direction); void setDirection(SlideDirection direction);
void setPixmaps(const QPixmap &oldContentCache, const QPixmap &newContentCache); void setPixmaps(
const QPixmap &oldContentCache,
const QPixmap &newContentCache);
void setTopBarShadow(bool enabled); void setTopBarShadow(bool enabled);
void setTopSkip(int skip); void setTopSkip(int skip);
void setWithFade(bool withFade); void setWithFade(bool withFade);