2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-02 14:54:09 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/window_list.h"
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/native_window.h"
|
|
|
|
#include "atom/browser/window_list_observer.h"
|
2014-03-16 01:37:04 +00:00
|
|
|
#include "base/logging.h"
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
// static
|
2015-09-02 07:16:49 +00:00
|
|
|
base::LazyInstance<base::ObserverList<WindowListObserver>>::Leaky
|
2013-05-02 14:54:09 +00:00
|
|
|
WindowList::observers_ = LAZY_INSTANCE_INITIALIZER;
|
|
|
|
|
|
|
|
// static
|
2016-07-10 09:52:28 +00:00
|
|
|
WindowList* WindowList::instance_ = nullptr;
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
// static
|
|
|
|
WindowList* WindowList::GetInstance() {
|
|
|
|
if (!instance_)
|
|
|
|
instance_ = new WindowList;
|
|
|
|
return instance_;
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:43:37 +00:00
|
|
|
// static
|
|
|
|
WindowList::WindowVector WindowList::GetWindows() {
|
|
|
|
return GetInstance()->windows_;
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:48:58 +00:00
|
|
|
// static
|
|
|
|
bool WindowList::IsEmpty() {
|
|
|
|
return GetInstance()->windows_.empty();
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:54:09 +00:00
|
|
|
// static
|
|
|
|
void WindowList::AddWindow(NativeWindow* window) {
|
|
|
|
DCHECK(window);
|
|
|
|
// Push |window| on the appropriate list instance.
|
|
|
|
WindowVector& windows = GetInstance()->windows_;
|
|
|
|
windows.push_back(window);
|
|
|
|
|
2017-01-24 03:34:39 +00:00
|
|
|
for (WindowListObserver& observer : observers_.Get())
|
|
|
|
observer.OnWindowAdded(window);
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveWindow(NativeWindow* window) {
|
|
|
|
WindowVector& windows = GetInstance()->windows_;
|
|
|
|
windows.erase(std::remove(windows.begin(), windows.end(), window),
|
|
|
|
windows.end());
|
|
|
|
|
2017-01-24 03:34:39 +00:00
|
|
|
for (WindowListObserver& observer : observers_.Get())
|
|
|
|
observer.OnWindowRemoved(window);
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2017-03-30 19:59:18 +00:00
|
|
|
if (windows.empty()) {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (WindowListObserver& observer : observers_.Get())
|
|
|
|
observer.OnWindowAllClosed();
|
|
|
|
}
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
// static
|
|
|
|
void WindowList::WindowCloseCancelled(NativeWindow* window) {
|
2017-01-24 03:34:39 +00:00
|
|
|
for (WindowListObserver& observer : observers_.Get())
|
|
|
|
observer.OnWindowCloseCancelled(window);
|
2013-05-02 15:43:23 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 14:54:09 +00:00
|
|
|
// static
|
|
|
|
void WindowList::AddObserver(WindowListObserver* observer) {
|
|
|
|
observers_.Get().AddObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveObserver(WindowListObserver* observer) {
|
|
|
|
observers_.Get().RemoveObserver(observer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::CloseAllWindows() {
|
|
|
|
WindowVector windows = GetInstance()->windows_;
|
2017-08-28 02:56:51 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
std::reverse(windows.begin(), windows.end());
|
|
|
|
#endif
|
2018-04-17 22:41:47 +00:00
|
|
|
for (auto* const& window : windows)
|
2016-09-30 22:04:13 +00:00
|
|
|
if (!window->IsClosed())
|
|
|
|
window->Close();
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:25:22 +00:00
|
|
|
// static
|
|
|
|
void WindowList::DestroyAllWindows() {
|
|
|
|
WindowVector windows = GetInstance()->windows_;
|
2018-04-17 22:41:47 +00:00
|
|
|
for (auto* const& window : windows)
|
2018-02-22 07:52:08 +00:00
|
|
|
window->CloseImmediately(); // e.g. Destroy()
|
2017-04-06 21:25:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
WindowList::WindowList() {}
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
WindowList::~WindowList() {}
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
} // namespace atom
|