forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_level_window_factory.cc
244 lines (219 loc) · 9.58 KB
/
top_level_window_factory.cc
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/mus/top_level_window_factory.h"
#include "ash/mus/disconnected_app_handler.h"
#include "ash/mus/frame/detached_title_area_renderer.h"
#include "ash/mus/non_client_frame_controller.h"
#include "ash/mus/property_util.h"
#include "ash/mus/window_manager.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/root_window_controller.h"
#include "ash/root_window_settings.h"
#include "ash/shell.h"
#include "ash/wm/container_finder.h"
#include "ash/wm/window_state.h"
#include "mojo/public/cpp/bindings/type_converter.h"
#include "services/ui/public/cpp/property_type_converters.h"
#include "services/ui/public/interfaces/window_manager.mojom.h"
#include "services/ui/public/interfaces/window_manager_constants.mojom.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/mus/property_converter.h"
#include "ui/aura/mus/property_utils.h"
#include "ui/aura/mus/window_tree_client.h"
#include "ui/aura/window.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/views/widget/widget.h"
namespace ash {
namespace mus {
namespace {
// Returns true if a fullscreen window was requested.
bool IsFullscreen(aura::PropertyConverter* property_converter,
const std::vector<uint8_t>& transport_data) {
using ui::mojom::WindowManager;
aura::PropertyConverter::PrimitiveType show_state = 0;
return property_converter->GetPropertyValueFromTransportValue(
WindowManager::kShowState_Property, transport_data, &show_state) &&
(static_cast<ui::WindowShowState>(show_state) ==
ui::SHOW_STATE_FULLSCREEN);
}
bool ShouldRenderTitleArea(
aura::PropertyConverter* property_converter,
const std::map<std::string, std::vector<uint8_t>>& properties) {
auto iter = properties.find(
ui::mojom::WindowManager::kRenderParentTitleArea_Property);
if (iter == properties.end())
return false;
aura::PropertyConverter::PrimitiveType value = 0;
return property_converter->GetPropertyValueFromTransportValue(
ui::mojom::WindowManager::kRenderParentTitleArea_Property,
iter->second, &value) &&
value == 1;
}
// Returns the RootWindowController where new top levels are created.
// |properties| is the properties supplied during window creation.
RootWindowController* GetRootWindowControllerForNewTopLevelWindow(
std::map<std::string, std::vector<uint8_t>>* properties) {
// If a specific display was requested, use it.
const int64_t display_id = GetInitialDisplayId(*properties);
if (display_id != display::kInvalidDisplayId) {
for (RootWindowController* root_window_controller :
RootWindowController::root_window_controllers()) {
if (GetRootWindowSettings(root_window_controller->GetRootWindow())
->display_id == display_id) {
return root_window_controller;
}
}
}
return RootWindowController::ForWindow(Shell::GetRootWindowForNewWindows());
}
// Returns the bounds for the new window.
gfx::Rect CalculateDefaultBounds(
WindowManager* window_manager,
RootWindowController* root_window_controller,
aura::Window* container_window,
const std::map<std::string, std::vector<uint8_t>>* properties) {
gfx::Rect requested_bounds;
if (GetInitialBounds(*properties, &requested_bounds))
return requested_bounds;
const gfx::Size root_size =
root_window_controller->GetRootWindow()->bounds().size();
auto show_state_iter =
properties->find(ui::mojom::WindowManager::kShowState_Property);
if (show_state_iter != properties->end()) {
if (IsFullscreen(window_manager->property_converter(),
show_state_iter->second)) {
gfx::Rect bounds(root_size);
if (!container_window) {
const display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(
root_window_controller->GetRootWindow());
bounds.Offset(display.bounds().OffsetFromOrigin());
}
return bounds;
}
}
gfx::Size window_size;
if (GetWindowPreferredSize(*properties, &window_size) &&
!window_size.IsEmpty()) {
// TODO(sky): likely want to constrain more than root size.
window_size.SetToMin(root_size);
} else {
static constexpr int kRootSizeDelta = 240;
window_size.SetSize(root_size.width() - kRootSizeDelta,
root_size.height() - kRootSizeDelta);
}
// TODO(sky): this should use code in chrome/browser/ui/window_sizer.
static constexpr int kOriginOffset = 40;
return gfx::Rect(gfx::Point(kOriginOffset, kOriginOffset), window_size);
}
// Does the real work of CreateAndParentTopLevelWindow() once the appropriate
// RootWindowController was found.
aura::Window* CreateAndParentTopLevelWindowInRoot(
WindowManager* window_manager,
RootWindowController* root_window_controller,
ui::mojom::WindowType window_type,
std::map<std::string, std::vector<uint8_t>>* properties) {
// TODO(sky): constrain and validate properties.
int32_t container_id = kShellWindowId_Invalid;
aura::Window* context = nullptr;
aura::Window* container_window = nullptr;
if (GetInitialContainerId(*properties, &container_id)) {
container_window =
root_window_controller->GetRootWindow()->GetChildById(container_id);
} else {
context = root_window_controller->GetRootWindow();
}
gfx::Rect bounds = CalculateDefaultBounds(
window_manager, root_window_controller, container_window, properties);
const bool provide_non_client_frame =
window_type == ui::mojom::WindowType::WINDOW ||
window_type == ui::mojom::WindowType::PANEL;
if (provide_non_client_frame) {
// See NonClientFrameController for details on lifetime.
NonClientFrameController* non_client_frame_controller =
new NonClientFrameController(container_window, context, bounds,
window_type, properties, window_manager);
return non_client_frame_controller->window();
}
aura::PropertyConverter* property_converter =
window_manager->property_converter();
if (window_type == ui::mojom::WindowType::POPUP &&
ShouldRenderTitleArea(property_converter, *properties)) {
// Pick a parent so display information is obtained. Will pick the real one
// once transient parent found.
aura::Window* unparented_control_container =
root_window_controller->GetRootWindow()->GetChildById(
kShellWindowId_UnparentedControlContainer);
// DetachedTitleAreaRendererForClient is owned by the client.
DetachedTitleAreaRendererForClient* renderer =
new DetachedTitleAreaRendererForClient(unparented_control_container,
properties, window_manager);
return renderer->widget()->GetNativeView();
}
aura::Window* window = new aura::Window(nullptr);
aura::SetWindowType(window, window_type);
window->SetProperty(aura::client::kEmbedType,
aura::client::WindowEmbedType::TOP_LEVEL_IN_WM);
ApplyProperties(window, property_converter, *properties);
window->Init(ui::LAYER_TEXTURED);
window->SetBounds(bounds);
if (container_window) {
container_window->AddChild(window);
} else {
aura::Window* root = root_window_controller->GetRootWindow();
gfx::Point origin;
aura::Window::ConvertPointToTarget(root, root->GetRootWindow(), &origin);
const display::Display display =
display::Screen::GetScreen()->GetDisplayNearestWindow(
root_window_controller->GetRootWindow());
origin += display.bounds().OffsetFromOrigin();
gfx::Rect bounds_in_screen(origin, bounds.size());
ash::wm::GetDefaultParent(window, bounds_in_screen)->AddChild(window);
}
return window;
}
} // namespace
aura::Window* CreateAndParentTopLevelWindow(
WindowManager* window_manager,
ui::mojom::WindowType window_type,
std::map<std::string, std::vector<uint8_t>>* properties) {
RootWindowController* root_window_controller =
GetRootWindowControllerForNewTopLevelWindow(properties);
aura::Window* window = CreateAndParentTopLevelWindowInRoot(
window_manager, root_window_controller, window_type, properties);
DisconnectedAppHandler::Create(window);
auto ignored_by_shelf_iter = properties->find(
ui::mojom::WindowManager::kWindowIgnoredByShelf_InitProperty);
if (ignored_by_shelf_iter != properties->end()) {
wm::WindowState* window_state = wm::GetWindowState(window);
window_state->set_ignored_by_shelf(
mojo::ConvertTo<bool>(ignored_by_shelf_iter->second));
// No need to persist this value.
properties->erase(ignored_by_shelf_iter);
}
auto focusable_iter =
properties->find(ui::mojom::WindowManager::kFocusable_InitProperty);
if (focusable_iter != properties->end()) {
bool can_focus = mojo::ConvertTo<bool>(focusable_iter->second);
window_manager->window_tree_client()->SetCanFocus(window, can_focus);
NonClientFrameController* non_client_frame_controller =
NonClientFrameController::Get(window);
if (non_client_frame_controller)
non_client_frame_controller->set_can_activate(can_focus);
// No need to persist this value.
properties->erase(focusable_iter);
}
auto translucent_iter =
properties->find(ui::mojom::WindowManager::kTranslucent_InitProperty);
if (translucent_iter != properties->end()) {
bool translucent = mojo::ConvertTo<bool>(translucent_iter->second);
window->SetTransparent(translucent);
// No need to persist this value.
properties->erase(translucent_iter);
}
return window;
}
} // namespace mus
} // namespace ash