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/native_widget_aura.h"
8 #include "base/strings/string_util.h"
9 #include "third_party/skia/include/core/SkRegion.h"
10 #include "ui/aura/client/aura_constants.h"
11 #include "ui/aura/client/cursor_client.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/client/screen_position_client.h"
14 #include "ui/aura/client/window_tree_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/aura/window_observer.h"
19 #include "ui/base/dragdrop/os_exchange_data.h"
20 #include "ui/base/ui_base_switches_util.h"
21 #include "ui/base/ui_base_types.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/font_list.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/native_theme/native_theme_aura.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/views_delegate.h"
32 #include "ui/views/widget/drop_helper.h"
33 #include "ui/views/widget/native_widget_delegate.h"
34 #include "ui/views/widget/root_view.h"
35 #include "ui/views/widget/tooltip_manager_aura.h"
36 #include "ui/views/widget/widget_aura_utils.h"
37 #include "ui/views/widget/widget_delegate.h"
38 #include "ui/views/widget/window_reorderer.h"
39 #include "ui/wm/core/shadow_types.h"
40 #include "ui/wm/core/window_animations.h"
41 #include "ui/wm/core/window_util.h"
42 #include "ui/wm/public/activation_client.h"
43 #include "ui/wm/public/drag_drop_client.h"
44 #include "ui/wm/public/window_move_client.h"
45 #include "ui/wm/public/window_types.h"
48 #include "base/win/scoped_gdi_object.h"
49 #include "base/win/win_util.h"
50 #include "ui/base/l10n/l10n_util_win.h"
51 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
54 #if defined(USE_X11) && !defined(OS_CHROMEOS)
55 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
58 #if !defined(OS_CHROMEOS)
59 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
60 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
67 void SetRestoreBounds(aura::Window
* window
, const gfx::Rect
& bounds
) {
68 window
->SetProperty(aura::client::kRestoreBoundsKey
, new gfx::Rect(bounds
));
73 ////////////////////////////////////////////////////////////////////////////////
74 // NativeWidgetAura, public:
76 NativeWidgetAura::NativeWidgetAura(internal::NativeWidgetDelegate
* delegate
)
77 : delegate_(delegate
),
78 window_(new aura::Window(this)),
79 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
81 cursor_(gfx::kNullCursor
),
82 saved_window_state_(ui::SHOW_STATE_DEFAULT
),
83 close_widget_factory_(this) {
84 aura::client::SetFocusChangeObserver(window_
, this);
85 aura::client::SetActivationChangeObserver(window_
, this);
89 void NativeWidgetAura::RegisterNativeWidgetForWindow(
90 internal::NativeWidgetPrivate
* native_widget
,
91 aura::Window
* window
) {
92 window
->set_user_data(native_widget
);
95 ////////////////////////////////////////////////////////////////////////////////
96 // NativeWidgetAura, internal::NativeWidgetPrivate implementation:
98 void NativeWidgetAura::InitNativeWidget(const Widget::InitParams
& params
) {
99 // Aura needs to know which desktop (Ash or regular) will manage this widget.
100 // See Widget::InitParams::context for details.
101 DCHECK(params
.parent
|| params
.context
);
103 ownership_
= params
.ownership
;
105 RegisterNativeWidgetForWindow(this, window_
);
106 window_
->SetType(GetAuraWindowTypeForWidgetType(params
.type
));
107 window_
->SetProperty(aura::client::kShowStateKey
, params
.show_state
);
108 if (params
.type
== Widget::InitParams::TYPE_BUBBLE
)
109 aura::client::SetHideOnDeactivate(window_
, true);
110 window_
->SetTransparent(
111 params
.opacity
== Widget::InitParams::TRANSLUCENT_WINDOW
);
112 window_
->Init(params
.layer_type
);
113 if (params
.shadow_type
== Widget::InitParams::SHADOW_TYPE_NONE
)
114 SetShadowType(window_
, wm::SHADOW_TYPE_NONE
);
115 else if (params
.shadow_type
== Widget::InitParams::SHADOW_TYPE_DROP
)
116 SetShadowType(window_
, wm::SHADOW_TYPE_RECTANGULAR
);
117 if (params
.type
== Widget::InitParams::TYPE_CONTROL
)
120 delegate_
->OnNativeWidgetCreated(false);
122 gfx::Rect window_bounds
= params
.bounds
;
123 gfx::NativeView parent
= params
.parent
;
124 gfx::NativeView context
= params
.context
;
126 // Set up the transient child before the window is added. This way the
127 // LayoutManager knows the window has a transient parent.
128 if (parent
&& parent
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
) {
129 wm::AddTransientChild(parent
, window_
);
134 // SetAlwaysOnTop before SetParent so that always-on-top container is used.
135 SetAlwaysOnTop(params
.keep_on_top
);
136 // Make sure we have a real |window_bounds|.
137 if (parent
&& window_bounds
== gfx::Rect()) {
138 // If a parent is specified but no bounds are given,
139 // use the origin of the parent's display so that the widget
140 // will be added to the same display as the parent.
141 gfx::Rect bounds
= gfx::Screen::GetScreenFor(parent
)->
142 GetDisplayNearestWindow(parent
).bounds();
143 window_bounds
.set_origin(bounds
.origin());
147 // Set properties before adding to the parent so that its layout manager sees
148 // the correct values.
149 OnSizeConstraintsChanged();
152 parent
->AddChild(window_
);
154 aura::client::ParentWindowWithContext(
155 window_
, context
->GetRootWindow(), window_bounds
);
158 // Start observing property changes.
159 window_
->AddObserver(this);
161 // Wait to set the bounds until we have a parent. That way we can know our
162 // true state/bounds (the LayoutManager may enforce a particular
165 SetRestoreBounds(window_
, window_bounds
);
167 SetBounds(window_bounds
);
168 window_
->set_ignore_events(!params
.accept_events
);
169 DCHECK(GetWidget()->GetRootView());
170 if (params
.type
!= Widget::InitParams::TYPE_TOOLTIP
)
171 tooltip_manager_
.reset(new views::TooltipManagerAura(GetWidget()));
173 drop_helper_
.reset(new DropHelper(GetWidget()->GetRootView()));
174 if (params
.type
!= Widget::InitParams::TYPE_TOOLTIP
&&
175 params
.type
!= Widget::InitParams::TYPE_POPUP
) {
176 aura::client::SetDragDropDelegate(window_
, this);
179 aura::client::SetActivationDelegate(window_
, this);
181 window_reorderer_
.reset(new WindowReorderer(window_
,
182 GetWidget()->GetRootView()));
185 NonClientFrameView
* NativeWidgetAura::CreateNonClientFrameView() {
189 bool NativeWidgetAura::ShouldUseNativeFrame() const {
190 // There is only one frame type for aura.
194 bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const {
198 void NativeWidgetAura::FrameTypeChanged() {
199 // This is called when the Theme has changed; forward the event to the root
201 GetWidget()->ThemeChanged();
202 GetWidget()->GetRootView()->SchedulePaint();
205 Widget
* NativeWidgetAura::GetWidget() {
206 return delegate_
->AsWidget();
209 const Widget
* NativeWidgetAura::GetWidget() const {
210 return delegate_
->AsWidget();
213 gfx::NativeView
NativeWidgetAura::GetNativeView() const {
217 gfx::NativeWindow
NativeWidgetAura::GetNativeWindow() const {
221 Widget
* NativeWidgetAura::GetTopLevelWidget() {
222 NativeWidgetPrivate
* native_widget
= GetTopLevelNativeWidget(GetNativeView());
223 return native_widget
? native_widget
->GetWidget() : NULL
;
226 const ui::Compositor
* NativeWidgetAura::GetCompositor() const {
227 return window_
? window_
->layer()->GetCompositor() : NULL
;
230 const ui::Layer
* NativeWidgetAura::GetLayer() const {
231 return window_
? window_
->layer() : NULL
;
234 void NativeWidgetAura::ReorderNativeViews() {
235 window_reorderer_
->ReorderChildWindows();
238 void NativeWidgetAura::ViewRemoved(View
* view
) {
239 DCHECK(drop_helper_
.get() != NULL
);
240 drop_helper_
->ResetTargetViewIfEquals(view
);
243 void NativeWidgetAura::SetNativeWindowProperty(const char* name
, void* value
) {
245 window_
->SetNativeWindowProperty(name
, value
);
248 void* NativeWidgetAura::GetNativeWindowProperty(const char* name
) const {
249 return window_
? window_
->GetNativeWindowProperty(name
) : NULL
;
252 TooltipManager
* NativeWidgetAura::GetTooltipManager() const {
253 return tooltip_manager_
.get();
256 void NativeWidgetAura::SetCapture() {
258 window_
->SetCapture();
261 void NativeWidgetAura::ReleaseCapture() {
263 window_
->ReleaseCapture();
266 bool NativeWidgetAura::HasCapture() const {
267 return window_
&& window_
->HasCapture();
270 InputMethod
* NativeWidgetAura::CreateInputMethod() {
274 if (switches::IsTextInputFocusManagerEnabled())
275 return new NullInputMethod();
277 aura::Window
* root_window
= window_
->GetRootWindow();
278 ui::InputMethod
* host
=
279 root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
280 return new InputMethodBridge(this, host
, true);
283 internal::InputMethodDelegate
* NativeWidgetAura::GetInputMethodDelegate() {
287 ui::InputMethod
* NativeWidgetAura::GetHostInputMethod() {
288 aura::Window
* root_window
= window_
->GetRootWindow();
289 return root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
292 void NativeWidgetAura::CenterWindow(const gfx::Size
& size
) {
296 gfx::Rect
parent_bounds(window_
->parent()->GetBoundsInRootWindow());
297 // When centering window, we take the intersection of the host and
298 // the parent. We assume the root window represents the visible
299 // rect of a single screen.
300 gfx::Rect work_area
= gfx::Screen::GetScreenFor(window_
)->
301 GetDisplayNearestWindow(window_
).work_area();
303 aura::client::ScreenPositionClient
* screen_position_client
=
304 aura::client::GetScreenPositionClient(window_
->GetRootWindow());
305 if (screen_position_client
) {
306 gfx::Point origin
= work_area
.origin();
307 screen_position_client
->ConvertPointFromScreen(window_
->GetRootWindow(),
309 work_area
.set_origin(origin
);
312 parent_bounds
.Intersect(work_area
);
314 // If |window_|'s transient parent's bounds are big enough to fit it, then we
315 // center it with respect to the transient parent.
316 if (wm::GetTransientParent(window_
)) {
317 gfx::Rect transient_parent_rect
=
318 wm::GetTransientParent(window_
)->GetBoundsInRootWindow();
319 transient_parent_rect
.Intersect(work_area
);
320 if (transient_parent_rect
.height() >= size
.height() &&
321 transient_parent_rect
.width() >= size
.width())
322 parent_bounds
= transient_parent_rect
;
325 gfx::Rect
window_bounds(
326 parent_bounds
.x() + (parent_bounds
.width() - size
.width()) / 2,
327 parent_bounds
.y() + (parent_bounds
.height() - size
.height()) / 2,
330 // Don't size the window bigger than the parent, otherwise the user may not be
331 // able to close or move it.
332 window_bounds
.AdjustToFit(parent_bounds
);
334 // Convert the bounds back relative to the parent.
335 gfx::Point origin
= window_bounds
.origin();
336 aura::Window::ConvertPointToTarget(window_
->GetRootWindow(),
337 window_
->parent(), &origin
);
338 window_bounds
.set_origin(origin
);
339 window_
->SetBounds(window_bounds
);
342 void NativeWidgetAura::GetWindowPlacement(
344 ui::WindowShowState
* show_state
) const {
345 // The interface specifies returning restored bounds, not current bounds.
346 *bounds
= GetRestoredBounds();
347 *show_state
= window_
? window_
->GetProperty(aura::client::kShowStateKey
) :
348 ui::SHOW_STATE_DEFAULT
;
351 bool NativeWidgetAura::SetWindowTitle(const base::string16
& title
) {
354 if (window_
->title() == title
)
356 window_
->SetTitle(title
);
360 void NativeWidgetAura::SetWindowIcons(const gfx::ImageSkia
& window_icon
,
361 const gfx::ImageSkia
& app_icon
) {
362 // Aura doesn't have window icons.
365 void NativeWidgetAura::InitModalType(ui::ModalType modal_type
) {
366 if (modal_type
!= ui::MODAL_TYPE_NONE
)
367 window_
->SetProperty(aura::client::kModalKey
, modal_type
);
370 gfx::Rect
NativeWidgetAura::GetWindowBoundsInScreen() const {
371 return window_
? window_
->GetBoundsInScreen() : gfx::Rect();
374 gfx::Rect
NativeWidgetAura::GetClientAreaBoundsInScreen() const {
375 // View-to-screen coordinate system transformations depend on this returning
376 // the full window bounds, for example View::ConvertPointToScreen().
377 return window_
? window_
->GetBoundsInScreen() : gfx::Rect();
380 gfx::Rect
NativeWidgetAura::GetRestoredBounds() const {
384 // Restored bounds should only be relevant if the window is minimized or
385 // maximized. However, in some places the code expects GetRestoredBounds()
386 // to return the current window bounds if the window is not in either state.
387 if (IsMinimized() || IsMaximized() || IsFullscreen()) {
388 // Restore bounds are in screen coordinates, no need to convert.
389 gfx::Rect
* restore_bounds
=
390 window_
->GetProperty(aura::client::kRestoreBoundsKey
);
392 return *restore_bounds
;
394 return window_
->GetBoundsInScreen();
397 void NativeWidgetAura::SetBounds(const gfx::Rect
& bounds
) {
401 aura::Window
* root
= window_
->GetRootWindow();
403 aura::client::ScreenPositionClient
* screen_position_client
=
404 aura::client::GetScreenPositionClient(root
);
405 if (screen_position_client
) {
406 gfx::Display dst_display
=
407 gfx::Screen::GetScreenFor(window_
)->GetDisplayMatching(bounds
);
408 screen_position_client
->SetBounds(window_
, bounds
, dst_display
);
412 window_
->SetBounds(bounds
);
415 void NativeWidgetAura::SetSize(const gfx::Size
& size
) {
417 window_
->SetBounds(gfx::Rect(window_
->bounds().origin(), size
));
420 void NativeWidgetAura::StackAbove(gfx::NativeView native_view
) {
421 if (window_
&& window_
->parent() &&
422 window_
->parent() == native_view
->parent())
423 window_
->parent()->StackChildAbove(window_
, native_view
);
426 void NativeWidgetAura::StackAtTop() {
428 window_
->parent()->StackChildAtTop(window_
);
431 void NativeWidgetAura::StackBelow(gfx::NativeView native_view
) {
432 if (window_
&& window_
->parent() &&
433 window_
->parent() == native_view
->parent())
434 window_
->parent()->StackChildBelow(window_
, native_view
);
437 void NativeWidgetAura::SetShape(gfx::NativeRegion region
) {
439 window_
->layer()->SetAlphaShape(make_scoped_ptr(region
));
444 void NativeWidgetAura::Close() {
445 // |window_| may already be deleted by parent window. This can happen
446 // when this widget is child widget or has transient parent
447 // and ownership is WIDGET_OWNS_NATIVE_WIDGET.
449 ownership_
== Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
);
451 window_
->SuppressPaint();
453 window_
->SetProperty(aura::client::kModalKey
, ui::MODAL_TYPE_NONE
);
456 if (!close_widget_factory_
.HasWeakPtrs()) {
457 base::MessageLoop::current()->PostTask(
459 base::Bind(&NativeWidgetAura::CloseNow
,
460 close_widget_factory_
.GetWeakPtr()));
464 void NativeWidgetAura::CloseNow() {
468 void NativeWidgetAura::Show() {
469 ShowWithWindowState(ui::SHOW_STATE_NORMAL
);
472 void NativeWidgetAura::Hide() {
477 void NativeWidgetAura::ShowMaximizedWithBounds(
478 const gfx::Rect
& restored_bounds
) {
479 SetRestoreBounds(window_
, restored_bounds
);
480 ShowWithWindowState(ui::SHOW_STATE_MAXIMIZED
);
483 void NativeWidgetAura::ShowWithWindowState(ui::WindowShowState state
) {
487 if (state
== ui::SHOW_STATE_MAXIMIZED
|| state
== ui::SHOW_STATE_FULLSCREEN
)
488 window_
->SetProperty(aura::client::kShowStateKey
, state
);
490 if (delegate_
->CanActivate()) {
491 if (state
!= ui::SHOW_STATE_INACTIVE
)
493 // SetInitialFocus() should be always be called, even for
494 // SHOW_STATE_INACTIVE. If the window has to stay inactive, the method will
495 // do the right thing.
496 SetInitialFocus(state
);
500 bool NativeWidgetAura::IsVisible() const {
501 return window_
&& window_
->IsVisible();
504 void NativeWidgetAura::Activate() {
508 // We don't necessarily have a root window yet. This can happen with
509 // constrained windows.
510 if (window_
->GetRootWindow()) {
511 aura::client::GetActivationClient(window_
->GetRootWindow())->ActivateWindow(
514 if (window_
->GetProperty(aura::client::kDrawAttentionKey
))
515 window_
->SetProperty(aura::client::kDrawAttentionKey
, false);
518 void NativeWidgetAura::Deactivate() {
521 aura::client::GetActivationClient(window_
->GetRootWindow())->DeactivateWindow(
525 bool NativeWidgetAura::IsActive() const {
526 return window_
&& wm::IsActiveWindow(window_
);
529 void NativeWidgetAura::SetAlwaysOnTop(bool on_top
) {
531 window_
->SetProperty(aura::client::kAlwaysOnTopKey
, on_top
);
534 bool NativeWidgetAura::IsAlwaysOnTop() const {
535 return window_
&& window_
->GetProperty(aura::client::kAlwaysOnTopKey
);
538 void NativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible
) {
539 // Not implemented on chromeos or for child widgets.
542 void NativeWidgetAura::Maximize() {
544 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
547 void NativeWidgetAura::Minimize() {
549 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
552 bool NativeWidgetAura::IsMaximized() const {
553 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
554 ui::SHOW_STATE_MAXIMIZED
;
557 bool NativeWidgetAura::IsMinimized() const {
558 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
559 ui::SHOW_STATE_MINIMIZED
;
562 void NativeWidgetAura::Restore() {
564 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
567 void NativeWidgetAura::SetFullscreen(bool fullscreen
) {
568 if (!window_
|| IsFullscreen() == fullscreen
)
569 return; // Nothing to do.
571 // Save window state before entering full screen so that it could restored
572 // when exiting full screen.
574 saved_window_state_
= window_
->GetProperty(aura::client::kShowStateKey
);
576 window_
->SetProperty(
577 aura::client::kShowStateKey
,
578 fullscreen
? ui::SHOW_STATE_FULLSCREEN
: saved_window_state_
);
581 bool NativeWidgetAura::IsFullscreen() const {
582 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
583 ui::SHOW_STATE_FULLSCREEN
;
586 void NativeWidgetAura::SetOpacity(unsigned char opacity
) {
588 window_
->layer()->SetOpacity(opacity
/ 255.0);
591 void NativeWidgetAura::SetUseDragFrame(bool use_drag_frame
) {
595 void NativeWidgetAura::FlashFrame(bool flash
) {
597 window_
->SetProperty(aura::client::kDrawAttentionKey
, flash
);
600 void NativeWidgetAura::RunShellDrag(View
* view
,
601 const ui::OSExchangeData
& data
,
602 const gfx::Point
& location
,
604 ui::DragDropTypes::DragEventSource source
) {
606 views::RunShellDrag(window_
, data
, location
, operation
, source
);
609 void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect
& rect
) {
611 window_
->SchedulePaintInRect(rect
);
614 void NativeWidgetAura::SetCursor(gfx::NativeCursor cursor
) {
616 aura::client::CursorClient
* cursor_client
=
617 aura::client::GetCursorClient(window_
->GetRootWindow());
619 cursor_client
->SetCursor(cursor
);
622 bool NativeWidgetAura::IsMouseEventsEnabled() const {
625 aura::client::CursorClient
* cursor_client
=
626 aura::client::GetCursorClient(window_
->GetRootWindow());
627 return cursor_client
? cursor_client
->IsMouseEventsEnabled() : true;
630 void NativeWidgetAura::ClearNativeFocus() {
631 aura::client::FocusClient
* client
= aura::client::GetFocusClient(window_
);
632 if (window_
&& client
&& window_
->Contains(client
->GetFocusedWindow()))
633 client
->ResetFocusWithinActiveWindow(window_
);
636 gfx::Rect
NativeWidgetAura::GetWorkAreaBoundsInScreen() const {
639 return gfx::Screen::GetScreenFor(window_
)->
640 GetDisplayNearestWindow(window_
).work_area();
643 Widget::MoveLoopResult
NativeWidgetAura::RunMoveLoop(
644 const gfx::Vector2d
& drag_offset
,
645 Widget::MoveLoopSource source
,
646 Widget::MoveLoopEscapeBehavior escape_behavior
) {
647 // |escape_behavior| is only needed on windows when running the native message
649 if (!window_
|| !window_
->GetRootWindow())
650 return Widget::MOVE_LOOP_CANCELED
;
651 aura::client::WindowMoveClient
* move_client
=
652 aura::client::GetWindowMoveClient(window_
->GetRootWindow());
654 return Widget::MOVE_LOOP_CANCELED
;
657 aura::client::WindowMoveSource window_move_source
=
658 source
== Widget::MOVE_LOOP_SOURCE_MOUSE
?
659 aura::client::WINDOW_MOVE_SOURCE_MOUSE
:
660 aura::client::WINDOW_MOVE_SOURCE_TOUCH
;
661 if (move_client
->RunMoveLoop(window_
, drag_offset
, window_move_source
) ==
662 aura::client::MOVE_SUCCESSFUL
) {
663 return Widget::MOVE_LOOP_SUCCESSFUL
;
665 return Widget::MOVE_LOOP_CANCELED
;
668 void NativeWidgetAura::EndMoveLoop() {
669 if (!window_
|| !window_
->GetRootWindow())
671 aura::client::WindowMoveClient
* move_client
=
672 aura::client::GetWindowMoveClient(window_
->GetRootWindow());
674 move_client
->EndMoveLoop();
677 void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value
) {
679 window_
->SetProperty(aura::client::kAnimationsDisabledKey
, !value
);
682 void NativeWidgetAura::SetVisibilityAnimationDuration(
683 const base::TimeDelta
& duration
) {
684 wm::SetWindowVisibilityAnimationDuration(window_
, duration
);
687 void NativeWidgetAura::SetVisibilityAnimationTransition(
688 Widget::VisibilityTransition transition
) {
689 wm::WindowVisibilityAnimationTransition wm_transition
= wm::ANIMATE_NONE
;
690 switch (transition
) {
691 case Widget::ANIMATE_SHOW
:
692 wm_transition
= wm::ANIMATE_SHOW
;
694 case Widget::ANIMATE_HIDE
:
695 wm_transition
= wm::ANIMATE_HIDE
;
697 case Widget::ANIMATE_BOTH
:
698 wm_transition
= wm::ANIMATE_BOTH
;
700 case Widget::ANIMATE_NONE
:
701 wm_transition
= wm::ANIMATE_NONE
;
704 wm::SetWindowVisibilityAnimationTransition(window_
, wm_transition
);
707 ui::NativeTheme
* NativeWidgetAura::GetNativeTheme() const {
708 #if !defined(OS_CHROMEOS)
709 return DesktopWindowTreeHost::GetNativeTheme(window_
);
711 return ui::NativeThemeAura::instance();
715 void NativeWidgetAura::OnRootViewLayout() {
718 bool NativeWidgetAura::IsTranslucentWindowOpacitySupported() const {
722 void NativeWidgetAura::OnSizeConstraintsChanged() {
723 window_
->SetProperty(aura::client::kCanMaximizeKey
,
724 GetWidget()->widget_delegate()->CanMaximize());
725 window_
->SetProperty(aura::client::kCanMinimizeKey
,
726 GetWidget()->widget_delegate()->CanMinimize());
727 window_
->SetProperty(aura::client::kCanResizeKey
,
728 GetWidget()->widget_delegate()->CanResize());
731 void NativeWidgetAura::RepostNativeEvent(gfx::NativeEvent native_event
) {
732 OnEvent(native_event
);
735 ////////////////////////////////////////////////////////////////////////////////
736 // NativeWidgetAura, views::InputMethodDelegate implementation:
738 void NativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent
& key
) {
739 FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
740 delegate_
->OnKeyEvent(const_cast<ui::KeyEvent
*>(&key
));
741 if (key
.handled() || !focus_manager
)
743 focus_manager
->OnKeyEvent(key
);
746 ////////////////////////////////////////////////////////////////////////////////
747 // NativeWidgetAura, aura::WindowDelegate implementation:
749 gfx::Size
NativeWidgetAura::GetMinimumSize() const {
750 return delegate_
->GetMinimumSize();
753 gfx::Size
NativeWidgetAura::GetMaximumSize() const {
754 // If a window have a maximum size, the window should not be
756 DCHECK(delegate_
->GetMaximumSize().IsEmpty() ||
757 !window_
->GetProperty(aura::client::kCanMaximizeKey
));
758 return delegate_
->GetMaximumSize();
761 void NativeWidgetAura::OnBoundsChanged(const gfx::Rect
& old_bounds
,
762 const gfx::Rect
& new_bounds
) {
763 // Assume that if the old bounds was completely empty a move happened. This
764 // handles the case of a maximize animation acquiring the layer (acquiring a
765 // layer results in clearing the bounds).
766 if (old_bounds
.origin() != new_bounds
.origin() ||
767 (old_bounds
== gfx::Rect(0, 0, 0, 0) && !new_bounds
.IsEmpty())) {
768 delegate_
->OnNativeWidgetMove();
770 if (old_bounds
.size() != new_bounds
.size())
771 delegate_
->OnNativeWidgetSizeChanged(new_bounds
.size());
774 ui::TextInputClient
* NativeWidgetAura::GetFocusedTextInputClient() {
775 return GetWidget()->GetFocusedTextInputClient();
778 gfx::NativeCursor
NativeWidgetAura::GetCursor(const gfx::Point
& point
) {
782 int NativeWidgetAura::GetNonClientComponent(const gfx::Point
& point
) const {
783 return delegate_
->GetNonClientComponent(point
);
786 bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling(
788 const gfx::Point
& location
) {
789 views::WidgetDelegate
* widget_delegate
= GetWidget()->widget_delegate();
790 if (widget_delegate
&&
791 !widget_delegate
->ShouldDescendIntoChildForEventHandling(child
, location
))
794 // Don't descend into |child| if there is a view with a Layer that contains
795 // the point and is stacked above |child|s layer.
796 typedef std::vector
<ui::Layer
*> Layers
;
797 const Layers
& root_layers(delegate_
->GetRootLayers());
798 if (root_layers
.empty())
801 Layers::const_iterator
child_layer_iter(
802 std::find(window_
->layer()->children().begin(),
803 window_
->layer()->children().end(), child
->layer()));
804 if (child_layer_iter
== window_
->layer()->children().end())
807 for (std::vector
<ui::Layer
*>::const_reverse_iterator i
= root_layers
.rbegin();
808 i
!= root_layers
.rend(); ++i
) {
809 ui::Layer
* layer
= *i
;
810 if (layer
->visible() && layer
->bounds().Contains(location
)) {
811 Layers::const_iterator
root_layer_iter(
812 std::find(window_
->layer()->children().begin(),
813 window_
->layer()->children().end(), layer
));
814 if (root_layer_iter
> child_layer_iter
)
821 bool NativeWidgetAura::CanFocus() {
822 return ShouldActivate();
825 void NativeWidgetAura::OnCaptureLost() {
826 delegate_
->OnMouseCaptureLost();
829 void NativeWidgetAura::OnPaint(gfx::Canvas
* canvas
) {
830 delegate_
->OnNativeWidgetPaint(canvas
);
833 void NativeWidgetAura::OnDeviceScaleFactorChanged(float device_scale_factor
) {
834 GetWidget()->DeviceScaleFactorChanged(device_scale_factor
);
837 void NativeWidgetAura::OnWindowDestroying(aura::Window
* window
) {
838 window_
->RemoveObserver(this);
839 delegate_
->OnNativeWidgetDestroying();
841 // If the aura::Window is destroyed, we can no longer show tooltips.
842 tooltip_manager_
.reset();
845 void NativeWidgetAura::OnWindowDestroyed(aura::Window
* window
) {
847 delegate_
->OnNativeWidgetDestroyed();
848 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
852 void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible
) {
853 delegate_
->OnNativeWidgetVisibilityChanged(visible
);
856 bool NativeWidgetAura::HasHitTestMask() const {
857 return delegate_
->HasHitTestMask();
860 void NativeWidgetAura::GetHitTestMask(gfx::Path
* mask
) const {
862 delegate_
->GetHitTestMask(mask
);
865 ////////////////////////////////////////////////////////////////////////////////
866 // NativeWidgetAura, aura::WindowObserver implementation:
868 void NativeWidgetAura::OnWindowPropertyChanged(aura::Window
* window
,
871 if (key
== aura::client::kShowStateKey
)
872 delegate_
->OnNativeWidgetWindowShowStateChanged();
875 ////////////////////////////////////////////////////////////////////////////////
876 // NativeWidgetAura, ui::EventHandler implementation:
878 void NativeWidgetAura::OnKeyEvent(ui::KeyEvent
* event
) {
880 if (event
->is_char()) {
881 // If a ui::InputMethod object is attached to the root window, character
882 // events are handled inside the object and are not passed to this function.
883 // If such object is not attached, character events might be sent (e.g. on
884 // Windows). In this case, we just skip these.
887 // Renderer may send a key event back to us if the key event wasn't handled,
888 // and the window may be invisible by that time.
889 if (!window_
->IsVisible())
891 InputMethod
* input_method
= GetWidget()->GetInputMethod();
894 input_method
->DispatchKeyEvent(*event
);
895 if (switches::IsTextInputFocusManagerEnabled()) {
896 FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
897 delegate_
->OnKeyEvent(event
);
898 if (!event
->handled() && focus_manager
)
899 focus_manager
->OnKeyEvent(*event
);
904 void NativeWidgetAura::OnMouseEvent(ui::MouseEvent
* event
) {
906 DCHECK(window_
->IsVisible());
907 if (event
->type() == ui::ET_MOUSEWHEEL
) {
908 delegate_
->OnMouseEvent(event
);
909 if (event
->handled())
913 if (tooltip_manager_
.get())
914 tooltip_manager_
->UpdateTooltip();
915 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
916 delegate_
->OnMouseEvent(event
);
919 void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent
* event
) {
920 delegate_
->OnScrollEvent(event
);
923 void NativeWidgetAura::OnGestureEvent(ui::GestureEvent
* event
) {
925 DCHECK(window_
->IsVisible() || event
->IsEndingEvent());
926 delegate_
->OnGestureEvent(event
);
929 ////////////////////////////////////////////////////////////////////////////////
930 // NativeWidgetAura, aura::client::ActivationDelegate implementation:
932 bool NativeWidgetAura::ShouldActivate() const {
933 return delegate_
->CanActivate();
936 ////////////////////////////////////////////////////////////////////////////////
937 // NativeWidgetAura, aura::client::ActivationChangeObserver implementation:
939 void NativeWidgetAura::OnWindowActivated(aura::Window
* gained_active
,
940 aura::Window
* lost_active
) {
941 DCHECK(window_
== gained_active
|| window_
== lost_active
);
942 if (GetWidget()->GetFocusManager()) {
943 if (window_
== gained_active
)
944 GetWidget()->GetFocusManager()->RestoreFocusedView();
945 else if (window_
== lost_active
)
946 GetWidget()->GetFocusManager()->StoreFocusedView(true);
948 delegate_
->OnNativeWidgetActivationChanged(window_
== gained_active
);
951 ////////////////////////////////////////////////////////////////////////////////
952 // NativeWidgetAura, aura::client::FocusChangeObserver:
954 void NativeWidgetAura::OnWindowFocused(aura::Window
* gained_focus
,
955 aura::Window
* lost_focus
) {
956 if (window_
== gained_focus
) {
957 // In aura, it is possible for child native widgets to take input and focus,
958 // this differs from the behavior on windows.
959 if (GetWidget()->GetInputMethod()) // Null in tests.
960 GetWidget()->GetInputMethod()->OnFocus();
961 delegate_
->OnNativeFocus(lost_focus
);
962 } else if (window_
== lost_focus
) {
963 // GetInputMethod() recreates the input method if it's previously been
964 // destroyed. If we get called during destruction, the input method will be
965 // gone, and creating a new one and telling it that we lost the focus will
966 // trigger a DCHECK (the new input method doesn't think that we have the
967 // focus and doesn't expect a blur). OnBlur() shouldn't be called during
968 // destruction unless WIDGET_OWNS_NATIVE_WIDGET is set (which is just the
971 if (GetWidget()->GetInputMethod())
972 GetWidget()->GetInputMethod()->OnBlur();
974 DCHECK_EQ(ownership_
, Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
);
977 delegate_
->OnNativeBlur(gained_focus
);
981 ////////////////////////////////////////////////////////////////////////////////
982 // NativeWidgetAura, aura::WindowDragDropDelegate implementation:
984 void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent
& event
) {
985 DCHECK(drop_helper_
.get() != NULL
);
986 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
987 event
.location(), event
.source_operations());
990 int NativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent
& event
) {
991 DCHECK(drop_helper_
.get() != NULL
);
992 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
993 event
.location(), event
.source_operations());
994 return last_drop_operation_
;
997 void NativeWidgetAura::OnDragExited() {
998 DCHECK(drop_helper_
.get() != NULL
);
999 drop_helper_
->OnDragExit();
1002 int NativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent
& event
) {
1003 DCHECK(drop_helper_
.get() != NULL
);
1004 return drop_helper_
->OnDrop(event
.data(), event
.location(),
1005 last_drop_operation_
);
1008 ////////////////////////////////////////////////////////////////////////////////
1009 // NativeWidgetAura, protected:
1011 NativeWidgetAura::~NativeWidgetAura() {
1013 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
1019 ////////////////////////////////////////////////////////////////////////////////
1020 // NativeWidgetAura, private:
1022 void NativeWidgetAura::SetInitialFocus(ui::WindowShowState show_state
) {
1023 // The window does not get keyboard messages unless we focus it.
1024 if (!GetWidget()->SetInitialFocus(show_state
))
1028 ////////////////////////////////////////////////////////////////////////////////
1032 #if defined(OS_WIN) || (defined(USE_X11) && !defined(OS_CHROMEOS))
1033 void CloseWindow(aura::Window
* window
) {
1035 Widget
* widget
= Widget::GetWidgetForNativeView(window
);
1036 if (widget
&& widget
->is_secondary_widget())
1037 // To avoid the delay in shutdown caused by using Close which may wait
1038 // for animations, use CloseNow. Because this is only used on secondary
1039 // widgets it seems relatively safe to skip the extra processing of
1047 BOOL CALLBACK
WindowCallbackProc(HWND hwnd
, LPARAM lParam
) {
1048 aura::Window
* root_window
=
1049 DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd
);
1050 CloseWindow(root_window
);
1057 void Widget::CloseAllSecondaryWidgets() {
1059 EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc
, 0);
1062 #if defined(USE_X11) && !defined(OS_CHROMEOS)
1063 std::vector
<aura::Window
*> open_windows
=
1064 DesktopWindowTreeHostX11::GetAllOpenWindows();
1065 std::for_each(open_windows
.begin(), open_windows
.end(), CloseWindow
);
1066 DesktopWindowTreeHostX11::CleanUpWindowList();
1070 bool Widget::ConvertRect(const Widget
* source
,
1071 const Widget
* target
,
1076 namespace internal
{
1078 ////////////////////////////////////////////////////////////////////////////////
1079 // internal::NativeWidgetPrivate, public:
1082 NativeWidgetPrivate
* NativeWidgetPrivate::CreateNativeWidget(
1083 internal::NativeWidgetDelegate
* delegate
) {
1084 return new NativeWidgetAura(delegate
);
1088 NativeWidgetPrivate
* NativeWidgetPrivate::GetNativeWidgetForNativeView(
1089 gfx::NativeView native_view
) {
1090 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1091 return reinterpret_cast<NativeWidgetPrivate
*>(native_view
->user_data());
1095 NativeWidgetPrivate
* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
1096 gfx::NativeWindow native_window
) {
1097 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1098 return reinterpret_cast<NativeWidgetPrivate
*>(native_window
->user_data());
1102 NativeWidgetPrivate
* NativeWidgetPrivate::GetTopLevelNativeWidget(
1103 gfx::NativeView native_view
) {
1104 aura::Window
* window
= native_view
;
1105 NativeWidgetPrivate
* top_level_native_widget
= NULL
;
1107 NativeWidgetPrivate
* native_widget
= GetNativeWidgetForNativeView(window
);
1109 top_level_native_widget
= native_widget
;
1110 window
= window
->parent();
1112 return top_level_native_widget
;
1116 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view
,
1117 Widget::Widgets
* children
) {
1119 // Code expects widget for |native_view| to be added to |children|.
1120 NativeWidgetPrivate
* native_widget
= static_cast<NativeWidgetPrivate
*>(
1121 GetNativeWidgetForNativeView(native_view
));
1122 if (native_widget
&& native_widget
->GetWidget())
1123 children
->insert(native_widget
->GetWidget());
1126 const aura::Window::Windows
& child_windows
= native_view
->children();
1127 for (aura::Window::Windows::const_iterator i
= child_windows
.begin();
1128 i
!= child_windows
.end(); ++i
) {
1129 GetAllChildWidgets((*i
), children
);
1134 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view
,
1135 Widget::Widgets
* owned
) {
1136 // Add all owned widgets.
1137 for (aura::Window
* transient_child
: wm::GetTransientChildren(native_view
)) {
1138 NativeWidgetPrivate
* native_widget
= static_cast<NativeWidgetPrivate
*>(
1139 GetNativeWidgetForNativeView(transient_child
));
1140 if (native_widget
&& native_widget
->GetWidget())
1141 owned
->insert(native_widget
->GetWidget());
1142 GetAllOwnedWidgets(transient_child
, owned
);
1145 // Add all child windows.
1146 for (aura::Window
* child
: native_view
->children())
1147 GetAllChildWidgets(child
, owned
);
1151 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view
,
1152 gfx::NativeView new_parent
) {
1153 DCHECK(native_view
!= new_parent
);
1155 gfx::NativeView previous_parent
= native_view
->parent();
1156 if (previous_parent
== new_parent
)
1159 Widget::Widgets widgets
;
1160 GetAllChildWidgets(native_view
, &widgets
);
1162 // First notify all the widgets that they are being disassociated
1163 // from their previous parent.
1164 for (Widget::Widgets::iterator it
= widgets
.begin();
1165 it
!= widgets
.end(); ++it
) {
1166 (*it
)->NotifyNativeViewHierarchyWillChange();
1170 new_parent
->AddChild(native_view
);
1172 // The following looks weird, but it's the equivalent of what aura has
1173 // always done. (The previous behaviour of aura::Window::SetParent() used
1174 // NULL as a special value that meant ask the WindowTreeClient where things
1177 // This probably isn't strictly correct, but its an invariant that a Window
1178 // in use will be attached to a RootWindow, so we can't just call
1179 // RemoveChild here. The only possible thing that could assign a RootWindow
1180 // in this case is the stacking client of the current RootWindow. This
1181 // matches our previous behaviour; the global stacking client would almost
1182 // always reattach the window to the same RootWindow.
1183 aura::Window
* root_window
= native_view
->GetRootWindow();
1184 aura::client::ParentWindowWithContext(
1185 native_view
, root_window
, root_window
->GetBoundsInScreen());
1188 // And now, notify them that they have a brand new parent.
1189 for (Widget::Widgets::iterator it
= widgets
.begin();
1190 it
!= widgets
.end(); ++it
) {
1191 (*it
)->NotifyNativeViewHierarchyChanged();
1196 bool NativeWidgetPrivate::IsMouseButtonDown() {
1197 return aura::Env::GetInstance()->IsMouseButtonDown();
1201 gfx::FontList
NativeWidgetPrivate::GetWindowTitleFontList() {
1203 NONCLIENTMETRICS_XP ncm
;
1204 base::win::GetNonClientMetrics(&ncm
);
1205 l10n_util::AdjustUIFont(&(ncm
.lfCaptionFont
));
1206 base::win::ScopedHFONT
caption_font(CreateFontIndirect(&(ncm
.lfCaptionFont
)));
1207 return gfx::FontList(gfx::Font(caption_font
));
1209 return gfx::FontList();
1213 } // namespace internal
1214 } // namespace views