Fix toast notification destruction.

This commit is contained in:
John Preston 2019-09-20 13:10:23 +03:00
parent d22f5f405f
commit b4beb6abed
3 changed files with 39 additions and 32 deletions

View file

@ -16,11 +16,11 @@ class Manager;
class Widget; class Widget;
} // namespace internal } // namespace internal
static constexpr const int DefaultDuration = 1500; inline constexpr auto kDefaultDuration = crl::time(1500);
struct Config { struct Config {
QString text; QString text;
QMargins padding; QMargins padding;
int durationMs = DefaultDuration; crl::time durationMs = kDefaultDuration;
int minWidth = 0; int minWidth = 0;
int maxWidth = 0; int maxWidth = 0;
int maxLines = 16; int maxLines = 16;

View file

@ -25,8 +25,8 @@ Manager::Manager(not_null<QWidget*> parent, const CreateTag &)
bool Manager::eventFilter(QObject *o, QEvent *e) { bool Manager::eventFilter(QObject *o, QEvent *e) {
if (e->type() == QEvent::Resize) { if (e->type() == QEvent::Resize) {
for (auto i = _toastByWidget.cbegin(), e = _toastByWidget.cend(); i != e; ++i) { for (auto i = _toastByWidget.cbegin(), e = _toastByWidget.cend(); i != e; ++i) {
if (i.key()->parentWidget() == o) { if (i->first->parentWidget() == o) {
i.key()->onParentResized(); i->first->onParentResized();
} }
} }
} }
@ -45,13 +45,13 @@ not_null<Manager*> Manager::instance(not_null<QWidget*> parent) {
} }
void Manager::addToast(std::unique_ptr<Instance> &&toast) { void Manager::addToast(std::unique_ptr<Instance> &&toast) {
_toasts.push_back(toast.release()); _toasts.push_back(std::move(toast));
Instance *t = _toasts.back(); const auto t = _toasts.back().get();
Widget *widget = t->_widget.get(); const auto widget = t->_widget.get();
_toastByWidget.insert(widget, t); _toastByWidget.emplace(widget, t);
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(onToastWidgetDestroyed(QObject*))); connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(onToastWidgetDestroyed(QObject*)));
if (auto parent = widget->parentWidget()) { if (const auto parent = widget->parentWidget()) {
auto found = false; auto found = false;
for (auto i = _toastParents.begin(); i != _toastParents.cend();) { for (auto i = _toastParents.begin(); i != _toastParents.cend();) {
if (*i == parent) { if (*i == parent) {
@ -64,14 +64,16 @@ void Manager::addToast(std::unique_ptr<Instance> &&toast) {
} }
} }
if (!found) { if (!found) {
_toastParents.insert(parent); _toastParents.push_back(parent);
parent->installEventFilter(this); parent->installEventFilter(this);
} }
} }
auto oldHideNearestMs = _toastByHideTime.isEmpty() ? 0LL : _toastByHideTime.firstKey(); const auto nearestHide = _toastByHideTime.empty()
_toastByHideTime.insert(t->_hideAtMs, t); ? 0LL
if (!oldHideNearestMs || _toastByHideTime.firstKey() < oldHideNearestMs) { : _toastByHideTime.begin()->first;
_toastByHideTime.emplace(t->_hideAtMs, t);
if (!nearestHide || _toastByHideTime.begin()->first < nearestHide) {
startNextHideTimer(); startNextHideTimer();
} }
} }
@ -79,8 +81,8 @@ void Manager::addToast(std::unique_ptr<Instance> &&toast) {
void Manager::hideByTimer() { void Manager::hideByTimer() {
auto now = crl::now(); auto now = crl::now();
for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) { for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) {
if (i.key() <= now) { if (i->first <= now) {
auto toast = i.value(); const auto toast = i->second;
i = _toastByHideTime.erase(i); i = _toastByHideTime.erase(i);
toast->hideAnimated(); toast->hideAnimated();
} else { } else {
@ -91,35 +93,40 @@ void Manager::hideByTimer() {
} }
void Manager::onToastWidgetDestroyed(QObject *widget) { void Manager::onToastWidgetDestroyed(QObject *widget) {
auto i = _toastByWidget.find(static_cast<Widget*>(widget)); const auto i = _toastByWidget.find(static_cast<Widget*>(widget));
if (i != _toastByWidget.cend()) { if (i == _toastByWidget.cend()) {
auto toast = i.value(); return;
}
const auto toast = i->second;
_toastByWidget.erase(i); _toastByWidget.erase(i);
toast->_widget.release(); toast->_widget.release();
int index = _toasts.indexOf(toast); const auto j = ranges::find(
if (index >= 0) { _toasts,
_toasts.removeAt(index); toast.get(),
delete toast; &std::unique_ptr<Instance>::get);
} if (j != end(_toasts)) {
_toasts.erase(j);
} }
} }
void Manager::startNextHideTimer() { void Manager::startNextHideTimer() {
if (_toastByHideTime.isEmpty()) return; if (_toastByHideTime.empty()) return;
auto ms = crl::now(); auto ms = crl::now();
if (ms >= _toastByHideTime.firstKey()) { if (ms >= _toastByHideTime.begin()->first) {
crl::on_main(this, [=] { crl::on_main(this, [=] {
hideByTimer(); hideByTimer();
}); });
} else { } else {
_hideTimer.callOnce(_toastByHideTime.firstKey() - ms); _hideTimer.callOnce(_toastByHideTime.begin()->first - ms);
} }
} }
Manager::~Manager() { Manager::~Manager() {
ManagersMap.remove(parent()); ManagersMap.remove(parent());
_toastByWidget.clear();
_toasts.clear();
} }
} // namespace internal } // namespace internal

View file

@ -43,10 +43,10 @@ private:
base::Timer _hideTimer; base::Timer _hideTimer;
crl::time _nextHide = 0; crl::time _nextHide = 0;
QMultiMap<crl::time, Instance*> _toastByHideTime; base::flat_multi_map<crl::time, not_null<Instance*>> _toastByHideTime;
QMap<Widget*, Instance*> _toastByWidget; base::flat_map<not_null<Widget*>, not_null<Instance*>> _toastByWidget;
QList<Instance*> _toasts; std::vector<std::unique_ptr<Instance>> _toasts;
OrderedSet<QPointer<QWidget>> _toastParents; std::vector<QPointer<QWidget>> _toastParents;
}; };