2016-07-11 18:05:46 +00:00
|
|
|
/*
|
|
|
|
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"
|
2016-10-26 10:06:00 +00:00
|
|
|
#include "ui/effects/widget_fade_wrap.h"
|
2016-07-11 18:05:46 +00:00
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
FadeAnimation::FadeAnimation(TWidget *widget) : _widget(widget) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FadeAnimation::paint(Painter &p) {
|
|
|
|
if (_cache.isNull()) return false;
|
|
|
|
|
2016-12-09 18:56:01 +00:00
|
|
|
auto opacity = _animation.current(getms(), _visible ? 1. : 0.);
|
|
|
|
p.setOpacity(opacity);
|
2016-07-11 18:05:46 +00:00
|
|
|
p.drawPixmap(0, 0, _cache);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
void FadeAnimation::refreshCache() {
|
|
|
|
if (!_cache.isNull()) {
|
|
|
|
_cache = QPixmap();
|
|
|
|
_cache = myGrab(_widget);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
void FadeAnimation::setFinishedCallback(FinishedCallback &&callback) {
|
|
|
|
_finishedCallback = std_::move(callback);
|
|
|
|
}
|
|
|
|
|
2016-07-12 11:38:16 +00:00
|
|
|
void FadeAnimation::setUpdatedCallback(UpdatedCallback &&callback) {
|
|
|
|
_updatedCallback = std_::move(callback);
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
void FadeAnimation::show() {
|
|
|
|
_visible = true;
|
|
|
|
stopAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::hide() {
|
|
|
|
_visible = false;
|
|
|
|
stopAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FadeAnimation::stopAnimation() {
|
|
|
|
_animation.finish();
|
|
|
|
if (!_cache.isNull()) {
|
|
|
|
_cache = QPixmap();
|
|
|
|
updateCallback();
|
2016-12-09 18:56:01 +00:00
|
|
|
if (_visible) {
|
|
|
|
_widget->showChildren();
|
|
|
|
}
|
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()) {
|
2016-11-24 19:28:23 +00:00
|
|
|
_widget->showChildren();
|
2016-07-11 18:05:46 +00:00
|
|
|
_cache = myGrab(_widget);
|
|
|
|
_widget->hideChildren();
|
|
|
|
}
|
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) {
|
|
|
|
_updatedCallback(_animation.current(_visible ? 1. : 0.));
|
|
|
|
}
|
2016-07-12 11:38:16 +00:00
|
|
|
} else {
|
|
|
|
stopAnimation();
|
|
|
|
}
|
2016-07-11 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:06:00 +00:00
|
|
|
WidgetFadeWrap<TWidget>::WidgetFadeWrap(QWidget *parent
|
|
|
|
, TWidget *entity
|
2016-11-20 12:54:07 +00:00
|
|
|
, base::lambda<void()> &&updateCallback
|
2016-10-26 10:06:00 +00:00
|
|
|
, int duration) : TWidget(parent)
|
|
|
|
, _entity(entity)
|
|
|
|
, _duration(duration)
|
|
|
|
, _updateCallback(std_::move(updateCallback))
|
|
|
|
, _animation(this) {
|
|
|
|
_animation.show();
|
|
|
|
if (_updateCallback) {
|
|
|
|
_animation.setFinishedCallback([this] { _updateCallback(); });
|
|
|
|
_animation.setUpdatedCallback([this](float64 opacity) { _updateCallback(); });
|
|
|
|
}
|
|
|
|
_entity->setParent(this);
|
|
|
|
_entity->moveToLeft(0, 0);
|
|
|
|
_entity->installEventFilter(this);
|
|
|
|
resize(_entity->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WidgetFadeWrap<TWidget>::eventFilter(QObject *object, QEvent *event) {
|
|
|
|
if (object == _entity && event->type() == QEvent::Resize) {
|
|
|
|
resize(_entity->rect().size());
|
|
|
|
}
|
|
|
|
return TWidget::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WidgetFadeWrap<TWidget>::paintEvent(QPaintEvent *e) {
|
|
|
|
Painter p(this);
|
|
|
|
_animation.paint(p);
|
|
|
|
}
|
|
|
|
|
2016-07-11 18:05:46 +00:00
|
|
|
} // namespace Ui
|