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.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/window_list.h"
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2024-01-10 20:01:49 +00:00
|
|
|
#include "base/no_destructor.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/window_list_observer.h"
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2020-04-08 03:41:14 +00:00
|
|
|
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
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2013-05-02 14:54:09 +00:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveWindow(NativeWindow* window) {
|
|
|
|
WindowVector& windows = GetInstance()->windows_;
|
2024-04-19 15:55:59 +00:00
|
|
|
std::erase(windows, window);
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2017-03-30 19:59:18 +00:00
|
|
|
if (windows.empty()) {
|
2024-01-10 20:01:49 +00:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-24 03:34:39 +00:00
|
|
|
observer.OnWindowAllClosed();
|
|
|
|
}
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
2013-05-02 15:43:23 +00:00
|
|
|
// static
|
|
|
|
void WindowList::WindowCloseCancelled(NativeWindow* window) {
|
2024-01-10 20:01:49 +00:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-24 03:34:39 +00:00
|
|
|
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) {
|
2024-01-10 20:01:49 +00:00
|
|
|
GetObservers().AddObserver(observer);
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveObserver(WindowListObserver* observer) {
|
2024-01-10 20:01:49 +00:00
|
|
|
GetObservers().RemoveObserver(observer);
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::CloseAllWindows() {
|
2020-04-08 03:41:14 +00:00
|
|
|
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
|
|
|
|
ConvertToWeakPtrVector(GetInstance()->windows_);
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2024-08-26 14:58:32 +00:00
|
|
|
std::ranges::reverse(weak_windows);
|
2017-08-28 02:56:51 +00:00
|
|
|
#endif
|
2020-04-08 03:41:14 +00:00
|
|
|
for (const auto& window : weak_windows) {
|
|
|
|
if (window && !window->IsClosed())
|
2016-09-30 22:04:13 +00:00
|
|
|
window->Close();
|
2020-04-08 03:41:14 +00:00
|
|
|
}
|
2013-05-02 14:54:09 +00:00
|
|
|
}
|
|
|
|
|
2017-04-06 21:25:22 +00:00
|
|
|
// static
|
|
|
|
void WindowList::DestroyAllWindows() {
|
2020-04-08 03:41:14 +00:00
|
|
|
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
|
|
|
|
ConvertToWeakPtrVector(GetInstance()->windows_);
|
|
|
|
|
|
|
|
for (const auto& window : weak_windows) {
|
2022-02-23 15:27:54 +00:00
|
|
|
if (window && !window->IsClosed())
|
2020-04-08 03:41:14 +00:00
|
|
|
window->CloseImmediately();
|
|
|
|
}
|
2017-04-06 21:25:22 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
WindowList::WindowList() = default;
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
WindowList::~WindowList() = default;
|
2013-05-02 14:54:09 +00:00
|
|
|
|
2024-01-10 20:01:49 +00:00
|
|
|
// static
|
|
|
|
base::ObserverList<WindowListObserver>& WindowList::GetObservers() {
|
|
|
|
static base::NoDestructor<base::ObserverList<WindowListObserver>> instance;
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|