OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 | |
5 #include "apps/shell/browser/shell_desktop_controller.h" | |
6 | |
7 #include "apps/shell/browser/shell_app_window_controller.h" | |
8 #include "apps/shell/common/switches.h" | |
9 #include "base/command_line.h" | |
10 #include "content/public/browser/context_factory.h" | |
11 #include "ui/aura/client/cursor_client.h" | |
12 #include "ui/aura/client/default_capture_client.h" | |
13 #include "ui/aura/env.h" | |
14 #include "ui/aura/layout_manager.h" | |
15 #include "ui/aura/test/test_screen.h" | |
16 #include "ui/aura/window.h" | |
17 #include "ui/aura/window_event_dispatcher.h" | |
18 #include "ui/aura/window_tree_host.h" | |
19 #include "ui/base/cursor/cursor.h" | |
20 #include "ui/base/cursor/image_cursors.h" | |
21 #include "ui/base/ime/input_method_initializer.h" | |
22 #include "ui/gfx/native_widget_types.h" | |
23 #include "ui/gfx/screen.h" | |
24 #include "ui/wm/core/base_focus_rules.h" | |
25 #include "ui/wm/core/compound_event_filter.h" | |
26 #include "ui/wm/core/cursor_manager.h" | |
27 #include "ui/wm/core/focus_controller.h" | |
28 #include "ui/wm/core/input_method_event_filter.h" | |
29 #include "ui/wm/core/native_cursor_manager.h" | |
30 #include "ui/wm/core/native_cursor_manager_delegate.h" | |
31 #include "ui/wm/core/user_activity_detector.h" | |
32 | |
33 #if defined(OS_CHROMEOS) | |
34 #include "ui/chromeos/user_activity_power_manager_notifier.h" | |
35 #include "ui/display/types/chromeos/display_mode.h" | |
36 #include "ui/display/types/chromeos/display_snapshot.h" | |
37 #endif | |
38 | |
39 namespace apps { | |
40 namespace { | |
41 | |
42 // A simple layout manager that makes each new window fill its parent. | |
43 class FillLayout : public aura::LayoutManager { | |
44 public: | |
45 FillLayout() {} | |
46 virtual ~FillLayout() {} | |
47 | |
48 private: | |
49 // aura::LayoutManager: | |
50 virtual void OnWindowResized() OVERRIDE {} | |
51 | |
52 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE { | |
53 if (!child->parent()) | |
54 return; | |
55 | |
56 // Create a rect at 0,0 with the size of the parent. | |
57 gfx::Size parent_size = child->parent()->bounds().size(); | |
58 child->SetBounds(gfx::Rect(parent_size)); | |
59 } | |
60 | |
61 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} | |
62 | |
63 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} | |
64 | |
65 virtual void OnChildWindowVisibilityChanged(aura::Window* child, | |
66 bool visible) OVERRIDE {} | |
67 | |
68 virtual void SetChildBounds(aura::Window* child, | |
69 const gfx::Rect& requested_bounds) OVERRIDE { | |
70 SetChildBoundsDirect(child, requested_bounds); | |
71 } | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(FillLayout); | |
74 }; | |
75 | |
76 // A class that bridges the gap between CursorManager and Aura. It borrows | |
77 // heavily from AshNativeCursorManager. | |
78 class ShellNativeCursorManager : public wm::NativeCursorManager { | |
79 public: | |
80 explicit ShellNativeCursorManager(aura::WindowTreeHost* host) | |
81 : host_(host), | |
82 image_cursors_(new ui::ImageCursors) {} | |
83 virtual ~ShellNativeCursorManager() {} | |
84 | |
85 // wm::NativeCursorManager overrides. | |
86 virtual void SetDisplay( | |
87 const gfx::Display& display, | |
88 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
89 if (image_cursors_->SetDisplay(display, display.device_scale_factor())) | |
90 SetCursor(delegate->GetCursor(), delegate); | |
91 } | |
92 | |
93 virtual void SetCursor( | |
94 gfx::NativeCursor cursor, | |
95 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
96 image_cursors_->SetPlatformCursor(&cursor); | |
97 cursor.set_device_scale_factor(image_cursors_->GetScale()); | |
98 delegate->CommitCursor(cursor); | |
99 | |
100 if (delegate->IsCursorVisible()) | |
101 ApplyCursor(cursor); | |
102 } | |
103 | |
104 virtual void SetVisibility( | |
105 bool visible, | |
106 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
107 delegate->CommitVisibility(visible); | |
108 | |
109 if (visible) { | |
110 SetCursor(delegate->GetCursor(), delegate); | |
111 } else { | |
112 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | |
113 image_cursors_->SetPlatformCursor(&invisible_cursor); | |
114 ApplyCursor(invisible_cursor); | |
115 } | |
116 } | |
117 | |
118 virtual void SetCursorSet( | |
119 ui::CursorSetType cursor_set, | |
120 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
121 image_cursors_->SetCursorSet(cursor_set); | |
122 delegate->CommitCursorSet(cursor_set); | |
123 if (delegate->IsCursorVisible()) | |
124 SetCursor(delegate->GetCursor(), delegate); | |
125 } | |
126 | |
127 virtual void SetMouseEventsEnabled( | |
128 bool enabled, | |
129 wm::NativeCursorManagerDelegate* delegate) OVERRIDE { | |
130 delegate->CommitMouseEventsEnabled(enabled); | |
131 SetVisibility(delegate->IsCursorVisible(), delegate); | |
132 } | |
133 | |
134 private: | |
135 // Sets |cursor| as the active cursor within Aura. | |
136 void ApplyCursor(gfx::NativeCursor cursor) { | |
137 host_->SetCursor(cursor); | |
138 } | |
139 | |
140 aura::WindowTreeHost* host_; // Not owned. | |
141 | |
142 scoped_ptr<ui::ImageCursors> image_cursors_; | |
143 | |
144 DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager); | |
145 }; | |
146 | |
147 class AppsFocusRules : public wm::BaseFocusRules { | |
148 public: | |
149 AppsFocusRules() {} | |
150 virtual ~AppsFocusRules() {} | |
151 | |
152 virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE { | |
153 return true; | |
154 } | |
155 | |
156 private: | |
157 DISALLOW_COPY_AND_ASSIGN(AppsFocusRules); | |
158 }; | |
159 | |
160 ShellDesktopController* g_instance = NULL; | |
161 | |
162 } // namespace | |
163 | |
164 ShellDesktopController::ShellDesktopController() { | |
165 #if defined(OS_CHROMEOS) | |
166 display_configurator_.reset(new ui::DisplayConfigurator); | |
167 display_configurator_->Init(false); | |
168 display_configurator_->ForceInitialConfigure(0); | |
169 display_configurator_->AddObserver(this); | |
170 #endif | |
171 aura::Env::CreateInstance(true); | |
172 aura::Env::GetInstance()->set_context_factory(content::GetContextFactory()); | |
173 | |
174 g_instance = this; | |
175 } | |
176 | |
177 ShellDesktopController::~ShellDesktopController() { | |
178 app_window_controller_.reset(); | |
179 g_instance = NULL; | |
180 DestroyRootWindow(); | |
181 aura::Env::DeleteInstance(); | |
182 } | |
183 | |
184 // static | |
185 ShellDesktopController* ShellDesktopController::instance() { | |
186 return g_instance; | |
187 } | |
188 | |
189 void ShellDesktopController::SetAppWindowController( | |
190 ShellAppWindowController* app_window_controller) { | |
191 app_window_controller_.reset(app_window_controller); | |
192 } | |
193 | |
194 ShellAppWindow* ShellDesktopController::CreateAppWindow( | |
195 content::BrowserContext* context) { | |
196 return app_window_controller_->CreateAppWindow(context); | |
197 } | |
198 | |
199 void ShellDesktopController::CloseAppWindows() { | |
200 if (app_window_controller_) | |
201 app_window_controller_->CloseAppWindows(); | |
202 } | |
203 | |
204 aura::Window* ShellDesktopController::GetDefaultParent( | |
205 aura::Window* context, | |
206 aura::Window* window, | |
207 const gfx::Rect& bounds) { | |
208 return host_->window(); | |
209 } | |
210 | |
211 #if defined(OS_CHROMEOS) | |
212 void ShellDesktopController::OnDisplayModeChanged( | |
213 const std::vector<ui::DisplayConfigurator::DisplayState>& displays) { | |
214 gfx::Size size = GetPrimaryDisplaySize(); | |
215 if (!size.IsEmpty()) | |
216 host_->UpdateRootWindowSize(size); | |
217 } | |
218 #endif | |
219 | |
220 void ShellDesktopController::OnHostCloseRequested( | |
221 const aura::WindowTreeHost* host) { | |
222 DCHECK_EQ(host_.get(), host); | |
223 CloseAppWindows(); | |
224 base::MessageLoop::current()->PostTask(FROM_HERE, | |
225 base::MessageLoop::QuitClosure()); | |
226 } | |
227 | |
228 void ShellDesktopController::CreateRootWindow() { | |
229 // Set up basic pieces of ui::wm. | |
230 gfx::Size size; | |
231 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
232 if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) { | |
233 const std::string size_str = | |
234 command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds); | |
235 int width, height; | |
236 CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height)); | |
237 size = gfx::Size(width, height); | |
238 } else { | |
239 size = GetPrimaryDisplaySize(); | |
240 } | |
241 if (size.IsEmpty()) | |
242 size = gfx::Size(1280, 720); | |
243 | |
244 test_screen_.reset(aura::TestScreen::Create(size)); | |
245 // TODO(jamescook): Replace this with a real Screen implementation. | |
246 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get()); | |
247 // TODO(mukai): Set up input method. | |
248 | |
249 host_.reset(test_screen_->CreateHostForPrimaryDisplay()); | |
250 host_->InitHost(); | |
251 aura::client::SetWindowTreeClient(host_->window(), this); | |
252 root_window_event_filter_.reset(new wm::CompoundEventFilter); | |
253 host_->window()->AddPreTargetHandler(root_window_event_filter_.get()); | |
254 InitWindowManager(); | |
255 | |
256 host_->AddObserver(this); | |
257 | |
258 // Ensure the X window gets mapped. | |
259 host_->Show(); | |
260 } | |
261 | |
262 void ShellDesktopController::InitWindowManager() { | |
263 wm::FocusController* focus_controller = | |
264 new wm::FocusController(CreateFocusRules()); | |
265 aura::client::SetFocusClient(host_->window(), focus_controller); | |
266 host_->window()->AddPreTargetHandler(focus_controller); | |
267 aura::client::SetActivationClient(host_->window(), focus_controller); | |
268 focus_client_.reset(focus_controller); | |
269 | |
270 input_method_filter_.reset( | |
271 new wm::InputMethodEventFilter(host_->GetAcceleratedWidget())); | |
272 input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window()); | |
273 root_window_event_filter_->AddHandler(input_method_filter_.get()); | |
274 | |
275 capture_client_.reset( | |
276 new aura::client::DefaultCaptureClient(host_->window())); | |
277 | |
278 // Ensure new windows fill the display. | |
279 host_->window()->SetLayoutManager(new FillLayout); | |
280 | |
281 cursor_manager_.reset( | |
282 new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>( | |
283 new ShellNativeCursorManager(host_.get())))); | |
284 cursor_manager_->SetDisplay( | |
285 gfx::Screen::GetNativeScreen()->GetPrimaryDisplay()); | |
286 cursor_manager_->SetCursor(ui::kCursorPointer); | |
287 aura::client::SetCursorClient(host_->window(), cursor_manager_.get()); | |
288 | |
289 user_activity_detector_.reset(new wm::UserActivityDetector); | |
290 host_->event_processor()->GetRootTarget()->AddPreTargetHandler( | |
291 user_activity_detector_.get()); | |
292 #if defined(OS_CHROMEOS) | |
293 user_activity_notifier_.reset( | |
294 new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get())); | |
295 #endif | |
296 } | |
297 | |
298 wm::FocusRules* ShellDesktopController::CreateFocusRules() { | |
299 return new AppsFocusRules(); | |
300 } | |
301 | |
302 void ShellDesktopController::DestroyRootWindow() { | |
303 host_->RemoveObserver(this); | |
304 if (input_method_filter_) | |
305 root_window_event_filter_->RemoveHandler(input_method_filter_.get()); | |
306 if (user_activity_detector_) { | |
307 host_->event_processor()->GetRootTarget()->RemovePreTargetHandler( | |
308 user_activity_detector_.get()); | |
309 } | |
310 wm::FocusController* focus_controller = | |
311 static_cast<wm::FocusController*>(focus_client_.get()); | |
312 if (focus_controller) { | |
313 host_->window()->RemovePreTargetHandler(focus_controller); | |
314 aura::client::SetActivationClient(host_->window(), NULL); | |
315 } | |
316 root_window_event_filter_.reset(); | |
317 capture_client_.reset(); | |
318 input_method_filter_.reset(); | |
319 focus_client_.reset(); | |
320 cursor_manager_.reset(); | |
321 #if defined(OS_CHROMEOS) | |
322 user_activity_notifier_.reset(); | |
323 #endif | |
324 user_activity_detector_.reset(); | |
325 host_.reset(); | |
326 } | |
327 | |
328 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() { | |
329 #if defined(OS_CHROMEOS) | |
330 const std::vector<ui::DisplayConfigurator::DisplayState>& displays = | |
331 display_configurator_->cached_displays(); | |
332 if (displays.empty()) | |
333 return gfx::Size(); | |
334 const ui::DisplayMode* mode = displays[0].display->current_mode(); | |
335 return mode ? mode->size() : gfx::Size(); | |
336 #else | |
337 return gfx::Size(); | |
338 #endif | |
339 } | |
340 | |
341 } // namespace apps | |
OLD | NEW |