2016-07-11 18:05:46 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-07-11 18:05:46 +00:00
|
|
|
*/
|
2017-09-30 18:26:45 +00:00
|
|
|
#include "ui/effects/fade_animation.h"
|
2016-07-11 18:05:46 +00:00
|
|
|
|
|
|
|
namespace Ui {
|
2017-03-07 09:46:01 +00:00
|
|
|
namespace {
|
2016-07-11 18:05:46 +00:00
|
|
|
|
2017-03-07 09:46:01 +00:00
|
|
|
constexpr int kWideScale = 5;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-11-27 11:43:57 +00:00
|
|
|
FadeAnimation::FadeAnimation(TWidget *widget, float64 scale)
|
2017-03-07 09:46:01 +00:00
|
|
|
: _widget(widget)
|
2017-11-27 11:43:57 +00:00
|
|
|
, _scale(scale) {
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FadeAnimation::paint(Painter &p) {
|
|
|
|
if (_cache.isNull()) return false;
|
|
|
|
|
2018-04-06 16:23:09 +00:00
|
|
|
const auto cache = _cache;
|
2019-03-07 11:35:28 +00:00
|
|
|
auto opacity = _animation.value(_visible ? 1. : 0.);
|
2016-12-09 18:56:01 +00:00
|
|
|
p.setOpacity(opacity);
|
2017-11-27 11:43:57 +00:00
|
|
|
if (_scale < 1.) {
|
2017-03-07 09:46:01 +00:00
|
|
|
PainterHighQualityEnabler hq(p);
|
2017-11-27 11:43:57 +00:00
|
|
|
auto targetRect = QRect(
|
|
|
|
(1 - kWideScale) / 2 * _size.width(),
|
|
|
|
(1 - kWideScale) / 2 * _size.height(),
|
|
|
|
kWideScale * _size.width(),
|
|
|
|
kWideScale * _size.height());
|
|
|
|
auto scale = opacity + (1. - opacity) * _scale;
|
|
|
|
auto shownWidth = anim::interpolate(
|
|
|
|
(1 - kWideScale) / 2 * _size.width(),
|
|
|
|
0,
|
|
|
|
scale);
|
|
|
|
auto shownHeight = anim::interpolate(
|
|
|
|
(1 - kWideScale) / 2 * _size.height(),
|
|
|
|
0,
|
|
|
|
scale);
|
|
|
|
auto margins = QMargins(
|
|
|
|
shownWidth,
|
|
|
|
shownHeight,
|
|
|
|
shownWidth,
|
|
|
|
shownHeight);
|
2018-04-06 16:23:09 +00:00
|
|
|
p.drawPixmap(targetRect.marginsAdded(margins), cache);
|
2017-03-07 09:46:01 +00:00
|
|
|
} else {
|
2018-04-06 16:23:09 +00:00
|
|
|
p.drawPixmap(0, 0, cache);
|
2017-03-07 09:46:01 +00:00
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
void FadeAnimation::refreshCache() {
|
|
|
|
if (!_cache.isNull()) {
|
|
|
|
_cache = QPixmap();
|
2017-03-07 09:46:01 +00:00
|
|
|
_cache = grabContent();
|
2017-10-03 13:05:58 +00:00
|
|
|
Assert(!_cache.isNull());
|
2017-03-07 09:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap FadeAnimation::grabContent() {
|
2017-12-26 12:41:48 +00:00
|
|
|
SendPendingMoveResizeEvents(_widget);
|
2017-03-07 09:46:01 +00:00
|
|
|
_size = _widget->size();
|
2017-10-03 13:05:58 +00:00
|
|
|
if (_size.isEmpty()) {
|
|
|
|
auto image = QImage(
|
|
|
|
cIntRetinaFactor(),
|
|
|
|
cIntRetinaFactor(),
|
|
|
|
QImage::Format_ARGB32_Premultiplied);
|
|
|
|
image.fill(Qt::transparent);
|
|
|
|
return App::pixmapFromImageInPlace(std::move(image));
|
|
|
|
}
|
2017-12-26 12:41:48 +00:00
|
|
|
auto widgetContent = GrabWidget(_widget);
|
2017-11-27 11:43:57 +00:00
|
|
|
if (_scale < 1.) {
|
|
|
|
auto result = QImage(kWideScale * _size * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&result);
|
|
|
|
p.drawPixmap((kWideScale - 1) / 2 * _size.width(), (kWideScale - 1) / 2 * _size.height(), widgetContent);
|
|
|
|
}
|
|
|
|
return App::pixmapFromImageInPlace(std::move(result));
|
2017-03-07 09:46:01 +00:00
|
|
|
}
|
2017-11-27 11:43:57 +00:00
|
|
|
return widgetContent;
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
void FadeAnimation::setFinishedCallback(FinishedCallback &&callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_finishedCallback = std::move(callback);
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
void FadeAnimation::setUpdatedCallback(UpdatedCallback &&callback) {
|
2017-02-21 13:45:56 +00:00
|
|
|
_updatedCallback = std::move(callback);
|
2016-07-12 11:38:16 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
void FadeAnimation::show() {
|
2018-04-06 16:23:09 +00:00
|
|
|
_visible = true;
|
2016-07-11 18:05:46 +00:00
|
|
|
stopAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::hide() {
|
|
|
|
_visible = false;
|
|
|
|
stopAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::stopAnimation() {
|
2019-03-07 11:35:28 +00:00
|
|
|
_animation.stop();
|
2016-07-11 18:05:46 +00:00
|
|
|
if (!_cache.isNull()) {
|
|
|
|
_cache = QPixmap();
|
2016-09-26 12:09:59 +00:00
|
|
|
if (_finishedCallback) {
|
|
|
|
_finishedCallback();
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
if (_visible == _widget->isHidden()) {
|
|
|
|
_widget->setVisible(_visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::fadeIn(int duration) {
|
|
|
|
if (_visible) return;
|
|
|
|
|
|
|
|
_visible = true;
|
|
|
|
startAnimation(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::fadeOut(int duration) {
|
|
|
|
if (!_visible) return;
|
|
|
|
|
|
|
|
_visible = false;
|
|
|
|
startAnimation(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::startAnimation(int duration) {
|
|
|
|
if (_cache.isNull()) {
|
2017-03-07 09:46:01 +00:00
|
|
|
_cache = grabContent();
|
2017-10-03 13:05:58 +00:00
|
|
|
Assert(!_cache.isNull());
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
2016-09-26 12:09:59 +00:00
|
|
|
auto from = _visible ? 0. : 1.;
|
|
|
|
auto to = _visible ? 1. : 0.;
|
|
|
|
_animation.start([this]() { updateCallback(); }, from, to, duration);
|
2016-07-11 18:05:46 +00:00
|
|
|
updateCallback();
|
|
|
|
if (_widget->isHidden()) {
|
|
|
|
_widget->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::updateCallback() {
|
2016-10-03 10:08:22 +00:00
|
|
|
if (_animation.animating()) {
|
2016-07-12 11:38:16 +00:00
|
|
|
_widget->update();
|
2016-09-26 12:09:59 +00:00
|
|
|
if (_updatedCallback) {
|
2019-03-07 11:35:28 +00:00
|
|
|
_updatedCallback(_animation.value(_visible ? 1. : 0.));
|
2016-09-26 12:09:59 +00:00
|
|
|
}
|
2016-07-12 11:38:16 +00:00
|
|
|
} else {
|
|
|
|
stopAnimation();
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Ui
|