1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/shell/browser/root_window_controller.h"
#include "base/memory/raw_ptr.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/native_app_window.h"
#include "extensions/shell/browser/shell_app_delegate.h"
#include "ui/aura/layout_manager.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tracker.h"
#include "ui/aura/window_tree_host.h"
#include "ui/display/display.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/platform_window/platform_window_init_properties.h"
#include "ui/wm/core/default_screen_position_client.h"
namespace extensions {
namespace {
// A simple layout manager that makes each new window fill its parent.
class FillLayout : public aura::LayoutManager {
public:
FillLayout(aura::Window* owner) : owner_(owner) { DCHECK(owner_); }
FillLayout(const FillLayout&) = delete;
FillLayout& operator=(const FillLayout&) = delete;
~FillLayout() override = default;
private:
// aura::LayoutManager:
void OnWindowResized() override {
// Size the owner's immediate child windows.
aura::WindowTracker children_tracker(owner_->children());
while (!children_tracker.windows().empty()) {
aura::Window* child = children_tracker.Pop();
child->SetBounds(gfx::Rect(owner_->bounds().size()));
}
}
void OnWindowAddedToLayout(aura::Window* child) override {
DCHECK_EQ(owner_, child->parent());
// Create a rect at 0,0 with the size of the parent.
gfx::Size parent_size = child->parent()->bounds().size();
child->SetBounds(gfx::Rect(parent_size));
}
void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
void OnWindowRemovedFromLayout(aura::Window* child) override {}
void OnChildWindowVisibilityChanged(aura::Window* child,
bool visible) override {}
void SetChildBounds(aura::Window* child,
const gfx::Rect& requested_bounds) override {
SetChildBoundsDirect(child, requested_bounds);
}
raw_ptr<aura::Window> owner_; // Not owned.
};
// A simple screen positioning client that translates bounds to screen
// coordinates using the offset of the root window in screen coordinates.
class ScreenPositionClient : public wm::DefaultScreenPositionClient {
public:
using DefaultScreenPositionClient::DefaultScreenPositionClient;
ScreenPositionClient(const ScreenPositionClient&) = delete;
ScreenPositionClient& operator=(const ScreenPositionClient&) = delete;
~ScreenPositionClient() override = default;
// wm::DefaultScreenPositionClient:
void SetBounds(aura::Window* window,
const gfx::Rect& bounds,
const display::Display& display) override {
aura::Window* root_window = window->GetRootWindow();
DCHECK(window);
// Convert the window's origin to its root window's coordinates.
gfx::Point origin = bounds.origin();
aura::Window::ConvertPointToTarget(window->parent(), root_window, &origin);
// Translate the origin by the root window's offset in screen coordinates.
gfx::Point host_origin = GetRootWindowOriginInScreen(root_window);
origin.Offset(-host_origin.x(), -host_origin.y());
window->SetBounds(gfx::Rect(origin, bounds.size()));
}
};
} // namespace
RootWindowController::RootWindowController(
DesktopDelegate* desktop_delegate,
const gfx::Rect& bounds,
content::BrowserContext* browser_context)
: desktop_delegate_(desktop_delegate), browser_context_(browser_context) {
DCHECK(desktop_delegate_);
DCHECK(browser_context_);
host_ =
aura::WindowTreeHost::Create(ui::PlatformWindowInitProperties{bounds});
host_->InitHost();
host_->window()->Show();
aura::client::SetWindowParentingClient(host_->window(), this);
screen_position_client_ =
std::make_unique<ScreenPositionClient>(host_->window());
// Ensure the window fills the display.
host_->window()->SetLayoutManager(
std::make_unique<FillLayout>(host_->window()));
host_->AddObserver(this);
host_->Show();
}
RootWindowController::~RootWindowController() {
CloseAppWindows();
// The screen position client holds a pointer to the root window, so free it
// before destroying the window tree host.
screen_position_client_.reset();
DestroyWindowTreeHost();
}
void RootWindowController::AddAppWindow(AppWindow* app_window,
gfx::NativeWindow window) {
if (app_windows_.empty()) {
// Start observing for OnAppWindowRemoved.
AppWindowRegistry* registry = AppWindowRegistry::Get(browser_context_);
registry->AddObserver(this);
}
app_windows_.push_back(app_window);
aura::Window* root_window = host_->window();
root_window->AddChild(window);
}
void RootWindowController::RemoveAppWindow(AppWindow* app_window) {
host_->window()->RemoveChild(app_window->GetNativeWindow());
app_windows_.remove(app_window);
if (app_windows_.empty())
AppWindowRegistry::Get(browser_context_)->RemoveObserver(this);
}
void RootWindowController::CloseAppWindows() {
if (app_windows_.empty())
return;
// Remove the observer before closing windows to avoid triggering
// OnAppWindowRemoved, which would mutate |app_windows_|.
AppWindowRegistry::Get(browser_context_)->RemoveObserver(this);
for (AppWindow* app_window : app_windows_)
app_window->GetBaseWindow()->Close(); // Close() deletes |app_window|.
app_windows_.clear();
}
void RootWindowController::UpdateSize(const gfx::Size& size) {
host_->SetBoundsInPixels(gfx::Rect(size));
}
aura::Window* RootWindowController::GetDefaultParent(aura::Window* window,
const gfx::Rect& bounds,
const int64_t display_id) {
return host_->window();
}
void RootWindowController::OnHostCloseRequested(aura::WindowTreeHost* host) {
DCHECK_EQ(host_.get(), host);
CloseAppWindows();
// The ShellDesktopControllerAura will delete us.
desktop_delegate_->CloseRootWindowController(this);
}
void RootWindowController::OnAppWindowRemoved(AppWindow* window) {
if (app_windows_.empty())
return;
// If we created this AppWindow, remove it from our list so we don't try to
// close it again later.
app_windows_.remove(window);
// Close when all AppWindows are closed.
if (app_windows_.empty()) {
AppWindowRegistry::Get(browser_context_)->RemoveObserver(this);
desktop_delegate_->CloseRootWindowController(this);
}
}
void RootWindowController::DestroyWindowTreeHost() {
host_->RemoveObserver(this);
host_.reset();
}
} // namespace extensions
|