window_list.cc 2.9 KB

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