1 // Copyright (c) 2012 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.
5 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
8 #include "base/debug/trace_event.h"
9 #include "ui/aura/client/aura_constants.h"
10 #include "ui/aura/client/cursor_client.h"
11 #include "ui/aura/client/focus_client.h"
12 #include "ui/aura/client/window_tree_client.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_observer.h"
15 #include "ui/aura/window_property.h"
16 #include "ui/aura/window_tree_host.h"
17 #include "ui/base/hit_test.h"
18 #include "ui/base/ui_base_switches_util.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/point_conversions.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/gfx/size_conversions.h"
25 #include "ui/native_theme/native_theme.h"
26 #include "ui/views/corewm/tooltip.h"
27 #include "ui/views/corewm/tooltip_controller.h"
28 #include "ui/views/drag_utils.h"
29 #include "ui/views/ime/input_method_bridge.h"
30 #include "ui/views/ime/null_input_method.h"
31 #include "ui/views/view_constants_aura.h"
32 #include "ui/views/widget/desktop_aura/desktop_capture_client.h"
33 #include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
34 #include "ui/views/widget/desktop_aura/desktop_dispatcher_client.h"
35 #include "ui/views/widget/desktop_aura/desktop_event_client.h"
36 #include "ui/views/widget/desktop_aura/desktop_focus_rules.h"
37 #include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
38 #include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
39 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
40 #include "ui/views/widget/drop_helper.h"
41 #include "ui/views/widget/native_widget_aura.h"
42 #include "ui/views/widget/root_view.h"
43 #include "ui/views/widget/tooltip_manager_aura.h"
44 #include "ui/views/widget/widget.h"
45 #include "ui/views/widget/widget_aura_utils.h"
46 #include "ui/views/widget/widget_delegate.h"
47 #include "ui/views/widget/window_reorderer.h"
48 #include "ui/wm/core/compound_event_filter.h"
49 #include "ui/wm/core/cursor_manager.h"
50 #include "ui/wm/core/focus_controller.h"
51 #include "ui/wm/core/input_method_event_filter.h"
52 #include "ui/wm/core/native_cursor_manager.h"
53 #include "ui/wm/core/shadow_controller.h"
54 #include "ui/wm/core/shadow_types.h"
55 #include "ui/wm/core/visibility_controller.h"
56 #include "ui/wm/core/window_modality_controller.h"
57 #include "ui/wm/public/activation_client.h"
58 #include "ui/wm/public/drag_drop_client.h"
61 #include "ui/base/win/shell.h"
62 #include "ui/gfx/win/dpi.h"
65 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(VIEWS_EXPORT,
66 views::DesktopNativeWidgetAura*);
70 DEFINE_WINDOW_PROPERTY_KEY(DesktopNativeWidgetAura*,
71 kDesktopNativeWidgetAuraKey, NULL);
75 // This class provides functionality to create a top level widget to host a
77 class DesktopNativeWidgetTopLevelHandler : public aura::WindowObserver {
79 // This function creates a widget with the bounds passed in which eventually
80 // becomes the parent of the child window passed in.
81 static aura::Window* CreateParentWindow(aura::Window* child_window,
82 const gfx::Rect& bounds,
84 bool root_is_always_on_top) {
85 // This instance will get deleted when the widget is destroyed.
86 DesktopNativeWidgetTopLevelHandler* top_level_handler =
87 new DesktopNativeWidgetTopLevelHandler;
89 child_window->SetBounds(gfx::Rect(bounds.size()));
91 Widget::InitParams init_params;
92 init_params.type = full_screen ? Widget::InitParams::TYPE_WINDOW :
93 Widget::InitParams::TYPE_POPUP;
94 init_params.bounds = bounds;
95 init_params.ownership = Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET;
96 init_params.layer_type = aura::WINDOW_LAYER_NOT_DRAWN;
97 init_params.can_activate = full_screen;
98 init_params.keep_on_top = root_is_always_on_top;
100 // This widget instance will get deleted when the window is
102 top_level_handler->top_level_widget_ = new Widget();
103 top_level_handler->top_level_widget_->Init(init_params);
105 top_level_handler->top_level_widget_->SetFullscreen(full_screen);
106 top_level_handler->top_level_widget_->Show();
108 aura::Window* native_window =
109 top_level_handler->top_level_widget_->GetNativeView();
110 child_window->AddObserver(top_level_handler);
111 native_window->AddObserver(top_level_handler);
112 top_level_handler->child_window_ = child_window;
113 return native_window;
116 // aura::WindowObserver overrides
117 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
118 window->RemoveObserver(this);
120 // If the widget is being destroyed by the OS then we should not try and
122 if (top_level_widget_ &&
123 window == top_level_widget_->GetNativeView()) {
124 top_level_widget_ = NULL;
128 if (top_level_widget_) {
129 DCHECK(top_level_widget_->GetNativeView());
130 top_level_widget_->GetNativeView()->RemoveObserver(this);
131 // When we receive a notification that the child of the window created
132 // above is being destroyed we go ahead and initiate the destruction of
133 // the corresponding widget.
134 top_level_widget_->Close();
135 top_level_widget_ = NULL;
140 virtual void OnWindowBoundsChanged(aura::Window* window,
141 const gfx::Rect& old_bounds,
142 const gfx::Rect& new_bounds) OVERRIDE {
143 if (top_level_widget_ && window == child_window_)
144 top_level_widget_->SetSize(new_bounds.size());
148 DesktopNativeWidgetTopLevelHandler()
149 : top_level_widget_(NULL),
150 child_window_(NULL) {}
152 virtual ~DesktopNativeWidgetTopLevelHandler() {}
154 Widget* top_level_widget_;
155 aura::Window* child_window_;
157 DISALLOW_COPY_AND_ASSIGN(DesktopNativeWidgetTopLevelHandler);
160 class DesktopNativeWidgetAuraWindowTreeClient :
161 public aura::client::WindowTreeClient {
163 explicit DesktopNativeWidgetAuraWindowTreeClient(
164 aura::Window* root_window)
165 : root_window_(root_window) {
166 aura::client::SetWindowTreeClient(root_window_, this);
168 virtual ~DesktopNativeWidgetAuraWindowTreeClient() {
169 aura::client::SetWindowTreeClient(root_window_, NULL);
172 // Overridden from client::WindowTreeClient:
173 virtual aura::Window* GetDefaultParent(aura::Window* context,
174 aura::Window* window,
175 const gfx::Rect& bounds) OVERRIDE {
176 bool is_fullscreen = window->GetProperty(aura::client::kShowStateKey) ==
177 ui::SHOW_STATE_FULLSCREEN;
178 bool is_menu = window->type() == ui::wm::WINDOW_TYPE_MENU;
180 if (is_fullscreen || is_menu) {
181 bool root_is_always_on_top = false;
182 internal::NativeWidgetPrivate* native_widget =
183 DesktopNativeWidgetAura::ForWindow(root_window_);
185 root_is_always_on_top = native_widget->IsAlwaysOnTop();
187 return DesktopNativeWidgetTopLevelHandler::CreateParentWindow(
188 window, bounds, is_fullscreen, root_is_always_on_top);
194 aura::Window* root_window_;
196 DISALLOW_COPY_AND_ASSIGN(DesktopNativeWidgetAuraWindowTreeClient);
201 class FocusManagerEventHandler : public ui::EventHandler {
203 FocusManagerEventHandler(DesktopNativeWidgetAura* desktop_native_widget_aura)
204 : desktop_native_widget_aura_(desktop_native_widget_aura) {}
206 // Implementation of ui::EventHandler:
207 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
208 Widget* widget = desktop_native_widget_aura_->GetWidget();
209 if (widget && widget->GetFocusManager()->GetFocusedView() &&
210 !widget->GetFocusManager()->OnKeyEvent(*event)) {
216 DesktopNativeWidgetAura* desktop_native_widget_aura_;
218 DISALLOW_COPY_AND_ASSIGN(FocusManagerEventHandler);
221 class RootWindowDestructionObserver : public aura::WindowObserver {
223 explicit RootWindowDestructionObserver(DesktopNativeWidgetAura* parent)
225 virtual ~RootWindowDestructionObserver() {}
228 // Overridden from aura::WindowObserver:
229 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {
230 parent_->RootWindowDestroyed();
231 window->RemoveObserver(this);
235 DesktopNativeWidgetAura* parent_;
237 DISALLOW_COPY_AND_ASSIGN(RootWindowDestructionObserver);
240 ////////////////////////////////////////////////////////////////////////////////
241 // DesktopNativeWidgetAura, public:
243 int DesktopNativeWidgetAura::cursor_reference_count_ = 0;
244 DesktopNativeCursorManager* DesktopNativeWidgetAura::native_cursor_manager_ =
246 wm::CursorManager* DesktopNativeWidgetAura::cursor_manager_ = NULL;
248 DesktopNativeWidgetAura::DesktopNativeWidgetAura(
249 internal::NativeWidgetDelegate* delegate)
250 : desktop_window_tree_host_(NULL),
251 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET),
252 close_widget_factory_(this),
254 content_window_container_(NULL),
255 content_window_(new aura::Window(this)),
256 native_widget_delegate_(delegate),
257 last_drop_operation_(ui::DragDropTypes::DRAG_NONE),
258 restore_focus_on_activate_(false),
259 cursor_(gfx::kNullCursor),
260 widget_type_(Widget::InitParams::TYPE_WINDOW) {
261 aura::client::SetFocusChangeObserver(content_window_, this);
262 aura::client::SetActivationChangeObserver(content_window_, this);
265 DesktopNativeWidgetAura::~DesktopNativeWidgetAura() {
266 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
267 delete native_widget_delegate_;
273 DesktopNativeWidgetAura* DesktopNativeWidgetAura::ForWindow(
274 aura::Window* window) {
275 return window->GetProperty(kDesktopNativeWidgetAuraKey);
278 void DesktopNativeWidgetAura::OnHostClosed() {
279 // Don't invoke Widget::OnNativeWidgetDestroying(), its done by
280 // DesktopWindowTreeHost.
282 // The WindowModalityController is at the front of the event pretarget
283 // handler list. We destroy it first to preserve order symantics.
284 if (window_modality_controller_)
285 window_modality_controller_.reset();
287 // Make sure we don't have capture. Otherwise CaptureController and
288 // WindowEventDispatcher are left referencing a deleted Window.
290 aura::Window* capture_window = capture_client_->GetCaptureWindow();
291 if (capture_window && host_->window()->Contains(capture_window))
292 capture_window->ReleaseCapture();
295 // DesktopWindowTreeHost owns the ActivationController which ShadowController
296 // references. Make sure we destroy ShadowController early on.
297 shadow_controller_.reset();
298 tooltip_manager_.reset();
299 host_->window()->RemovePreTargetHandler(tooltip_controller_.get());
300 aura::client::SetTooltipClient(host_->window(), NULL);
301 tooltip_controller_.reset();
303 root_window_event_filter_->RemoveHandler(input_method_event_filter_.get());
305 window_tree_client_.reset(); // Uses host_->dispatcher() at destruction.
307 capture_client_.reset(); // Uses host_->dispatcher() at destruction.
309 // FocusController uses |content_window_|. Destroy it now so that we don't
310 // have to worry about the possibility of FocusController attempting to use
311 // |content_window_| after it's been destroyed but before all child windows
312 // have been destroyed.
313 host_->window()->RemovePreTargetHandler(focus_client_.get());
314 aura::client::SetFocusClient(host_->window(), NULL);
315 aura::client::SetActivationClient(host_->window(), NULL);
316 focus_client_.reset();
318 host_->RemoveObserver(this);
319 host_.reset(); // Uses input_method_event_filter_ at destruction.
320 // WindowEventDispatcher owns |desktop_window_tree_host_|.
321 desktop_window_tree_host_ = NULL;
322 content_window_ = NULL;
324 native_widget_delegate_->OnNativeWidgetDestroyed();
325 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
329 void DesktopNativeWidgetAura::OnDesktopWindowTreeHostDestroyed(
330 aura::WindowTreeHost* host) {
331 // |dispatcher_| is still valid, but DesktopWindowTreeHost is nearly
332 // destroyed. Do cleanup here of members DesktopWindowTreeHost may also use.
333 aura::client::SetDispatcherClient(host->window(), NULL);
334 dispatcher_client_.reset();
336 // We explicitly do NOT clear the cursor client property. Since the cursor
337 // manager is a singleton, it can outlive any window hierarchy, and it's
338 // important that objects attached to this destroying window hierarchy have
339 // an opportunity to deregister their observers from the cursor manager.
340 // They may want to do this when they are notified that they're being
341 // removed from the window hierarchy, which happens soon after this
342 // function when DesktopWindowTreeHost* calls DestroyDispatcher().
343 native_cursor_manager_->RemoveHost(host);
345 aura::client::SetScreenPositionClient(host->window(), NULL);
346 position_client_.reset();
348 aura::client::SetDragDropClient(host->window(), NULL);
349 drag_drop_client_.reset();
351 aura::client::SetEventClient(host->window(), NULL);
352 event_client_.reset();
355 void DesktopNativeWidgetAura::HandleActivationChanged(bool active) {
356 native_widget_delegate_->OnNativeWidgetActivationChanged(active);
357 aura::client::ActivationClient* activation_client =
358 aura::client::GetActivationClient(host_->window());
359 if (!activation_client)
362 if (GetWidget()->HasFocusManager()) {
363 // This function can be called before the focus manager has had a
364 // chance to set the focused view. In which case we should get the
365 // last focused view.
366 View* view_for_activation =
367 GetWidget()->GetFocusManager()->GetFocusedView() ?
368 GetWidget()->GetFocusManager()->GetFocusedView() :
369 GetWidget()->GetFocusManager()->GetStoredFocusView();
370 if (!view_for_activation)
371 view_for_activation = GetWidget()->GetRootView();
372 activation_client->ActivateWindow(
373 view_for_activation->GetWidget()->GetNativeView());
376 // If we're not active we need to deactivate the corresponding
377 // aura::Window. This way if a child widget is active it gets correctly
378 // deactivated (child widgets don't get native desktop activation changes,
379 // only aura activation changes).
380 aura::Window* active_window = activation_client->GetActiveWindow();
382 activation_client->DeactivateWindow(active_window);
386 ////////////////////////////////////////////////////////////////////////////////
387 // DesktopNativeWidgetAura, internal::NativeWidgetPrivate implementation:
389 void DesktopNativeWidgetAura::InitNativeWidget(
390 const Widget::InitParams& params) {
391 ownership_ = params.ownership;
392 widget_type_ = params.type;
394 NativeWidgetAura::RegisterNativeWidgetForWindow(this, content_window_);
395 // Animations on TYPE_WINDOW are handled by the OS. Additionally if we animate
396 // these windows the size of the window gets augmented, effecting restore
397 // bounds and maximized windows in bad ways.
398 if (params.type == Widget::InitParams::TYPE_WINDOW &&
399 !params.remove_standard_frame) {
400 content_window_->SetProperty(aura::client::kAnimationsDisabledKey, true);
402 content_window_->SetType(GetAuraWindowTypeForWidgetType(params.type));
403 content_window_->Init(params.layer_type);
404 wm::SetShadowType(content_window_, wm::SHADOW_TYPE_NONE);
406 content_window_container_ = new aura::Window(NULL);
407 content_window_container_->Init(aura::WINDOW_LAYER_NOT_DRAWN);
408 content_window_container_->Show();
409 content_window_container_->AddChild(content_window_);
411 desktop_window_tree_host_ = params.desktop_window_tree_host ?
412 params.desktop_window_tree_host :
413 DesktopWindowTreeHost::Create(native_widget_delegate_, this);
414 host_.reset(desktop_window_tree_host_->AsWindowTreeHost());
415 desktop_window_tree_host_->Init(content_window_, params);
417 // Mark this window as Desktop root window.
418 host_->window()->SetProperty(views::kDesktopRootWindow, true);
421 host_->window()->AddChild(content_window_container_);
422 host_->window()->SetProperty(kDesktopNativeWidgetAuraKey, this);
424 host_->window()->AddObserver(new RootWindowDestructionObserver(this));
426 // The WindowsModalityController event filter should be at the head of the
427 // pre target handlers list. This ensures that it handles input events first
428 // when modal windows are at the top of the Zorder.
429 if (widget_type_ == Widget::InitParams::TYPE_WINDOW)
430 window_modality_controller_.reset(
431 new wm::WindowModalityController(host_->window()));
433 // |root_window_event_filter_| must be created before
434 // OnWindowTreeHostCreated() is invoked.
436 // CEF sets focus to the window the user clicks down on.
437 // TODO(beng): see if we can't do this some other way. CEF seems a heavy-
438 // handed way of accomplishing focus.
439 // No event filter for aura::Env. Create CompoundEvnetFilter per
440 // WindowEventDispatcher.
441 root_window_event_filter_.reset(new wm::CompoundEventFilter);
442 host_->window()->AddPreTargetHandler(root_window_event_filter_.get());
444 // The host's dispatcher must be added to |native_cursor_manager_| before
445 // OnNativeWidgetCreated() is called.
446 cursor_reference_count_++;
447 if (!native_cursor_manager_) {
448 native_cursor_manager_ = new DesktopNativeCursorManager(
449 DesktopCursorLoaderUpdater::Create());
451 if (!cursor_manager_) {
452 cursor_manager_ = new wm::CursorManager(
453 scoped_ptr<wm::NativeCursorManager>(native_cursor_manager_));
455 native_cursor_manager_->AddHost(host());
456 aura::client::SetCursorClient(host_->window(), cursor_manager_);
458 desktop_window_tree_host_->OnNativeWidgetCreated(params);
460 UpdateWindowTransparency();
462 capture_client_.reset(new DesktopCaptureClient(host_->window()));
464 wm::FocusController* focus_controller =
465 new wm::FocusController(new DesktopFocusRules(content_window_));
466 focus_client_.reset(focus_controller);
467 aura::client::SetFocusClient(host_->window(), focus_controller);
468 aura::client::SetActivationClient(host_->window(), focus_controller);
469 host_->window()->AddPreTargetHandler(focus_controller);
471 dispatcher_client_.reset(new DesktopDispatcherClient);
472 aura::client::SetDispatcherClient(host_->window(),
473 dispatcher_client_.get());
475 position_client_.reset(new DesktopScreenPositionClient(host_->window()));
477 InstallInputMethodEventFilter();
479 drag_drop_client_ = desktop_window_tree_host_->CreateDragDropClient(
480 native_cursor_manager_);
481 aura::client::SetDragDropClient(host_->window(),
482 drag_drop_client_.get());
484 static_cast<aura::client::FocusClient*>(focus_client_.get())->
485 FocusWindow(content_window_);
487 OnHostResized(host());
489 host_->AddObserver(this);
491 window_tree_client_.reset(
492 new DesktopNativeWidgetAuraWindowTreeClient(host_->window()));
493 drop_helper_.reset(new DropHelper(GetWidget()->GetRootView()));
494 aura::client::SetDragDropDelegate(content_window_, this);
496 tooltip_manager_.reset(new TooltipManagerAura(GetWidget()));
498 tooltip_controller_.reset(
499 new corewm::TooltipController(
500 desktop_window_tree_host_->CreateTooltip()));
501 aura::client::SetTooltipClient(host_->window(),
502 tooltip_controller_.get());
503 host_->window()->AddPreTargetHandler(tooltip_controller_.get());
505 if (params.opacity == Widget::InitParams::TRANSLUCENT_WINDOW) {
506 visibility_controller_.reset(new wm::VisibilityController);
507 aura::client::SetVisibilityClient(host_->window(),
508 visibility_controller_.get());
509 wm::SetChildWindowVisibilityChangesAnimated(host_->window());
510 wm::SetChildWindowVisibilityChangesAnimated(
511 content_window_container_);
514 if (params.type == Widget::InitParams::TYPE_WINDOW) {
515 focus_manager_event_handler_.reset(new FocusManagerEventHandler(this));
516 host_->window()->AddPreTargetHandler(focus_manager_event_handler_.get());
519 event_client_.reset(new DesktopEventClient);
520 aura::client::SetEventClient(host_->window(), event_client_.get());
522 aura::client::GetFocusClient(content_window_)->FocusWindow(content_window_);
524 aura::client::SetActivationDelegate(content_window_, this);
526 shadow_controller_.reset(new wm::ShadowController(
527 aura::client::GetActivationClient(host_->window())));
529 content_window_->SetProperty(aura::client::kCanMaximizeKey,
530 GetWidget()->widget_delegate()->CanMaximize());
531 content_window_->SetProperty(aura::client::kCanResizeKey,
532 GetWidget()->widget_delegate()->CanResize());
534 window_reorderer_.reset(new WindowReorderer(content_window_,
535 GetWidget()->GetRootView()));
538 NonClientFrameView* DesktopNativeWidgetAura::CreateNonClientFrameView() {
539 return desktop_window_tree_host_->CreateNonClientFrameView();
542 bool DesktopNativeWidgetAura::ShouldUseNativeFrame() const {
543 return desktop_window_tree_host_->ShouldUseNativeFrame();
546 bool DesktopNativeWidgetAura::ShouldWindowContentsBeTransparent() const {
547 return desktop_window_tree_host_->ShouldWindowContentsBeTransparent();
550 void DesktopNativeWidgetAura::FrameTypeChanged() {
551 desktop_window_tree_host_->FrameTypeChanged();
552 UpdateWindowTransparency();
555 Widget* DesktopNativeWidgetAura::GetWidget() {
556 return native_widget_delegate_->AsWidget();
559 const Widget* DesktopNativeWidgetAura::GetWidget() const {
560 return native_widget_delegate_->AsWidget();
563 gfx::NativeView DesktopNativeWidgetAura::GetNativeView() const {
564 return content_window_;
567 gfx::NativeWindow DesktopNativeWidgetAura::GetNativeWindow() const {
568 return content_window_;
571 Widget* DesktopNativeWidgetAura::GetTopLevelWidget() {
575 const ui::Compositor* DesktopNativeWidgetAura::GetCompositor() const {
576 return content_window_ ? content_window_->layer()->GetCompositor() : NULL;
579 ui::Compositor* DesktopNativeWidgetAura::GetCompositor() {
580 return const_cast<ui::Compositor*>(
581 const_cast<const DesktopNativeWidgetAura*>(this)->GetCompositor());
584 ui::Layer* DesktopNativeWidgetAura::GetLayer() {
585 return content_window_ ? content_window_->layer() : NULL;
588 void DesktopNativeWidgetAura::ReorderNativeViews() {
589 window_reorderer_->ReorderChildWindows();
592 void DesktopNativeWidgetAura::ViewRemoved(View* view) {
593 DCHECK(drop_helper_.get() != NULL);
594 drop_helper_->ResetTargetViewIfEquals(view);
597 void DesktopNativeWidgetAura::SetNativeWindowProperty(const char* name,
600 content_window_->SetNativeWindowProperty(name, value);
603 void* DesktopNativeWidgetAura::GetNativeWindowProperty(const char* name) const {
604 return content_window_ ?
605 content_window_->GetNativeWindowProperty(name) : NULL;
608 TooltipManager* DesktopNativeWidgetAura::GetTooltipManager() const {
609 return tooltip_manager_.get();
612 void DesktopNativeWidgetAura::SetCapture() {
613 if (!content_window_)
616 content_window_->SetCapture();
619 void DesktopNativeWidgetAura::ReleaseCapture() {
620 if (!content_window_)
623 content_window_->ReleaseCapture();
626 bool DesktopNativeWidgetAura::HasCapture() const {
627 return content_window_ && content_window_->HasCapture() &&
628 desktop_window_tree_host_->HasCapture();
631 InputMethod* DesktopNativeWidgetAura::CreateInputMethod() {
632 if (switches::IsTextInputFocusManagerEnabled())
633 return new NullInputMethod();
635 ui::InputMethod* host = input_method_event_filter_->input_method();
636 return new InputMethodBridge(this, host, false);
639 internal::InputMethodDelegate*
640 DesktopNativeWidgetAura::GetInputMethodDelegate() {
644 ui::InputMethod* DesktopNativeWidgetAura::GetHostInputMethod() {
645 return input_method_event_filter_->input_method();
648 void DesktopNativeWidgetAura::CenterWindow(const gfx::Size& size) {
650 desktop_window_tree_host_->CenterWindow(size);
653 void DesktopNativeWidgetAura::GetWindowPlacement(
655 ui::WindowShowState* maximized) const {
657 desktop_window_tree_host_->GetWindowPlacement(bounds, maximized);
660 bool DesktopNativeWidgetAura::SetWindowTitle(const base::string16& title) {
661 if (!content_window_)
663 content_window_->set_title(title);
664 return desktop_window_tree_host_->SetWindowTitle(title);
667 void DesktopNativeWidgetAura::SetWindowIcons(const gfx::ImageSkia& window_icon,
668 const gfx::ImageSkia& app_icon) {
670 desktop_window_tree_host_->SetWindowIcons(window_icon, app_icon);
673 void DesktopNativeWidgetAura::InitModalType(ui::ModalType modal_type) {
674 // 99% of the time, we should not be asked to create a
675 // DesktopNativeWidgetAura that is modal. We only support window modal
676 // dialogs on the same lines as non AURA.
677 desktop_window_tree_host_->InitModalType(modal_type);
680 gfx::Rect DesktopNativeWidgetAura::GetWindowBoundsInScreen() const {
681 return content_window_ ?
682 desktop_window_tree_host_->GetWindowBoundsInScreen() : gfx::Rect();
685 gfx::Rect DesktopNativeWidgetAura::GetClientAreaBoundsInScreen() const {
686 return content_window_ ?
687 desktop_window_tree_host_->GetClientAreaBoundsInScreen() : gfx::Rect();
690 gfx::Rect DesktopNativeWidgetAura::GetRestoredBounds() const {
691 return content_window_ ?
692 desktop_window_tree_host_->GetRestoredBounds() : gfx::Rect();
695 void DesktopNativeWidgetAura::SetBounds(const gfx::Rect& bounds) {
696 if (!content_window_)
699 // This code by default scales the bounds rectangle by 1.
700 // We could probably get rid of this and similar logic from
701 // the DesktopNativeWidgetAura::OnWindowTreeHostResized function.
703 aura::Window* root = host_->window();
705 scale = gfx::Screen::GetScreenFor(root)->
706 GetDisplayNearestWindow(root).device_scale_factor();
708 gfx::Rect bounds_in_pixels(
709 gfx::ToCeiledPoint(gfx::ScalePoint(bounds.origin(), scale)),
710 gfx::ToFlooredSize(gfx::ScaleSize(bounds.size(), scale)));
711 desktop_window_tree_host_->AsWindowTreeHost()->SetBounds(bounds_in_pixels);
714 void DesktopNativeWidgetAura::SetSize(const gfx::Size& size) {
716 desktop_window_tree_host_->SetSize(size);
719 void DesktopNativeWidgetAura::StackAbove(gfx::NativeView native_view) {
722 void DesktopNativeWidgetAura::StackAtTop() {
724 desktop_window_tree_host_->StackAtTop();
727 void DesktopNativeWidgetAura::StackBelow(gfx::NativeView native_view) {
730 void DesktopNativeWidgetAura::SetShape(gfx::NativeRegion shape) {
732 desktop_window_tree_host_->SetShape(shape);
735 void DesktopNativeWidgetAura::Close() {
736 if (!content_window_)
739 content_window_->SuppressPaint();
740 content_window_->Hide();
742 desktop_window_tree_host_->Close();
745 void DesktopNativeWidgetAura::CloseNow() {
747 desktop_window_tree_host_->CloseNow();
750 void DesktopNativeWidgetAura::Show() {
751 if (!content_window_)
753 desktop_window_tree_host_->AsWindowTreeHost()->Show();
754 content_window_->Show();
757 void DesktopNativeWidgetAura::Hide() {
758 if (!content_window_)
760 desktop_window_tree_host_->AsWindowTreeHost()->Hide();
761 content_window_->Hide();
764 void DesktopNativeWidgetAura::ShowMaximizedWithBounds(
765 const gfx::Rect& restored_bounds) {
766 if (!content_window_)
768 desktop_window_tree_host_->ShowMaximizedWithBounds(restored_bounds);
769 content_window_->Show();
772 void DesktopNativeWidgetAura::ShowWithWindowState(ui::WindowShowState state) {
773 if (!content_window_)
775 desktop_window_tree_host_->ShowWindowWithState(state);
776 content_window_->Show();
779 bool DesktopNativeWidgetAura::IsVisible() const {
780 return content_window_ && desktop_window_tree_host_->IsVisible();
783 void DesktopNativeWidgetAura::Activate() {
785 desktop_window_tree_host_->Activate();
788 void DesktopNativeWidgetAura::Deactivate() {
790 desktop_window_tree_host_->Deactivate();
793 bool DesktopNativeWidgetAura::IsActive() const {
794 return content_window_ && desktop_window_tree_host_->IsActive();
797 void DesktopNativeWidgetAura::SetAlwaysOnTop(bool always_on_top) {
799 desktop_window_tree_host_->SetAlwaysOnTop(always_on_top);
802 bool DesktopNativeWidgetAura::IsAlwaysOnTop() const {
803 return content_window_ && desktop_window_tree_host_->IsAlwaysOnTop();
806 void DesktopNativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible) {
808 desktop_window_tree_host_->SetVisibleOnAllWorkspaces(always_visible);
811 void DesktopNativeWidgetAura::Maximize() {
813 desktop_window_tree_host_->Maximize();
816 void DesktopNativeWidgetAura::Minimize() {
818 desktop_window_tree_host_->Minimize();
821 bool DesktopNativeWidgetAura::IsMaximized() const {
822 return content_window_ && desktop_window_tree_host_->IsMaximized();
825 bool DesktopNativeWidgetAura::IsMinimized() const {
826 return content_window_ && desktop_window_tree_host_->IsMinimized();
829 void DesktopNativeWidgetAura::Restore() {
831 desktop_window_tree_host_->Restore();
834 void DesktopNativeWidgetAura::SetFullscreen(bool fullscreen) {
836 desktop_window_tree_host_->SetFullscreen(fullscreen);
839 bool DesktopNativeWidgetAura::IsFullscreen() const {
840 return content_window_ && desktop_window_tree_host_->IsFullscreen();
843 void DesktopNativeWidgetAura::SetOpacity(unsigned char opacity) {
845 desktop_window_tree_host_->SetOpacity(opacity);
848 void DesktopNativeWidgetAura::SetUseDragFrame(bool use_drag_frame) {
851 void DesktopNativeWidgetAura::FlashFrame(bool flash_frame) {
853 desktop_window_tree_host_->FlashFrame(flash_frame);
856 void DesktopNativeWidgetAura::RunShellDrag(
858 const ui::OSExchangeData& data,
859 const gfx::Point& location,
861 ui::DragDropTypes::DragEventSource source) {
862 views::RunShellDrag(content_window_, data, location, operation, source);
865 void DesktopNativeWidgetAura::SchedulePaintInRect(const gfx::Rect& rect) {
867 content_window_->SchedulePaintInRect(rect);
870 void DesktopNativeWidgetAura::SetCursor(gfx::NativeCursor cursor) {
872 aura::client::CursorClient* cursor_client =
873 aura::client::GetCursorClient(host_->window());
875 cursor_client->SetCursor(cursor);
878 bool DesktopNativeWidgetAura::IsMouseEventsEnabled() const {
879 if (!content_window_)
881 aura::client::CursorClient* cursor_client =
882 aura::client::GetCursorClient(host_->window());
883 return cursor_client ? cursor_client->IsMouseEventsEnabled() : true;
886 void DesktopNativeWidgetAura::ClearNativeFocus() {
887 desktop_window_tree_host_->ClearNativeFocus();
889 if (ShouldActivate()) {
890 aura::client::GetFocusClient(content_window_)->
891 ResetFocusWithinActiveWindow(content_window_);
895 gfx::Rect DesktopNativeWidgetAura::GetWorkAreaBoundsInScreen() const {
896 return desktop_window_tree_host_ ?
897 desktop_window_tree_host_->GetWorkAreaBoundsInScreen() : gfx::Rect();
900 Widget::MoveLoopResult DesktopNativeWidgetAura::RunMoveLoop(
901 const gfx::Vector2d& drag_offset,
902 Widget::MoveLoopSource source,
903 Widget::MoveLoopEscapeBehavior escape_behavior) {
904 if (!content_window_)
905 return Widget::MOVE_LOOP_CANCELED;
906 return desktop_window_tree_host_->RunMoveLoop(drag_offset, source,
910 void DesktopNativeWidgetAura::EndMoveLoop() {
912 desktop_window_tree_host_->EndMoveLoop();
915 void DesktopNativeWidgetAura::SetVisibilityChangedAnimationsEnabled(
918 desktop_window_tree_host_->SetVisibilityChangedAnimationsEnabled(value);
921 ui::NativeTheme* DesktopNativeWidgetAura::GetNativeTheme() const {
922 return DesktopWindowTreeHost::GetNativeTheme(content_window_);
925 void DesktopNativeWidgetAura::OnRootViewLayout() const {
927 desktop_window_tree_host_->OnRootViewLayout();
930 ////////////////////////////////////////////////////////////////////////////////
931 // DesktopNativeWidgetAura, aura::WindowDelegate implementation:
933 gfx::Size DesktopNativeWidgetAura::GetMinimumSize() const {
934 return native_widget_delegate_->GetMinimumSize();
937 gfx::Size DesktopNativeWidgetAura::GetMaximumSize() const {
938 return native_widget_delegate_->GetMaximumSize();
941 gfx::NativeCursor DesktopNativeWidgetAura::GetCursor(const gfx::Point& point) {
945 int DesktopNativeWidgetAura::GetNonClientComponent(
946 const gfx::Point& point) const {
947 return native_widget_delegate_->GetNonClientComponent(point);
950 bool DesktopNativeWidgetAura::ShouldDescendIntoChildForEventHandling(
952 const gfx::Point& location) {
953 views::WidgetDelegate* widget_delegate = GetWidget()->widget_delegate();
954 return !widget_delegate ||
955 widget_delegate->ShouldDescendIntoChildForEventHandling(child, location);
958 bool DesktopNativeWidgetAura::CanFocus() {
962 void DesktopNativeWidgetAura::OnCaptureLost() {
963 native_widget_delegate_->OnMouseCaptureLost();
966 void DesktopNativeWidgetAura::OnPaint(gfx::Canvas* canvas) {
967 native_widget_delegate_->OnNativeWidgetPaint(canvas);
970 void DesktopNativeWidgetAura::OnDeviceScaleFactorChanged(
971 float device_scale_factor) {
974 void DesktopNativeWidgetAura::OnWindowDestroying(aura::Window* window) {
975 // Cleanup happens in OnHostClosed().
978 void DesktopNativeWidgetAura::OnWindowDestroyed(aura::Window* window) {
979 // Cleanup happens in OnHostClosed(). We own |content_window_| (indirectly by
980 // way of |dispatcher_|) so there should be no need to do any processing
984 void DesktopNativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible) {
987 bool DesktopNativeWidgetAura::HasHitTestMask() const {
988 return native_widget_delegate_->HasHitTestMask();
991 void DesktopNativeWidgetAura::GetHitTestMask(gfx::Path* mask) const {
992 native_widget_delegate_->GetHitTestMask(mask);
995 ////////////////////////////////////////////////////////////////////////////////
996 // DesktopNativeWidgetAura, ui::EventHandler implementation:
998 void DesktopNativeWidgetAura::OnKeyEvent(ui::KeyEvent* event) {
999 if (event->is_char()) {
1000 // If a ui::InputMethod object is attached to the root window, character
1001 // events are handled inside the object and are not passed to this function.
1002 // If such object is not attached, character events might be sent (e.g. on
1003 // Windows). In this case, we just skip these.
1006 // Renderer may send a key event back to us if the key event wasn't handled,
1007 // and the window may be invisible by that time.
1008 if (!content_window_->IsVisible())
1011 native_widget_delegate_->OnKeyEvent(event);
1012 if (event->handled())
1015 if (GetWidget()->HasFocusManager() &&
1016 !GetWidget()->GetFocusManager()->OnKeyEvent(*event))
1017 event->SetHandled();
1020 void DesktopNativeWidgetAura::OnMouseEvent(ui::MouseEvent* event) {
1021 DCHECK(content_window_->IsVisible());
1022 if (tooltip_manager_.get())
1023 tooltip_manager_->UpdateTooltip();
1024 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
1025 native_widget_delegate_->OnMouseEvent(event);
1026 // WARNING: we may have been deleted.
1029 void DesktopNativeWidgetAura::OnScrollEvent(ui::ScrollEvent* event) {
1030 if (event->type() == ui::ET_SCROLL) {
1031 native_widget_delegate_->OnScrollEvent(event);
1032 if (event->handled())
1035 // Convert unprocessed scroll events into wheel events.
1036 ui::MouseWheelEvent mwe(*static_cast<ui::ScrollEvent*>(event));
1037 native_widget_delegate_->OnMouseEvent(&mwe);
1039 event->SetHandled();
1041 native_widget_delegate_->OnScrollEvent(event);
1045 void DesktopNativeWidgetAura::OnGestureEvent(ui::GestureEvent* event) {
1046 native_widget_delegate_->OnGestureEvent(event);
1049 ////////////////////////////////////////////////////////////////////////////////
1050 // DesktopNativeWidgetAura, aura::client::ActivationDelegate implementation:
1052 bool DesktopNativeWidgetAura::ShouldActivate() const {
1053 return can_activate_ && native_widget_delegate_->CanActivate();
1056 ////////////////////////////////////////////////////////////////////////////////
1057 // DesktopNativeWidgetAura, aura::client::ActivationChangeObserver
1060 void DesktopNativeWidgetAura::OnWindowActivated(aura::Window* gained_active,
1061 aura::Window* lost_active) {
1062 DCHECK(content_window_ == gained_active || content_window_ == lost_active);
1063 if (gained_active == content_window_ && restore_focus_on_activate_) {
1064 restore_focus_on_activate_ = false;
1065 GetWidget()->GetFocusManager()->RestoreFocusedView();
1066 } else if (lost_active == content_window_ && GetWidget()->HasFocusManager()) {
1067 DCHECK(!restore_focus_on_activate_);
1068 restore_focus_on_activate_ = true;
1069 // Pass in false so that ClearNativeFocus() isn't invoked.
1070 GetWidget()->GetFocusManager()->StoreFocusedView(false);
1074 ////////////////////////////////////////////////////////////////////////////////
1075 // DesktopNativeWidgetAura, aura::client::FocusChangeObserver implementation:
1077 void DesktopNativeWidgetAura::OnWindowFocused(aura::Window* gained_focus,
1078 aura::Window* lost_focus) {
1079 if (content_window_ == gained_focus) {
1080 desktop_window_tree_host_->OnNativeWidgetFocus();
1081 native_widget_delegate_->OnNativeFocus(lost_focus);
1083 // If focus is moving from a descendant Window to |content_window_| then
1084 // native activation hasn't changed. We still need to inform the InputMethod
1085 // we've been focused though.
1086 InputMethod* input_method = GetWidget()->GetInputMethod();
1088 input_method->OnFocus();
1089 } else if (content_window_ == lost_focus) {
1090 desktop_window_tree_host_->OnNativeWidgetBlur();
1091 native_widget_delegate_->OnNativeBlur(
1092 aura::client::GetFocusClient(content_window_)->GetFocusedWindow());
1096 ////////////////////////////////////////////////////////////////////////////////
1097 // DesktopNativeWidgetAura, views::internal::InputMethodDelegate:
1099 void DesktopNativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent& key) {
1100 FocusManager* focus_manager =
1101 native_widget_delegate_->AsWidget()->GetFocusManager();
1102 native_widget_delegate_->OnKeyEvent(const_cast<ui::KeyEvent*>(&key));
1103 if (key.handled() || !focus_manager)
1105 focus_manager->OnKeyEvent(key);
1108 ////////////////////////////////////////////////////////////////////////////////
1109 // DesktopNativeWidgetAura, aura::WindowDragDropDelegate implementation:
1111 void DesktopNativeWidgetAura::OnDragEntered(const ui::DropTargetEvent& event) {
1112 DCHECK(drop_helper_.get() != NULL);
1113 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
1114 event.location(), event.source_operations());
1117 int DesktopNativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent& event) {
1118 DCHECK(drop_helper_.get() != NULL);
1119 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
1120 event.location(), event.source_operations());
1121 return last_drop_operation_;
1124 void DesktopNativeWidgetAura::OnDragExited() {
1125 DCHECK(drop_helper_.get() != NULL);
1126 drop_helper_->OnDragExit();
1129 int DesktopNativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent& event) {
1130 DCHECK(drop_helper_.get() != NULL);
1132 return drop_helper_->OnDrop(event.data(), event.location(),
1133 last_drop_operation_);
1136 ////////////////////////////////////////////////////////////////////////////////
1137 // DesktopNativeWidgetAura, aura::WindowTreeHostObserver implementation:
1139 void DesktopNativeWidgetAura::OnHostCloseRequested(
1140 const aura::WindowTreeHost* host) {
1141 GetWidget()->Close();
1144 void DesktopNativeWidgetAura::OnHostResized(const aura::WindowTreeHost* host) {
1145 // Don't update the bounds of the child layers when animating closed. If we
1146 // did it would force a paint, which we don't want. We don't want the paint
1147 // as we can't assume any of the children are valid.
1148 if (desktop_window_tree_host_->IsAnimatingClosed())
1151 gfx::Rect new_bounds = gfx::Rect(host->window()->bounds().size());
1152 content_window_->SetBounds(new_bounds);
1153 // Can be NULL at start.
1154 if (content_window_container_)
1155 content_window_container_->SetBounds(new_bounds);
1156 native_widget_delegate_->OnNativeWidgetSizeChanged(new_bounds.size());
1159 void DesktopNativeWidgetAura::OnHostMoved(const aura::WindowTreeHost* host,
1160 const gfx::Point& new_origin) {
1161 TRACE_EVENT1("views", "DesktopNativeWidgetAura::OnHostMoved",
1162 "new_origin", new_origin.ToString());
1164 native_widget_delegate_->OnNativeWidgetMove();
1167 ////////////////////////////////////////////////////////////////////////////////
1168 // DesktopNativeWidgetAura, NativeWidget implementation:
1170 ui::EventHandler* DesktopNativeWidgetAura::GetEventHandler() {
1174 void DesktopNativeWidgetAura::InstallInputMethodEventFilter() {
1175 DCHECK(!input_method_event_filter_.get());
1177 input_method_event_filter_.reset(new wm::InputMethodEventFilter(
1178 host_->GetAcceleratedWidget()));
1179 input_method_event_filter_->SetInputMethodPropertyInRootWindow(
1181 root_window_event_filter_->AddHandler(input_method_event_filter_.get());
1184 void DesktopNativeWidgetAura::UpdateWindowTransparency() {
1185 content_window_->SetTransparent(
1186 desktop_window_tree_host_->ShouldWindowContentsBeTransparent());
1187 // Regardless of transparency or not, this root content window will always
1188 // fill its bounds completely, so set this flag to true to avoid an
1189 // unecessary clear before update.
1190 content_window_->SetFillsBoundsCompletely(true);
1193 void DesktopNativeWidgetAura::RootWindowDestroyed() {
1194 cursor_reference_count_--;
1195 if (cursor_reference_count_ == 0) {
1196 // We are the last DesktopNativeWidgetAura instance, and we are responsible
1197 // for cleaning up |cursor_manager_|.
1198 delete cursor_manager_;
1199 native_cursor_manager_ = NULL;
1200 cursor_manager_ = NULL;
1204 } // namespace views