toast_manager.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/toast/toast_manager.h"
  8. #include "ui/toast/toast_widget.h"
  9. #include "ui/qt_object_factory.h"
  10. namespace Ui {
  11. namespace Toast {
  12. namespace internal {
  13. namespace {
  14. base::flat_map<not_null<QObject*>, not_null<Manager*>> ManagersMap;
  15. } // namespace
  16. Manager::Manager(not_null<QWidget*> parent, const CreateTag &)
  17. : QObject(parent)
  18. , _hideTimer([=] { hideByTimer(); }) {
  19. }
  20. bool Manager::eventFilter(QObject *o, QEvent *e) {
  21. if (e->type() == QEvent::Resize) {
  22. for (auto i = _toastByWidget.cbegin(), e = _toastByWidget.cend(); i != e; ++i) {
  23. if (i->first->parentWidget() == o) {
  24. i->first->parentResized();
  25. }
  26. }
  27. }
  28. return QObject::eventFilter(o, e);
  29. }
  30. not_null<Manager*> Manager::instance(not_null<QWidget*> parent) {
  31. auto i = ManagersMap.find(parent);
  32. if (i == end(ManagersMap)) {
  33. i = ManagersMap.emplace(
  34. parent,
  35. Ui::CreateChild<Manager>(parent.get(), CreateTag())
  36. ).first;
  37. }
  38. return i->second;
  39. }
  40. base::weak_ptr<Instance> Manager::addToast(
  41. std::unique_ptr<Instance> &&toast) {
  42. _toasts.push_back(std::move(toast));
  43. const auto t = _toasts.back().get();
  44. const auto widget = t->_widget.get();
  45. _toastByWidget.emplace(widget, t);
  46. connect(widget, &QObject::destroyed, [=] {
  47. toastWidgetDestroyed(widget);
  48. });
  49. if (const auto parent = widget->parentWidget()) {
  50. auto found = false;
  51. for (auto i = _toastParents.begin(); i != _toastParents.cend();) {
  52. if (*i == parent) {
  53. found = true;
  54. break;
  55. } else if (!*i) {
  56. i = _toastParents.erase(i);
  57. } else {
  58. ++i;
  59. }
  60. }
  61. if (!found) {
  62. _toastParents.push_back(parent);
  63. parent->installEventFilter(this);
  64. }
  65. }
  66. if (t->_hideAt > 0) {
  67. const auto nearestHide = _toastByHideTime.empty()
  68. ? 0LL
  69. : _toastByHideTime.begin()->first;
  70. _toastByHideTime.emplace(t->_hideAt, t);
  71. if (!nearestHide || _toastByHideTime.begin()->first < nearestHide) {
  72. startNextHideTimer();
  73. }
  74. }
  75. return make_weak(t);
  76. }
  77. void Manager::hideByTimer() {
  78. auto now = crl::now();
  79. for (auto i = _toastByHideTime.begin(); i != _toastByHideTime.cend();) {
  80. if (i->first <= now) {
  81. const auto toast = i->second;
  82. i = _toastByHideTime.erase(i);
  83. toast->hideAnimated();
  84. } else {
  85. break;
  86. }
  87. }
  88. startNextHideTimer();
  89. }
  90. void Manager::toastWidgetDestroyed(QObject *widget) {
  91. const auto i = _toastByWidget.find(static_cast<Widget*>(widget));
  92. if (i == _toastByWidget.cend()) {
  93. return;
  94. }
  95. const auto toast = i->second;
  96. _toastByWidget.erase(i);
  97. toast->_widget.release();
  98. for (auto i = begin(_toastByHideTime); i != end(_toastByHideTime); ++i) {
  99. if (i->second == toast) {
  100. _toastByHideTime.erase(i);
  101. break;
  102. }
  103. }
  104. const auto j = ranges::find(
  105. _toasts,
  106. toast.get(),
  107. &std::unique_ptr<Instance>::get);
  108. if (j != end(_toasts)) {
  109. _toasts.erase(j);
  110. }
  111. }
  112. void Manager::startNextHideTimer() {
  113. if (_toastByHideTime.empty()) {
  114. return;
  115. }
  116. auto ms = crl::now();
  117. if (ms >= _toastByHideTime.begin()->first) {
  118. crl::on_main(this, [=] {
  119. hideByTimer();
  120. });
  121. } else {
  122. _hideTimer.callOnce(_toastByHideTime.begin()->first - ms);
  123. }
  124. }
  125. Manager::~Manager() {
  126. ManagersMap.remove(parent());
  127. _toastByWidget.clear();
  128. _toasts.clear();
  129. }
  130. } // namespace internal
  131. } // namespace Toast
  132. } // namespace Ui