2
0
mirror of https://github.com/kotatogram/kotatogram-desktop synced 2025-08-31 06:35:14 +00:00

Profile done as a new generic Window::SectionWidget.

Slide animation reimplemented.
This commit is contained in:
John Preston
2016-05-19 15:03:51 +03:00
parent 6e2dea7030
commit 1d42144c95
37 changed files with 1052 additions and 373 deletions

View File

@@ -365,7 +365,7 @@ public:
using Callback = Function<void>;
SimpleAnimation() : _data(0) {
SimpleAnimation() {
}
bool animating(uint64 ms) {
@@ -376,7 +376,7 @@ public:
return false;
}
bool isNull() {
bool isNull() const {
return !_data;
}
@@ -384,6 +384,10 @@ public:
return _data ? _data->a.current() : typename AnimType::Type();
}
typename AnimType::Type current(const typename AnimType::Type &def) {
return _data ? _data->a.current() : def;
}
typename AnimType::Type current(uint64 ms, const typename AnimType::Type &def) {
return animating(ms) ? current() : def;
}
@@ -405,6 +409,17 @@ public:
}
}
void finish() {
if (isNull()) {
return;
}
_data->a.finish();
_data->_a.stop();
delete _data;
_data = nullptr;
}
~SimpleAnimation() {
deleteAndMark(_data);
}
@@ -424,7 +439,7 @@ private:
float64 duration;
anim::transition transition;
};
Data *_data;
Data *_data = nullptr;
void step(float64 ms, bool timer) {
float64 dt = (ms >= _data->duration) ? 1 : (ms / _data->duration);
@@ -439,15 +454,15 @@ private:
}
if (!_data->_a.animating()) {
delete _data;
_data = 0;
_data = nullptr;
}
}
};
typedef SimpleAnimation<anim::fvalue> FloatAnimation;
typedef SimpleAnimation<anim::ivalue> IntAnimation;
typedef SimpleAnimation<anim::cvalue> ColorAnimation;
using FloatAnimation = SimpleAnimation<anim::fvalue>;
using IntAnimation = SimpleAnimation<anim::ivalue>;
using ColorAnimation = SimpleAnimation<anim::cvalue>;
#define EnsureAnimation(animation, from, callback) if ((animation).isNull()) { (animation).setup((from), (callback)); }

View File

@@ -1,97 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#include "stdafx.h"
#include "ui/slide_animation.h"
SlideAnimation::SlideAnimation()
: _animation(animation(this, &SlideAnimation::step)) {
}
void SlideAnimation::paintContents(Painter &p, const QRect &update) const {
_animation.step(getms());
if (a_progress.current() < 1) {
p.fillRect(update, st::white);
int retina = cIntRetinaFactor();
int underLeft = a_coordUnder.current();
int underWidth = _cacheUnder.width() / retina;
int underHeight = _cacheUnder.height() / retina;
QRect underDest(0, 0, underWidth + underLeft, underHeight);
QRect underSrc(-underLeft * retina, 0, (underWidth + underLeft) * retina, underHeight * retina);
p.setOpacity(1. - a_progress.current());
p.drawPixmap(underDest, _cacheUnder, underSrc);
p.setOpacity(a_progress.current());
}
p.drawPixmap(a_coordOver.current(), 0, _cacheOver);
}
void SlideAnimation::setDirection(SlideDirection direction) {
_direction = direction;
}
void SlideAnimation::setPixmaps(const QPixmap &oldContentCache, const QPixmap &newContentCache) {
_cacheUnder = oldContentCache;
_cacheOver = newContentCache;
}
void SlideAnimation::setRepaintCallback(RepaintCallback &&callback) {
_repaintCallback = std_::move(callback);
}
void SlideAnimation::setFinishedCallback(FinishedCallback &&callback) {
_finishedCallback = std_::move(callback);
}
void SlideAnimation::start() {
int width = _cacheUnder.width() / cIntRetinaFactor();
int delta = st::slideShift;
a_progress = anim::fvalue(0, 1);
if (_direction == SlideDirection::FromLeft) {
std::swap(_cacheUnder, _cacheOver);
a_coordUnder = anim::ivalue(-delta, 0);
a_coordOver = anim::ivalue(0, delta);
} else {
a_coordUnder = anim::ivalue(0, -delta);
a_coordOver = anim::ivalue(delta, 0);
}
_animation.start();
}
void SlideAnimation::step(float64 ms, bool timer) {
float64 dt = ms / 3000;// st::slideDuration;
if (dt >= 1) {
dt = 1;
if (timer) {
_animation.stop();
a_coordUnder.finish();
a_coordOver.finish();
_finishedCallback.call();
return;
}
}
a_coordUnder.update(dt, anim::linear);
a_coordOver.update(dt, anim::linear);
a_progress.update(dt, anim::linear);
if (timer) {
_repaintCallback.call();
}
}

View File

@@ -1,58 +0,0 @@
/*
This file is part of Telegram Desktop,
the official desktop version of Telegram messaging app, see https://telegram.org
Telegram Desktop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the copyright holders give permission
to link the code of portions of this program with the OpenSSL library.
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
*/
#pragma once
enum class SlideDirection {
FromRight,
FromLeft,
};
class SlideAnimation {
public:
SlideAnimation();
void paintContents(Painter &p, const QRect &update) const;
void setDirection(SlideDirection direction);
void setPixmaps(const QPixmap &oldContentCache, const QPixmap &newContentCache);
using RepaintCallback = Function<void>;
void setRepaintCallback(RepaintCallback &&callback);
using FinishedCallback = Function<void>;
void setFinishedCallback(FinishedCallback &&callback);
void start();
private:
void step(float64 ms, bool timer);
SlideDirection _direction = SlideDirection::FromRight;
mutable Animation _animation;
QPixmap _cacheUnder, _cacheOver;
anim::ivalue a_coordUnder, a_coordOver;
anim::fvalue a_progress;
RepaintCallback _repaintCallback;
FinishedCallback _finishedCallback;
};

View File

@@ -77,3 +77,45 @@ QPixmap myGrab(TWidget *target, QRect rect) {
return result;
}
enum class Mode {
Shown,
ShownFast,
Hidden,
HiddenFast
};
void ToggleableShadow::setMode(Mode mode) {
if (mode == Mode::ShownFast || mode == Mode::HiddenFast) {
if (!_a_opacity.isNull()) {
_a_opacity.finish();
update();
}
}
if (_shown && (mode == Mode::Hidden || mode == Mode::HiddenFast)) {
_shown = false;
if (mode == Mode::Hidden) {
if (_a_opacity.isNull()) {
_a_opacity.setup(1., func(this, &ToggleableShadow::repaintCallback));
}
_a_opacity.start(0., st::shadowToggleDuration);
}
} else if (!_shown && (mode == Mode::Shown || mode == Mode::ShownFast)) {
_shown = true;
if (mode == Mode::Shown) {
if (_a_opacity.isNull()) {
_a_opacity.setup(0., func(this, &ToggleableShadow::repaintCallback));
}
_a_opacity.start(1., st::shadowToggleDuration);
}
}
}
void ToggleableShadow::paintEvent(QPaintEvent *e) {
Painter p(this);
if (_a_opacity.animating(getms())) {
p.setOpacity(_a_opacity.current());
} else if (!_shown) {
return;
}
p.fillRect(e->rect(), _color);
}

View File

@@ -208,7 +208,9 @@ class PlainShadow : public TWidget {
public:
PlainShadow(QWidget *parent, const style::color &color) : TWidget(parent), _color(color) {
}
void paintEvent(QPaintEvent *e) {
protected:
void paintEvent(QPaintEvent *e) override {
Painter(this).fillRect(e->rect(), _color->b);
}
@@ -217,6 +219,37 @@ private:
};
class ToggleableShadow : public TWidget {
public:
ToggleableShadow(QWidget *parent, const style::color &color) : TWidget(parent), _color(color) {
}
enum class Mode {
Shown,
ShownFast,
Hidden,
HiddenFast
};
void setMode(Mode mode);
bool isFullyShown() const {
return _shown && _a_opacity.isNull();
}
protected:
void paintEvent(QPaintEvent *e) override;
private:
void repaintCallback() {
update();
}
const style::color &_color;
FloatAnimation _a_opacity;
bool _shown = true;
};
class SingleDelayedCall : public QObject {
Q_OBJECT