2014-10-31 12:17:05 -06:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 03:49:37 -06:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-05-02 08:54:09 -06:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/window_list.h"
|
2013-05-02 08:54:09 -06:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-03-15 19:37:04 -06:00
|
|
|
#include "base/logging.h"
|
2024-01-10 13:01:49 -07:00
|
|
|
#include "base/no_destructor.h"
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/native_window.h"
|
|
|
|
#include "shell/browser/window_list_observer.h"
|
2013-05-02 08:54:09 -06:00
|
|
|
|
2020-04-07 21:41:14 -06: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 15:23:04 -06:00
|
|
|
namespace electron {
|
2013-05-02 08:54:09 -06:00
|
|
|
|
|
|
|
// static
|
2016-07-10 03:52:28 -06:00
|
|
|
WindowList* WindowList::instance_ = nullptr;
|
2013-05-02 08:54:09 -06:00
|
|
|
|
|
|
|
// static
|
|
|
|
WindowList* WindowList::GetInstance() {
|
|
|
|
if (!instance_)
|
|
|
|
instance_ = new WindowList;
|
|
|
|
return instance_;
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:43:37 -06:00
|
|
|
// static
|
|
|
|
WindowList::WindowVector WindowList::GetWindows() {
|
|
|
|
return GetInstance()->windows_;
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:48:58 -06:00
|
|
|
// static
|
|
|
|
bool WindowList::IsEmpty() {
|
|
|
|
return GetInstance()->windows_.empty();
|
|
|
|
}
|
|
|
|
|
2013-05-02 08:54:09 -06:00
|
|
|
// static
|
|
|
|
void WindowList::AddWindow(NativeWindow* window) {
|
|
|
|
DCHECK(window);
|
|
|
|
// Push |window| on the appropriate list instance.
|
|
|
|
WindowVector& windows = GetInstance()->windows_;
|
|
|
|
windows.push_back(window);
|
|
|
|
|
2024-01-10 13:01:49 -07:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-23 20:34:39 -07:00
|
|
|
observer.OnWindowAdded(window);
|
2013-05-02 08:54:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveWindow(NativeWindow* window) {
|
|
|
|
WindowVector& windows = GetInstance()->windows_;
|
2024-04-19 09:55:59 -06:00
|
|
|
std::erase(windows, window);
|
2013-05-02 08:54:09 -06:00
|
|
|
|
2024-01-10 13:01:49 -07:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-23 20:34:39 -07:00
|
|
|
observer.OnWindowRemoved(window);
|
2013-05-02 08:54:09 -06:00
|
|
|
|
2017-03-30 13:59:18 -06:00
|
|
|
if (windows.empty()) {
|
2024-01-10 13:01:49 -07:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-23 20:34:39 -07:00
|
|
|
observer.OnWindowAllClosed();
|
|
|
|
}
|
2013-05-02 08:54:09 -06:00
|
|
|
}
|
|
|
|
|
2013-05-02 09:43:23 -06:00
|
|
|
// static
|
|
|
|
void WindowList::WindowCloseCancelled(NativeWindow* window) {
|
2024-01-10 13:01:49 -07:00
|
|
|
for (WindowListObserver& observer : GetObservers())
|
2017-01-23 20:34:39 -07:00
|
|
|
observer.OnWindowCloseCancelled(window);
|
2013-05-02 09:43:23 -06:00
|
|
|
}
|
|
|
|
|
2013-05-02 08:54:09 -06:00
|
|
|
// static
|
|
|
|
void WindowList::AddObserver(WindowListObserver* observer) {
|
2024-01-10 13:01:49 -07:00
|
|
|
GetObservers().AddObserver(observer);
|
2013-05-02 08:54:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::RemoveObserver(WindowListObserver* observer) {
|
2024-01-10 13:01:49 -07:00
|
|
|
GetObservers().RemoveObserver(observer);
|
2013-05-02 08:54:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void WindowList::CloseAllWindows() {
|
2020-04-07 21:41:14 -06:00
|
|
|
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
|
|
|
|
ConvertToWeakPtrVector(GetInstance()->windows_);
|
2022-02-09 19:58:52 -07:00
|
|
|
#if BUILDFLAG(IS_MAC)
|
2020-04-07 21:41:14 -06:00
|
|
|
std::reverse(weak_windows.begin(), weak_windows.end());
|
2017-08-27 20:56:51 -06:00
|
|
|
#endif
|
2020-04-07 21:41:14 -06:00
|
|
|
for (const auto& window : weak_windows) {
|
|
|
|
if (window && !window->IsClosed())
|
2016-09-30 16:04:13 -06:00
|
|
|
window->Close();
|
2020-04-07 21:41:14 -06:00
|
|
|
}
|
2013-05-02 08:54:09 -06:00
|
|
|
}
|
|
|
|
|
2017-04-06 15:25:22 -06:00
|
|
|
// static
|
|
|
|
void WindowList::DestroyAllWindows() {
|
2020-04-07 21:41:14 -06:00
|
|
|
std::vector<base::WeakPtr<NativeWindow>> weak_windows =
|
|
|
|
ConvertToWeakPtrVector(GetInstance()->windows_);
|
|
|
|
|
|
|
|
for (const auto& window : weak_windows) {
|
2022-02-23 08:27:54 -07:00
|
|
|
if (window && !window->IsClosed())
|
2020-04-07 21:41:14 -06:00
|
|
|
window->CloseImmediately();
|
|
|
|
}
|
2017-04-06 15:25:22 -06:00
|
|
|
}
|
|
|
|
|
2019-09-16 16:12:00 -06:00
|
|
|
WindowList::WindowList() = default;
|
2013-05-02 08:54:09 -06:00
|
|
|
|
2019-09-16 16:12:00 -06:00
|
|
|
WindowList::~WindowList() = default;
|
2013-05-02 08:54:09 -06:00
|
|
|
|
2024-01-10 13:01:49 -07:00
|
|
|
// static
|
|
|
|
base::ObserverList<WindowListObserver>& WindowList::GetObservers() {
|
|
|
|
static base::NoDestructor<base::ObserverList<WindowListObserver>> instance;
|
|
|
|
return *instance;
|
|
|
|
}
|
|
|
|
|
2019-06-19 15:23:04 -06:00
|
|
|
} // namespace electron
|