window_list.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "atom/browser/window_list.h"
  5. #include <algorithm>
  6. #include "atom/browser/native_window.h"
  7. #include "atom/browser/window_list_observer.h"
  8. #include "base/logging.h"
  9. namespace atom {
  10. // static
  11. base::LazyInstance<base::ObserverList<WindowListObserver>>::Leaky
  12. WindowList::observers_ = LAZY_INSTANCE_INITIALIZER;
  13. // static
  14. WindowList* WindowList::instance_ = nullptr;
  15. // static
  16. WindowList* WindowList::GetInstance() {
  17. if (!instance_)
  18. instance_ = new WindowList;
  19. return instance_;
  20. }
  21. // static
  22. WindowList::WindowVector WindowList::GetWindows() {
  23. return GetInstance()->windows_;
  24. }
  25. // static
  26. bool WindowList::IsEmpty() {
  27. return GetInstance()->windows_.empty();
  28. }
  29. // static
  30. void WindowList::AddWindow(NativeWindow* window) {
  31. DCHECK(window);
  32. // Push |window| on the appropriate list instance.
  33. WindowVector& windows = GetInstance()->windows_;
  34. windows.push_back(window);
  35. for (WindowListObserver& observer : observers_.Get())
  36. observer.OnWindowAdded(window);
  37. }
  38. // static
  39. void WindowList::RemoveWindow(NativeWindow* window) {
  40. WindowVector& windows = GetInstance()->windows_;
  41. windows.erase(std::remove(windows.begin(), windows.end(), window),
  42. windows.end());
  43. for (WindowListObserver& observer : observers_.Get())
  44. observer.OnWindowRemoved(window);
  45. if (windows.empty()) {
  46. for (WindowListObserver& observer : observers_.Get())
  47. observer.OnWindowAllClosed();
  48. }
  49. }
  50. // static
  51. void WindowList::WindowCloseCancelled(NativeWindow* window) {
  52. for (WindowListObserver& observer : observers_.Get())
  53. observer.OnWindowCloseCancelled(window);
  54. }
  55. // static
  56. void WindowList::AddObserver(WindowListObserver* observer) {
  57. observers_.Get().AddObserver(observer);
  58. }
  59. // static
  60. void WindowList::RemoveObserver(WindowListObserver* observer) {
  61. observers_.Get().RemoveObserver(observer);
  62. }
  63. // static
  64. void WindowList::CloseAllWindows() {
  65. WindowVector windows = GetInstance()->windows_;
  66. #if defined(OS_MACOSX)
  67. std::reverse(windows.begin(), windows.end());
  68. #endif
  69. for (auto* const& window : windows)
  70. if (!window->IsClosed())
  71. window->Close();
  72. }
  73. // static
  74. void WindowList::DestroyAllWindows() {
  75. WindowVector windows = GetInstance()->windows_;
  76. for (auto* const& window : windows)
  77. window->CloseImmediately(); // e.g. Destroy()
  78. }
  79. WindowList::WindowList() {}
  80. WindowList::~WindowList() {}
  81. } // namespace atom