Fix toast notification destruction.
This commit is contained in:
parent
d22f5f405f
commit
b4beb6abed
3 changed files with 39 additions and 32 deletions
|
|
@ -16,11 +16,11 @@ class Manager;
|
|||
class Widget;
|
||||
} // namespace internal
|
||||
|
||||
static constexpr const int DefaultDuration = 1500;
|
||||
inline constexpr auto kDefaultDuration = crl::time(1500);
|
||||
struct Config {
|
||||
QString text;
|
||||
QMargins padding;
|
||||
int durationMs = DefaultDuration;
|
||||
crl::time durationMs = kDefaultDuration;
|
||||
int minWidth = 0;
|
||||
int maxWidth = 0;
|
||||
int maxLines = 16;
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ Manager::Manager(not_null<QWidget*> parent, const CreateTag &)
|
|||
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();
|
||||
if (i->first->parentWidget() == o) {
|
||||
i->first->onParentResized();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -45,13 +45,13 @@ not_null<Manager*> Manager::instance(not_null<QWidget*> parent) {
|
|||
}
|
||||
|
||||
void Manager::addToast(std::unique_ptr<Instance> &&toast) {
|
||||
_toasts.push_back(toast.release());
|
||||
Instance *t = _toasts.back();
|
||||
Widget *widget = t->_widget.get();
|
||||
_toasts.push_back(std::move(toast));
|
||||
const auto t = _toasts.back().get();
|
||||
const auto widget = t->_widget.get();
|
||||
|
||||
_toastByWidget.insert(widget, t);
|
||||
_toastByWidget.emplace(widget, t);
|
||||
connect(widget, SIGNAL(destroyed(QObject*)), this, SLOT(onToastWidgetDestroyed(QObject*)));
|
||||
if (auto parent = widget->parentWidget()) {
|
||||
if (const auto parent = widget->parentWidget()) {
|
||||
auto found = false;
|
||||
for (auto i = _toastParents.begin(); i != _toastParents.cend();) {
|
||||
if (*i == parent) {
|
||||
|
|
@ -64,14 +64,16 @@ void Manager::addToast(std::unique_ptr<Instance> &&toast) {
|
|||
}
|
||||
}
|
||||
if (!found) {
|
||||
_toastParents.insert(parent);
|
||||
_toastParents.push_back(parent);
|
||||
parent->installEventFilter(this);
|
||||
}
|
||||
}
|
||||
|
||||
auto oldHideNearestMs = _toastByHideTime.isEmpty() ? 0LL : _toastByHideTime.firstKey();
|
||||
_toastByHideTime.insert(t->_hideAtMs, t);
|
||||
if (!oldHideNearestMs || _toastByHideTime.firstKey() < oldHideNearestMs) {
|
||||
const auto nearestHide = _toastByHideTime.empty()
|
||||
? 0LL
|
||||
: _toastByHideTime.begin()->first;
|
||||
_toastByHideTime.emplace(t->_hideAtMs, t);
|
||||
if (!nearestHide || _toastByHideTime.begin()->first < nearestHide) {
|
||||
startNextHideTimer();
|
||||
}
|
||||
}
|
||||
|
|
@ -79,8 +81,8 @@ void Manager::addToast(std::unique_ptr<Instance> &&toast) {
|
|||
void Manager::hideByTimer() {
|
||||
auto now = crl::now();
|
||||
for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) {
|
||||
if (i.key() <= now) {
|
||||
auto toast = i.value();
|
||||
if (i->first <= now) {
|
||||
const auto toast = i->second;
|
||||
i = _toastByHideTime.erase(i);
|
||||
toast->hideAnimated();
|
||||
} else {
|
||||
|
|
@ -91,35 +93,40 @@ void Manager::hideByTimer() {
|
|||
}
|
||||
|
||||
void Manager::onToastWidgetDestroyed(QObject *widget) {
|
||||
auto i = _toastByWidget.find(static_cast<Widget*>(widget));
|
||||
if (i != _toastByWidget.cend()) {
|
||||
auto toast = i.value();
|
||||
const auto i = _toastByWidget.find(static_cast<Widget*>(widget));
|
||||
if (i == _toastByWidget.cend()) {
|
||||
return;
|
||||
}
|
||||
const auto toast = i->second;
|
||||
_toastByWidget.erase(i);
|
||||
toast->_widget.release();
|
||||
|
||||
int index = _toasts.indexOf(toast);
|
||||
if (index >= 0) {
|
||||
_toasts.removeAt(index);
|
||||
delete toast;
|
||||
}
|
||||
const auto j = ranges::find(
|
||||
_toasts,
|
||||
toast.get(),
|
||||
&std::unique_ptr<Instance>::get);
|
||||
if (j != end(_toasts)) {
|
||||
_toasts.erase(j);
|
||||
}
|
||||
}
|
||||
|
||||
void Manager::startNextHideTimer() {
|
||||
if (_toastByHideTime.isEmpty()) return;
|
||||
if (_toastByHideTime.empty()) return;
|
||||
|
||||
auto ms = crl::now();
|
||||
if (ms >= _toastByHideTime.firstKey()) {
|
||||
if (ms >= _toastByHideTime.begin()->first) {
|
||||
crl::on_main(this, [=] {
|
||||
hideByTimer();
|
||||
});
|
||||
} else {
|
||||
_hideTimer.callOnce(_toastByHideTime.firstKey() - ms);
|
||||
_hideTimer.callOnce(_toastByHideTime.begin()->first - ms);
|
||||
}
|
||||
}
|
||||
|
||||
Manager::~Manager() {
|
||||
ManagersMap.remove(parent());
|
||||
_toastByWidget.clear();
|
||||
_toasts.clear();
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
|
|
|||
|
|
@ -43,10 +43,10 @@ private:
|
|||
base::Timer _hideTimer;
|
||||
crl::time _nextHide = 0;
|
||||
|
||||
QMultiMap<crl::time, Instance*> _toastByHideTime;
|
||||
QMap<Widget*, Instance*> _toastByWidget;
|
||||
QList<Instance*> _toasts;
|
||||
OrderedSet<QPointer<QWidget>> _toastParents;
|
||||
base::flat_multi_map<crl::time, not_null<Instance*>> _toastByHideTime;
|
||||
base::flat_map<not_null<Widget*>, not_null<Instance*>> _toastByWidget;
|
||||
std::vector<std::unique_ptr<Instance>> _toasts;
|
||||
std::vector<QPointer<QWidget>> _toastParents;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue