tdesktop/Telegram/SourceFiles/ui/effects/fade_animation.cpp

159 lines
3.6 KiB
C++
Raw Normal View History

2016-07-11 18:05:46 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2016-07-11 18:05:46 +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
*/
#include "ui/effects/fade_animation.h"
2016-07-11 18:05:46 +00:00
namespace Ui {
namespace {
2016-07-11 18:05:46 +00:00
constexpr int kWideScale = 5;
} // namespace
FadeAnimation::FadeAnimation(TWidget *widget, float64 scale)
: _widget(widget)
, _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.);
p.setOpacity(opacity);
if (_scale < 1.) {
PainterHighQualityEnabler hq(p);
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);
} else {
2018-04-06 16:23:09 +00:00
p.drawPixmap(0, 0, cache);
}
2016-07-11 18:05:46 +00:00
return true;
}
void FadeAnimation::refreshCache() {
if (!_cache.isNull()) {
_cache = QPixmap();
_cache = grabContent();
Assert(!_cache.isNull());
}
}
QPixmap FadeAnimation::grabContent() {
SendPendingMoveResizeEvents(_widget);
_size = _widget->size();
if (_size.isEmpty()) {
auto image = QImage(
cIntRetinaFactor(),
cIntRetinaFactor(),
QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
return App::pixmapFromImageInPlace(std::move(image));
}
auto widgetContent = GrabWidget(_widget);
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));
}
return widgetContent;
}
2016-07-11 18:05:46 +00:00
void FadeAnimation::setFinishedCallback(FinishedCallback &&callback) {
_finishedCallback = std::move(callback);
2016-07-11 18:05:46 +00:00
}
void FadeAnimation::setUpdatedCallback(UpdatedCallback &&callback) {
_updatedCallback = std::move(callback);
}
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();
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()) {
_cache = grabContent();
Assert(!_cache.isNull());
2016-07-11 18:05:46 +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() {
_widget->update();
if (_updatedCallback) {
_updatedCallback(_animation.value(_visible ? 1. : 0.));
}
if (!_animation.animating()) {
stopAnimation();
}
2016-07-11 18:05:46 +00:00
}
} // namespace Ui