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_util.h"
41 #include "ui/wm/public/activation_client.h"
42 #include "ui/wm/public/drag_drop_client.h"
43 #include "ui/wm/public/window_move_client.h"
44 #include "ui/wm/public/window_types.h"
47 #include "base/win/scoped_gdi_object.h"
48 #include "base/win/win_util.h"
49 #include "ui/base/l10n/l10n_util_win.h"
50 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
53 #if defined(USE_X11) && !defined(OS_CHROMEOS)
54 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
57 #if !defined(OS_CHROMEOS)
58 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
59 #include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
66 void SetRestoreBounds(aura::Window
* window
, const gfx::Rect
& bounds
) {
67 window
->SetProperty(aura::client::kRestoreBoundsKey
, new gfx::Rect(bounds
));
72 ////////////////////////////////////////////////////////////////////////////////
73 // NativeWidgetAura, public:
75 NativeWidgetAura::NativeWidgetAura(internal::NativeWidgetDelegate
* delegate
)
76 : delegate_(delegate
),
77 window_(new aura::Window(this)),
78 ownership_(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
),
79 close_widget_factory_(this),
81 cursor_(gfx::kNullCursor
),
82 saved_window_state_(ui::SHOW_STATE_DEFAULT
) {
83 aura::client::SetFocusChangeObserver(window_
, this);
84 aura::client::SetActivationChangeObserver(window_
, this);
88 void NativeWidgetAura::RegisterNativeWidgetForWindow(
89 internal::NativeWidgetPrivate
* native_widget
,
90 aura::Window
* window
) {
91 window
->set_user_data(native_widget
);
94 ////////////////////////////////////////////////////////////////////////////////
95 // NativeWidgetAura, internal::NativeWidgetPrivate implementation:
97 void NativeWidgetAura::InitNativeWidget(const Widget::InitParams
& params
) {
98 // Aura needs to know which desktop (Ash or regular) will manage this widget.
99 // See Widget::InitParams::context for details.
100 DCHECK(params
.parent
|| params
.context
);
102 ownership_
= params
.ownership
;
104 RegisterNativeWidgetForWindow(this, window_
);
105 window_
->SetType(GetAuraWindowTypeForWidgetType(params
.type
));
106 window_
->SetProperty(aura::client::kShowStateKey
, params
.show_state
);
107 if (params
.type
== Widget::InitParams::TYPE_BUBBLE
)
108 aura::client::SetHideOnDeactivate(window_
, true);
109 window_
->SetTransparent(
110 params
.opacity
== Widget::InitParams::TRANSLUCENT_WINDOW
);
111 window_
->Init(params
.layer_type
);
112 if (params
.shadow_type
== Widget::InitParams::SHADOW_TYPE_NONE
)
113 SetShadowType(window_
, wm::SHADOW_TYPE_NONE
);
114 else if (params
.shadow_type
== Widget::InitParams::SHADOW_TYPE_DROP
)
115 SetShadowType(window_
, wm::SHADOW_TYPE_RECTANGULAR
);
116 if (params
.type
== Widget::InitParams::TYPE_CONTROL
)
119 delegate_
->OnNativeWidgetCreated(false);
121 gfx::Rect window_bounds
= params
.bounds
;
122 gfx::NativeView parent
= params
.parent
;
123 gfx::NativeView context
= params
.context
;
125 // Set up the transient child before the window is added. This way the
126 // LayoutManager knows the window has a transient parent.
127 if (parent
&& parent
->type() != ui::wm::WINDOW_TYPE_UNKNOWN
) {
128 wm::AddTransientChild(parent
, window_
);
133 // SetAlwaysOnTop before SetParent so that always-on-top container is used.
134 SetAlwaysOnTop(params
.keep_on_top
);
135 // Make sure we have a real |window_bounds|.
136 if (parent
&& window_bounds
== gfx::Rect()) {
137 // If a parent is specified but no bounds are given,
138 // use the origin of the parent's display so that the widget
139 // will be added to the same display as the parent.
140 gfx::Rect bounds
= gfx::Screen::GetScreenFor(parent
)->
141 GetDisplayNearestWindow(parent
).bounds();
142 window_bounds
.set_origin(bounds
.origin());
146 // Set properties before addeing to the parent so that its layout manager
147 // sees the correct values.
148 window_
->SetProperty(aura::client::kCanMaximizeKey
,
149 GetWidget()->widget_delegate()->CanMaximize());
150 window_
->SetProperty(aura::client::kCanResizeKey
,
151 GetWidget()->widget_delegate()->CanResize());
154 parent
->AddChild(window_
);
156 aura::client::ParentWindowWithContext(
157 window_
, context
->GetRootWindow(), window_bounds
);
160 // Wait to set the bounds until we have a parent. That way we can know our
161 // true state/bounds (the LayoutManager may enforce a particular
164 SetRestoreBounds(window_
, window_bounds
);
166 SetBounds(window_bounds
);
167 window_
->set_ignore_events(!params
.accept_events
);
168 DCHECK(GetWidget()->GetRootView());
169 if (params
.type
!= Widget::InitParams::TYPE_TOOLTIP
)
170 tooltip_manager_
.reset(new views::TooltipManagerAura(GetWidget()));
172 drop_helper_
.reset(new DropHelper(GetWidget()->GetRootView()));
173 if (params
.type
!= Widget::InitParams::TYPE_TOOLTIP
&&
174 params
.type
!= Widget::InitParams::TYPE_POPUP
) {
175 aura::client::SetDragDropDelegate(window_
, this);
178 aura::client::SetActivationDelegate(window_
, this);
180 window_reorderer_
.reset(new WindowReorderer(window_
,
181 GetWidget()->GetRootView()));
184 NonClientFrameView
* NativeWidgetAura::CreateNonClientFrameView() {
188 bool NativeWidgetAura::ShouldUseNativeFrame() const {
189 // There is only one frame type for aura.
193 bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const {
197 void NativeWidgetAura::FrameTypeChanged() {
198 // This is called when the Theme has changed; forward the event to the root
200 GetWidget()->ThemeChanged();
201 GetWidget()->GetRootView()->SchedulePaint();
204 Widget
* NativeWidgetAura::GetWidget() {
205 return delegate_
->AsWidget();
208 const Widget
* NativeWidgetAura::GetWidget() const {
209 return delegate_
->AsWidget();
212 gfx::NativeView
NativeWidgetAura::GetNativeView() const {
216 gfx::NativeWindow
NativeWidgetAura::GetNativeWindow() const {
220 Widget
* NativeWidgetAura::GetTopLevelWidget() {
221 NativeWidgetPrivate
* native_widget
= GetTopLevelNativeWidget(GetNativeView());
222 return native_widget
? native_widget
->GetWidget() : NULL
;
225 const ui::Compositor
* NativeWidgetAura::GetCompositor() const {
226 return window_
? window_
->layer()->GetCompositor() : NULL
;
229 ui::Compositor
* NativeWidgetAura::GetCompositor() {
230 return window_
? window_
->layer()->GetCompositor() : NULL
;
233 ui::Layer
* NativeWidgetAura::GetLayer() {
234 return window_
? window_
->layer() : NULL
;
237 void NativeWidgetAura::ReorderNativeViews() {
238 window_reorderer_
->ReorderChildWindows();
241 void NativeWidgetAura::ViewRemoved(View
* view
) {
242 DCHECK(drop_helper_
.get() != NULL
);
243 drop_helper_
->ResetTargetViewIfEquals(view
);
246 void NativeWidgetAura::SetNativeWindowProperty(const char* name
, void* value
) {
248 window_
->SetNativeWindowProperty(name
, value
);
251 void* NativeWidgetAura::GetNativeWindowProperty(const char* name
) const {
252 return window_
? window_
->GetNativeWindowProperty(name
) : NULL
;
255 TooltipManager
* NativeWidgetAura::GetTooltipManager() const {
256 return tooltip_manager_
.get();
259 void NativeWidgetAura::SetCapture() {
261 window_
->SetCapture();
264 void NativeWidgetAura::ReleaseCapture() {
266 window_
->ReleaseCapture();
269 bool NativeWidgetAura::HasCapture() const {
270 return window_
&& window_
->HasCapture();
273 InputMethod
* NativeWidgetAura::CreateInputMethod() {
277 if (switches::IsTextInputFocusManagerEnabled())
278 return new NullInputMethod();
280 aura::Window
* root_window
= window_
->GetRootWindow();
281 ui::InputMethod
* host
=
282 root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
283 return new InputMethodBridge(this, host
, true);
286 internal::InputMethodDelegate
* NativeWidgetAura::GetInputMethodDelegate() {
290 ui::InputMethod
* NativeWidgetAura::GetHostInputMethod() {
291 aura::Window
* root_window
= window_
->GetRootWindow();
292 return root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
295 void NativeWidgetAura::CenterWindow(const gfx::Size
& size
) {
299 gfx::Rect
parent_bounds(window_
->parent()->GetBoundsInRootWindow());
300 // When centering window, we take the intersection of the host and
301 // the parent. We assume the root window represents the visible
302 // rect of a single screen.
303 gfx::Rect work_area
= gfx::Screen::GetScreenFor(window_
)->
304 GetDisplayNearestWindow(window_
).work_area();
306 aura::client::ScreenPositionClient
* screen_position_client
=
307 aura::client::GetScreenPositionClient(window_
->GetRootWindow());
308 if (screen_position_client
) {
309 gfx::Point origin
= work_area
.origin();
310 screen_position_client
->ConvertPointFromScreen(window_
->GetRootWindow(),
312 work_area
.set_origin(origin
);
315 parent_bounds
.Intersect(work_area
);
317 // If |window_|'s transient parent's bounds are big enough to fit it, then we
318 // center it with respect to the transient parent.
319 if (wm::GetTransientParent(window_
)) {
320 gfx::Rect transient_parent_rect
=
321 wm::GetTransientParent(window_
)->GetBoundsInRootWindow();
322 transient_parent_rect
.Intersect(work_area
);
323 if (transient_parent_rect
.height() >= size
.height() &&
324 transient_parent_rect
.width() >= size
.width())
325 parent_bounds
= transient_parent_rect
;
328 gfx::Rect
window_bounds(
329 parent_bounds
.x() + (parent_bounds
.width() - size
.width()) / 2,
330 parent_bounds
.y() + (parent_bounds
.height() - size
.height()) / 2,
333 // Don't size the window bigger than the parent, otherwise the user may not be
334 // able to close or move it.
335 window_bounds
.AdjustToFit(parent_bounds
);
337 // Convert the bounds back relative to the parent.
338 gfx::Point origin
= window_bounds
.origin();
339 aura::Window::ConvertPointToTarget(window_
->GetRootWindow(),
340 window_
->parent(), &origin
);
341 window_bounds
.set_origin(origin
);
342 window_
->SetBounds(window_bounds
);
345 void NativeWidgetAura::GetWindowPlacement(
347 ui::WindowShowState
* show_state
) const {
348 // The interface specifies returning restored bounds, not current bounds.
349 *bounds
= GetRestoredBounds();
350 *show_state
= window_
? window_
->GetProperty(aura::client::kShowStateKey
) :
351 ui::SHOW_STATE_DEFAULT
;
354 bool NativeWidgetAura::SetWindowTitle(const base::string16
& title
) {
357 if (window_
->title() == title
)
359 window_
->SetTitle(title
);
363 void NativeWidgetAura::SetWindowIcons(const gfx::ImageSkia
& window_icon
,
364 const gfx::ImageSkia
& app_icon
) {
365 // Aura doesn't have window icons.
368 void NativeWidgetAura::InitModalType(ui::ModalType modal_type
) {
369 if (modal_type
!= ui::MODAL_TYPE_NONE
)
370 window_
->SetProperty(aura::client::kModalKey
, modal_type
);
373 gfx::Rect
NativeWidgetAura::GetWindowBoundsInScreen() const {
374 return window_
? window_
->GetBoundsInScreen() : gfx::Rect();
377 gfx::Rect
NativeWidgetAura::GetClientAreaBoundsInScreen() const {
378 // View-to-screen coordinate system transformations depend on this returning
379 // the full window bounds, for example View::ConvertPointToScreen().
380 return window_
? window_
->GetBoundsInScreen() : gfx::Rect();
383 gfx::Rect
NativeWidgetAura::GetRestoredBounds() const {
387 // Restored bounds should only be relevant if the window is minimized or
388 // maximized. However, in some places the code expects GetRestoredBounds()
389 // to return the current window bounds if the window is not in either state.
390 if (IsMinimized() || IsMaximized() || IsFullscreen()) {
391 // Restore bounds are in screen coordinates, no need to convert.
392 gfx::Rect
* restore_bounds
=
393 window_
->GetProperty(aura::client::kRestoreBoundsKey
);
395 return *restore_bounds
;
397 return window_
->GetBoundsInScreen();
400 void NativeWidgetAura::SetBounds(const gfx::Rect
& bounds
) {
404 aura::Window
* root
= window_
->GetRootWindow();
406 aura::client::ScreenPositionClient
* screen_position_client
=
407 aura::client::GetScreenPositionClient(root
);
408 if (screen_position_client
) {
409 gfx::Display dst_display
=
410 gfx::Screen::GetScreenFor(window_
)->GetDisplayMatching(bounds
);
411 screen_position_client
->SetBounds(window_
, bounds
, dst_display
);
415 window_
->SetBounds(bounds
);
418 void NativeWidgetAura::SetSize(const gfx::Size
& size
) {
420 window_
->SetBounds(gfx::Rect(window_
->bounds().origin(), size
));
423 void NativeWidgetAura::StackAbove(gfx::NativeView native_view
) {
424 if (window_
&& window_
->parent() &&
425 window_
->parent() == native_view
->parent())
426 window_
->parent()->StackChildAbove(window_
, native_view
);
429 void NativeWidgetAura::StackAtTop() {
431 window_
->parent()->StackChildAtTop(window_
);
434 void NativeWidgetAura::StackBelow(gfx::NativeView native_view
) {
435 if (window_
&& window_
->parent() &&
436 window_
->parent() == native_view
->parent())
437 window_
->parent()->StackChildBelow(window_
, native_view
);
440 void NativeWidgetAura::SetShape(gfx::NativeRegion region
) {
441 // No need for this. Just delete and ignore.
445 void NativeWidgetAura::Close() {
446 // |window_| may already be deleted by parent window. This can happen
447 // when this widget is child widget or has transient parent
448 // and ownership is WIDGET_OWNS_NATIVE_WIDGET.
450 ownership_
== Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
);
452 window_
->SuppressPaint();
454 window_
->SetProperty(aura::client::kModalKey
, ui::MODAL_TYPE_NONE
);
457 if (!close_widget_factory_
.HasWeakPtrs()) {
458 base::MessageLoop::current()->PostTask(
460 base::Bind(&NativeWidgetAura::CloseNow
,
461 close_widget_factory_
.GetWeakPtr()));
465 void NativeWidgetAura::CloseNow() {
469 void NativeWidgetAura::Show() {
470 ShowWithWindowState(ui::SHOW_STATE_NORMAL
);
473 void NativeWidgetAura::Hide() {
478 void NativeWidgetAura::ShowMaximizedWithBounds(
479 const gfx::Rect
& restored_bounds
) {
480 SetRestoreBounds(window_
, restored_bounds
);
481 ShowWithWindowState(ui::SHOW_STATE_MAXIMIZED
);
484 void NativeWidgetAura::ShowWithWindowState(ui::WindowShowState state
) {
488 if (state
== ui::SHOW_STATE_MAXIMIZED
|| state
== ui::SHOW_STATE_FULLSCREEN
)
489 window_
->SetProperty(aura::client::kShowStateKey
, state
);
491 if (delegate_
->CanActivate()) {
492 if (state
!= ui::SHOW_STATE_INACTIVE
)
494 // SetInitialFocus() should be always be called, even for
495 // SHOW_STATE_INACTIVE. If the window has to stay inactive, the method will
496 // do the right thing.
497 SetInitialFocus(state
);
501 bool NativeWidgetAura::IsVisible() const {
502 return window_
&& window_
->IsVisible();
505 void NativeWidgetAura::Activate() {
509 // We don't necessarily have a root window yet. This can happen with
510 // constrained windows.
511 if (window_
->GetRootWindow()) {
512 aura::client::GetActivationClient(window_
->GetRootWindow())->ActivateWindow(
515 if (window_
->GetProperty(aura::client::kDrawAttentionKey
))
516 window_
->SetProperty(aura::client::kDrawAttentionKey
, false);
519 void NativeWidgetAura::Deactivate() {
522 aura::client::GetActivationClient(window_
->GetRootWindow())->DeactivateWindow(
526 bool NativeWidgetAura::IsActive() const {
527 return window_
&& wm::IsActiveWindow(window_
);
530 void NativeWidgetAura::SetAlwaysOnTop(bool on_top
) {
532 window_
->SetProperty(aura::client::kAlwaysOnTopKey
, on_top
);
535 bool NativeWidgetAura::IsAlwaysOnTop() const {
536 return window_
&& window_
->GetProperty(aura::client::kAlwaysOnTopKey
);
539 void NativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible
) {
540 // Not implemented on chromeos or for child widgets.
543 void NativeWidgetAura::Maximize() {
545 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
548 void NativeWidgetAura::Minimize() {
550 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
553 bool NativeWidgetAura::IsMaximized() const {
554 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
555 ui::SHOW_STATE_MAXIMIZED
;
558 bool NativeWidgetAura::IsMinimized() const {
559 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
560 ui::SHOW_STATE_MINIMIZED
;
563 void NativeWidgetAura::Restore() {
565 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_NORMAL
);
568 void NativeWidgetAura::SetFullscreen(bool fullscreen
) {
569 if (!window_
|| IsFullscreen() == fullscreen
)
570 return; // Nothing to do.
572 // Save window state before entering full screen so that it could restored
573 // when exiting full screen.
575 saved_window_state_
= window_
->GetProperty(aura::client::kShowStateKey
);
577 window_
->SetProperty(
578 aura::client::kShowStateKey
,
579 fullscreen
? ui::SHOW_STATE_FULLSCREEN
: saved_window_state_
);
582 bool NativeWidgetAura::IsFullscreen() const {
583 return window_
&& window_
->GetProperty(aura::client::kShowStateKey
) ==
584 ui::SHOW_STATE_FULLSCREEN
;
587 void NativeWidgetAura::SetOpacity(unsigned char opacity
) {
589 window_
->layer()->SetOpacity(opacity
/ 255.0);
592 void NativeWidgetAura::SetUseDragFrame(bool use_drag_frame
) {
596 void NativeWidgetAura::FlashFrame(bool flash
) {
598 window_
->SetProperty(aura::client::kDrawAttentionKey
, flash
);
601 void NativeWidgetAura::RunShellDrag(View
* view
,
602 const ui::OSExchangeData
& data
,
603 const gfx::Point
& location
,
605 ui::DragDropTypes::DragEventSource source
) {
607 views::RunShellDrag(window_
, data
, location
, operation
, source
);
610 void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect
& rect
) {
612 window_
->SchedulePaintInRect(rect
);
615 void NativeWidgetAura::SetCursor(gfx::NativeCursor cursor
) {
617 aura::client::CursorClient
* cursor_client
=
618 aura::client::GetCursorClient(window_
->GetRootWindow());
620 cursor_client
->SetCursor(cursor
);
623 bool NativeWidgetAura::IsMouseEventsEnabled() const {
626 aura::client::CursorClient
* cursor_client
=
627 aura::client::GetCursorClient(window_
->GetRootWindow());
628 return cursor_client
? cursor_client
->IsMouseEventsEnabled() : true;
631 void NativeWidgetAura::ClearNativeFocus() {
632 aura::client::FocusClient
* client
= aura::client::GetFocusClient(window_
);
633 if (window_
&& client
&& window_
->Contains(client
->GetFocusedWindow()))
634 client
->ResetFocusWithinActiveWindow(window_
);
637 gfx::Rect
NativeWidgetAura::GetWorkAreaBoundsInScreen() const {
640 return gfx::Screen::GetScreenFor(window_
)->
641 GetDisplayNearestWindow(window_
).work_area();
644 Widget::MoveLoopResult
NativeWidgetAura::RunMoveLoop(
645 const gfx::Vector2d
& drag_offset
,
646 Widget::MoveLoopSource source
,
647 Widget::MoveLoopEscapeBehavior escape_behavior
) {
648 // |escape_behavior| is only needed on windows when running the native message
650 if (!window_
|| !window_
->GetRootWindow())
651 return Widget::MOVE_LOOP_CANCELED
;
652 aura::client::WindowMoveClient
* move_client
=
653 aura::client::GetWindowMoveClient(window_
->GetRootWindow());
655 return Widget::MOVE_LOOP_CANCELED
;
658 aura::client::WindowMoveSource window_move_source
=
659 source
== Widget::MOVE_LOOP_SOURCE_MOUSE
?
660 aura::client::WINDOW_MOVE_SOURCE_MOUSE
:
661 aura::client::WINDOW_MOVE_SOURCE_TOUCH
;
662 if (move_client
->RunMoveLoop(window_
, drag_offset
, window_move_source
) ==
663 aura::client::MOVE_SUCCESSFUL
) {
664 return Widget::MOVE_LOOP_SUCCESSFUL
;
666 return Widget::MOVE_LOOP_CANCELED
;
669 void NativeWidgetAura::EndMoveLoop() {
670 if (!window_
|| !window_
->GetRootWindow())
672 aura::client::WindowMoveClient
* move_client
=
673 aura::client::GetWindowMoveClient(window_
->GetRootWindow());
675 move_client
->EndMoveLoop();
678 void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value
) {
680 window_
->SetProperty(aura::client::kAnimationsDisabledKey
, !value
);
683 ui::NativeTheme
* NativeWidgetAura::GetNativeTheme() const {
684 #if !defined(OS_CHROMEOS)
685 return DesktopWindowTreeHost::GetNativeTheme(window_
);
687 return ui::NativeThemeAura::instance();
691 void NativeWidgetAura::OnRootViewLayout() const {
694 void NativeWidgetAura::RepostNativeEvent(gfx::NativeEvent native_event
) {
695 OnEvent(native_event
);
698 ////////////////////////////////////////////////////////////////////////////////
699 // NativeWidgetAura, views::InputMethodDelegate implementation:
701 void NativeWidgetAura::DispatchKeyEventPostIME(const ui::KeyEvent
& key
) {
702 FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
703 delegate_
->OnKeyEvent(const_cast<ui::KeyEvent
*>(&key
));
704 if (key
.handled() || !focus_manager
)
706 focus_manager
->OnKeyEvent(key
);
709 ////////////////////////////////////////////////////////////////////////////////
710 // NativeWidgetAura, aura::WindowDelegate implementation:
712 gfx::Size
NativeWidgetAura::GetMinimumSize() const {
713 return delegate_
->GetMinimumSize();
716 gfx::Size
NativeWidgetAura::GetMaximumSize() const {
717 // If a window have a maximum size, the window should not be
719 DCHECK(delegate_
->GetMaximumSize().IsEmpty() ||
720 !window_
->GetProperty(aura::client::kCanMaximizeKey
));
721 return delegate_
->GetMaximumSize();
724 void NativeWidgetAura::OnBoundsChanged(const gfx::Rect
& old_bounds
,
725 const gfx::Rect
& new_bounds
) {
726 // Assume that if the old bounds was completely empty a move happened. This
727 // handles the case of a maximize animation acquiring the layer (acquiring a
728 // layer results in clearing the bounds).
729 if (old_bounds
.origin() != new_bounds
.origin() ||
730 (old_bounds
== gfx::Rect(0, 0, 0, 0) && !new_bounds
.IsEmpty())) {
731 delegate_
->OnNativeWidgetMove();
733 if (old_bounds
.size() != new_bounds
.size())
734 delegate_
->OnNativeWidgetSizeChanged(new_bounds
.size());
737 gfx::NativeCursor
NativeWidgetAura::GetCursor(const gfx::Point
& point
) {
741 int NativeWidgetAura::GetNonClientComponent(const gfx::Point
& point
) const {
742 return delegate_
->GetNonClientComponent(point
);
745 bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling(
747 const gfx::Point
& location
) {
748 views::WidgetDelegate
* widget_delegate
= GetWidget()->widget_delegate();
749 if (widget_delegate
&&
750 !widget_delegate
->ShouldDescendIntoChildForEventHandling(child
, location
))
753 // Don't descend into |child| if there is a view with a Layer that contains
754 // the point and is stacked above |child|s layer.
755 typedef std::vector
<ui::Layer
*> Layers
;
756 const Layers
& root_layers(delegate_
->GetRootLayers());
757 if (root_layers
.empty())
760 Layers::const_iterator
child_layer_iter(
761 std::find(window_
->layer()->children().begin(),
762 window_
->layer()->children().end(), child
->layer()));
763 if (child_layer_iter
== window_
->layer()->children().end())
766 for (std::vector
<ui::Layer
*>::const_reverse_iterator i
= root_layers
.rbegin();
767 i
!= root_layers
.rend(); ++i
) {
768 ui::Layer
* layer
= *i
;
769 if (layer
->visible() && layer
->bounds().Contains(location
)) {
770 Layers::const_iterator
root_layer_iter(
771 std::find(window_
->layer()->children().begin(),
772 window_
->layer()->children().end(), layer
));
773 if (root_layer_iter
> child_layer_iter
)
780 bool NativeWidgetAura::CanFocus() {
781 return ShouldActivate();
784 void NativeWidgetAura::OnCaptureLost() {
785 delegate_
->OnMouseCaptureLost();
788 void NativeWidgetAura::OnPaint(gfx::Canvas
* canvas
) {
789 delegate_
->OnNativeWidgetPaint(canvas
);
792 void NativeWidgetAura::OnDeviceScaleFactorChanged(float device_scale_factor
) {
793 // Repainting with new scale factor will paint the content at the right scale.
796 void NativeWidgetAura::OnWindowDestroying(aura::Window
* window
) {
797 delegate_
->OnNativeWidgetDestroying();
799 // If the aura::Window is destroyed, we can no longer show tooltips.
800 tooltip_manager_
.reset();
803 void NativeWidgetAura::OnWindowDestroyed(aura::Window
* window
) {
805 delegate_
->OnNativeWidgetDestroyed();
806 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
810 void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible
) {
811 delegate_
->OnNativeWidgetVisibilityChanged(visible
);
814 bool NativeWidgetAura::HasHitTestMask() const {
815 return delegate_
->HasHitTestMask();
818 void NativeWidgetAura::GetHitTestMask(gfx::Path
* mask
) const {
820 delegate_
->GetHitTestMask(mask
);
823 ////////////////////////////////////////////////////////////////////////////////
824 // NativeWidgetAura, ui::EventHandler implementation:
826 void NativeWidgetAura::OnKeyEvent(ui::KeyEvent
* event
) {
828 if (event
->is_char()) {
829 // If a ui::InputMethod object is attached to the root window, character
830 // events are handled inside the object and are not passed to this function.
831 // If such object is not attached, character events might be sent (e.g. on
832 // Windows). In this case, we just skip these.
835 // Renderer may send a key event back to us if the key event wasn't handled,
836 // and the window may be invisible by that time.
837 if (!window_
->IsVisible())
839 GetWidget()->GetInputMethod()->DispatchKeyEvent(*event
);
840 if (switches::IsTextInputFocusManagerEnabled()) {
841 FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
842 delegate_
->OnKeyEvent(event
);
843 if (!event
->handled() && focus_manager
)
844 focus_manager
->OnKeyEvent(*event
);
849 void NativeWidgetAura::OnMouseEvent(ui::MouseEvent
* event
) {
851 DCHECK(window_
->IsVisible());
852 if (event
->type() == ui::ET_MOUSEWHEEL
) {
853 delegate_
->OnMouseEvent(event
);
854 if (event
->handled())
858 if (tooltip_manager_
.get())
859 tooltip_manager_
->UpdateTooltip();
860 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
861 delegate_
->OnMouseEvent(event
);
864 void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent
* event
) {
865 delegate_
->OnScrollEvent(event
);
868 void NativeWidgetAura::OnGestureEvent(ui::GestureEvent
* event
) {
870 DCHECK(window_
->IsVisible() || event
->IsEndingEvent());
871 delegate_
->OnGestureEvent(event
);
874 ////////////////////////////////////////////////////////////////////////////////
875 // NativeWidgetAura, aura::client::ActivationDelegate implementation:
877 bool NativeWidgetAura::ShouldActivate() const {
878 return delegate_
->CanActivate();
881 ////////////////////////////////////////////////////////////////////////////////
882 // NativeWidgetAura, aura::client::ActivationChangeObserver implementation:
884 void NativeWidgetAura::OnWindowActivated(aura::Window
* gained_active
,
885 aura::Window
* lost_active
) {
886 DCHECK(window_
== gained_active
|| window_
== lost_active
);
887 if (GetWidget()->GetFocusManager()) {
888 if (window_
== gained_active
)
889 GetWidget()->GetFocusManager()->RestoreFocusedView();
890 else if (window_
== lost_active
)
891 GetWidget()->GetFocusManager()->StoreFocusedView(true);
893 delegate_
->OnNativeWidgetActivationChanged(window_
== gained_active
);
896 ////////////////////////////////////////////////////////////////////////////////
897 // NativeWidgetAura, aura::client::FocusChangeObserver:
899 void NativeWidgetAura::OnWindowFocused(aura::Window
* gained_focus
,
900 aura::Window
* lost_focus
) {
901 if (window_
== gained_focus
) {
902 // In aura, it is possible for child native widgets to take input and focus,
903 // this differs from the behavior on windows.
904 if (GetWidget()->GetInputMethod()) // Null in tests.
905 GetWidget()->GetInputMethod()->OnFocus();
906 delegate_
->OnNativeFocus(lost_focus
);
907 GetWidget()->GetFocusManager()->RestoreFocusedView();
908 } else if (window_
== lost_focus
) {
909 // GetInputMethod() recreates the input method if it's previously been
910 // destroyed. If we get called during destruction, the input method will be
911 // gone, and creating a new one and telling it that we lost the focus will
912 // trigger a DCHECK (the new input method doesn't think that we have the
913 // focus and doesn't expect a blur). OnBlur() shouldn't be called during
914 // destruction unless WIDGET_OWNS_NATIVE_WIDGET is set (which is just the
917 if (GetWidget()->GetInputMethod())
918 GetWidget()->GetInputMethod()->OnBlur();
920 DCHECK_EQ(ownership_
, Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
);
923 delegate_
->OnNativeBlur(gained_focus
);
924 if (GetWidget()->GetFocusManager())
925 GetWidget()->GetFocusManager()->StoreFocusedView(true);
929 ////////////////////////////////////////////////////////////////////////////////
930 // NativeWidgetAura, aura::WindowDragDropDelegate implementation:
932 void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent
& event
) {
933 DCHECK(drop_helper_
.get() != NULL
);
934 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
935 event
.location(), event
.source_operations());
938 int NativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent
& event
) {
939 DCHECK(drop_helper_
.get() != NULL
);
940 last_drop_operation_
= drop_helper_
->OnDragOver(event
.data(),
941 event
.location(), event
.source_operations());
942 return last_drop_operation_
;
945 void NativeWidgetAura::OnDragExited() {
946 DCHECK(drop_helper_
.get() != NULL
);
947 drop_helper_
->OnDragExit();
950 int NativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent
& event
) {
951 DCHECK(drop_helper_
.get() != NULL
);
952 return drop_helper_
->OnDrop(event
.data(), event
.location(),
953 last_drop_operation_
);
956 ////////////////////////////////////////////////////////////////////////////////
957 // NativeWidgetAura, protected:
959 NativeWidgetAura::~NativeWidgetAura() {
961 if (ownership_
== Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
)
967 ////////////////////////////////////////////////////////////////////////////////
968 // NativeWidgetAura, private:
970 void NativeWidgetAura::SetInitialFocus(ui::WindowShowState show_state
) {
971 // The window does not get keyboard messages unless we focus it.
972 if (!GetWidget()->SetInitialFocus(show_state
))
976 ////////////////////////////////////////////////////////////////////////////////
980 #if defined(OS_WIN) || (defined(USE_X11) && !defined(OS_CHROMEOS))
981 void CloseWindow(aura::Window
* window
) {
983 Widget
* widget
= Widget::GetWidgetForNativeView(window
);
984 if (widget
&& widget
->is_secondary_widget())
985 // To avoid the delay in shutdown caused by using Close which may wait
986 // for animations, use CloseNow. Because this is only used on secondary
987 // widgets it seems relatively safe to skip the extra processing of
995 BOOL CALLBACK
WindowCallbackProc(HWND hwnd
, LPARAM lParam
) {
996 aura::Window
* root_window
=
997 DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd
);
998 CloseWindow(root_window
);
1005 void Widget::CloseAllSecondaryWidgets() {
1007 EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc
, 0);
1010 #if defined(USE_X11) && !defined(OS_CHROMEOS)
1011 std::vector
<aura::Window
*> open_windows
=
1012 DesktopWindowTreeHostX11::GetAllOpenWindows();
1013 std::for_each(open_windows
.begin(), open_windows
.end(), CloseWindow
);
1014 DesktopWindowTreeHostX11::CleanUpWindowList();
1018 bool Widget::ConvertRect(const Widget
* source
,
1019 const Widget
* target
,
1024 namespace internal
{
1026 ////////////////////////////////////////////////////////////////////////////////
1027 // internal::NativeWidgetPrivate, public:
1030 NativeWidgetPrivate
* NativeWidgetPrivate::CreateNativeWidget(
1031 internal::NativeWidgetDelegate
* delegate
) {
1032 return new NativeWidgetAura(delegate
);
1036 NativeWidgetPrivate
* NativeWidgetPrivate::GetNativeWidgetForNativeView(
1037 gfx::NativeView native_view
) {
1038 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1039 return reinterpret_cast<NativeWidgetPrivate
*>(native_view
->user_data());
1043 NativeWidgetPrivate
* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
1044 gfx::NativeWindow native_window
) {
1045 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1046 return reinterpret_cast<NativeWidgetPrivate
*>(native_window
->user_data());
1050 NativeWidgetPrivate
* NativeWidgetPrivate::GetTopLevelNativeWidget(
1051 gfx::NativeView native_view
) {
1052 aura::Window
* window
= native_view
;
1053 NativeWidgetPrivate
* top_level_native_widget
= NULL
;
1055 NativeWidgetPrivate
* native_widget
= GetNativeWidgetForNativeView(window
);
1057 top_level_native_widget
= native_widget
;
1058 window
= window
->parent();
1060 return top_level_native_widget
;
1064 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view
,
1065 Widget::Widgets
* children
) {
1067 // Code expects widget for |native_view| to be added to |children|.
1068 NativeWidgetPrivate
* native_widget
= static_cast<NativeWidgetPrivate
*>(
1069 GetNativeWidgetForNativeView(native_view
));
1070 if (native_widget
&& native_widget
->GetWidget())
1071 children
->insert(native_widget
->GetWidget());
1074 const aura::Window::Windows
& child_windows
= native_view
->children();
1075 for (aura::Window::Windows::const_iterator i
= child_windows
.begin();
1076 i
!= child_windows
.end(); ++i
) {
1077 GetAllChildWidgets((*i
), children
);
1082 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view
,
1083 Widget::Widgets
* owned
) {
1084 const aura::Window::Windows
& transient_children
=
1085 wm::GetTransientChildren(native_view
);
1086 for (aura::Window::Windows::const_iterator i
= transient_children
.begin();
1087 i
!= transient_children
.end(); ++i
) {
1088 NativeWidgetPrivate
* native_widget
= static_cast<NativeWidgetPrivate
*>(
1089 GetNativeWidgetForNativeView(*i
));
1090 if (native_widget
&& native_widget
->GetWidget())
1091 owned
->insert(native_widget
->GetWidget());
1092 GetAllOwnedWidgets((*i
), owned
);
1097 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view
,
1098 gfx::NativeView new_parent
) {
1099 DCHECK(native_view
!= new_parent
);
1101 gfx::NativeView previous_parent
= native_view
->parent();
1102 if (previous_parent
== new_parent
)
1105 Widget::Widgets widgets
;
1106 GetAllChildWidgets(native_view
, &widgets
);
1108 // First notify all the widgets that they are being disassociated
1109 // from their previous parent.
1110 for (Widget::Widgets::iterator it
= widgets
.begin();
1111 it
!= widgets
.end(); ++it
) {
1112 (*it
)->NotifyNativeViewHierarchyWillChange();
1116 new_parent
->AddChild(native_view
);
1118 // The following looks weird, but it's the equivalent of what aura has
1119 // always done. (The previous behaviour of aura::Window::SetParent() used
1120 // NULL as a special value that meant ask the WindowTreeClient where things
1123 // This probably isn't strictly correct, but its an invariant that a Window
1124 // in use will be attached to a RootWindow, so we can't just call
1125 // RemoveChild here. The only possible thing that could assign a RootWindow
1126 // in this case is the stacking client of the current RootWindow. This
1127 // matches our previous behaviour; the global stacking client would almost
1128 // always reattach the window to the same RootWindow.
1129 aura::Window
* root_window
= native_view
->GetRootWindow();
1130 aura::client::ParentWindowWithContext(
1131 native_view
, root_window
, root_window
->GetBoundsInScreen());
1134 // And now, notify them that they have a brand new parent.
1135 for (Widget::Widgets::iterator it
= widgets
.begin();
1136 it
!= widgets
.end(); ++it
) {
1137 (*it
)->NotifyNativeViewHierarchyChanged();
1142 bool NativeWidgetPrivate::IsMouseButtonDown() {
1143 return aura::Env::GetInstance()->IsMouseButtonDown();
1147 gfx::FontList
NativeWidgetPrivate::GetWindowTitleFontList() {
1149 NONCLIENTMETRICS ncm
;
1150 base::win::GetNonClientMetrics(&ncm
);
1151 l10n_util::AdjustUIFont(&(ncm
.lfCaptionFont
));
1152 base::win::ScopedHFONT
caption_font(CreateFontIndirect(&(ncm
.lfCaptionFont
)));
1153 return gfx::FontList(gfx::Font(caption_font
));
1155 return gfx::FontList();
1159 } // namespace internal
1160 } // namespace views