2016-06-17 18:18:01 +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 16:43:13 +00:00
|
|
|
#include "ui/widgets/inner_dropdown.h"
|
2016-06-17 18:18:01 +00:00
|
|
|
|
|
|
|
#include "mainwindow.h"
|
2016-11-16 10:44:06 +00:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
2016-11-09 13:40:51 +00:00
|
|
|
#include "ui/widgets/shadow.h"
|
|
|
|
#include "ui/effects/panel_animation.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr float64 kFadeHeight = 1. / 3;
|
|
|
|
constexpr int kFadeAlphaMax = 160;
|
|
|
|
|
|
|
|
} // namespace
|
2016-06-17 18:18:01 +00:00
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
InnerDropdown::InnerDropdown(QWidget *parent, const style::InnerDropdown &st) : TWidget(parent)
|
2016-06-17 18:18:01 +00:00
|
|
|
, _st(st)
|
2016-10-26 16:43:13 +00:00
|
|
|
, _scroll(this, _st.scroll) {
|
2016-06-17 18:18:01 +00:00
|
|
|
_hideTimer.setSingleShot(true);
|
2016-10-26 16:43:13 +00:00
|
|
|
connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(onHideAnimated()));
|
2016-06-17 18:18:01 +00:00
|
|
|
|
|
|
|
connect(_scroll, SIGNAL(scrolled()), this, SLOT(onScroll()));
|
|
|
|
|
|
|
|
if (cPlatform() == dbipMac || cPlatform() == dbipMacOld) {
|
|
|
|
connect(App::wnd()->windowHandle(), SIGNAL(activeChanged()), this, SLOT(onWindowActiveChanged()));
|
|
|
|
}
|
|
|
|
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::setOwnedWidget(TWidget *widget) {
|
|
|
|
connect(widget, SIGNAL(heightUpdated()), this, SLOT(onWidgetHeightUpdated()));
|
2016-12-13 17:07:56 +00:00
|
|
|
auto container = _scroll->setOwnedWidget(object_ptr<Container>(_scroll, widget, _st));
|
2016-06-20 11:31:12 +00:00
|
|
|
container->resizeToWidth(_scroll->width());
|
|
|
|
container->moveToLeft(0, 0);
|
|
|
|
container->show();
|
2016-06-17 18:18:01 +00:00
|
|
|
widget->show();
|
2016-06-20 11:31:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::setMaxHeight(int newMaxHeight) {
|
|
|
|
_maxHeight = newMaxHeight;
|
2016-10-26 16:43:13 +00:00
|
|
|
resizeToContent();
|
2016-06-20 11:31:12 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::resizeToContent() {
|
|
|
|
auto newWidth = _st.padding.left() + _st.scrollMargin.left() + _st.scrollMargin.right() + _st.padding.right();
|
|
|
|
auto newHeight = _st.padding.top() + _st.scrollMargin.top() + _st.scrollMargin.bottom() + _st.padding.bottom();
|
|
|
|
if (auto widget = static_cast<Container*>(_scroll->widget())) {
|
|
|
|
widget->resizeToContent();
|
|
|
|
newWidth += widget->width();
|
2016-06-20 11:31:12 +00:00
|
|
|
newHeight += widget->height();
|
|
|
|
}
|
|
|
|
if (_maxHeight > 0) {
|
|
|
|
accumulate_min(newHeight, _maxHeight);
|
|
|
|
}
|
2016-10-26 16:43:13 +00:00
|
|
|
if (newWidth != width() || newHeight != height()) {
|
|
|
|
resize(newWidth, newHeight);
|
2016-06-20 11:31:12 +00:00
|
|
|
}
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::onWindowActiveChanged() {
|
|
|
|
if (!App::wnd()->windowHandle()->isActive() && !isHidden()) {
|
|
|
|
leaveEvent(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::resizeEvent(QResizeEvent *e) {
|
2016-06-20 11:31:12 +00:00
|
|
|
_scroll->setGeometry(rect().marginsRemoved(_st.padding).marginsRemoved(_st.scrollMargin));
|
2016-10-26 16:43:13 +00:00
|
|
|
if (auto widget = static_cast<TWidget*>(_scroll->widget())) {
|
2016-06-20 11:31:12 +00:00
|
|
|
widget->resizeToWidth(_scroll->width());
|
2016-06-17 18:18:01 +00:00
|
|
|
onScroll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::onScroll() {
|
2016-10-26 16:43:13 +00:00
|
|
|
if (auto widget = static_cast<TWidget*>(_scroll->widget())) {
|
2016-06-17 18:18:01 +00:00
|
|
|
int visibleTop = _scroll->scrollTop();
|
|
|
|
int visibleBottom = visibleTop + _scroll->height();
|
|
|
|
widget->setVisibleTopBottom(visibleTop, visibleBottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::paintEvent(QPaintEvent *e) {
|
2016-06-20 11:31:12 +00:00
|
|
|
Painter p(this);
|
2016-06-17 18:18:01 +00:00
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
auto ms = getms();
|
|
|
|
if (_a_show.animating(ms)) {
|
|
|
|
if (auto opacity = _a_opacity.current(ms, _hiding ? 0. : 1.)) {
|
2016-11-12 15:02:19 +00:00
|
|
|
_showAnimation->paintFrame(p, 0, 0, width(), _a_show.current(1.), opacity);
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
2016-11-09 13:40:51 +00:00
|
|
|
} else if (_a_opacity.animating(ms)) {
|
|
|
|
p.setOpacity(_a_opacity.current(0.));
|
2016-06-17 18:18:01 +00:00
|
|
|
p.drawPixmap(0, 0, _cache);
|
2016-11-09 13:40:51 +00:00
|
|
|
} else if (_hiding || isHidden()) {
|
|
|
|
hideFinished();
|
|
|
|
} else if (_showAnimation) {
|
2016-11-12 15:02:19 +00:00
|
|
|
_showAnimation->paintFrame(p, 0, 0, width(), 1., 1.);
|
2016-11-09 13:40:51 +00:00
|
|
|
_showAnimation.reset();
|
|
|
|
showChildren();
|
2016-11-10 18:10:31 +00:00
|
|
|
} else {
|
2016-11-12 15:02:19 +00:00
|
|
|
if (!_cache.isNull()) _cache = QPixmap();
|
2016-11-10 18:10:31 +00:00
|
|
|
auto inner = rect().marginsRemoved(_st.padding);
|
|
|
|
Shadow::paint(p, inner, width(), _st.shadow);
|
|
|
|
App::roundRect(p, inner, _st.bg, ImageRoundRadius::Small);
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::enterEvent(QEvent *e) {
|
2016-11-09 13:40:51 +00:00
|
|
|
showAnimated(_origin);
|
2016-06-17 18:18:01 +00:00
|
|
|
return TWidget::enterEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::leaveEvent(QEvent *e) {
|
2016-11-09 13:40:51 +00:00
|
|
|
auto ms = getms();
|
|
|
|
if (_a_show.animating(ms) || _a_opacity.animating(ms)) {
|
2016-10-26 16:43:13 +00:00
|
|
|
hideAnimated();
|
2016-06-17 18:18:01 +00:00
|
|
|
} else {
|
|
|
|
_hideTimer.start(300);
|
|
|
|
}
|
|
|
|
return TWidget::leaveEvent(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::otherEnter() {
|
2016-11-09 13:40:51 +00:00
|
|
|
showAnimated(_origin);
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::otherLeave() {
|
2016-11-09 13:40:51 +00:00
|
|
|
auto ms = getms();
|
|
|
|
if (_a_show.animating(ms) || _a_opacity.animating(ms)) {
|
2016-10-26 16:43:13 +00:00
|
|
|
hideAnimated();
|
2016-06-17 18:18:01 +00:00
|
|
|
} else {
|
|
|
|
_hideTimer.start(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
void InnerDropdown::setOrigin(PanelAnimation::Origin origin) {
|
|
|
|
_origin = origin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::showAnimated(PanelAnimation::Origin origin) {
|
|
|
|
setOrigin(origin);
|
2016-10-26 16:43:13 +00:00
|
|
|
_hideTimer.stop();
|
|
|
|
showStarted();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::hideAnimated(HideOption option) {
|
|
|
|
if (isHidden()) return;
|
|
|
|
if (option == HideOption::IgnoreShow) {
|
|
|
|
_ignoreShowEvents = true;
|
|
|
|
}
|
2016-06-17 18:18:01 +00:00
|
|
|
if (_hiding) return;
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
_hideTimer.stop();
|
2016-11-09 13:40:51 +00:00
|
|
|
startOpacityAnimation(true);
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::hideFast() {
|
|
|
|
if (isHidden()) return;
|
|
|
|
|
|
|
|
_hideTimer.stop();
|
|
|
|
_hiding = false;
|
2016-11-09 13:40:51 +00:00
|
|
|
_a_opacity.finish();
|
2016-10-26 16:43:13 +00:00
|
|
|
hideFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::hideFinished() {
|
2016-11-09 13:40:51 +00:00
|
|
|
_a_show.finish();
|
2016-10-26 16:43:13 +00:00
|
|
|
_cache = QPixmap();
|
|
|
|
_ignoreShowEvents = false;
|
|
|
|
if (!isHidden()) {
|
2016-11-05 08:36:24 +00:00
|
|
|
if (_hiddenCallback) {
|
|
|
|
_hiddenCallback();
|
|
|
|
}
|
2016-10-26 16:43:13 +00:00
|
|
|
hide();
|
|
|
|
}
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
void InnerDropdown::prepareCache() {
|
|
|
|
if (_a_opacity.animating()) return;
|
|
|
|
|
|
|
|
auto showAnimation = base::take(_a_show);
|
|
|
|
auto showAnimationData = base::take(_showAnimation);
|
|
|
|
showChildren();
|
|
|
|
_cache = myGrab(this);
|
|
|
|
_showAnimation = base::take(showAnimationData);
|
|
|
|
_a_show = base::take(showAnimation);
|
2016-11-12 15:02:19 +00:00
|
|
|
if (_a_show.animating()) {
|
|
|
|
hideChildren();
|
|
|
|
}
|
2016-11-09 13:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::startOpacityAnimation(bool hiding) {
|
2016-11-16 12:06:02 +00:00
|
|
|
if (hiding) {
|
|
|
|
if (_hideStartCallback) {
|
|
|
|
_hideStartCallback();
|
|
|
|
}
|
|
|
|
} else if (_showStartCallback) {
|
|
|
|
_showStartCallback();
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
_hiding = false;
|
|
|
|
prepareCache();
|
|
|
|
_hiding = hiding;
|
|
|
|
hideChildren();
|
|
|
|
_a_opacity.start([this] { opacityAnimationCallback(); }, _hiding ? 1. : 0., _hiding ? 0. : 1., _st.duration);
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::showStarted() {
|
|
|
|
if (_ignoreShowEvents) return;
|
2016-06-17 18:18:01 +00:00
|
|
|
if (isHidden()) {
|
|
|
|
show();
|
2016-11-09 13:40:51 +00:00
|
|
|
startShowAnimation();
|
|
|
|
return;
|
2016-06-17 18:18:01 +00:00
|
|
|
} else if (!_hiding) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-09 13:40:51 +00:00
|
|
|
startOpacityAnimation(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::startShowAnimation() {
|
2016-11-16 12:06:02 +00:00
|
|
|
if (_showStartCallback) {
|
|
|
|
_showStartCallback();
|
|
|
|
}
|
2016-11-09 13:40:51 +00:00
|
|
|
if (!_a_show.animating()) {
|
|
|
|
auto opacityAnimation = base::take(_a_opacity);
|
|
|
|
showChildren();
|
|
|
|
auto cache = grabForPanelAnimation();
|
|
|
|
_a_opacity = base::take(opacityAnimation);
|
|
|
|
|
|
|
|
_showAnimation = std_::make_unique<PanelAnimation>(_st.animation, _origin);
|
2016-11-11 08:59:55 +00:00
|
|
|
auto inner = rect().marginsRemoved(_st.padding);
|
|
|
|
_showAnimation->setFinalImage(std_::move(cache), QRect(inner.topLeft() * cIntRetinaFactor(), inner.size() * cIntRetinaFactor()));
|
2016-11-09 13:40:51 +00:00
|
|
|
auto corners = App::cornersMask(ImageRoundRadius::Small);
|
|
|
|
_showAnimation->setCornerMasks(QImage(*corners[0]), QImage(*corners[1]), QImage(*corners[2]), QImage(*corners[3]));
|
|
|
|
_showAnimation->start();
|
|
|
|
}
|
|
|
|
hideChildren();
|
|
|
|
_a_show.start([this] { showAnimationCallback(); }, 0., 1., _st.showDuration);
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
QImage InnerDropdown::grabForPanelAnimation() {
|
|
|
|
myEnsureResized(this);
|
|
|
|
auto result = QImage(size() * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied);
|
|
|
|
result.setDevicePixelRatio(cRetinaFactor());
|
|
|
|
result.fill(Qt::transparent);
|
|
|
|
{
|
|
|
|
Painter p(&result);
|
|
|
|
App::roundRect(p, rect().marginsRemoved(_st.padding), _st.bg, ImageRoundRadius::Small);
|
|
|
|
for (auto child : children()) {
|
|
|
|
if (auto widget = qobject_cast<QWidget*>(child)) {
|
|
|
|
widget->render(&p, widget->pos(), widget->rect(), QWidget::DrawChildren | QWidget::IgnoreMask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std_::move(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InnerDropdown::opacityAnimationCallback() {
|
2016-06-17 18:18:01 +00:00
|
|
|
update();
|
2016-11-10 18:10:31 +00:00
|
|
|
if (!_a_opacity.animating()) {
|
|
|
|
if (_hiding) {
|
|
|
|
_hiding = false;
|
|
|
|
hideFinished();
|
2016-11-12 15:02:19 +00:00
|
|
|
} else if (!_a_show.animating()) {
|
2016-11-10 18:10:31 +00:00
|
|
|
showChildren();
|
|
|
|
}
|
2016-06-17 18:18:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 13:40:51 +00:00
|
|
|
void InnerDropdown::showAnimationCallback() {
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-06-17 18:18:01 +00:00
|
|
|
bool InnerDropdown::eventFilter(QObject *obj, QEvent *e) {
|
|
|
|
if (e->type() == QEvent::Enter) {
|
|
|
|
otherEnter();
|
|
|
|
} else if (e->type() == QEvent::Leave) {
|
|
|
|
otherLeave();
|
2016-11-16 12:06:02 +00:00
|
|
|
} else if (e->type() == QEvent::MouseButtonRelease && static_cast<QMouseEvent*>(e)->button() == Qt::LeftButton) {
|
2016-06-17 18:18:01 +00:00
|
|
|
if (isHidden() || _hiding) {
|
|
|
|
otherEnter();
|
|
|
|
} else {
|
|
|
|
otherLeave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
int InnerDropdown::resizeGetHeight(int newWidth) {
|
|
|
|
auto newHeight = _st.padding.top() + _st.scrollMargin.top() + _st.scrollMargin.bottom() + _st.padding.bottom();
|
|
|
|
if (auto widget = static_cast<TWidget*>(_scroll->widget())) {
|
2016-12-31 15:19:22 +00:00
|
|
|
auto containerWidth = newWidth - _st.padding.left() - _st.padding.right() - _st.scrollMargin.left() - _st.scrollMargin.right();
|
|
|
|
widget->resizeToWidth(containerWidth);
|
2016-10-26 16:43:13 +00:00
|
|
|
newHeight += widget->height();
|
|
|
|
}
|
|
|
|
if (_maxHeight > 0) {
|
|
|
|
accumulate_min(newHeight, _maxHeight);
|
|
|
|
}
|
|
|
|
return newHeight;
|
|
|
|
}
|
2016-06-20 11:31:12 +00:00
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
InnerDropdown::Container::Container(QWidget *parent, TWidget *child, const style::InnerDropdown &st) : TWidget(parent), _st(st) {
|
2016-06-20 11:31:12 +00:00
|
|
|
child->setParent(this);
|
|
|
|
child->moveToLeft(_st.scrollPadding.left(), _st.scrollPadding.top());
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::Container::setVisibleTopBottom(int visibleTop, int visibleBottom) {
|
|
|
|
if (auto child = static_cast<TWidget*>(children().front())) {
|
2016-06-20 11:31:12 +00:00
|
|
|
child->setVisibleTopBottom(visibleTop - _st.scrollPadding.top(), visibleBottom - _st.scrollPadding.top());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
void InnerDropdown::Container::resizeToContent() {
|
2016-12-31 15:19:22 +00:00
|
|
|
auto newWidth = _st.scrollPadding.left() + _st.scrollPadding.right();
|
2016-10-26 16:43:13 +00:00
|
|
|
auto newHeight = _st.scrollPadding.top() + _st.scrollPadding.bottom();
|
|
|
|
if (auto child = static_cast<TWidget*>(children().front())) {
|
|
|
|
newWidth += child->width();
|
2016-06-20 11:31:12 +00:00
|
|
|
newHeight += child->height();
|
|
|
|
}
|
2016-10-26 16:43:13 +00:00
|
|
|
if (newWidth != width() || newHeight != height()) {
|
|
|
|
resize(newWidth, newHeight);
|
2016-06-20 11:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:43:13 +00:00
|
|
|
int InnerDropdown::Container::resizeGetHeight(int newWidth) {
|
2016-12-31 15:19:22 +00:00
|
|
|
auto innerWidth = newWidth - _st.scrollPadding.left() - _st.scrollPadding.right();
|
|
|
|
auto result = _st.scrollPadding.top() + _st.scrollPadding.bottom();
|
2016-10-26 16:43:13 +00:00
|
|
|
if (auto child = static_cast<TWidget*>(children().front())) {
|
2016-06-20 11:31:12 +00:00
|
|
|
child->resizeToWidth(innerWidth);
|
|
|
|
child->moveToLeft(_st.scrollPadding.left(), _st.scrollPadding.top());
|
|
|
|
result += child->height();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-06-17 18:18:01 +00:00
|
|
|
} // namespace Ui
|