blob: 5bcd15e1a7424491c0ec6a58249005a16e2bc1f9 [file] [log] [blame]
ben294d04b2015-10-15 01:23:331// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
sky7a35dfa2016-02-01 21:37:285#include "mash/wm/window_manager.h"
ben294d04b2015-10-15 01:23:336
avi703dbbf2015-12-22 01:46:287#include <stdint.h>
dchenge48600452015-12-28 02:24:508#include <utility>
avi703dbbf2015-12-22 01:46:289
fsamuelb370b5a2015-11-14 01:15:1010#include "components/mus/common/types.h"
fsamuel68afcf452015-10-16 00:12:4511#include "components/mus/public/cpp/window.h"
benc6f7263b2015-10-27 18:17:3312#include "components/mus/public/cpp/window_property.h"
fsamuel68afcf452015-10-16 00:12:4513#include "components/mus/public/cpp/window_tree_connection.h"
sadrul46e2c77b2015-10-29 01:39:1614#include "components/mus/public/interfaces/input_events.mojom.h"
skyfd409492015-12-08 00:52:3515#include "components/mus/public/interfaces/mus_constants.mojom.h"
16#include "components/mus/public/interfaces/window_manager.mojom.h"
skyb0446352015-11-18 00:43:5617#include "mash/wm/non_client_frame_controller.h"
18#include "mash/wm/property_util.h"
19#include "mash/wm/public/interfaces/container.mojom.h"
sky7a35dfa2016-02-01 21:37:2820#include "mash/wm/root_window_controller.h"
sky05b04262015-10-28 22:44:5921#include "mojo/converters/geometry/geometry_type_converters.h"
skyd4ca12f2015-10-28 18:22:2622
sky89c6eb072015-11-25 20:12:2723namespace mash {
24namespace wm {
25
sky7a35dfa2016-02-01 21:37:2826WindowManager::WindowManager()
ben83cd090692016-02-03 05:07:2727 : root_controller_(nullptr),
28 window_manager_client_(nullptr),
29 binding_(this) {}
ben294d04b2015-10-15 01:23:3330
sky7a35dfa2016-02-01 21:37:2831WindowManager::~WindowManager() {
32 if (!root_controller_)
sadruleaa1be22015-11-30 23:30:4833 return;
sky7a35dfa2016-02-01 21:37:2834 for (auto container : root_controller_->root()->children()) {
benfe6381bc2015-12-02 16:07:2335 container->RemoveObserver(this);
36 for (auto child : container->children())
37 child->RemoveObserver(this);
38 }
skyd4ca12f2015-10-28 18:22:2639}
ben294d04b2015-10-15 01:23:3340
ben83cd090692016-02-03 05:07:2741void WindowManager::Initialize(RootWindowController* root_controller,
benc740d5b2016-03-18 04:54:2742 session::mojom::Session* session) {
sky7a35dfa2016-02-01 21:37:2843 DCHECK(root_controller);
44 DCHECK(!root_controller_);
45 root_controller_ = root_controller;
benfe6381bc2015-12-02 16:07:2346 // The children of the root are considered containers.
sky7a35dfa2016-02-01 21:37:2847 for (auto container : root_controller_->root()->children()) {
benfe6381bc2015-12-02 16:07:2348 container->AddObserver(this);
49 for (auto child : container->children())
50 child->AddObserver(this);
51 }
skyf5ece4e2016-01-26 19:40:3652
53 // The insets are roughly what is needed by CustomFrameView. The expectation
54 // is at some point we'll write our own NonClientFrameView and get the insets
55 // from it.
56 mus::mojom::FrameDecorationValuesPtr frame_decoration_values =
57 mus::mojom::FrameDecorationValues::New();
58 const gfx::Insets client_area_insets =
59 NonClientFrameController::GetPreferredClientAreaInsets();
60 frame_decoration_values->normal_client_area_insets =
61 mojo::Insets::From(client_area_insets);
62 frame_decoration_values->maximized_client_area_insets =
63 mojo::Insets::From(client_area_insets);
64 frame_decoration_values->max_title_bar_button_width =
65 NonClientFrameController::GetMaxTitleBarButtonWidth();
66 window_manager_client_->SetFrameDecorationValues(
67 std::move(frame_decoration_values));
ben83cd090692016-02-03 05:07:2768
benc740d5b2016-03-18 04:54:2769 if (session)
70 session->AddScreenlockStateListener(binding_.CreateInterfacePtrAndBind());
sadruleaa1be22015-11-30 23:30:4871}
72
sky7a35dfa2016-02-01 21:37:2873gfx::Rect WindowManager::CalculateDefaultBounds(mus::Window* window) const {
74 DCHECK(root_controller_);
sky05111cd2015-11-16 22:41:5775 int width, height;
76 const gfx::Size pref = GetWindowPreferredSize(window);
sky7a35dfa2016-02-01 21:37:2877 const mus::Window* root = root_controller_->root();
sky05111cd2015-11-16 22:41:5778 if (pref.IsEmpty()) {
79 width = root->bounds().width() - 240;
80 height = root->bounds().height() - 240;
81 } else {
82 // TODO(sky): likely want to constrain more than root size.
83 const gfx::Size max_size = GetMaximizedWindowBounds().size();
84 width = std::max(0, std::min(max_size.width(), pref.width()));
85 height = std::max(0, std::min(max_size.height(), pref.height()));
86 }
sky7a35dfa2016-02-01 21:37:2887 return gfx::Rect(40 + (root_controller_->window_count() % 4) * 40,
88 40 + (root_controller_->window_count() % 4) * 40, width,
89 height);
sky05111cd2015-11-16 22:41:5790}
91
sky7a35dfa2016-02-01 21:37:2892gfx::Rect WindowManager::GetMaximizedWindowBounds() const {
93 DCHECK(root_controller_);
94 return gfx::Rect(root_controller_->root()->bounds().size());
sky05111cd2015-11-16 22:41:5795}
96
sky7a35dfa2016-02-01 21:37:2897mus::Window* WindowManager::NewTopLevelWindow(
skyc125e4b2016-01-30 00:47:4998 std::map<std::string, std::vector<uint8_t>>* properties) {
sky7a35dfa2016-02-01 21:37:2899 DCHECK(root_controller_);
100 mus::Window* root = root_controller_->root();
skyadfb92372016-01-07 17:34:24101 DCHECK(root);
102
103 const bool provide_non_client_frame =
sammc16fb38a82016-01-21 05:30:18104 GetWindowType(*properties) == mus::mojom::WindowType::WINDOW;
skyadfb92372016-01-07 17:34:24105 if (provide_non_client_frame)
106 (*properties)[mus::mojom::kWaitForUnderlay_Property].clear();
107
108 // TODO(sky): constrain and validate properties before passing to server.
109 mus::Window* window = root->connection()->NewWindow(properties);
110 window->SetBounds(CalculateDefaultBounds(window));
111
112 mojom::Container container = GetRequestedContainer(window);
sky7a35dfa2016-02-01 21:37:28113 root_controller_->GetWindowForContainer(container)->AddChild(window);
skyadfb92372016-01-07 17:34:24114
skyadfb92372016-01-07 17:34:24115 if (provide_non_client_frame) {
sky423bc2b2016-03-31 20:48:09116 NonClientFrameController::Create(root_controller_->GetConnector(), window,
117 root_controller_->window_manager_client());
skyadfb92372016-01-07 17:34:24118 }
119
sky7a35dfa2016-02-01 21:37:28120 root_controller_->IncrementWindowCount();
skyadfb92372016-01-07 17:34:24121
122 return window;
123}
124
sky7a35dfa2016-02-01 21:37:28125void WindowManager::OnTreeChanging(const TreeChangeParams& params) {
126 DCHECK(root_controller_);
127 if (root_controller_->WindowIsContainer(params.old_parent))
sadruleaa1be22015-11-30 23:30:48128 params.target->RemoveObserver(this);
sky7a35dfa2016-02-01 21:37:28129 else if (root_controller_->WindowIsContainer(params.new_parent))
sadruleaa1be22015-11-30 23:30:48130 params.target->AddObserver(this);
131}
132
sky7a35dfa2016-02-01 21:37:28133void WindowManager::OnWindowEmbeddedAppDisconnected(mus::Window* window) {
sadruleaa1be22015-11-30 23:30:48134 window->Destroy();
135}
136
sky7a35dfa2016-02-01 21:37:28137void WindowManager::SetWindowManagerClient(mus::WindowManagerClient* client) {
skyf5ece4e2016-01-26 19:40:36138 window_manager_client_ = client;
skybbcf04eb2015-10-15 23:07:56139}
skyd4ca12f2015-10-28 18:22:26140
sky7a35dfa2016-02-01 21:37:28141bool WindowManager::OnWmSetBounds(mus::Window* window, gfx::Rect* bounds) {
sky6905d4c2015-11-26 01:53:21142 // By returning true the bounds of |window| is updated.
143 return true;
144}
145
sky7a35dfa2016-02-01 21:37:28146bool WindowManager::OnWmSetProperty(
sky6905d4c2015-11-26 01:53:21147 mus::Window* window,
148 const std::string& name,
149 scoped_ptr<std::vector<uint8_t>>* new_data) {
150 // TODO(sky): constrain this to set of keys we know about, and allowed
151 // values.
152 return name == mus::mojom::WindowManager::kShowState_Property ||
153 name == mus::mojom::WindowManager::kPreferredSize_Property ||
erga665cd12015-12-11 22:06:53154 name == mus::mojom::WindowManager::kResizeBehavior_Property ||
jamescookcac87092016-03-25 18:11:53155 name == mus::mojom::WindowManager::kWindowAppIcon_Property ||
erga665cd12015-12-11 22:06:53156 name == mus::mojom::WindowManager::kWindowTitle_Property;
sky6905d4c2015-11-26 01:53:21157}
158
sky7a35dfa2016-02-01 21:37:28159mus::Window* WindowManager::OnWmCreateTopLevelWindow(
skyadfb92372016-01-07 17:34:24160 std::map<std::string, std::vector<uint8_t>>* properties) {
skyc125e4b2016-01-30 00:47:49161 return NewTopLevelWindow(properties);
skyadfb92372016-01-07 17:34:24162}
163
moshayedid41e5ab2016-03-18 00:08:15164void WindowManager::OnAccelerator(uint32_t id, const ui::Event& event) {
skyffa8df22016-02-02 05:47:34165 root_controller_->OnAccelerator(id, std::move(event));
166}
167
ben83cd090692016-02-03 05:07:27168void WindowManager::ScreenlockStateChanged(bool locked) {
169 // Hide USER_PRIVATE windows when the screen is locked.
170 mus::Window* window = root_controller_->GetWindowForContainer(
171 mash::wm::mojom::Container::USER_PRIVATE);
172 window->SetVisible(!locked);
173}
174
sky89c6eb072015-11-25 20:12:27175} // namespace wm
176} // namespace mash