2016-10-26 10:06:00 +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
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-10-26 10:06:00 +00:00
|
|
|
*/
|
|
|
|
#include "ui/effects/widget_slide_wrap.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
WidgetSlideWrap<TWidget>::WidgetSlideWrap(QWidget *parent
|
2016-12-13 17:07:56 +00:00
|
|
|
, object_ptr<TWidget> entity
|
2016-10-26 10:06:00 +00:00
|
|
|
, style::margins entityPadding
|
2017-02-26 11:32:13 +00:00
|
|
|
, base::lambda<void()> updateCallback
|
2016-10-26 10:06:00 +00:00
|
|
|
, int duration) : TWidget(parent)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _entity(std::move(entity))
|
2016-10-26 10:06:00 +00:00
|
|
|
, _padding(entityPadding)
|
|
|
|
, _duration(duration)
|
2017-02-21 13:45:56 +00:00
|
|
|
, _updateCallback(std::move(updateCallback)) {
|
2016-10-26 10:06:00 +00:00
|
|
|
_entity->setParent(this);
|
2016-12-02 19:16:35 +00:00
|
|
|
auto margins = getMargins();
|
|
|
|
_entity->moveToLeft(margins.left() + _padding.left(), margins.top() + _padding.top());
|
|
|
|
_realSize = _entity->rectNoMargins().marginsAdded(_padding).size();
|
2016-10-26 10:06:00 +00:00
|
|
|
_entity->installEventFilter(this);
|
2016-12-02 19:16:35 +00:00
|
|
|
resizeToWidth(_realSize.width());
|
2016-10-26 10:06:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:10:18 +00:00
|
|
|
void WidgetSlideWrap<TWidget>::hideAnimated() {
|
2016-10-26 10:06:00 +00:00
|
|
|
if (isHidden()) {
|
|
|
|
_forceHeight = 0;
|
2016-12-02 19:16:35 +00:00
|
|
|
resizeToWidth(_realSize.width());
|
2016-10-26 10:06:00 +00:00
|
|
|
if (_updateCallback) _updateCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_a_height.animating()) {
|
|
|
|
if (_hiding) return;
|
|
|
|
}
|
|
|
|
_hiding = true;
|
2016-12-07 13:32:25 +00:00
|
|
|
_a_height.start([this] { animationCallback(); }, _realSize.height(), 0., _duration);
|
2016-10-26 10:06:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:10:18 +00:00
|
|
|
void WidgetSlideWrap<TWidget>::showAnimated() {
|
2016-10-26 10:06:00 +00:00
|
|
|
if (isHidden()) {
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
if (_forceHeight < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_a_height.animating()) {
|
|
|
|
if (!_hiding) return;
|
|
|
|
}
|
|
|
|
_hiding = false;
|
2016-12-07 13:32:25 +00:00
|
|
|
_forceHeight = qRound(_a_height.current(0.));
|
|
|
|
_a_height.start([this] { animationCallback(); }, 0., _realSize.height(), _duration);
|
2016-10-26 10:06:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-15 15:10:18 +00:00
|
|
|
void WidgetSlideWrap<TWidget>::toggleFast(bool visible) {
|
2017-08-14 12:48:11 +00:00
|
|
|
_hiding = !visible;
|
|
|
|
if (!_hiding) show();
|
2016-12-07 13:32:25 +00:00
|
|
|
_a_height.finish();
|
2017-08-14 12:48:11 +00:00
|
|
|
_forceHeight = _hiding ? 0 : -1;
|
2016-12-02 19:16:35 +00:00
|
|
|
resizeToWidth(_realSize.width());
|
2017-08-14 12:48:11 +00:00
|
|
|
if (_hiding) hide();
|
2016-10-26 10:06:00 +00:00
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 19:16:35 +00:00
|
|
|
QMargins WidgetSlideWrap<TWidget>::getMargins() const {
|
|
|
|
auto entityMargins = _entity->getMargins();
|
|
|
|
if (_forceHeight < 0) {
|
|
|
|
return entityMargins;
|
|
|
|
}
|
|
|
|
return QMargins(entityMargins.left(), 0, entityMargins.right(), 0);
|
|
|
|
}
|
|
|
|
|
2016-10-26 10:06:00 +00:00
|
|
|
int WidgetSlideWrap<TWidget>::naturalWidth() const {
|
|
|
|
auto inner = _entity->naturalWidth();
|
|
|
|
return (inner < 0) ? inner : (_padding.left() + inner + _padding.right());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WidgetSlideWrap<TWidget>::eventFilter(QObject *object, QEvent *event) {
|
|
|
|
if (object == _entity && event->type() == QEvent::Resize) {
|
2016-12-02 19:16:35 +00:00
|
|
|
_realSize = _entity->rectNoMargins().marginsAdded(_padding).size();
|
2016-10-26 10:06:00 +00:00
|
|
|
if (!_inResizeToWidth) {
|
2016-12-02 19:16:35 +00:00
|
|
|
resizeToWidth(_realSize.width());
|
2016-10-26 10:06:00 +00:00
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TWidget::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WidgetSlideWrap<TWidget>::resizeGetHeight(int newWidth) {
|
|
|
|
_inResizeToWidth = true;
|
2016-12-02 19:16:35 +00:00
|
|
|
auto resized = (_forceHeight >= 0);
|
2016-10-26 10:06:00 +00:00
|
|
|
_entity->resizeToWidth(newWidth - _padding.left() - _padding.right());
|
2016-12-02 19:16:35 +00:00
|
|
|
auto margins = getMargins();
|
|
|
|
_entity->moveToLeft(margins.left() + _padding.left(), margins.top() + _padding.top());
|
2016-10-26 10:06:00 +00:00
|
|
|
_inResizeToWidth = false;
|
2016-12-02 19:16:35 +00:00
|
|
|
if (resized) {
|
|
|
|
return _forceHeight;
|
|
|
|
}
|
2017-08-14 12:48:11 +00:00
|
|
|
_realSize = _entity->rectNoMargins().marginsAdded(_padding).size();
|
2016-12-02 19:16:35 +00:00
|
|
|
return _realSize.height();
|
2016-10-26 10:06:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 13:32:25 +00:00
|
|
|
void WidgetSlideWrap<TWidget>::animationCallback() {
|
|
|
|
_forceHeight = qRound(_a_height.current(_hiding ? 0 : -1));
|
|
|
|
resizeToWidth(_realSize.width());
|
|
|
|
if (!_a_height.animating()) {
|
2016-10-26 10:06:00 +00:00
|
|
|
_forceHeight = _hiding ? 0 : -1;
|
|
|
|
if (_hiding) hide();
|
|
|
|
}
|
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Ui
|