window_list.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/window_list.h"
  5. #include <algorithm>
  6. #include "base/no_destructor.h"
  7. #include "shell/browser/native_window.h"
  8. #include "shell/browser/window_list_observer.h"
  9. namespace {
  10. template <typename T>
  11. std::vector<base::WeakPtr<T>> ConvertToWeakPtrVector(std::vector<T*> raw_ptrs) {
  12. std::vector<base::WeakPtr<T>> converted_to_weak;
  13. converted_to_weak.reserve(raw_ptrs.size());
  14. for (auto* raw_ptr : raw_ptrs) {
  15. converted_to_weak.push_back(raw_ptr->GetWeakPtr());
  16. }
  17. return converted_to_weak;
  18. }
  19. } // namespace
  20. namespace electron {
  21. // static
  22. WindowList* WindowList::instance_ = nullptr;
  23. // static
  24. WindowList* WindowList::GetInstance() {
  25. if (!instance_)
  26. instance_ = new WindowList;
  27. return instance_;
  28. }
  29. // static
  30. WindowList::WindowVector WindowList::GetWindows() {
  31. return GetInstance()->windows_;
  32. }
  33. // static
  34. bool WindowList::IsEmpty() {
  35. return GetInstance()->windows_.empty();
  36. }
  37. // static
  38. void WindowList::AddWindow(NativeWindow* window) {
  39. DCHECK(window);
  40. // Push |window| on the appropriate list instance.
  41. WindowVector& windows = GetInstance()->windows_;
  42. windows.push_back(window);
  43. }
  44. // static
  45. void WindowList::RemoveWindow(NativeWindow* window) {
  46. WindowVector& windows = GetInstance()->windows_;
  47. std::erase(windows, window);
  48. if (windows.empty()) {
  49. for (WindowListObserver& observer : GetObservers())
  50. observer.OnWindowAllClosed();
  51. }
  52. }
  53. // static
  54. void WindowList::WindowCloseCancelled(NativeWindow* window) {
  55. for (WindowListObserver& observer : GetObservers())
  56. observer.OnWindowCloseCancelled(window);
  57. }
  58. // static
  59. void WindowList::AddObserver(WindowListObserver* observer) {
  60. GetObservers().AddObserver(observer);
  61. }
  62. // static
  63. void WindowList::RemoveObserver(WindowListObserver* observer) {
  64. GetObservers().RemoveObserver(observer);
  65. }
  66. // static
  67. void WindowList::CloseAllWindows() {
  68. std::vector<base::WeakPtr<NativeWindow>> weak_windows =
  69. ConvertToWeakPtrVector(GetInstance()->windows_);
  70. #if BUILDFLAG(IS_MAC)
  71. std::ranges::reverse(weak_windows);
  72. #endif
  73. for (const auto& window : weak_windows) {
  74. if (window && !window->IsClosed())
  75. window->Close();
  76. }
  77. }
  78. // static
  79. void WindowList::DestroyAllWindows() {
  80. std::vector<base::WeakPtr<NativeWindow>> weak_windows =
  81. ConvertToWeakPtrVector(GetInstance()->windows_);
  82. for (const auto& window : weak_windows) {
  83. if (window && !window->IsClosed())
  84. window->CloseImmediately();
  85. }
  86. }
  87. WindowList::WindowList() = default;
  88. WindowList::~WindowList() = default;
  89. // static
  90. base::ObserverList<WindowListObserver>& WindowList::GetObservers() {
  91. static base::NoDestructor<base::ObserverList<WindowListObserver>> instance;
  92. return *instance;
  93. }
  94. } // namespace electron