electron/shell/browser/window_list.cc

122 lines
3 KiB
C++
Raw Normal View History

// 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
// found in the LICENSE file.
#include "shell/browser/window_list.h"
#include <algorithm>
2014-03-16 01:37:04 +00:00
#include "base/logging.h"
#include "shell/browser/native_window.h"
#include "shell/browser/window_list_observer.h"
namespace {
template <typename T>
std::vector<base::WeakPtr<T>> ConvertToWeakPtrVector(std::vector<T*> raw_ptrs) {
std::vector<base::WeakPtr<T>> converted_to_weak;
converted_to_weak.reserve(raw_ptrs.size());
for (auto* raw_ptr : raw_ptrs) {
converted_to_weak.push_back(raw_ptr->GetWeakPtr());
}
return converted_to_weak;
}
} // namespace
namespace electron {
// static
2015-09-02 07:16:49 +00:00
base::LazyInstance<base::ObserverList<WindowListObserver>>::Leaky
WindowList::observers_ = LAZY_INSTANCE_INITIALIZER;
// static
WindowList* WindowList::instance_ = nullptr;
// static
WindowList* WindowList::GetInstance() {
if (!instance_)
instance_ = new WindowList;
return instance_;
}
// static
WindowList::WindowVector WindowList::GetWindows() {
return GetInstance()->windows_;
}
// static
bool WindowList::IsEmpty() {
return GetInstance()->windows_.empty();
}
// 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);
}
// 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);
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();
}
}
// static
void WindowList::WindowCloseCancelled(NativeWindow* window) {
2017-01-24 03:34:39 +00:00
for (WindowListObserver& observer : observers_.Get())
observer.OnWindowCloseCancelled(window);
}
// static
void WindowList::AddObserver(WindowListObserver* observer) {
observers_.Get().AddObserver(observer);
}
// static
void WindowList::RemoveObserver(WindowListObserver* observer) {
observers_.Get().RemoveObserver(observer);
}
// static
void WindowList::CloseAllWindows() {
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
ConvertToWeakPtrVector(GetInstance()->windows_);
#if defined(OS_MACOSX)
std::reverse(weak_windows.begin(), weak_windows.end());
#endif
for (const auto& window : weak_windows) {
if (window && !window->IsClosed())
2016-09-30 22:04:13 +00:00
window->Close();
}
}
// static
void WindowList::DestroyAllWindows() {
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
ConvertToWeakPtrVector(GetInstance()->windows_);
for (const auto& window : weak_windows) {
if (window)
window->CloseImmediately();
}
}
WindowList::WindowList() = default;
WindowList::~WindowList() = default;
} // namespace electron