Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / views / widget / native_widget_aura.cc
blobb5b5394ded4c6afd2489ee83fd594e021536d0de
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"
7 #include "base/bind.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"
47 #if defined(OS_WIN)
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"
52 #endif
54 #if defined(USE_X11) && !defined(OS_CHROMEOS)
55 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
56 #endif
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"
61 #endif
63 namespace views {
65 namespace {
67 void SetRestoreBounds(aura::Window* window, const gfx::Rect& bounds) {
68 window->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
71 } // namespace
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),
80 destroying_(false),
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);
88 // static
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)
118 window_->Show();
120 delegate_->OnNativeWidgetCreated(false);
122 gfx::Rect window_bounds = params.bounds;
123 gfx::NativeView parent = params.parent;
124 gfx::NativeView context = params.context;
125 if (!params.child) {
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_);
130 if (!context)
131 context = parent;
132 parent = NULL;
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();
151 if (parent) {
152 parent->AddChild(window_);
153 } else {
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
163 // state/bounds).
164 if (IsMaximized())
165 SetRestoreBounds(window_, window_bounds);
166 else
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() {
186 return NULL;
189 bool NativeWidgetAura::ShouldUseNativeFrame() const {
190 // There is only one frame type for aura.
191 return false;
194 bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const {
195 return false;
198 void NativeWidgetAura::FrameTypeChanged() {
199 // This is called when the Theme has changed; forward the event to the root
200 // widget.
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 {
214 return window_;
217 gfx::NativeWindow NativeWidgetAura::GetNativeWindow() const {
218 return window_;
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) {
244 if (window_)
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() {
257 if (window_)
258 window_->SetCapture();
261 void NativeWidgetAura::ReleaseCapture() {
262 if (window_)
263 window_->ReleaseCapture();
266 bool NativeWidgetAura::HasCapture() const {
267 return window_ && window_->HasCapture();
270 InputMethod* NativeWidgetAura::CreateInputMethod() {
271 if (!window_)
272 return NULL;
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() {
284 return this;
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) {
293 if (!window_)
294 return;
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(),
308 &origin);
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,
328 size.width(),
329 size.height());
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(
343 gfx::Rect* bounds,
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) {
352 if (!window_)
353 return false;
354 if (window_->title() == title)
355 return false;
356 window_->SetTitle(title);
357 return true;
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 {
381 if (!window_)
382 return gfx::Rect();
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);
391 if (restore_bounds)
392 return *restore_bounds;
394 return window_->GetBoundsInScreen();
397 void NativeWidgetAura::SetBounds(const gfx::Rect& bounds) {
398 if (!window_)
399 return;
401 aura::Window* root = window_->GetRootWindow();
402 if (root) {
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);
409 return;
412 window_->SetBounds(bounds);
415 void NativeWidgetAura::SetSize(const gfx::Size& size) {
416 if (window_)
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() {
427 if (window_)
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) {
438 if (window_)
439 window_->layer()->SetAlphaShape(make_scoped_ptr(region));
440 else
441 delete 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.
448 DCHECK(window_ ||
449 ownership_ == Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
450 if (window_) {
451 window_->SuppressPaint();
452 Hide();
453 window_->SetProperty(aura::client::kModalKey, ui::MODAL_TYPE_NONE);
456 if (!close_widget_factory_.HasWeakPtrs()) {
457 base::MessageLoop::current()->PostTask(
458 FROM_HERE,
459 base::Bind(&NativeWidgetAura::CloseNow,
460 close_widget_factory_.GetWeakPtr()));
464 void NativeWidgetAura::CloseNow() {
465 delete window_;
468 void NativeWidgetAura::Show() {
469 ShowWithWindowState(ui::SHOW_STATE_NORMAL);
472 void NativeWidgetAura::Hide() {
473 if (window_)
474 window_->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) {
484 if (!window_)
485 return;
487 if (state == ui::SHOW_STATE_MAXIMIZED || state == ui::SHOW_STATE_FULLSCREEN)
488 window_->SetProperty(aura::client::kShowStateKey, state);
489 window_->Show();
490 if (delegate_->CanActivate()) {
491 if (state != ui::SHOW_STATE_INACTIVE)
492 Activate();
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() {
505 if (!window_)
506 return;
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(
512 window_);
514 if (window_->GetProperty(aura::client::kDrawAttentionKey))
515 window_->SetProperty(aura::client::kDrawAttentionKey, false);
518 void NativeWidgetAura::Deactivate() {
519 if (!window_)
520 return;
521 aura::client::GetActivationClient(window_->GetRootWindow())->DeactivateWindow(
522 window_);
525 bool NativeWidgetAura::IsActive() const {
526 return window_ && wm::IsActiveWindow(window_);
529 void NativeWidgetAura::SetAlwaysOnTop(bool on_top) {
530 if (window_)
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() {
543 if (window_)
544 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
547 void NativeWidgetAura::Minimize() {
548 if (window_)
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() {
563 if (window_)
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.
573 if (fullscreen)
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) {
587 if (window_)
588 window_->layer()->SetOpacity(opacity / 255.0);
591 void NativeWidgetAura::SetUseDragFrame(bool use_drag_frame) {
592 NOTIMPLEMENTED();
595 void NativeWidgetAura::FlashFrame(bool flash) {
596 if (window_)
597 window_->SetProperty(aura::client::kDrawAttentionKey, flash);
600 void NativeWidgetAura::RunShellDrag(View* view,
601 const ui::OSExchangeData& data,
602 const gfx::Point& location,
603 int operation,
604 ui::DragDropTypes::DragEventSource source) {
605 if (window_)
606 views::RunShellDrag(window_, data, location, operation, source);
609 void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect& rect) {
610 if (window_)
611 window_->SchedulePaintInRect(rect);
614 void NativeWidgetAura::SetCursor(gfx::NativeCursor cursor) {
615 cursor_ = cursor;
616 aura::client::CursorClient* cursor_client =
617 aura::client::GetCursorClient(window_->GetRootWindow());
618 if (cursor_client)
619 cursor_client->SetCursor(cursor);
622 bool NativeWidgetAura::IsMouseEventsEnabled() const {
623 if (!window_)
624 return false;
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 {
637 if (!window_)
638 return gfx::Rect();
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
648 // loop.
649 if (!window_ || !window_->GetRootWindow())
650 return Widget::MOVE_LOOP_CANCELED;
651 aura::client::WindowMoveClient* move_client =
652 aura::client::GetWindowMoveClient(window_->GetRootWindow());
653 if (!move_client)
654 return Widget::MOVE_LOOP_CANCELED;
656 SetCapture();
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())
670 return;
671 aura::client::WindowMoveClient* move_client =
672 aura::client::GetWindowMoveClient(window_->GetRootWindow());
673 if (move_client)
674 move_client->EndMoveLoop();
677 void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value) {
678 if (window_)
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;
693 break;
694 case Widget::ANIMATE_HIDE:
695 wm_transition = wm::ANIMATE_HIDE;
696 break;
697 case Widget::ANIMATE_BOTH:
698 wm_transition = wm::ANIMATE_BOTH;
699 break;
700 case Widget::ANIMATE_NONE:
701 wm_transition = wm::ANIMATE_NONE;
702 break;
704 wm::SetWindowVisibilityAnimationTransition(window_, wm_transition);
707 ui::NativeTheme* NativeWidgetAura::GetNativeTheme() const {
708 #if !defined(OS_CHROMEOS)
709 return DesktopWindowTreeHost::GetNativeTheme(window_);
710 #else
711 return ui::NativeThemeAura::instance();
712 #endif
715 void NativeWidgetAura::OnRootViewLayout() {
718 bool NativeWidgetAura::IsTranslucentWindowOpacitySupported() const {
719 return true;
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)
742 return;
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
755 // maximizable.
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 gfx::NativeCursor NativeWidgetAura::GetCursor(const gfx::Point& point) {
775 return cursor_;
778 int NativeWidgetAura::GetNonClientComponent(const gfx::Point& point) const {
779 return delegate_->GetNonClientComponent(point);
782 bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling(
783 aura::Window* child,
784 const gfx::Point& location) {
785 views::WidgetDelegate* widget_delegate = GetWidget()->widget_delegate();
786 if (widget_delegate &&
787 !widget_delegate->ShouldDescendIntoChildForEventHandling(child, location))
788 return false;
790 // Don't descend into |child| if there is a view with a Layer that contains
791 // the point and is stacked above |child|s layer.
792 typedef std::vector<ui::Layer*> Layers;
793 const Layers& root_layers(delegate_->GetRootLayers());
794 if (root_layers.empty())
795 return true;
797 Layers::const_iterator child_layer_iter(
798 std::find(window_->layer()->children().begin(),
799 window_->layer()->children().end(), child->layer()));
800 if (child_layer_iter == window_->layer()->children().end())
801 return true;
803 for (std::vector<ui::Layer*>::const_reverse_iterator i = root_layers.rbegin();
804 i != root_layers.rend(); ++i) {
805 ui::Layer* layer = *i;
806 if (layer->visible() && layer->bounds().Contains(location)) {
807 Layers::const_iterator root_layer_iter(
808 std::find(window_->layer()->children().begin(),
809 window_->layer()->children().end(), layer));
810 if (root_layer_iter > child_layer_iter)
811 return false;
814 return true;
817 bool NativeWidgetAura::CanFocus() {
818 return ShouldActivate();
821 void NativeWidgetAura::OnCaptureLost() {
822 delegate_->OnMouseCaptureLost();
825 void NativeWidgetAura::OnPaint(gfx::Canvas* canvas) {
826 delegate_->OnNativeWidgetPaint(canvas);
829 void NativeWidgetAura::OnDeviceScaleFactorChanged(float device_scale_factor) {
830 // Repainting with new scale factor will paint the content at the right scale.
833 void NativeWidgetAura::OnWindowDestroying(aura::Window* window) {
834 window_->RemoveObserver(this);
835 delegate_->OnNativeWidgetDestroying();
837 // If the aura::Window is destroyed, we can no longer show tooltips.
838 tooltip_manager_.reset();
841 void NativeWidgetAura::OnWindowDestroyed(aura::Window* window) {
842 window_ = NULL;
843 delegate_->OnNativeWidgetDestroyed();
844 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
845 delete this;
848 void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible) {
849 delegate_->OnNativeWidgetVisibilityChanged(visible);
852 bool NativeWidgetAura::HasHitTestMask() const {
853 return delegate_->HasHitTestMask();
856 void NativeWidgetAura::GetHitTestMask(gfx::Path* mask) const {
857 DCHECK(mask);
858 delegate_->GetHitTestMask(mask);
861 ////////////////////////////////////////////////////////////////////////////////
862 // NativeWidgetAura, aura::WindowObserver implementation:
864 void NativeWidgetAura::OnWindowPropertyChanged(aura::Window* window,
865 const void* key,
866 intptr_t old) {
867 if (key == aura::client::kShowStateKey)
868 delegate_->OnNativeWidgetWindowShowStateChanged();
871 ////////////////////////////////////////////////////////////////////////////////
872 // NativeWidgetAura, ui::EventHandler implementation:
874 void NativeWidgetAura::OnKeyEvent(ui::KeyEvent* event) {
875 DCHECK(window_);
876 if (event->is_char()) {
877 // If a ui::InputMethod object is attached to the root window, character
878 // events are handled inside the object and are not passed to this function.
879 // If such object is not attached, character events might be sent (e.g. on
880 // Windows). In this case, we just skip these.
881 return;
883 // Renderer may send a key event back to us if the key event wasn't handled,
884 // and the window may be invisible by that time.
885 if (!window_->IsVisible())
886 return;
887 InputMethod* input_method = GetWidget()->GetInputMethod();
888 if (!input_method)
889 return;
890 input_method->DispatchKeyEvent(*event);
891 if (switches::IsTextInputFocusManagerEnabled()) {
892 FocusManager* focus_manager = GetWidget()->GetFocusManager();
893 delegate_->OnKeyEvent(event);
894 if (!event->handled() && focus_manager)
895 focus_manager->OnKeyEvent(*event);
897 event->SetHandled();
900 void NativeWidgetAura::OnMouseEvent(ui::MouseEvent* event) {
901 DCHECK(window_);
902 DCHECK(window_->IsVisible());
903 if (event->type() == ui::ET_MOUSEWHEEL) {
904 delegate_->OnMouseEvent(event);
905 if (event->handled())
906 return;
909 if (tooltip_manager_.get())
910 tooltip_manager_->UpdateTooltip();
911 TooltipManagerAura::UpdateTooltipManagerForCapture(GetWidget());
912 delegate_->OnMouseEvent(event);
915 void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent* event) {
916 delegate_->OnScrollEvent(event);
919 void NativeWidgetAura::OnGestureEvent(ui::GestureEvent* event) {
920 DCHECK(window_);
921 DCHECK(window_->IsVisible() || event->IsEndingEvent());
922 delegate_->OnGestureEvent(event);
925 ////////////////////////////////////////////////////////////////////////////////
926 // NativeWidgetAura, aura::client::ActivationDelegate implementation:
928 bool NativeWidgetAura::ShouldActivate() const {
929 return delegate_->CanActivate();
932 ////////////////////////////////////////////////////////////////////////////////
933 // NativeWidgetAura, aura::client::ActivationChangeObserver implementation:
935 void NativeWidgetAura::OnWindowActivated(aura::Window* gained_active,
936 aura::Window* lost_active) {
937 DCHECK(window_ == gained_active || window_ == lost_active);
938 if (GetWidget()->GetFocusManager()) {
939 if (window_ == gained_active)
940 GetWidget()->GetFocusManager()->RestoreFocusedView();
941 else if (window_ == lost_active)
942 GetWidget()->GetFocusManager()->StoreFocusedView(true);
944 delegate_->OnNativeWidgetActivationChanged(window_ == gained_active);
947 ////////////////////////////////////////////////////////////////////////////////
948 // NativeWidgetAura, aura::client::FocusChangeObserver:
950 void NativeWidgetAura::OnWindowFocused(aura::Window* gained_focus,
951 aura::Window* lost_focus) {
952 if (window_ == gained_focus) {
953 // In aura, it is possible for child native widgets to take input and focus,
954 // this differs from the behavior on windows.
955 if (GetWidget()->GetInputMethod()) // Null in tests.
956 GetWidget()->GetInputMethod()->OnFocus();
957 delegate_->OnNativeFocus(lost_focus);
958 } else if (window_ == lost_focus) {
959 // GetInputMethod() recreates the input method if it's previously been
960 // destroyed. If we get called during destruction, the input method will be
961 // gone, and creating a new one and telling it that we lost the focus will
962 // trigger a DCHECK (the new input method doesn't think that we have the
963 // focus and doesn't expect a blur). OnBlur() shouldn't be called during
964 // destruction unless WIDGET_OWNS_NATIVE_WIDGET is set (which is just the
965 // case in tests).
966 if (!destroying_) {
967 if (GetWidget()->GetInputMethod())
968 GetWidget()->GetInputMethod()->OnBlur();
969 } else {
970 DCHECK_EQ(ownership_, Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
973 delegate_->OnNativeBlur(gained_focus);
977 ////////////////////////////////////////////////////////////////////////////////
978 // NativeWidgetAura, aura::WindowDragDropDelegate implementation:
980 void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent& event) {
981 DCHECK(drop_helper_.get() != NULL);
982 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
983 event.location(), event.source_operations());
986 int NativeWidgetAura::OnDragUpdated(const ui::DropTargetEvent& event) {
987 DCHECK(drop_helper_.get() != NULL);
988 last_drop_operation_ = drop_helper_->OnDragOver(event.data(),
989 event.location(), event.source_operations());
990 return last_drop_operation_;
993 void NativeWidgetAura::OnDragExited() {
994 DCHECK(drop_helper_.get() != NULL);
995 drop_helper_->OnDragExit();
998 int NativeWidgetAura::OnPerformDrop(const ui::DropTargetEvent& event) {
999 DCHECK(drop_helper_.get() != NULL);
1000 return drop_helper_->OnDrop(event.data(), event.location(),
1001 last_drop_operation_);
1004 ////////////////////////////////////////////////////////////////////////////////
1005 // NativeWidgetAura, protected:
1007 NativeWidgetAura::~NativeWidgetAura() {
1008 destroying_ = true;
1009 if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET)
1010 delete delegate_;
1011 else
1012 CloseNow();
1015 ////////////////////////////////////////////////////////////////////////////////
1016 // NativeWidgetAura, private:
1018 void NativeWidgetAura::SetInitialFocus(ui::WindowShowState show_state) {
1019 // The window does not get keyboard messages unless we focus it.
1020 if (!GetWidget()->SetInitialFocus(show_state))
1021 window_->Focus();
1024 ////////////////////////////////////////////////////////////////////////////////
1025 // Widget, public:
1027 namespace {
1028 #if defined(OS_WIN) || (defined(USE_X11) && !defined(OS_CHROMEOS))
1029 void CloseWindow(aura::Window* window) {
1030 if (window) {
1031 Widget* widget = Widget::GetWidgetForNativeView(window);
1032 if (widget && widget->is_secondary_widget())
1033 // To avoid the delay in shutdown caused by using Close which may wait
1034 // for animations, use CloseNow. Because this is only used on secondary
1035 // widgets it seems relatively safe to skip the extra processing of
1036 // Close.
1037 widget->CloseNow();
1040 #endif
1042 #if defined(OS_WIN)
1043 BOOL CALLBACK WindowCallbackProc(HWND hwnd, LPARAM lParam) {
1044 aura::Window* root_window =
1045 DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd);
1046 CloseWindow(root_window);
1047 return TRUE;
1049 #endif
1050 } // namespace
1052 // static
1053 void Widget::CloseAllSecondaryWidgets() {
1054 #if defined(OS_WIN)
1055 EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc, 0);
1056 #endif
1058 #if defined(USE_X11) && !defined(OS_CHROMEOS)
1059 std::vector<aura::Window*> open_windows =
1060 DesktopWindowTreeHostX11::GetAllOpenWindows();
1061 std::for_each(open_windows.begin(), open_windows.end(), CloseWindow);
1062 DesktopWindowTreeHostX11::CleanUpWindowList();
1063 #endif
1066 bool Widget::ConvertRect(const Widget* source,
1067 const Widget* target,
1068 gfx::Rect* rect) {
1069 return false;
1072 namespace internal {
1074 ////////////////////////////////////////////////////////////////////////////////
1075 // internal::NativeWidgetPrivate, public:
1077 // static
1078 NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
1079 internal::NativeWidgetDelegate* delegate) {
1080 return new NativeWidgetAura(delegate);
1083 // static
1084 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
1085 gfx::NativeView native_view) {
1086 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1087 return reinterpret_cast<NativeWidgetPrivate*>(native_view->user_data());
1090 // static
1091 NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
1092 gfx::NativeWindow native_window) {
1093 // Cast must match type supplied to RegisterNativeWidgetForWindow().
1094 return reinterpret_cast<NativeWidgetPrivate*>(native_window->user_data());
1097 // static
1098 NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
1099 gfx::NativeView native_view) {
1100 aura::Window* window = native_view;
1101 NativeWidgetPrivate* top_level_native_widget = NULL;
1102 while (window) {
1103 NativeWidgetPrivate* native_widget = GetNativeWidgetForNativeView(window);
1104 if (native_widget)
1105 top_level_native_widget = native_widget;
1106 window = window->parent();
1108 return top_level_native_widget;
1111 // static
1112 void NativeWidgetPrivate::GetAllChildWidgets(gfx::NativeView native_view,
1113 Widget::Widgets* children) {
1115 // Code expects widget for |native_view| to be added to |children|.
1116 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
1117 GetNativeWidgetForNativeView(native_view));
1118 if (native_widget && native_widget->GetWidget())
1119 children->insert(native_widget->GetWidget());
1122 const aura::Window::Windows& child_windows = native_view->children();
1123 for (aura::Window::Windows::const_iterator i = child_windows.begin();
1124 i != child_windows.end(); ++i) {
1125 GetAllChildWidgets((*i), children);
1129 // static
1130 void NativeWidgetPrivate::GetAllOwnedWidgets(gfx::NativeView native_view,
1131 Widget::Widgets* owned) {
1132 // Add all owned widgets.
1133 for (aura::Window* transient_child : wm::GetTransientChildren(native_view)) {
1134 NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
1135 GetNativeWidgetForNativeView(transient_child));
1136 if (native_widget && native_widget->GetWidget())
1137 owned->insert(native_widget->GetWidget());
1138 GetAllOwnedWidgets(transient_child, owned);
1141 // Add all child windows.
1142 for (aura::Window* child : native_view->children())
1143 GetAllChildWidgets(child, owned);
1146 // static
1147 void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view,
1148 gfx::NativeView new_parent) {
1149 DCHECK(native_view != new_parent);
1151 gfx::NativeView previous_parent = native_view->parent();
1152 if (previous_parent == new_parent)
1153 return;
1155 Widget::Widgets widgets;
1156 GetAllChildWidgets(native_view, &widgets);
1158 // First notify all the widgets that they are being disassociated
1159 // from their previous parent.
1160 for (Widget::Widgets::iterator it = widgets.begin();
1161 it != widgets.end(); ++it) {
1162 (*it)->NotifyNativeViewHierarchyWillChange();
1165 if (new_parent) {
1166 new_parent->AddChild(native_view);
1167 } else {
1168 // The following looks weird, but it's the equivalent of what aura has
1169 // always done. (The previous behaviour of aura::Window::SetParent() used
1170 // NULL as a special value that meant ask the WindowTreeClient where things
1171 // should go.)
1173 // This probably isn't strictly correct, but its an invariant that a Window
1174 // in use will be attached to a RootWindow, so we can't just call
1175 // RemoveChild here. The only possible thing that could assign a RootWindow
1176 // in this case is the stacking client of the current RootWindow. This
1177 // matches our previous behaviour; the global stacking client would almost
1178 // always reattach the window to the same RootWindow.
1179 aura::Window* root_window = native_view->GetRootWindow();
1180 aura::client::ParentWindowWithContext(
1181 native_view, root_window, root_window->GetBoundsInScreen());
1184 // And now, notify them that they have a brand new parent.
1185 for (Widget::Widgets::iterator it = widgets.begin();
1186 it != widgets.end(); ++it) {
1187 (*it)->NotifyNativeViewHierarchyChanged();
1191 // static
1192 bool NativeWidgetPrivate::IsMouseButtonDown() {
1193 return aura::Env::GetInstance()->IsMouseButtonDown();
1196 // static
1197 gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() {
1198 #if defined(OS_WIN)
1199 NONCLIENTMETRICS_XP ncm;
1200 base::win::GetNonClientMetrics(&ncm);
1201 l10n_util::AdjustUIFont(&(ncm.lfCaptionFont));
1202 base::win::ScopedHFONT caption_font(CreateFontIndirect(&(ncm.lfCaptionFont)));
1203 return gfx::FontList(gfx::Font(caption_font));
1204 #else
1205 return gfx::FontList();
1206 #endif
1209 } // namespace internal
1210 } // namespace views