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/trace_event/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/ime/input_method.h"
19 #include "ui/base/ui_base_switches_util.h"
20 #include "ui/compositor/layer.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/display.h"
23 #include "ui/gfx/geometry/point_conversions.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/gfx/geometry/size_conversions.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/native_theme/native_theme.h"
28 #include "ui/views/corewm/tooltip.h"
29 #include "ui/views/corewm/tooltip_controller.h"
30 #include "ui/views/drag_utils.h"
31 #include "ui/views/ime/input_method_bridge.h"
32 #include "ui/views/ime/null_input_method.h"
33 #include "ui/views/view_constants_aura.h"
34 #include "ui/views/widget/desktop_aura/desktop_capture_client.h"
35 #include "ui/views/widget/desktop_aura/desktop_cursor_loader_updater.h"
36 #include "ui/views/widget/desktop_aura/desktop_dispatcher_client.h"
37 #include "ui/views/widget/desktop_aura/desktop_event_client.h"
38 #include "ui/views/widget/desktop_aura/desktop_focus_rules.h"
39 #include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
40 #include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
41 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
42 #include "ui/views/widget/drop_helper.h"
43 #include "ui/views/widget/native_widget_aura.h"
44 #include "ui/views/widget/root_view.h"
45 #include "ui/views/widget/tooltip_manager_aura.h"
46 #include "ui/views/widget/widget.h"
47 #include "ui/views/widget/widget_aura_utils.h"
48 #include "ui/views/widget/widget_delegate.h"
49 #include "ui/views/widget/window_reorderer.h"
50 #include "ui/views/window/native_frame_view.h"
51 #include "ui/wm/core/compound_event_filter.h"
52 #include "ui/wm/core/cursor_manager.h"
53 #include "ui/wm/core/focus_controller.h"
54 #include "ui/wm/core/input_method_event_filter.h"
55 #include "ui/wm/core/native_cursor_manager.h"
56 #include "ui/wm/core/shadow_controller.h"
57 #include "ui/wm/core/shadow_types.h"
58 #include "ui/wm/core/visibility_controller.h"
59 #include "ui/wm/core/window_animations.h"
60 #include "ui/wm/core/window_modality_controller.h"
61 #include "ui/wm/public/activation_client.h"
62 #include "ui/wm/public/drag_drop_client.h"
65 #include "ui/base/win/shell.h"
66 #include "ui/gfx/win/dpi.h"
69 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(VIEWS_EXPORT
,
70 views::DesktopNativeWidgetAura
*);
74 DEFINE_WINDOW_PROPERTY_KEY(DesktopNativeWidgetAura
*,
75 kDesktopNativeWidgetAuraKey
, NULL
);
79 // This class provides functionality to create a top level widget to host a
81 class DesktopNativeWidgetTopLevelHandler
: public aura::WindowObserver
{
83 // This function creates a widget with the bounds passed in which eventually
84 // becomes the parent of the child window passed in.
85 static aura::Window
* CreateParentWindow(aura::Window
* child_window
,
86 const gfx::Rect
& bounds
,
88 bool root_is_always_on_top
) {
89 // This instance will get deleted when the widget is destroyed.
90 DesktopNativeWidgetTopLevelHandler
* top_level_handler
=
91 new DesktopNativeWidgetTopLevelHandler
;
93 child_window
->SetBounds(gfx::Rect(bounds
.size()));
95 Widget::InitParams init_params
;
96 init_params
.type
= full_screen
? Widget::InitParams::TYPE_WINDOW
:
97 Widget::InitParams::TYPE_POPUP
;
98 init_params
.bounds
= bounds
;
99 init_params
.ownership
= Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
;
100 init_params
.layer_type
= ui::LAYER_NOT_DRAWN
;
101 init_params
.activatable
= full_screen
?
102 Widget::InitParams::ACTIVATABLE_YES
:
103 Widget::InitParams::ACTIVATABLE_NO
;
104 init_params
.keep_on_top
= root_is_always_on_top
;
106 // This widget instance will get deleted when the window is
108 top_level_handler
->top_level_widget_
= new Widget();
109 top_level_handler
->top_level_widget_
->Init(init_params
);
111 top_level_handler
->top_level_widget_
->SetFullscreen(full_screen
);
112 top_level_handler
->top_level_widget_
->Show();
114 aura::Window
* native_window
=
115 top_level_handler
->top_level_widget_
->GetNativeView();
116 child_window
->AddObserver(top_level_handler
);
117 native_window
->AddObserver(top_level_handler
);
118 top_level_handler
->child_window_
= child_window
;
119 return native_window
;
122 // aura::WindowObserver overrides
123 void OnWindowDestroying(aura::Window
* window
) override
{
124 window
->RemoveObserver(this);
126 // If the widget is being destroyed by the OS then we should not try and
128 if (top_level_widget_
&&
129 window
== top_level_widget_
->GetNativeView()) {
130 top_level_widget_
= NULL
;
134 if (top_level_widget_
) {
135 DCHECK(top_level_widget_
->GetNativeView());
136 top_level_widget_
->GetNativeView()->RemoveObserver(this);
137 // When we receive a notification that the child of the window created
138 // above is being destroyed we go ahead and initiate the destruction of
139 // the corresponding widget.
140 top_level_widget_
->Close();
141 top_level_widget_
= NULL
;
146 void OnWindowBoundsChanged(aura::Window
* window
,
147 const gfx::Rect
& old_bounds
,
148 const gfx::Rect
& new_bounds
) override
{
149 // The position of the window may have changed. Hence we use SetBounds in
150 // place of SetSize. We need to pass the bounds in screen coordinates to
151 // the Widget::SetBounds function.
152 if (top_level_widget_
&& window
== child_window_
)
153 top_level_widget_
->SetBounds(window
->GetBoundsInScreen());
157 DesktopNativeWidgetTopLevelHandler()
158 : top_level_widget_(NULL
),
159 child_window_(NULL
) {}
161 ~DesktopNativeWidgetTopLevelHandler() override
{}
163 Widget
* top_level_widget_
;
164 aura::Window
* child_window_
;
166 DISALLOW_COPY_AND_ASSIGN(DesktopNativeWidgetTopLevelHandler
);
169 class DesktopNativeWidgetAuraWindowTreeClient
:
170 public aura::client::WindowTreeClient
{
172 explicit DesktopNativeWidgetAuraWindowTreeClient(
173 aura::Window
* root_window
)
174 : root_window_(root_window
) {
175 aura::client::SetWindowTreeClient(root_window_
, this);
177 ~DesktopNativeWidgetAuraWindowTreeClient() override
{
178 aura::client::SetWindowTreeClient(root_window_
, NULL
);
181 // Overridden from client::WindowTreeClient:
182 aura::Window
* GetDefaultParent(aura::Window
* context
,
183 aura::Window
* window
,
184 const gfx::Rect
& bounds
) override
{
185 bool is_fullscreen
= window
->GetProperty(aura::client::kShowStateKey
) ==
186 ui::SHOW_STATE_FULLSCREEN
;
187 bool is_menu
= window
->type() == ui::wm::WINDOW_TYPE_MENU
;
189 if (is_fullscreen
|| is_menu
) {
190 bool root_is_always_on_top
= false;
191 internal::NativeWidgetPrivate
* native_widget
=
192 DesktopNativeWidgetAura::ForWindow(root_window_
);
194 root_is_always_on_top
= native_widget
->IsAlwaysOnTop();
196 return DesktopNativeWidgetTopLevelHandler::CreateParentWindow(
197 window
, bounds
, is_fullscreen
, root_is_always_on_top
);
203 aura::Window
* root_window_
;
205 DISALLOW_COPY_AND_ASSIGN(DesktopNativeWidgetAuraWindowTreeClient
);
210 class FocusManagerEventHandler
: public ui::EventHandler
{
212 FocusManagerEventHandler(DesktopNativeWidgetAura
* desktop_native_widget_aura
)
213 : desktop_native_widget_aura_(desktop_native_widget_aura
) {}
215 // Implementation of ui::EventHandler:
216 void OnKeyEvent(ui::KeyEvent
* event
) override
{
217 Widget
* widget
= desktop_native_widget_aura_
->GetWidget();
218 if (widget
&& widget
->GetFocusManager()->GetFocusedView() &&
219 !widget
->GetFocusManager()->OnKeyEvent(*event
)) {
225 DesktopNativeWidgetAura
* desktop_native_widget_aura_
;
227 DISALLOW_COPY_AND_ASSIGN(FocusManagerEventHandler
);
230 class RootWindowDestructionObserver
: public aura::WindowObserver
{
232 explicit RootWindowDestructionObserver(DesktopNativeWidgetAura
* parent
)
234 ~RootWindowDestructionObserver() override
{}
237 // Overridden from aura::WindowObserver:
238 void OnWindowDestroyed(aura::Window
* window
) override
{
239 parent_
->RootWindowDestroyed();
240 window
->RemoveObserver(this);
244 DesktopNativeWidgetAura
* parent_
;
246 DISALLOW_COPY_AND_ASSIGN(RootWindowDestructionObserver
);
249 ////////////////////////////////////////////////////////////////////////////////
250 // DesktopNativeWidgetAura, public:
252 int DesktopNativeWidgetAura::cursor_reference_count_
= 0;
253 DesktopNativeCursorManager
* DesktopNativeWidgetAura::native_cursor_manager_
=
255 wm::CursorManager
* DesktopNativeWidgetAura::cursor_manager_
= NULL
;
257 DesktopNativeWidgetAura::DesktopNativeWidgetAura(
258 internal::NativeWidgetDelegate
* delegate
)
259 : desktop_window_tree_host_(NULL
),
260 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
261 content_window_container_(NULL
),
262 content_window_(new aura::Window(this)),
263 native_widget_delegate_(delegate
),
264 last_drop_operation_(ui::DragDropTypes::DRAG_NONE
),
265 restore_focus_on_activate_(false),
266 cursor_(gfx::kNullCursor
),
267 widget_type_(Widget::InitParams::TYPE_WINDOW
),
268 close_widget_factory_(this) {
269 aura::client::SetFocusChangeObserver(content_window_
, this);
270 aura::client::SetActivationChangeObserver(content_window_
, this);
273 DesktopNativeWidgetAura::~DesktopNativeWidgetAura() {
274 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
275 delete native_widget_delegate_
;
281 DesktopNativeWidgetAura
* DesktopNativeWidgetAura::ForWindow(
282 aura::Window
* window
) {
283 return window
->GetProperty(kDesktopNativeWidgetAuraKey
);
286 void DesktopNativeWidgetAura::OnHostClosed() {
287 // Don't invoke Widget::OnNativeWidgetDestroying(), its done by
288 // DesktopWindowTreeHost.
290 // The WindowModalityController is at the front of the event pretarget
291 // handler list. We destroy it first to preserve order symantics.
292 if (window_modality_controller_
)
293 window_modality_controller_
.reset();
295 // Make sure we don't have capture. Otherwise CaptureController and
296 // WindowEventDispatcher are left referencing a deleted Window.
298 aura::Window
* capture_window
= capture_client_
->GetCaptureWindow();
299 if (capture_window
&& host_
->window()->Contains(capture_window
))
300 capture_window
->ReleaseCapture();
303 // DesktopWindowTreeHost owns the ActivationController which ShadowController
304 // references. Make sure we destroy ShadowController early on.
305 shadow_controller_
.reset();
306 tooltip_manager_
.reset();
307 if (tooltip_controller_
.get()) {
308 host_
->window()->RemovePreTargetHandler(tooltip_controller_
.get());
309 aura::client::SetTooltipClient(host_
->window(), NULL
);
310 tooltip_controller_
.reset();
313 root_window_event_filter_
->RemoveHandler(input_method_event_filter_
.get());
315 window_tree_client_
.reset(); // Uses host_->dispatcher() at destruction.
317 capture_client_
.reset(); // Uses host_->dispatcher() at destruction.
319 // FocusController uses |content_window_|. Destroy it now so that we don't
320 // have to worry about the possibility of FocusController attempting to use
321 // |content_window_| after it's been destroyed but before all child windows
322 // have been destroyed.
323 host_
->window()->RemovePreTargetHandler(focus_client_
.get());
324 aura::client::SetFocusClient(host_
->window(), NULL
);
325 aura::client::SetActivationClient(host_
->window(), NULL
);
326 focus_client_
.reset();
328 host_
->RemoveObserver(this);
329 host_
.reset(); // Uses input_method_event_filter_ at destruction.
330 // WindowEventDispatcher owns |desktop_window_tree_host_|.
331 desktop_window_tree_host_
= NULL
;
332 content_window_
= NULL
;
334 native_widget_delegate_
->OnNativeWidgetDestroyed();
335 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
339 void DesktopNativeWidgetAura::OnDesktopWindowTreeHostDestroyed(
340 aura::WindowTreeHost
* host
) {
341 // |dispatcher_| is still valid, but DesktopWindowTreeHost is nearly
342 // destroyed. Do cleanup here of members DesktopWindowTreeHost may also use.
343 aura::client::SetDispatcherClient(host
->window(), NULL
);
344 dispatcher_client_
.reset();
346 // We explicitly do NOT clear the cursor client property. Since the cursor
347 // manager is a singleton, it can outlive any window hierarchy, and it's
348 // important that objects attached to this destroying window hierarchy have
349 // an opportunity to deregister their observers from the cursor manager.
350 // They may want to do this when they are notified that they're being
351 // removed from the window hierarchy, which happens soon after this
352 // function when DesktopWindowTreeHost* calls DestroyDispatcher().
353 native_cursor_manager_
->RemoveHost(host
);
355 aura::client::SetScreenPositionClient(host
->window(), NULL
);
356 position_client_
.reset();
358 aura::client::SetDragDropClient(host
->window(), NULL
);
359 drag_drop_client_
.reset();
361 aura::client::SetEventClient(host
->window(), NULL
);
362 event_client_
.reset();
365 void DesktopNativeWidgetAura::HandleActivationChanged(bool active
) {
366 native_widget_delegate_
->OnNativeWidgetActivationChanged(active
);
367 aura::client::ActivationClient
* activation_client
=
368 aura::client::GetActivationClient(host_
->window());
369 if (!activation_client
)
372 if (GetWidget()->HasFocusManager()) {
373 // This function can be called before the focus manager has had a
374 // chance to set the focused view. In which case we should get the
375 // last focused view.
376 View
* view_for_activation
=
377 GetWidget()->GetFocusManager()->GetFocusedView() ?
378 GetWidget()->GetFocusManager()->GetFocusedView() :
379 GetWidget()->GetFocusManager()->GetStoredFocusView();
380 if (!view_for_activation
)
381 view_for_activation
= GetWidget()->GetRootView();
382 activation_client
->ActivateWindow(
383 view_for_activation
->GetWidget()->GetNativeView());
384 // Refreshes the focus info to IMF in case that IMF cached the old info
385 // about focused text input client when it was "inactive".
386 GetHostInputMethod()->OnFocus();
389 // If we're not active we need to deactivate the corresponding
390 // aura::Window. This way if a child widget is active it gets correctly
391 // deactivated (child widgets don't get native desktop activation changes,
392 // only aura activation changes).
393 aura::Window
* active_window
= activation_client
->GetActiveWindow();
395 activation_client
->DeactivateWindow(active_window
);
396 GetHostInputMethod()->OnBlur();
401 ////////////////////////////////////////////////////////////////////////////////
402 // DesktopNativeWidgetAura, internal::NativeWidgetPrivate implementation:
404 void DesktopNativeWidgetAura::InitNativeWidget(
405 const Widget::InitParams
& params
) {
406 ownership_
= params
.ownership
;
407 widget_type_
= params
.type
;
409 NativeWidgetAura::RegisterNativeWidgetForWindow(this, content_window_
);
410 // Animations on TYPE_WINDOW are handled by the OS. Additionally if we animate
411 // these windows the size of the window gets augmented, effecting restore
412 // bounds and maximized windows in bad ways.
413 if (params
.type
== Widget::InitParams::TYPE_WINDOW
&&
414 !params
.remove_standard_frame
) {
415 content_window_
->SetProperty(aura::client::kAnimationsDisabledKey
, true);
417 content_window_
->SetType(GetAuraWindowTypeForWidgetType(params
.type
));
418 content_window_
->Init(params
.layer_type
);
419 wm::SetShadowType(content_window_
, wm::SHADOW_TYPE_NONE
);
421 content_window_container_
= new aura::Window(NULL
);
422 content_window_container_
->Init(ui::LAYER_NOT_DRAWN
);
423 content_window_container_
->Show();
424 content_window_container_
->AddChild(content_window_
);
426 desktop_window_tree_host_
= params
.desktop_window_tree_host
?
427 params
.desktop_window_tree_host
:
428 DesktopWindowTreeHost::Create(native_widget_delegate_
, this);
429 host_
.reset(desktop_window_tree_host_
->AsWindowTreeHost());
430 desktop_window_tree_host_
->Init(content_window_
, params
);
433 host_
->window()->AddChild(content_window_container_
);
434 host_
->window()->SetProperty(kDesktopNativeWidgetAuraKey
, this);
436 host_
->window()->AddObserver(new RootWindowDestructionObserver(this));
438 // The WindowsModalityController event filter should be at the head of the
439 // pre target handlers list. This ensures that it handles input events first
440 // when modal windows are at the top of the Zorder.
441 if (widget_type_
== Widget::InitParams::TYPE_WINDOW
)
442 window_modality_controller_
.reset(
443 new wm::WindowModalityController(host_
->window()));
445 // |root_window_event_filter_| must be created before
446 // OnWindowTreeHostCreated() is invoked.
448 // CEF sets focus to the window the user clicks down on.
449 // TODO(beng): see if we can't do this some other way. CEF seems a heavy-
450 // handed way of accomplishing focus.
451 // No event filter for aura::Env. Create CompoundEventFilter per
452 // WindowEventDispatcher.
453 root_window_event_filter_
.reset(new wm::CompoundEventFilter
);
454 host_
->window()->AddPreTargetHandler(root_window_event_filter_
.get());
456 // The host's dispatcher must be added to |native_cursor_manager_| before
457 // OnNativeWidgetCreated() is called.
458 cursor_reference_count_
++;
459 if (!native_cursor_manager_
) {
460 native_cursor_manager_
= new DesktopNativeCursorManager(
461 DesktopCursorLoaderUpdater::Create());
463 if (!cursor_manager_
) {
464 cursor_manager_
= new wm::CursorManager(
465 scoped_ptr
<wm::NativeCursorManager
>(native_cursor_manager_
));
467 native_cursor_manager_
->AddHost(host());
468 aura::client::SetCursorClient(host_
->window(), cursor_manager_
);
470 desktop_window_tree_host_
->OnNativeWidgetCreated(params
);
472 UpdateWindowTransparency();
474 capture_client_
.reset(new DesktopCaptureClient(host_
->window()));
476 wm::FocusController
* focus_controller
=
477 new wm::FocusController(new DesktopFocusRules(content_window_
));
478 focus_client_
.reset(focus_controller
);
479 aura::client::SetFocusClient(host_
->window(), focus_controller
);
480 aura::client::SetActivationClient(host_
->window(), focus_controller
);
481 host_
->window()->AddPreTargetHandler(focus_controller
);
483 dispatcher_client_
.reset(new DesktopDispatcherClient
);
484 aura::client::SetDispatcherClient(host_
->window(),
485 dispatcher_client_
.get());
487 position_client_
.reset(new DesktopScreenPositionClient(host_
->window()));
489 InstallInputMethodEventFilter();
491 drag_drop_client_
= desktop_window_tree_host_
->CreateDragDropClient(
492 native_cursor_manager_
);
493 aura::client::SetDragDropClient(host_
->window(),
494 drag_drop_client_
.get());
496 static_cast<aura::client::FocusClient
*>(focus_client_
.get())->
497 FocusWindow(content_window_
);
499 OnHostResized(host());
501 host_
->AddObserver(this);
503 window_tree_client_
.reset(
504 new DesktopNativeWidgetAuraWindowTreeClient(host_
->window()));
505 drop_helper_
.reset(new DropHelper(GetWidget()->GetRootView()));
506 aura::client::SetDragDropDelegate(content_window_
, this);
508 if (params
.type
!= Widget::InitParams::TYPE_TOOLTIP
) {
509 tooltip_manager_
.reset(new TooltipManagerAura(GetWidget()));
510 tooltip_controller_
.reset(
511 new corewm::TooltipController(
512 desktop_window_tree_host_
->CreateTooltip()));
513 aura::client::SetTooltipClient(host_
->window(),
514 tooltip_controller_
.get());
515 host_
->window()->AddPreTargetHandler(tooltip_controller_
.get());
518 if (params
.opacity
== Widget::InitParams::TRANSLUCENT_WINDOW
) {
519 visibility_controller_
.reset(new wm::VisibilityController
);
520 aura::client::SetVisibilityClient(host_
->window(),
521 visibility_controller_
.get());
522 wm::SetChildWindowVisibilityChangesAnimated(host_
->window());
523 wm::SetChildWindowVisibilityChangesAnimated(
524 content_window_container_
);
527 if (params
.type
== Widget::InitParams::TYPE_WINDOW
) {
528 focus_manager_event_handler_
.reset(new FocusManagerEventHandler(this));
529 host_
->window()->AddPreTargetHandler(focus_manager_event_handler_
.get());
532 event_client_
.reset(new DesktopEventClient
);
533 aura::client::SetEventClient(host_
->window(), event_client_
.get());
535 aura::client::GetFocusClient(content_window_
)->FocusWindow(content_window_
);
537 aura::client::SetActivationDelegate(content_window_
, this);
539 shadow_controller_
.reset(new wm::ShadowController(
540 aura::client::GetActivationClient(host_
->window())));
542 OnSizeConstraintsChanged();
544 window_reorderer_
.reset(new WindowReorderer(content_window_
,
545 GetWidget()->GetRootView()));
548 NonClientFrameView
* DesktopNativeWidgetAura::CreateNonClientFrameView() {
549 return ShouldUseNativeFrame() ? new NativeFrameView(GetWidget()) : NULL
;
552 bool DesktopNativeWidgetAura::ShouldUseNativeFrame() const {
553 return desktop_window_tree_host_
->ShouldUseNativeFrame();
556 bool DesktopNativeWidgetAura::ShouldWindowContentsBeTransparent() const {
557 return desktop_window_tree_host_
->ShouldWindowContentsBeTransparent();
560 void DesktopNativeWidgetAura::FrameTypeChanged() {
561 desktop_window_tree_host_
->FrameTypeChanged();
562 UpdateWindowTransparency();
565 Widget
* DesktopNativeWidgetAura::GetWidget() {
566 return native_widget_delegate_
->AsWidget();
569 const Widget
* DesktopNativeWidgetAura::GetWidget() const {
570 return native_widget_delegate_
->AsWidget();
573 gfx::NativeView
DesktopNativeWidgetAura::GetNativeView() const {
574 return content_window_
;
577 gfx::NativeWindow
DesktopNativeWidgetAura::GetNativeWindow() const {
578 return content_window_
;
581 Widget
* DesktopNativeWidgetAura::GetTopLevelWidget() {
585 const ui::Compositor
* DesktopNativeWidgetAura::GetCompositor() const {
586 return content_window_
? content_window_
->layer()->GetCompositor() : NULL
;
589 const ui::Layer
* DesktopNativeWidgetAura::GetLayer() const {
590 return content_window_
? content_window_
->layer() : NULL
;
593 void DesktopNativeWidgetAura::ReorderNativeViews() {
594 window_reorderer_
->ReorderChildWindows();
597 void DesktopNativeWidgetAura::ViewRemoved(View
* view
) {
598 DCHECK(drop_helper_
.get() != NULL
);
599 drop_helper_
->ResetTargetViewIfEquals(view
);
602 void DesktopNativeWidgetAura::SetNativeWindowProperty(const char* name
,
605 content_window_
->SetNativeWindowProperty(name
, value
);
608 void* DesktopNativeWidgetAura::GetNativeWindowProperty(const char* name
) const {
609 return content_window_
?
610 content_window_
->GetNativeWindowProperty(name
) : NULL
;
613 TooltipManager
* DesktopNativeWidgetAura::GetTooltipManager() const {
614 return tooltip_manager_
.get();
617 void DesktopNativeWidgetAura::SetCapture() {
618 if (!content_window_
)
621 content_window_
->SetCapture();
624 void DesktopNativeWidgetAura::ReleaseCapture() {
625 if (!content_window_
)
628 content_window_
->ReleaseCapture();
631 bool DesktopNativeWidgetAura::HasCapture() const {
632 return content_window_
&& content_window_
->HasCapture() &&
633 desktop_window_tree_host_
->HasCapture();
636 InputMethod
* DesktopNativeWidgetAura::CreateInputMethod() {
637 if (switches::IsTextInputFocusManagerEnabled())
638 return new NullInputMethod();
640 ui::InputMethod
* host
= input_method_event_filter_
->input_method();
641 return new InputMethodBridge(this, host
, false);
644 internal::InputMethodDelegate
*
645 DesktopNativeWidgetAura::GetInputMethodDelegate() {
649 ui::InputMethod
* DesktopNativeWidgetAura::GetHostInputMethod() {
650 return input_method_event_filter_
->input_method();
653 void DesktopNativeWidgetAura::CenterWindow(const gfx::Size
& size
) {
655 desktop_window_tree_host_
->CenterWindow(size
);
658 void DesktopNativeWidgetAura::GetWindowPlacement(
660 ui::WindowShowState
* maximized
) const {
662 desktop_window_tree_host_
->GetWindowPlacement(bounds
, maximized
);
665 bool DesktopNativeWidgetAura::SetWindowTitle(const base::string16
& title
) {
666 if (!content_window_
)
668 return desktop_window_tree_host_
->SetWindowTitle(title
);
671 void DesktopNativeWidgetAura::SetWindowIcons(const gfx::ImageSkia
& window_icon
,
672 const gfx::ImageSkia
& app_icon
) {
674 desktop_window_tree_host_
->SetWindowIcons(window_icon
, app_icon
);
677 void DesktopNativeWidgetAura::InitModalType(ui::ModalType modal_type
) {
678 // 99% of the time, we should not be asked to create a
679 // DesktopNativeWidgetAura that is modal. We only support window modal
680 // dialogs on the same lines as non AURA.
681 desktop_window_tree_host_
->InitModalType(modal_type
);
684 gfx::Rect
DesktopNativeWidgetAura::GetWindowBoundsInScreen() const {
685 return content_window_
?
686 desktop_window_tree_host_
->GetWindowBoundsInScreen() : gfx::Rect();
689 gfx::Rect
DesktopNativeWidgetAura::GetClientAreaBoundsInScreen() const {
690 return content_window_
?
691 desktop_window_tree_host_
->GetClientAreaBoundsInScreen() : gfx::Rect();
694 gfx::Rect
DesktopNativeWidgetAura::GetRestoredBounds() const {
695 return content_window_
?
696 desktop_window_tree_host_
->GetRestoredBounds() : gfx::Rect();
699 void DesktopNativeWidgetAura::SetBounds(const gfx::Rect
& bounds
) {
700 if (!content_window_
)
703 // This code by default scales the bounds rectangle by 1.
704 // We could probably get rid of this and similar logic from
705 // the DesktopNativeWidgetAura::OnWindowTreeHostResized function.
707 aura::Window
* root
= host_
->window();
709 scale
= gfx::Screen::GetScreenFor(root
)->
710 GetDisplayNearestWindow(root
).device_scale_factor();
712 gfx::Rect bounds_in_pixels
=
713 gfx::ScaleToEnclosingRect(bounds
, scale
, scale
);
714 desktop_window_tree_host_
->AsWindowTreeHost()->SetBounds(bounds_in_pixels
);
717 void DesktopNativeWidgetAura::SetSize(const gfx::Size
& size
) {
719 desktop_window_tree_host_
->SetSize(size
);
722 void DesktopNativeWidgetAura::StackAbove(gfx::NativeView native_view
) {
725 void DesktopNativeWidgetAura::StackAtTop() {
727 desktop_window_tree_host_
->StackAtTop();
730 void DesktopNativeWidgetAura::StackBelow(gfx::NativeView native_view
) {
733 void DesktopNativeWidgetAura::SetShape(gfx::NativeRegion shape
) {
735 desktop_window_tree_host_
->SetShape(shape
);
738 void DesktopNativeWidgetAura::Close() {
739 if (!content_window_
)
742 content_window_
->SuppressPaint();
743 content_window_
->Hide();
745 desktop_window_tree_host_
->Close();
748 void DesktopNativeWidgetAura::CloseNow() {
750 desktop_window_tree_host_
->CloseNow();
753 void DesktopNativeWidgetAura::Show() {
754 if (!content_window_
)
756 desktop_window_tree_host_
->AsWindowTreeHost()->Show();
757 content_window_
->Show();
760 void DesktopNativeWidgetAura::Hide() {
761 if (!content_window_
)
763 desktop_window_tree_host_
->AsWindowTreeHost()->Hide();
764 content_window_
->Hide();
767 void DesktopNativeWidgetAura::ShowMaximizedWithBounds(
768 const gfx::Rect
& restored_bounds
) {
769 if (!content_window_
)
771 desktop_window_tree_host_
->ShowMaximizedWithBounds(restored_bounds
);
772 content_window_
->Show();
775 void DesktopNativeWidgetAura::ShowWithWindowState(ui::WindowShowState state
) {
776 if (!content_window_
)
778 desktop_window_tree_host_
->ShowWindowWithState(state
);
779 content_window_
->Show();
782 bool DesktopNativeWidgetAura::IsVisible() const {
783 return content_window_
&& desktop_window_tree_host_
->IsVisible();
786 void DesktopNativeWidgetAura::Activate() {
788 desktop_window_tree_host_
->Activate();
791 void DesktopNativeWidgetAura::Deactivate() {
793 desktop_window_tree_host_
->Deactivate();
796 bool DesktopNativeWidgetAura::IsActive() const {
797 return content_window_
&& desktop_window_tree_host_
->IsActive();
800 void DesktopNativeWidgetAura::SetAlwaysOnTop(bool always_on_top
) {
802 desktop_window_tree_host_
->SetAlwaysOnTop(always_on_top
);
805 bool DesktopNativeWidgetAura::IsAlwaysOnTop() const {
806 return content_window_
&& desktop_window_tree_host_
->IsAlwaysOnTop();
809 void DesktopNativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible
) {
811 desktop_window_tree_host_
->SetVisibleOnAllWorkspaces(always_visible
);
814 void DesktopNativeWidgetAura::Maximize() {
816 desktop_window_tree_host_
->Maximize();
819 void DesktopNativeWidgetAura::Minimize() {
821 desktop_window_tree_host_
->Minimize();
824 bool DesktopNativeWidgetAura::IsMaximized() const {
825 return content_window_
&& desktop_window_tree_host_
->IsMaximized();
828 bool DesktopNativeWidgetAura::IsMinimized() const {
829 return content_window_
&& desktop_window_tree_host_
->IsMinimized();
832 void DesktopNativeWidgetAura::Restore() {
834 desktop_window_tree_host_
->Restore();
837 void DesktopNativeWidgetAura::SetFullscreen(bool fullscreen
) {
839 desktop_window_tree_host_
->SetFullscreen(fullscreen
);
842 bool DesktopNativeWidgetAura::IsFullscreen() const {
843 return content_window_
&& desktop_window_tree_host_
->IsFullscreen();
846 void DesktopNativeWidgetAura::SetOpacity(unsigned char opacity
) {
848 desktop_window_tree_host_
->SetOpacity(opacity
);
851 void DesktopNativeWidgetAura::SetUseDragFrame(bool use_drag_frame
) {
854 void DesktopNativeWidgetAura::FlashFrame(bool flash_frame
) {
856 desktop_window_tree_host_
->FlashFrame(flash_frame
);
859 void DesktopNativeWidgetAura::RunShellDrag(
861 const ui::OSExchangeData
& data
,
862 const gfx::Point
& location
,
864 ui::DragDropTypes::DragEventSource source
) {
865 views::RunShellDrag(content_window_
, data
, location
, operation
, source
);
868 void DesktopNativeWidgetAura::SchedulePaintInRect(const gfx::Rect
& rect
) {
870 content_window_
->SchedulePaintInRect(rect
);
873 void DesktopNativeWidgetAura::SetCursor(gfx::NativeCursor cursor
) {
875 aura::client::CursorClient
* cursor_client
=
876 aura::client::GetCursorClient(host_
->window());
878 cursor_client
->SetCursor(cursor
);
881 bool DesktopNativeWidgetAura::IsMouseEventsEnabled() const {
882 // We explicitly check |host_| here because it can be null during the process
883 // of widget shutdown (even if |content_window_| is not), and must be valid to
884 // determine if mouse events are enabled.
885 if (!content_window_
|| !host_
)
887 aura::client::CursorClient
* cursor_client
=
888 aura::client::GetCursorClient(host_
->window());
889 return cursor_client
? cursor_client
->IsMouseEventsEnabled() : true;
892 void DesktopNativeWidgetAura::ClearNativeFocus() {
893 desktop_window_tree_host_
->ClearNativeFocus();
895 if (ShouldActivate()) {
896 aura::client::GetFocusClient(content_window_
)->
897 ResetFocusWithinActiveWindow(content_window_
);
901 gfx::Rect
DesktopNativeWidgetAura::GetWorkAreaBoundsInScreen() const {
902 return desktop_window_tree_host_
?
903 desktop_window_tree_host_
->GetWorkAreaBoundsInScreen() : gfx::Rect();
906 Widget::MoveLoopResult
DesktopNativeWidgetAura::RunMoveLoop(
907 const gfx::Vector2d
& drag_offset
,
908 Widget::MoveLoopSource source
,
909 Widget::MoveLoopEscapeBehavior escape_behavior
) {
910 if (!content_window_
)
911 return Widget::MOVE_LOOP_CANCELED
;
912 return desktop_window_tree_host_
->RunMoveLoop(drag_offset
, source
,
916 void DesktopNativeWidgetAura::EndMoveLoop() {
918 desktop_window_tree_host_
->EndMoveLoop();
921 void DesktopNativeWidgetAura::SetVisibilityChangedAnimationsEnabled(
924 desktop_window_tree_host_
->SetVisibilityChangedAnimationsEnabled(value
);
927 void DesktopNativeWidgetAura::SetVisibilityAnimationDuration(
928 const base::TimeDelta
& duration
) {
929 wm::SetWindowVisibilityAnimationDuration(content_window_
, duration
);
932 void DesktopNativeWidgetAura::SetVisibilityAnimationTransition(
933 Widget::VisibilityTransition transition
) {
934 wm::WindowVisibilityAnimationTransition wm_transition
= wm::ANIMATE_NONE
;
935 switch (transition
) {
936 case Widget::ANIMATE_SHOW
:
937 wm_transition
= wm::ANIMATE_SHOW
;
939 case Widget::ANIMATE_HIDE
:
940 wm_transition
= wm::ANIMATE_HIDE
;
942 case Widget::ANIMATE_BOTH
:
943 wm_transition
= wm::ANIMATE_BOTH
;
945 case Widget::ANIMATE_NONE
:
946 wm_transition
= wm::ANIMATE_NONE
;
949 wm::SetWindowVisibilityAnimationTransition(content_window_
, wm_transition
);
952 ui::NativeTheme
* DesktopNativeWidgetAura::GetNativeTheme() const {
953 return DesktopWindowTreeHost::GetNativeTheme(content_window_
);
956 void DesktopNativeWidgetAura::OnRootViewLayout() {
958 desktop_window_tree_host_
->OnRootViewLayout();
961 bool DesktopNativeWidgetAura::IsTranslucentWindowOpacitySupported() const {
962 return content_window_
&&
963 desktop_window_tree_host_
->IsTranslucentWindowOpacitySupported();
966 void DesktopNativeWidgetAura::OnSizeConstraintsChanged() {
967 content_window_
->SetProperty(aura::client::kCanMaximizeKey
,
968 GetWidget()->widget_delegate()->CanMaximize());
969 content_window_
->SetProperty(aura::client::kCanMinimizeKey
,
970 GetWidget()->widget_delegate()->CanMinimize());
971 content_window_
->SetProperty(aura::client::kCanResizeKey
,
972 GetWidget()->widget_delegate()->CanResize());
973 desktop_window_tree_host_
->SizeConstraintsChanged();
976 void DesktopNativeWidgetAura::RepostNativeEvent(gfx::NativeEvent native_event
) {
977 OnEvent(native_event
);
980 ////////////////////////////////////////////////////////////////////////////////
981 // DesktopNativeWidgetAura, aura::WindowDelegate implementation:
983 gfx::Size
DesktopNativeWidgetAura::GetMinimumSize() const {
984 return native_widget_delegate_
->GetMinimumSize();
987 gfx::Size
DesktopNativeWidgetAura::GetMaximumSize() const {
988 return native_widget_delegate_
->GetMaximumSize();
991 ui::TextInputClient
* DesktopNativeWidgetAura::GetFocusedTextInputClient() {
992 return GetWidget()->GetFocusedTextInputClient();
995 gfx::NativeCursor
DesktopNativeWidgetAura::GetCursor(const gfx::Point
& point
) {
999 int DesktopNativeWidgetAura::GetNonClientComponent(
1000 const gfx::Point
& point
) const {
1001 return native_widget_delegate_
->GetNonClientComponent(point
);
1004 bool DesktopNativeWidgetAura::ShouldDescendIntoChildForEventHandling(
1005 aura::Window
* child
,
1006 const gfx::Point
& location
) {
1007 views::WidgetDelegate
* widget_delegate
= GetWidget()->widget_delegate();
1008 return !widget_delegate
||
1009 widget_delegate
->ShouldDescendIntoChildForEventHandling(child
, location
);
1012 bool DesktopNativeWidgetAura::CanFocus() {
1016 void DesktopNativeWidgetAura::OnCaptureLost() {
1017 native_widget_delegate_
->OnMouseCaptureLost();
1020 void DesktopNativeWidgetAura::OnPaint(const ui::PaintContext
& context
) {
1021 native_widget_delegate_
->OnNativeWidgetPaint(context
);
1024 void DesktopNativeWidgetAura::OnDeviceScaleFactorChanged(
1025 float device_scale_factor
) {
1028 void DesktopNativeWidgetAura::OnWindowDestroying(aura::Window
* window
) {
1029 // Cleanup happens in OnHostClosed().
1032 void DesktopNativeWidgetAura::OnWindowDestroyed(aura::Window
* window
) {
1033 // Cleanup happens in OnHostClosed(). We own |content_window_| (indirectly by
1034 // way of |dispatcher_|) so there should be no need to do any processing
1038 void DesktopNativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible
) {
1041 bool DesktopNativeWidgetAura::HasHitTestMask() const {
1042 return native_widget_delegate_
->HasHitTestMask();
1045 void DesktopNativeWidgetAura::GetHitTestMask(gfx::Path
* mask
) const {
1046 native_widget_delegate_
->GetHitTestMask(mask
);
1049 ////////////////////////////////////////////////////////////////////////////////
1050 // DesktopNativeWidgetAura, ui::EventHandler implementation:
1052 void DesktopNativeWidgetAura::OnKeyEvent(ui::KeyEvent
* event
) {
1053 if (event
->is_char()) {
1054 // If a ui::InputMethod object is attached to the root window, character
1055 // events are handled inside the object and are not passed to this function.
1056 // If such object is not attached, character events might be sent (e.g. on
1057 // Windows). In this case, we just skip these.
1060 // Renderer may send a key event back to us if the key event wasn't handled,
1061 // and the window may be invisible by that time.
1062 if (!content_window_
->IsVisible())
1065 native_widget_delegate_
->OnKeyEvent(event
);
1066 if (event
->handled())
1069 if (GetWidget()->HasFocusManager() &&
1070 !GetWidget()->GetFocusManager()->OnKeyEvent(*event
))
1071 event
->SetHandled();
1074 void DesktopNativeWidgetAura::OnMouseEvent(ui::MouseEvent
* event
) {
1075 DCHECK(content_window_
->IsVisible());
1076 if (tooltip_manager_
.get())
1077 tooltip_manager_
->UpdateTooltip();
1078 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
1079 native_widget_delegate_
->OnMouseEvent(event
);
1080 // WARNING: we may have been deleted.
1083 void DesktopNativeWidgetAura::OnScrollEvent(ui::ScrollEvent
* event
) {
1084 if (event
->type() == ui::ET_SCROLL
) {
1085 native_widget_delegate_
->OnScrollEvent(event
);
1086 if (event
->handled())
1089 // Convert unprocessed scroll events into wheel events.
1090 ui::MouseWheelEvent
mwe(*static_cast<ui::ScrollEvent
*>(event
));
1091 native_widget_delegate_
->OnMouseEvent(&mwe
);
1093 event
->SetHandled();
1095 native_widget_delegate_
->OnScrollEvent(event
);
1099 void DesktopNativeWidgetAura::OnGestureEvent(ui::GestureEvent
* event
) {
1100 native_widget_delegate_
->OnGestureEvent(event
);
1103 ////////////////////////////////////////////////////////////////////////////////
1104 // DesktopNativeWidgetAura, aura::client::ActivationDelegate implementation:
1106 bool DesktopNativeWidgetAura::ShouldActivate() const {
1107 return native_widget_delegate_
->CanActivate();
1110 ////////////////////////////////////////////////////////////////////////////////
1111 // DesktopNativeWidgetAura, aura::client::ActivationChangeObserver
1114 void DesktopNativeWidgetAura::OnWindowActivated(aura::Window
* gained_active
,
1115 aura::Window
* lost_active
) {
1116 DCHECK(content_window_
== gained_active
|| content_window_
== lost_active
);
1117 if (gained_active
== content_window_
&& restore_focus_on_activate_
) {
1118 restore_focus_on_activate_
= false;
1119 GetWidget()->GetFocusManager()->RestoreFocusedView();
1120 } else if (lost_active
== content_window_
&& GetWidget()->HasFocusManager()) {
1121 DCHECK(!restore_focus_on_activate_
);
1122 restore_focus_on_activate_
= true;
1123 // Pass in false so that ClearNativeFocus() isn't invoked.
1124 GetWidget()->GetFocusManager()->StoreFocusedView(false);
1128 ////////////////////////////////////////////////////////////////////////////////
1129 // DesktopNativeWidgetAura, aura::client::FocusChangeObserver implementation:
1131 void DesktopNativeWidgetAura::OnWindowFocused(aura::Window
* gained_focus
,
1132 aura::Window
* lost_focus
) {
1133 if (content_window_
== gained_focus
) {
1134 desktop_window_tree_host_
->OnNativeWidgetFocus();
1135 native_widget_delegate_
->OnNativeFocus();
1137 // If focus is moving from a descendant Window to |content_window_| then
1138 // native activation hasn't changed. Still, the InputMethod must be informed
1139 // of the Window focus change.
1140 InputMethod
* input_method
= GetWidget()->GetInputMethod();
1142 input_method
->OnFocus();
1143 } else if (content_window_
== lost_focus
) {
1144 desktop_window_tree_host_
->OnNativeWidgetBlur();
1145 native_widget_delegate_
->OnNativeBlur();
1149 ////////////////////////////////////////////////////////////////////////////////
1150 // DesktopNativeWidgetAura, views::internal::InputMethodDelegate:
1152 void DesktopNativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent
& key
) {
1153 FocusManager
* focus_manager
=
1154 native_widget_delegate_
->AsWidget()->GetFocusManager();
1155 native_widget_delegate_
->OnKeyEvent(const_cast<ui::KeyEvent
*>(&key
));
1156 if (key
.handled() || !focus_manager
)
1158 focus_manager
->OnKeyEvent(key
);
1161 ////////////////////////////////////////////////////////////////////////////////
1162 // DesktopNativeWidgetAura, aura::WindowDragDropDelegate implementation:
1164 void DesktopNativeWidgetAura::OnDragEntered(const ui::DropTargetEvent
& event
) {
1165 DCHECK(drop_helper_
.get() != NULL
);
1166 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
1167 event
.location(), event
.source_operations());
1170 int DesktopNativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent
& event
) {
1171 DCHECK(drop_helper_
.get() != NULL
);
1172 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
1173 event
.location(), event
.source_operations());
1174 return last_drop_operation_
;
1177 void DesktopNativeWidgetAura::OnDragExited() {
1178 DCHECK(drop_helper_
.get() != NULL
);
1179 drop_helper_
->OnDragExit();
1182 int DesktopNativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1183 DCHECK(drop_helper_
.get() != NULL
);
1184 if (ShouldActivate())
1186 return drop_helper_
->OnDrop(event
.data(), event
.location(),
1187 last_drop_operation_
);
1190 ////////////////////////////////////////////////////////////////////////////////
1191 // DesktopNativeWidgetAura, aura::WindowTreeHostObserver implementation:
1193 void DesktopNativeWidgetAura::OnHostCloseRequested(
1194 const aura::WindowTreeHost
* host
) {
1195 GetWidget()->Close();
1198 void DesktopNativeWidgetAura::OnHostResized(const aura::WindowTreeHost
* host
) {
1199 // Don't update the bounds of the child layers when animating closed. If we
1200 // did it would force a paint, which we don't want. We don't want the paint
1201 // as we can't assume any of the children are valid.
1202 if (desktop_window_tree_host_
->IsAnimatingClosed())
1205 gfx::Rect new_bounds
= gfx::Rect(host
->window()->bounds().size());
1206 content_window_
->SetBounds(new_bounds
);
1207 // Can be NULL at start.
1208 if (content_window_container_
)
1209 content_window_container_
->SetBounds(new_bounds
);
1210 native_widget_delegate_
->OnNativeWidgetSizeChanged(new_bounds
.size());
1213 void DesktopNativeWidgetAura::OnHostMoved(const aura::WindowTreeHost
* host
,
1214 const gfx::Point
& new_origin
) {
1215 TRACE_EVENT1("views", "DesktopNativeWidgetAura::OnHostMoved",
1216 "new_origin", new_origin
.ToString());
1218 native_widget_delegate_
->OnNativeWidgetMove();
1221 ////////////////////////////////////////////////////////////////////////////////
1222 // DesktopNativeWidgetAura, private:
1224 void DesktopNativeWidgetAura::InstallInputMethodEventFilter() {
1225 DCHECK(!input_method_event_filter_
.get());
1227 input_method_event_filter_
.reset(new wm::InputMethodEventFilter(
1228 host_
->GetAcceleratedWidget()));
1229 input_method_event_filter_
->SetInputMethodPropertyInRootWindow(
1231 root_window_event_filter_
->AddHandler(input_method_event_filter_
.get());
1234 void DesktopNativeWidgetAura::UpdateWindowTransparency() {
1235 content_window_
->SetTransparent(
1236 desktop_window_tree_host_
->ShouldWindowContentsBeTransparent());
1237 // Regardless of transparency or not, this root content window will always
1238 // fill its bounds completely, so set this flag to true to avoid an
1239 // unecessary clear before update.
1240 content_window_
->SetFillsBoundsCompletely(true);
1243 void DesktopNativeWidgetAura::RootWindowDestroyed() {
1244 cursor_reference_count_
--;
1245 if (cursor_reference_count_
== 0) {
1246 // We are the last DesktopNativeWidgetAura instance, and we are responsible
1247 // for cleaning up |cursor_manager_|.
1248 delete cursor_manager_
;
1249 native_cursor_manager_
= NULL
;
1250 cursor_manager_
= NULL
;
1254 } // namespace views