2017-09-13 16:57:44 +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.
|
2017-09-13 16:57:44 +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
|
2017-09-13 16:57:44 +00:00
|
|
|
*/
|
|
|
|
#include "ui/wrap/slide_wrap.h"
|
|
|
|
|
2017-10-04 12:39:59 +00:00
|
|
|
#include <rpl/combine.h>
|
2017-11-20 12:23:20 +00:00
|
|
|
#include <range/v3/algorithm/find.hpp>
|
2017-10-04 12:39:59 +00:00
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
SlideWrap<RpWidget>::SlideWrap(
|
|
|
|
QWidget *parent,
|
2017-09-30 18:26:45 +00:00
|
|
|
object_ptr<RpWidget> &&child)
|
2017-09-13 16:57:44 +00:00
|
|
|
: SlideWrap(
|
|
|
|
parent,
|
|
|
|
std::move(child),
|
2017-09-21 19:21:33 +00:00
|
|
|
style::margins()) {
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SlideWrap<RpWidget>::SlideWrap(
|
|
|
|
QWidget *parent,
|
|
|
|
const style::margins &padding)
|
2017-09-21 19:21:33 +00:00
|
|
|
: SlideWrap(parent, nullptr, padding) {
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SlideWrap<RpWidget>::SlideWrap(
|
|
|
|
QWidget *parent,
|
2017-09-30 18:26:45 +00:00
|
|
|
object_ptr<RpWidget> &&child,
|
2017-09-21 10:28:34 +00:00
|
|
|
const style::margins &padding)
|
|
|
|
: Parent(
|
|
|
|
parent,
|
|
|
|
object_ptr<PaddingWrap<RpWidget>>(
|
|
|
|
parent,
|
|
|
|
std::move(child),
|
|
|
|
padding))
|
2017-09-21 19:21:33 +00:00
|
|
|
, _duration(st::slideWrapDuration) {
|
|
|
|
}
|
|
|
|
|
|
|
|
SlideWrap<RpWidget> *SlideWrap<RpWidget>::setDuration(int duration) {
|
|
|
|
_duration = duration;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-10-01 08:15:40 +00:00
|
|
|
SlideWrap<RpWidget> *SlideWrap<RpWidget>::toggle(
|
|
|
|
bool shown,
|
|
|
|
anim::type animated) {
|
2017-11-19 11:37:15 +00:00
|
|
|
auto animate = (animated == anim::type::normal) && _duration;
|
2017-10-01 08:15:40 +00:00
|
|
|
auto changed = (_toggled != shown);
|
|
|
|
if (changed) {
|
|
|
|
_toggled = shown;
|
2017-11-19 11:37:15 +00:00
|
|
|
if (animate) {
|
2017-10-01 08:15:40 +00:00
|
|
|
_animation.start(
|
|
|
|
[this] { animationStep(); },
|
|
|
|
_toggled ? 0. : 1.,
|
|
|
|
_toggled ? 1. : 0.,
|
|
|
|
_duration,
|
|
|
|
anim::linear);
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 11:37:15 +00:00
|
|
|
if (animate) {
|
2017-10-01 08:15:40 +00:00
|
|
|
animationStep();
|
|
|
|
} else {
|
|
|
|
finishAnimating();
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
_toggledChanged.fire_copy(_toggled);
|
2017-09-21 19:21:33 +00:00
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-09-30 19:20:40 +00:00
|
|
|
SlideWrap<RpWidget> *SlideWrap<RpWidget>::finishAnimating() {
|
2017-09-30 18:26:45 +00:00
|
|
|
_animation.finish();
|
2017-09-21 19:21:33 +00:00
|
|
|
animationStep();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SlideWrap<RpWidget> *SlideWrap<RpWidget>::toggleOn(
|
|
|
|
rpl::producer<bool> &&shown) {
|
2017-12-22 07:05:20 +00:00
|
|
|
std::move(
|
|
|
|
shown
|
|
|
|
) | rpl::start_with_next([this](bool shown) {
|
|
|
|
toggle(shown, anim::type::normal);
|
|
|
|
}, lifetime());
|
2017-09-30 19:20:40 +00:00
|
|
|
finishAnimating();
|
2017-09-21 19:21:33 +00:00
|
|
|
return this;
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SlideWrap<RpWidget>::animationStep() {
|
|
|
|
auto newWidth = width();
|
|
|
|
if (auto weak = wrapped()) {
|
|
|
|
auto margins = getMargins();
|
|
|
|
weak->moveToLeft(margins.left(), margins.top());
|
|
|
|
newWidth = weak->width();
|
|
|
|
}
|
2017-10-01 08:15:40 +00:00
|
|
|
auto current = _animation.current(_toggled ? 1. : 0.);
|
2017-09-13 16:57:44 +00:00
|
|
|
auto newHeight = wrapped()
|
2017-09-30 18:26:45 +00:00
|
|
|
? (_animation.animating()
|
2017-09-13 16:57:44 +00:00
|
|
|
? anim::interpolate(0, wrapped()->heightNoMargins(), current)
|
2017-10-01 08:15:40 +00:00
|
|
|
: (_toggled ? wrapped()->height() : 0))
|
2017-09-13 16:57:44 +00:00
|
|
|
: 0;
|
|
|
|
if (newWidth != width() || newHeight != height()) {
|
|
|
|
resize(newWidth, newHeight);
|
|
|
|
}
|
2017-10-01 08:15:40 +00:00
|
|
|
auto shouldBeHidden = !_toggled && !_animation.animating();
|
2017-09-13 16:57:44 +00:00
|
|
|
if (shouldBeHidden != isHidden()) {
|
2017-12-27 07:18:09 +00:00
|
|
|
const auto guard = make_weak(this);
|
2017-09-13 16:57:44 +00:00
|
|
|
setVisible(!shouldBeHidden);
|
2017-12-27 07:18:09 +00:00
|
|
|
if (shouldBeHidden && guard) {
|
2017-12-26 12:41:48 +00:00
|
|
|
SendPendingMoveResizeEvents(this);
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QMargins SlideWrap<RpWidget>::getMargins() const {
|
|
|
|
auto result = wrapped()->getMargins();
|
2017-10-01 08:15:40 +00:00
|
|
|
return (animating() || !_toggled)
|
2017-09-13 16:57:44 +00:00
|
|
|
? QMargins(result.left(), 0, result.right(), 0)
|
|
|
|
: result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SlideWrap<RpWidget>::resizeGetHeight(int newWidth) {
|
|
|
|
if (wrapped()) {
|
|
|
|
wrapped()->resizeToWidth(newWidth);
|
|
|
|
}
|
|
|
|
return heightNoMargins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SlideWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
|
2017-09-30 18:26:45 +00:00
|
|
|
if (_animation.animating()) {
|
2017-09-13 16:57:44 +00:00
|
|
|
animationStep();
|
2017-10-01 08:15:40 +00:00
|
|
|
} else if (_toggled) {
|
2017-09-13 16:57:44 +00:00
|
|
|
resize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 12:39:59 +00:00
|
|
|
rpl::producer<bool> MultiSlideTracker::atLeastOneShownValue() const {
|
|
|
|
auto shown = std::vector<rpl::producer<bool>>();
|
|
|
|
shown.reserve(_widgets.size());
|
|
|
|
for (auto &widget : _widgets) {
|
|
|
|
shown.push_back(widget->toggledValue());
|
|
|
|
}
|
|
|
|
return rpl::combine(
|
|
|
|
std::move(shown),
|
|
|
|
[](const std::vector<bool> &values) {
|
2017-11-20 12:23:20 +00:00
|
|
|
return ranges::find(values, true) != values.end();
|
2017-10-04 12:39:59 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
} // namespace Ui
|
|
|
|
|