2016-08-22 17:16:21 +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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "styles/style_widgets.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
template <typename Widget>
|
|
|
|
class WidgetSlideWrap : public TWidget {
|
|
|
|
public:
|
2016-10-12 19:34:25 +00:00
|
|
|
using UpdateCallback = base::lambda_unique<void()>;
|
2016-08-22 17:16:21 +00:00
|
|
|
WidgetSlideWrap(QWidget *parent, Widget *entity
|
|
|
|
, style::margins entityPadding
|
2016-10-12 19:34:25 +00:00
|
|
|
, UpdateCallback &&updateCallback
|
2016-08-22 17:16:21 +00:00
|
|
|
, int duration = st::widgetSlideDuration) : TWidget(parent)
|
2016-10-12 19:34:25 +00:00
|
|
|
, _entity(entity)
|
|
|
|
, _padding(entityPadding)
|
|
|
|
, _duration(duration)
|
|
|
|
, _updateCallback(std_::move(updateCallback))
|
|
|
|
, _a_height(animation(this, &WidgetSlideWrap<Widget>::step_height)) {
|
|
|
|
_entity->setParent(this);
|
|
|
|
_entity->moveToLeft(_padding.left(), _padding.top());
|
|
|
|
_realSize = _entity->rect().marginsAdded(_padding).size();
|
|
|
|
_entity->installEventFilter(this);
|
2016-08-22 17:16:21 +00:00
|
|
|
resize(_realSize);
|
|
|
|
}
|
|
|
|
|
2016-08-28 20:23:32 +00:00
|
|
|
bool eventFilter(QObject *object, QEvent *event) override {
|
2016-08-22 17:16:21 +00:00
|
|
|
if (object == _entity && event->type() == QEvent::Resize) {
|
|
|
|
_realSize = _entity->rect().marginsAdded(_padding).size();
|
|
|
|
if (!_inResizeToWidth) {
|
|
|
|
resize(_realSize.width(), (_forceHeight >= 0) ? _forceHeight : _realSize.height());
|
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TWidget::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void slideUp() {
|
|
|
|
if (isHidden()) {
|
|
|
|
_forceHeight = 0;
|
|
|
|
resize(_realSize.width(), _forceHeight);
|
|
|
|
if (_updateCallback) _updateCallback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_a_height.animating()) {
|
|
|
|
if (_hiding) return;
|
|
|
|
} else {
|
|
|
|
a_height = anim::ivalue(_realSize.height());
|
|
|
|
}
|
|
|
|
a_height.start(0);
|
|
|
|
_hiding = true;
|
|
|
|
_a_height.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void slideDown() {
|
|
|
|
if (isHidden()) {
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
if (_forceHeight < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_a_height.animating()) {
|
|
|
|
if (!_hiding) return;
|
|
|
|
}
|
|
|
|
a_height.start(_realSize.height());
|
|
|
|
_forceHeight = a_height.current();
|
|
|
|
_hiding = false;
|
|
|
|
_a_height.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void showFast() {
|
2016-10-12 19:34:25 +00:00
|
|
|
show();
|
2016-08-22 17:16:21 +00:00
|
|
|
_a_height.stop();
|
|
|
|
resize(_realSize);
|
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hideFast() {
|
|
|
|
_a_height.stop();
|
|
|
|
a_height = anim::ivalue(0);
|
|
|
|
_forceHeight = 0;
|
|
|
|
resize(_realSize.width(), 0);
|
|
|
|
hide();
|
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget *entity() {
|
|
|
|
return _entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Widget *entity() const {
|
|
|
|
return _entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int naturalWidth() const override {
|
2016-08-27 17:52:05 +00:00
|
|
|
auto inner = _entity->naturalWidth();
|
|
|
|
return (inner < 0) ? inner : (_padding.left() + inner + _padding.right());
|
2016-08-22 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
int resizeGetHeight(int newWidth) override {
|
|
|
|
_inResizeToWidth = true;
|
|
|
|
_entity->resizeToWidth(newWidth - _padding.left() - _padding.right());
|
|
|
|
_inResizeToWidth = false;
|
|
|
|
return (_forceHeight >= 0) ? _forceHeight : _realSize.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void step_height(float64 ms, bool timer) {
|
|
|
|
auto dt = ms / _duration;
|
|
|
|
if (dt >= 1) {
|
|
|
|
a_height.finish();
|
|
|
|
_a_height.stop();
|
|
|
|
_forceHeight = _hiding ? 0 : -1;
|
|
|
|
if (_hiding) hide();
|
|
|
|
} else {
|
|
|
|
a_height.update(dt, anim::linear);
|
|
|
|
_forceHeight = a_height.current();
|
|
|
|
}
|
|
|
|
resize(_realSize.width(), (_forceHeight >= 0) ? _forceHeight : _realSize.height());
|
|
|
|
if (_updateCallback) {
|
|
|
|
_updateCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget *_entity;
|
|
|
|
bool _inResizeToWidth = false;
|
|
|
|
style::margins _padding;
|
|
|
|
int _duration;
|
2016-10-12 19:34:25 +00:00
|
|
|
UpdateCallback _updateCallback;
|
2016-08-22 17:16:21 +00:00
|
|
|
|
|
|
|
style::size _realSize;
|
|
|
|
int _forceHeight = -1;
|
|
|
|
anim::ivalue a_height;
|
|
|
|
Animation _a_height;
|
|
|
|
bool _hiding = false;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Ui
|