2016-04-08 07:12:48 +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.
|
2016-04-08 07:12:48 +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
|
2016-04-08 07:12:48 +00:00
|
|
|
*/
|
|
|
|
#include "ui/toast/toast_manager.h"
|
|
|
|
|
|
|
|
#include "ui/toast/toast_widget.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
namespace Toast {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
namespace {
|
2016-04-08 07:12:48 +00:00
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
NeverFreedPointer<QMap<QObject*, Manager*>> _managers;
|
2016-04-08 07:12:48 +00:00
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
} // namespace
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
Manager::Manager(QWidget *parent) : QObject(parent)
|
|
|
|
, _hideTimer([=] { hideByTimer(); }) {
|
2017-07-11 17:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::eventFilter(QObject *o, QEvent *e) {
|
|
|
|
if (e->type() == QEvent::Resize) {
|
|
|
|
for (auto i = _toastByWidget.cbegin(), e = _toastByWidget.cend(); i != e; ++i) {
|
|
|
|
if (i.key()->parentWidget() == o) {
|
|
|
|
i.key()->onParentResized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QObject::eventFilter(o, e);
|
2016-04-08 07:12:48 +00:00
|
|
|
}
|
|
|
|
|
2016-04-12 21:31:28 +00:00
|
|
|
Manager *Manager::instance(QWidget *parent) {
|
2017-02-03 20:07:26 +00:00
|
|
|
if (!parent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-12 19:34:25 +00:00
|
|
|
_managers.createIfNull();
|
2016-04-12 21:31:28 +00:00
|
|
|
auto i = _managers->constFind(parent);
|
|
|
|
if (i == _managers->cend()) {
|
|
|
|
i = _managers->insert(parent, new Manager(parent));
|
2016-04-08 07:12:48 +00:00
|
|
|
}
|
2016-04-12 21:31:28 +00:00
|
|
|
return i.value();
|
2016-04-08 07:12:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 13:45:56 +00:00
|
|
|
void Manager::addToast(std::unique_ptr<Instance> &&toast) {
|
2016-04-08 07:12:48 +00:00
|
|
|
_toasts.push_back(toast.release());
|
|
|
|
Instance *t = _toasts.back();
|
2016-04-10 19:20:48 +00:00
|
|
|
Widget *widget = t->_widget.get();
|
2016-04-08 07:12:48 +00:00
|
|
|
|
|
|
|
_toastByWidget.insert(widget, t);
|
|
|
|
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(onToastWidgetDestroyed(QObject*)));
|
2017-07-11 17:11:06 +00:00
|
|
|
if (auto parent = widget->parentWidget()) {
|
|
|
|
auto found = false;
|
|
|
|
for (auto i = _toastParents.begin(); i != _toastParents.cend();) {
|
|
|
|
if (*i == parent) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
} else if (!*i) {
|
|
|
|
i = _toastParents.erase(i);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
_toastParents.insert(parent);
|
|
|
|
parent->installEventFilter(this);
|
|
|
|
}
|
|
|
|
}
|
2016-04-08 07:12:48 +00:00
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
auto oldHideNearestMs = _toastByHideTime.isEmpty() ? 0LL : _toastByHideTime.firstKey();
|
2016-04-08 07:12:48 +00:00
|
|
|
_toastByHideTime.insert(t->_hideAtMs, t);
|
|
|
|
if (!oldHideNearestMs || _toastByHideTime.firstKey() < oldHideNearestMs) {
|
|
|
|
startNextHideTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 11:26:43 +00:00
|
|
|
void Manager::hideByTimer() {
|
2016-12-01 19:20:33 +00:00
|
|
|
auto now = getms(true);
|
2016-04-08 07:12:48 +00:00
|
|
|
for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) {
|
|
|
|
if (i.key() <= now) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto toast = i.value();
|
2016-04-08 07:12:48 +00:00
|
|
|
i = _toastByHideTime.erase(i);
|
2016-12-13 17:07:56 +00:00
|
|
|
toast->hideAnimated();
|
2016-04-08 07:12:48 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
startNextHideTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::onToastWidgetDestroyed(QObject *widget) {
|
|
|
|
auto i = _toastByWidget.find(static_cast<Widget*>(widget));
|
|
|
|
if (i != _toastByWidget.cend()) {
|
2016-12-13 17:07:56 +00:00
|
|
|
auto toast = i.value();
|
2016-04-08 07:12:48 +00:00
|
|
|
_toastByWidget.erase(i);
|
|
|
|
toast->_widget.release();
|
|
|
|
|
|
|
|
int index = _toasts.indexOf(toast);
|
|
|
|
if (index >= 0) {
|
|
|
|
_toasts.removeAt(index);
|
|
|
|
delete toast;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::startNextHideTimer() {
|
|
|
|
if (_toastByHideTime.isEmpty()) return;
|
|
|
|
|
2016-12-01 19:20:33 +00:00
|
|
|
auto ms = getms(true);
|
2016-04-08 07:12:48 +00:00
|
|
|
if (ms >= _toastByHideTime.firstKey()) {
|
2019-01-18 11:26:43 +00:00
|
|
|
crl::on_main(this, [=] {
|
|
|
|
hideByTimer();
|
|
|
|
});
|
2016-04-08 07:12:48 +00:00
|
|
|
} else {
|
2019-01-18 11:26:43 +00:00
|
|
|
_hideTimer.callOnce(_toastByHideTime.firstKey() - ms);
|
2016-04-08 07:12:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::~Manager() {
|
2016-04-12 21:31:28 +00:00
|
|
|
_managers->remove(parent());
|
2016-04-08 07:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace Toast
|
|
|
|
} // namespace Ui
|