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/aura/window.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "ui/aura/client/capture_client.h"
18 #include "ui/aura/client/cursor_client.h"
19 #include "ui/aura/client/event_client.h"
20 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/client/screen_position_client.h"
22 #include "ui/aura/client/visibility_client.h"
23 #include "ui/aura/client/window_stacking_client.h"
24 #include "ui/aura/env.h"
25 #include "ui/aura/layout_manager.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/aura/window_event_dispatcher.h"
28 #include "ui/aura/window_observer.h"
29 #include "ui/aura/window_tracker.h"
30 #include "ui/aura/window_tree_host.h"
31 #include "ui/compositor/compositor.h"
32 #include "ui/compositor/layer.h"
33 #include "ui/events/event_target_iterator.h"
34 #include "ui/gfx/canvas.h"
35 #include "ui/gfx/path.h"
36 #include "ui/gfx/scoped_canvas.h"
37 #include "ui/gfx/screen.h"
43 // Used when searching for a Window to stack relative to.
45 T
IteratorForDirectionBegin(aura::Window
* window
);
48 Window::Windows::const_iterator
IteratorForDirectionBegin(
49 aura::Window
* window
) {
50 return window
->children().begin();
54 Window::Windows::const_reverse_iterator
IteratorForDirectionBegin(
55 aura::Window
* window
) {
56 return window
->children().rbegin();
60 T
IteratorForDirectionEnd(aura::Window
* window
);
63 Window::Windows::const_iterator
IteratorForDirectionEnd(aura::Window
* window
) {
64 return window
->children().end();
68 Window::Windows::const_reverse_iterator
IteratorForDirectionEnd(
69 aura::Window
* window
) {
70 return window
->children().rend();
73 // Depth first search for the first Window with a layer to stack relative
74 // to. Starts at target. Does not descend into |ignore|.
76 ui::Layer
* FindStackingTargetLayerDown(aura::Window
* target
,
77 aura::Window
* ignore
) {
82 return target
->layer();
84 for (T i
= IteratorForDirectionBegin
<T
>(target
);
85 i
!= IteratorForDirectionEnd
<T
>(target
); ++i
) {
86 ui::Layer
* layer
= FindStackingTargetLayerDown
<T
>(*i
, ignore
);
93 // Depth first search through the siblings of |target||. This does not search
94 // all the siblings, only those before/after |target| (depening upon the
95 // template type) and ignoring |ignore|. Returns the Layer of the first Window
96 // encountered with a Layer.
98 ui::Layer
* FindStackingLayerInSiblings(aura::Window
* target
,
99 aura::Window
* ignore
) {
100 aura::Window
* parent
= target
->parent();
101 for (T i
= std::find(IteratorForDirectionBegin
<T
>(parent
),
102 IteratorForDirectionEnd
<T
>(parent
), target
);
103 i
!= IteratorForDirectionEnd
<T
>(parent
); ++i
) {
104 ui::Layer
* layer
= FindStackingTargetLayerDown
<T
>(*i
, ignore
);
111 // Returns the first Window that has a Layer. This does a depth first search
112 // through the descendants of |target| first, then ascends up doing a depth
113 // first search through siblings of all ancestors until a Layer is found or an
114 // ancestor with a layer is found. This is intended to locate a layer to stack
115 // other layers relative to.
117 ui::Layer
* FindStackingTargetLayer(aura::Window
* target
, aura::Window
* ignore
) {
118 ui::Layer
* result
= FindStackingTargetLayerDown
<T
>(target
, ignore
);
121 while (target
->parent()) {
122 ui::Layer
* result
= FindStackingLayerInSiblings
<T
>(target
, ignore
);
125 target
= target
->parent();
132 // Does a depth first search for all descendants of |child| that have layers.
133 // This stops at any descendants that have layers (and adds them to |layers|).
134 void GetLayersToStack(aura::Window
* child
, std::vector
<ui::Layer
*>* layers
) {
135 if (child
->layer()) {
136 layers
->push_back(child
->layer());
139 for (size_t i
= 0; i
< child
->children().size(); ++i
)
140 GetLayersToStack(child
->children()[i
], layers
);
145 class ScopedCursorHider
{
147 explicit ScopedCursorHider(Window
* window
)
150 if (!window_
->IsRootWindow())
152 const bool cursor_is_in_bounds
= window_
->GetBoundsInScreen().Contains(
153 Env::GetInstance()->last_mouse_location());
154 client::CursorClient
* cursor_client
= client::GetCursorClient(window_
);
155 if (cursor_is_in_bounds
&& cursor_client
&&
156 cursor_client
->IsCursorVisible()) {
157 cursor_client
->HideCursor();
161 ~ScopedCursorHider() {
162 if (!window_
->IsRootWindow())
165 // Update the device scale factor of the cursor client only when the last
166 // mouse location is on this root window.
168 client::CursorClient
* cursor_client
= client::GetCursorClient(window_
);
170 const gfx::Display
& display
=
171 gfx::Screen::GetScreenFor(window_
)->GetDisplayNearestWindow(
173 cursor_client
->SetDisplay(display
);
174 cursor_client
->ShowCursor();
183 DISALLOW_COPY_AND_ASSIGN(ScopedCursorHider
);
186 Window::Window(WindowDelegate
* delegate
)
188 type_(ui::wm::WINDOW_TYPE_UNKNOWN
),
189 owned_by_parent_(true),
196 ignore_events_(false),
197 // Don't notify newly added observers during notification. This causes
198 // problems for code that adds an observer as part of an observer
199 // notification (such as the workspace code).
200 observers_(ObserverList
<WindowObserver
>::NOTIFY_EXISTING_ONLY
) {
201 set_target_handler(delegate_
);
205 if (layer()->owner() == this)
206 layer()->CompleteAllAnimations();
207 layer()->SuppressPaint();
209 // Let the delegate know we're in the processing of destroying.
211 delegate_
->OnWindowDestroying(this);
212 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWindowDestroying(this));
214 // While we are being destroyed, our target handler may also be in the
215 // process of destruction or already destroyed, so do not forward any
216 // input events at the ui::EP_TARGET phase.
217 set_target_handler(nullptr);
219 // TODO(beng): See comment in window_event_dispatcher.h. This shouldn't be
220 // necessary but unfortunately is right now due to ordering
221 // peculiarities. WED must be notified _after_ other observers
222 // are notified of pending teardown but before the hierarchy
223 // is actually torn down.
224 WindowTreeHost
* host
= GetHost();
226 host
->dispatcher()->OnPostNotifiedWindowDestroying(this);
228 // The window should have already had its state cleaned up in
229 // WindowEventDispatcher::OnWindowHidden(), but there have been some crashes
230 // involving windows being destroyed without being hidden first. See
231 // crbug.com/342040. This should help us debug the issue. TODO(tdresser):
232 // remove this once we determine why we have windows that are destroyed
233 // without being hidden.
234 bool window_incorrectly_cleaned_up
= CleanupGestureState();
235 CHECK(!window_incorrectly_cleaned_up
);
237 // Then destroy the children.
238 RemoveOrDestroyChildren();
240 // The window needs to be removed from the parent before calling the
241 // WindowDestroyed callbacks of delegate and the observers.
243 parent_
->RemoveChild(this);
246 delegate_
->OnWindowDestroyed(this);
247 ObserverListBase
<WindowObserver
>::Iterator
iter(&observers_
);
248 for (WindowObserver
* observer
= iter
.GetNext(); observer
;
249 observer
= iter
.GetNext()) {
250 RemoveObserver(observer
);
251 observer
->OnWindowDestroyed(this);
255 for (std::map
<const void*, Value
>::const_iterator iter
= prop_map_
.begin();
256 iter
!= prop_map_
.end();
258 if (iter
->second
.deallocator
)
259 (*iter
->second
.deallocator
)(iter
->second
.value
);
263 // The layer will either be destroyed by |layer_owner_|'s dtor, or by whoever
265 layer()->set_delegate(NULL
);
269 void Window::Init(ui::LayerType layer_type
) {
270 SetLayer(new ui::Layer(layer_type
));
271 layer()->SetVisible(false);
272 layer()->set_delegate(this);
274 layer()->SetFillsBoundsOpaquely(!transparent_
);
275 Env::GetInstance()->NotifyWindowInitialized(this);
278 void Window::SetType(ui::wm::WindowType type
) {
279 // Cannot change type after the window is initialized.
284 void Window::SetName(const std::string
& name
) {
290 void Window::SetTitle(const base::string16
& title
) {
292 FOR_EACH_OBSERVER(WindowObserver
,
294 OnWindowTitleChanged(this));
297 void Window::SetTransparent(bool transparent
) {
298 transparent_
= transparent
;
300 layer()->SetFillsBoundsOpaquely(!transparent_
);
303 void Window::SetFillsBoundsCompletely(bool fills_bounds
) {
304 layer()->SetFillsBoundsCompletely(fills_bounds
);
307 Window
* Window::GetRootWindow() {
308 return const_cast<Window
*>(
309 static_cast<const Window
*>(this)->GetRootWindow());
312 const Window
* Window::GetRootWindow() const {
313 return IsRootWindow() ? this : parent_
? parent_
->GetRootWindow() : NULL
;
316 WindowTreeHost
* Window::GetHost() {
317 return const_cast<WindowTreeHost
*>(const_cast<const Window
*>(this)->
321 const WindowTreeHost
* Window::GetHost() const {
322 const Window
* root_window
= GetRootWindow();
323 return root_window
? root_window
->host_
: NULL
;
326 void Window::Show() {
327 DCHECK_EQ(visible_
, layer()->GetTargetVisibility());
328 // It is not allowed that a window is visible but the layers alpha is fully
329 // transparent since the window would still be considered to be active but
330 // could not be seen.
331 DCHECK_IMPLIES(visible_
, layer()->GetTargetOpacity() > 0.0f
);
335 void Window::Hide() {
336 // RootWindow::OnVisibilityChanged will call ReleaseCapture.
340 bool Window::IsVisible() const {
341 // Layer visibility can be inconsistent with window visibility, for example
342 // when a Window is hidden, we want this function to return false immediately
343 // after, even though the client may decide to animate the hide effect (and
344 // so the layer will be visible for some time after Hide() is called).
345 for (const Window
* window
= this; window
; window
= window
->parent()) {
346 if (!window
->visible_
)
349 return window
->layer()->IsDrawn();
354 gfx::Rect
Window::GetBoundsInRootWindow() const {
355 // TODO(beng): There may be a better way to handle this, and the existing code
356 // is likely wrong anyway in a multi-display world, but this will
358 if (!GetRootWindow())
360 gfx::Rect
bounds_in_root(bounds().size());
361 ConvertRectToTarget(this, GetRootWindow(), &bounds_in_root
);
362 return bounds_in_root
;
365 gfx::Rect
Window::GetBoundsInScreen() const {
366 gfx::Rect
bounds(GetBoundsInRootWindow());
367 const Window
* root
= GetRootWindow();
369 aura::client::ScreenPositionClient
* screen_position_client
=
370 aura::client::GetScreenPositionClient(root
);
371 if (screen_position_client
) {
372 gfx::Point origin
= bounds
.origin();
373 screen_position_client
->ConvertPointToScreen(root
, &origin
);
374 bounds
.set_origin(origin
);
380 void Window::SetTransform(const gfx::Transform
& transform
) {
382 // Transforms aren't supported on layerless windows.
386 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
387 OnWindowTransforming(this));
388 layer()->SetTransform(transform
);
389 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
390 OnWindowTransformed(this));
391 NotifyAncestorWindowTransformed(this);
394 void Window::SetLayoutManager(LayoutManager
* layout_manager
) {
395 if (layout_manager
== layout_manager_
)
397 layout_manager_
.reset(layout_manager
);
400 // If we're changing to a new layout manager, ensure it is aware of all the
401 // existing child windows.
402 for (Windows::const_iterator it
= children_
.begin();
403 it
!= children_
.end();
405 layout_manager_
->OnWindowAddedToLayout(*it
);
408 scoped_ptr
<ui::EventTargeter
>
409 Window::SetEventTargeter(scoped_ptr
<ui::EventTargeter
> targeter
) {
410 scoped_ptr
<ui::EventTargeter
> old_targeter
= targeter_
.Pass();
411 targeter_
= targeter
.Pass();
412 return old_targeter
.Pass();
415 void Window::SetBounds(const gfx::Rect
& new_bounds
) {
416 if (parent_
&& parent_
->layout_manager())
417 parent_
->layout_manager()->SetChildBounds(this, new_bounds
);
419 // Ensure we don't go smaller than our minimum bounds.
420 gfx::Rect
final_bounds(new_bounds
);
422 const gfx::Size
& min_size
= delegate_
->GetMinimumSize();
423 final_bounds
.set_width(std::max(min_size
.width(), final_bounds
.width()));
424 final_bounds
.set_height(std::max(min_size
.height(),
425 final_bounds
.height()));
427 SetBoundsInternal(final_bounds
);
431 void Window::SetBoundsInScreen(const gfx::Rect
& new_bounds_in_screen
,
432 const gfx::Display
& dst_display
) {
433 Window
* root
= GetRootWindow();
435 aura::client::ScreenPositionClient
* screen_position_client
=
436 aura::client::GetScreenPositionClient(root
);
437 screen_position_client
->SetBounds(this, new_bounds_in_screen
, dst_display
);
440 SetBounds(new_bounds_in_screen
);
443 gfx::Rect
Window::GetTargetBounds() const {
447 if (!parent_
|| parent_
->layer())
448 return layer()->GetTargetBounds();
450 // We have a layer but our parent (who is valid) doesn't. This means the
451 // coordinates of the layer are relative to the first ancestor with a layer;
452 // convert to be relative to parent.
453 gfx::Vector2d offset
;
454 const aura::Window
* ancestor_with_layer
=
455 parent_
->GetAncestorWithLayer(&offset
);
456 if (!ancestor_with_layer
)
457 return layer()->GetTargetBounds();
459 gfx::Rect layer_target_bounds
= layer()->GetTargetBounds();
460 layer_target_bounds
-= offset
;
461 return layer_target_bounds
;
464 void Window::SchedulePaintInRect(const gfx::Rect
& rect
) {
465 layer()->SchedulePaint(rect
);
468 void Window::StackChildAtTop(Window
* child
) {
469 if (children_
.size() <= 1 || child
== children_
.back())
470 return; // In the front already.
471 StackChildAbove(child
, children_
.back());
474 void Window::StackChildAbove(Window
* child
, Window
* target
) {
475 StackChildRelativeTo(child
, target
, STACK_ABOVE
);
478 void Window::StackChildAtBottom(Window
* child
) {
479 if (children_
.size() <= 1 || child
== children_
.front())
480 return; // At the bottom already.
481 StackChildBelow(child
, children_
.front());
484 void Window::StackChildBelow(Window
* child
, Window
* target
) {
485 StackChildRelativeTo(child
, target
, STACK_BELOW
);
488 void Window::AddChild(Window
* child
) {
489 WindowObserver::HierarchyChangeParams params
;
490 params
.target
= child
;
491 params
.new_parent
= this;
492 params
.old_parent
= child
->parent();
493 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
;
494 NotifyWindowHierarchyChange(params
);
496 Window
* old_root
= child
->GetRootWindow();
498 DCHECK(std::find(children_
.begin(), children_
.end(), child
) ==
501 child
->parent()->RemoveChildImpl(child
, this);
503 gfx::Vector2d offset
;
504 aura::Window
* ancestor_with_layer
= GetAncestorWithLayer(&offset
);
506 child
->parent_
= this;
508 if (ancestor_with_layer
) {
509 offset
+= child
->bounds().OffsetFromOrigin();
510 child
->ReparentLayers(ancestor_with_layer
->layer(), offset
);
513 children_
.push_back(child
);
515 layout_manager_
->OnWindowAddedToLayout(child
);
516 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWindowAdded(child
));
517 child
->OnParentChanged();
519 Window
* root_window
= GetRootWindow();
520 if (root_window
&& old_root
!= root_window
) {
521 root_window
->GetHost()->dispatcher()->OnWindowAddedToRootWindow(child
);
522 child
->NotifyAddedToRootWindow();
525 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
;
526 NotifyWindowHierarchyChange(params
);
529 void Window::RemoveChild(Window
* child
) {
530 WindowObserver::HierarchyChangeParams params
;
531 params
.target
= child
;
532 params
.new_parent
= NULL
;
533 params
.old_parent
= this;
534 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
;
535 NotifyWindowHierarchyChange(params
);
537 RemoveChildImpl(child
, NULL
);
539 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
;
540 NotifyWindowHierarchyChange(params
);
543 bool Window::Contains(const Window
* other
) const {
544 for (const Window
* parent
= other
; parent
; parent
= parent
->parent_
) {
551 Window
* Window::GetChildById(int id
) {
552 return const_cast<Window
*>(const_cast<const Window
*>(this)->GetChildById(id
));
555 const Window
* Window::GetChildById(int id
) const {
556 Windows::const_iterator i
;
557 for (i
= children_
.begin(); i
!= children_
.end(); ++i
) {
558 if ((*i
)->id() == id
)
560 const Window
* result
= (*i
)->GetChildById(id
);
568 void Window::ConvertPointToTarget(const Window
* source
,
569 const Window
* target
,
573 if (source
->GetRootWindow() != target
->GetRootWindow()) {
574 client::ScreenPositionClient
* source_client
=
575 client::GetScreenPositionClient(source
->GetRootWindow());
576 // |source_client| can be NULL in tests.
578 source_client
->ConvertPointToScreen(source
, point
);
580 client::ScreenPositionClient
* target_client
=
581 client::GetScreenPositionClient(target
->GetRootWindow());
582 // |target_client| can be NULL in tests.
584 target_client
->ConvertPointFromScreen(target
, point
);
585 } else if ((source
!= target
) && (!source
->layer() || !target
->layer())) {
586 if (!source
->layer()) {
587 gfx::Vector2d offset_to_layer
;
588 source
= source
->GetAncestorWithLayer(&offset_to_layer
);
589 *point
+= offset_to_layer
;
591 if (!target
->layer()) {
592 gfx::Vector2d offset_to_layer
;
593 target
= target
->GetAncestorWithLayer(&offset_to_layer
);
594 *point
-= offset_to_layer
;
596 ui::Layer::ConvertPointToLayer(source
->layer(), target
->layer(), point
);
598 ui::Layer::ConvertPointToLayer(source
->layer(), target
->layer(), point
);
603 void Window::ConvertRectToTarget(const Window
* source
,
604 const Window
* target
,
607 gfx::Point origin
= rect
->origin();
608 ConvertPointToTarget(source
, target
, &origin
);
609 rect
->set_origin(origin
);
612 ui::TextInputClient
* Window::GetFocusedTextInputClient() {
613 return delegate_
? delegate_
->GetFocusedTextInputClient() : nullptr;
616 void Window::MoveCursorTo(const gfx::Point
& point_in_window
) {
617 Window
* root_window
= GetRootWindow();
619 gfx::Point
point_in_root(point_in_window
);
620 ConvertPointToTarget(this, root_window
, &point_in_root
);
621 root_window
->GetHost()->MoveCursorTo(point_in_root
);
624 gfx::NativeCursor
Window::GetCursor(const gfx::Point
& point
) const {
625 return delegate_
? delegate_
->GetCursor(point
) : gfx::kNullCursor
;
628 void Window::AddObserver(WindowObserver
* observer
) {
629 observer
->OnObservingWindow(this);
630 observers_
.AddObserver(observer
);
633 void Window::RemoveObserver(WindowObserver
* observer
) {
634 observer
->OnUnobservingWindow(this);
635 observers_
.RemoveObserver(observer
);
638 bool Window::HasObserver(const WindowObserver
* observer
) const {
639 return observers_
.HasObserver(observer
);
642 bool Window::ContainsPointInRoot(const gfx::Point
& point_in_root
) const {
643 const Window
* root_window
= GetRootWindow();
646 gfx::Point
local_point(point_in_root
);
647 ConvertPointToTarget(root_window
, this, &local_point
);
648 return gfx::Rect(GetTargetBounds().size()).Contains(local_point
);
651 bool Window::ContainsPoint(const gfx::Point
& local_point
) const {
652 return gfx::Rect(bounds().size()).Contains(local_point
);
655 Window
* Window::GetEventHandlerForPoint(const gfx::Point
& local_point
) {
656 return GetWindowForPoint(local_point
, true, true);
659 Window
* Window::GetTopWindowContainingPoint(const gfx::Point
& local_point
) {
660 return GetWindowForPoint(local_point
, false, false);
663 Window
* Window::GetToplevelWindow() {
664 Window
* topmost_window_with_delegate
= NULL
;
665 for (aura::Window
* window
= this; window
!= NULL
; window
= window
->parent()) {
666 if (window
->delegate())
667 topmost_window_with_delegate
= window
;
669 return topmost_window_with_delegate
;
672 void Window::Focus() {
673 client::FocusClient
* client
= client::GetFocusClient(this);
675 client
->FocusWindow(this);
678 bool Window::HasFocus() const {
679 client::FocusClient
* client
= client::GetFocusClient(this);
680 return client
&& client
->GetFocusedWindow() == this;
683 bool Window::CanFocus() const {
687 // NOTE: as part of focusing the window the ActivationClient may make the
688 // window visible (by way of making a hidden ancestor visible). For this
689 // reason we can't check visibility here and assume the client is doing it.
690 if (!parent_
|| (delegate_
&& !delegate_
->CanFocus()))
693 // The client may forbid certain windows from receiving focus at a given point
695 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
696 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
699 return parent_
->CanFocus();
702 bool Window::CanReceiveEvents() const {
706 // The client may forbid certain windows from receiving events at a given
708 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
709 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
712 return parent_
&& IsVisible() && parent_
->CanReceiveEvents();
715 void Window::SetCapture() {
719 Window
* root_window
= GetRootWindow();
722 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
725 client::GetCaptureClient(root_window
)->SetCapture(this);
728 void Window::ReleaseCapture() {
729 Window
* root_window
= GetRootWindow();
732 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
735 client::GetCaptureClient(root_window
)->ReleaseCapture(this);
738 bool Window::HasCapture() {
739 Window
* root_window
= GetRootWindow();
742 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
743 return capture_client
&& capture_client
->GetCaptureWindow() == this;
746 void Window::SuppressPaint() {
747 layer()->SuppressPaint();
750 // {Set,Get,Clear}Property are implemented in window_property.h.
752 void Window::SetNativeWindowProperty(const char* key
, void* value
) {
754 key
, key
, NULL
, reinterpret_cast<int64
>(value
), 0);
757 void* Window::GetNativeWindowProperty(const char* key
) const {
758 return reinterpret_cast<void*>(GetPropertyInternal(key
, 0));
761 void Window::OnDeviceScaleFactorChanged(float device_scale_factor
) {
762 ScopedCursorHider
hider(this);
764 delegate_
->OnDeviceScaleFactorChanged(device_scale_factor
);
768 std::string
Window::GetDebugInfo() const {
769 return base::StringPrintf(
770 "%s<%d> bounds(%d, %d, %d, %d) %s %s opacity=%.1f",
771 name().empty() ? "Unknown" : name().c_str(), id(),
772 bounds().x(), bounds().y(), bounds().width(), bounds().height(),
773 visible_
? "WindowVisible" : "WindowHidden",
775 (layer()->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
777 layer() ? layer()->opacity() : 1.0f
);
780 void Window::PrintWindowHierarchy(int depth
) const {
781 VLOG(0) << base::StringPrintf(
782 "%*s%s", depth
* 2, "", GetDebugInfo().c_str());
783 for (Windows::const_iterator it
= children_
.begin();
784 it
!= children_
.end(); ++it
) {
786 child
->PrintWindowHierarchy(depth
+ 1);
791 void Window::RemoveOrDestroyChildren() {
792 while (!children_
.empty()) {
793 Window
* child
= children_
[0];
794 if (child
->owned_by_parent_
) {
796 // Deleting the child so remove it from out children_ list.
797 DCHECK(std::find(children_
.begin(), children_
.end(), child
) ==
800 // Even if we can't delete the child, we still need to remove it from the
801 // parent so that relevant bookkeeping (parent_ back-pointers etc) are
808 ///////////////////////////////////////////////////////////////////////////////
811 int64
Window::SetPropertyInternal(const void* key
,
813 PropertyDeallocator deallocator
,
815 int64 default_value
) {
816 int64 old
= GetPropertyInternal(key
, default_value
);
817 if (value
== default_value
) {
818 prop_map_
.erase(key
);
821 prop_value
.name
= name
;
822 prop_value
.value
= value
;
823 prop_value
.deallocator
= deallocator
;
824 prop_map_
[key
] = prop_value
;
826 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
827 OnWindowPropertyChanged(this, key
, old
));
831 int64
Window::GetPropertyInternal(const void* key
,
832 int64 default_value
) const {
833 std::map
<const void*, Value
>::const_iterator iter
= prop_map_
.find(key
);
834 if (iter
== prop_map_
.end())
835 return default_value
;
836 return iter
->second
.value
;
839 bool Window::HitTest(const gfx::Point
& local_point
) {
840 gfx::Rect
local_bounds(bounds().size());
841 if (!delegate_
|| !delegate_
->HasHitTestMask())
842 return local_bounds
.Contains(local_point
);
845 delegate_
->GetHitTestMask(&mask
);
847 SkRegion clip_region
;
848 clip_region
.setRect(local_bounds
.x(), local_bounds
.y(),
849 local_bounds
.width(), local_bounds
.height());
850 SkRegion mask_region
;
851 return mask_region
.setPath(mask
, clip_region
) &&
852 mask_region
.contains(local_point
.x(), local_point
.y());
855 void Window::SetBoundsInternal(const gfx::Rect
& new_bounds
) {
856 gfx::Rect
actual_new_bounds(new_bounds
);
857 gfx::Rect old_bounds
= GetTargetBounds();
859 // Always need to set the layer's bounds -- even if it is to the same thing.
860 // This may cause important side effects such as stopping animation.
862 const gfx::Vector2d origin_delta
= new_bounds
.OffsetFromOrigin() -
863 bounds_
.OffsetFromOrigin();
864 bounds_
= new_bounds
;
865 OffsetLayerBounds(origin_delta
);
867 if (parent_
&& !parent_
->layer()) {
868 gfx::Vector2d offset
;
869 const aura::Window
* ancestor_with_layer
=
870 parent_
->GetAncestorWithLayer(&offset
);
871 if (ancestor_with_layer
)
872 actual_new_bounds
.Offset(offset
);
874 layer()->SetBounds(actual_new_bounds
);
877 // If we are currently not the layer's delegate, we will not get bounds
878 // changed notification from the layer (this typically happens after animating
879 // hidden). We must notify ourselves.
880 if (!layer() || layer()->delegate() != this)
881 OnWindowBoundsChanged(old_bounds
);
884 void Window::SetVisible(bool visible
) {
885 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
886 tracked_objects::ScopedTracker
tracking_profile(
887 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 Window::SetVisible"));
889 if ((layer() && visible
== layer()->GetTargetVisibility()) ||
890 (!layer() && visible
== visible_
))
891 return; // No change.
893 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
894 OnWindowVisibilityChanging(this, visible
));
896 client::VisibilityClient
* visibility_client
=
897 client::GetVisibilityClient(this);
898 if (visibility_client
)
899 visibility_client
->UpdateLayerVisibility(this, visible
);
901 layer()->SetVisible(visible
);
904 if (parent_
&& parent_
->layout_manager_
)
905 parent_
->layout_manager_
->OnChildWindowVisibilityChanged(this, visible
);
908 delegate_
->OnWindowTargetVisibilityChanged(visible
);
910 NotifyWindowVisibilityChanged(this, visible
);
913 void Window::SchedulePaint() {
914 SchedulePaintInRect(gfx::Rect(0, 0, bounds().width(), bounds().height()));
917 void Window::Paint(const ui::PaintContext
& context
) {
919 delegate_
->OnPaint(context
);
922 Window
* Window::GetWindowForPoint(const gfx::Point
& local_point
,
923 bool return_tightest
,
924 bool for_event_handling
) {
928 if ((for_event_handling
&& !HitTest(local_point
)) ||
929 (!for_event_handling
&& !ContainsPoint(local_point
)))
932 // Check if I should claim this event and not pass it to my children because
933 // the location is inside my hit test override area. For details, see
934 // set_hit_test_bounds_override_inner().
935 if (for_event_handling
&& !hit_test_bounds_override_inner_
.empty()) {
936 gfx::Rect
inset_local_bounds(gfx::Point(), bounds().size());
937 inset_local_bounds
.Inset(hit_test_bounds_override_inner_
);
938 // We know we're inside the normal local bounds, so if we're outside the
939 // inset bounds we must be in the special hit test override area.
940 DCHECK(HitTest(local_point
));
941 if (!inset_local_bounds
.Contains(local_point
))
942 return delegate_
? this : NULL
;
945 if (!return_tightest
&& delegate_
)
948 for (Windows::const_reverse_iterator it
= children_
.rbegin(),
949 rend
= children_
.rend();
953 if (for_event_handling
) {
954 if (child
->ignore_events_
)
956 // The client may not allow events to be processed by certain subtrees.
957 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
958 if (client
&& !client
->CanProcessEventsWithinSubtree(child
))
960 if (delegate_
&& !delegate_
->ShouldDescendIntoChildForEventHandling(
965 gfx::Point
point_in_child_coords(local_point
);
966 ConvertPointToTarget(this, child
, &point_in_child_coords
);
967 Window
* match
= child
->GetWindowForPoint(point_in_child_coords
,
974 return delegate_
? this : NULL
;
977 void Window::RemoveChildImpl(Window
* child
, Window
* new_parent
) {
979 layout_manager_
->OnWillRemoveWindowFromLayout(child
);
980 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWillRemoveWindow(child
));
981 Window
* root_window
= child
->GetRootWindow();
982 Window
* new_root_window
= new_parent
? new_parent
->GetRootWindow() : NULL
;
983 if (root_window
&& root_window
!= new_root_window
)
984 child
->NotifyRemovingFromRootWindow(new_root_window
);
986 gfx::Vector2d offset
;
987 GetAncestorWithLayer(&offset
);
988 child
->UnparentLayers(!layer(), offset
);
989 child
->parent_
= NULL
;
990 Windows::iterator i
= std::find(children_
.begin(), children_
.end(), child
);
991 DCHECK(i
!= children_
.end());
993 child
->OnParentChanged();
995 layout_manager_
->OnWindowRemovedFromLayout(child
);
998 void Window::UnparentLayers(bool has_layerless_ancestor
,
999 const gfx::Vector2d
& offset
) {
1001 const gfx::Vector2d new_offset
= offset
+ bounds().OffsetFromOrigin();
1002 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1003 children_
[i
]->UnparentLayers(true, new_offset
);
1006 // Only remove the layer if we still own it. Someone else may have acquired
1007 // ownership of it via AcquireLayer() and may expect the hierarchy to go
1008 // unchanged as the Window is destroyed.
1010 if (layer()->parent())
1011 layer()->parent()->Remove(layer());
1012 if (has_layerless_ancestor
) {
1013 const gfx::Rect
real_bounds(bounds_
);
1014 gfx::Rect
layer_bounds(layer()->bounds());
1015 layer_bounds
.Offset(-offset
);
1016 layer()->SetBounds(layer_bounds
);
1017 bounds_
= real_bounds
;
1023 void Window::ReparentLayers(ui::Layer
* parent_layer
,
1024 const gfx::Vector2d
& offset
) {
1026 for (size_t i
= 0; i
< children_
.size(); ++i
) {
1027 children_
[i
]->ReparentLayers(
1029 offset
+ children_
[i
]->bounds().OffsetFromOrigin());
1032 const gfx::Rect
real_bounds(bounds());
1033 parent_layer
->Add(layer());
1034 gfx::Rect
layer_bounds(layer()->bounds().size());
1035 layer_bounds
+= offset
;
1036 layer()->SetBounds(layer_bounds
);
1037 bounds_
= real_bounds
;
1041 void Window::OffsetLayerBounds(const gfx::Vector2d
& offset
) {
1043 for (size_t i
= 0; i
< children_
.size(); ++i
)
1044 children_
[i
]->OffsetLayerBounds(offset
);
1046 gfx::Rect
layer_bounds(layer()->bounds());
1047 layer_bounds
+= offset
;
1048 layer()->SetBounds(layer_bounds
);
1052 void Window::OnParentChanged() {
1054 WindowObserver
, observers_
, OnWindowParentChanged(this, parent_
));
1057 void Window::StackChildRelativeTo(Window
* child
,
1059 StackDirection direction
) {
1060 DCHECK_NE(child
, target
);
1063 DCHECK_EQ(this, child
->parent());
1064 DCHECK_EQ(this, target
->parent());
1066 client::WindowStackingClient
* stacking_client
=
1067 client::GetWindowStackingClient();
1068 if (stacking_client
&&
1069 !stacking_client
->AdjustStacking(&child
, &target
, &direction
))
1072 const size_t child_i
=
1073 std::find(children_
.begin(), children_
.end(), child
) - children_
.begin();
1074 const size_t target_i
=
1075 std::find(children_
.begin(), children_
.end(), target
) - children_
.begin();
1077 // Don't move the child if it is already in the right place.
1078 if ((direction
== STACK_ABOVE
&& child_i
== target_i
+ 1) ||
1079 (direction
== STACK_BELOW
&& child_i
+ 1 == target_i
))
1082 const size_t dest_i
=
1083 direction
== STACK_ABOVE
?
1084 (child_i
< target_i
? target_i
: target_i
+ 1) :
1085 (child_i
< target_i
? target_i
- 1 : target_i
);
1086 children_
.erase(children_
.begin() + child_i
);
1087 children_
.insert(children_
.begin() + dest_i
, child
);
1089 StackChildLayerRelativeTo(child
, target
, direction
);
1091 child
->OnStackingChanged();
1094 void Window::StackChildLayerRelativeTo(Window
* child
,
1096 StackDirection direction
) {
1097 Window
* ancestor_with_layer
= GetAncestorWithLayer(NULL
);
1098 ui::Layer
* ancestor_layer
=
1099 ancestor_with_layer
? ancestor_with_layer
->layer() : NULL
;
1100 if (!ancestor_layer
)
1103 if (child
->layer() && target
->layer()) {
1104 if (direction
== STACK_ABOVE
)
1105 ancestor_layer
->StackAbove(child
->layer(), target
->layer());
1107 ancestor_layer
->StackBelow(child
->layer(), target
->layer());
1110 typedef std::vector
<ui::Layer
*> Layers
;
1112 GetLayersToStack(child
, &layers
);
1116 ui::Layer
* target_layer
;
1117 if (direction
== STACK_ABOVE
) {
1119 FindStackingTargetLayer
<Windows::const_reverse_iterator
>(target
, child
);
1122 FindStackingTargetLayer
<Windows::const_iterator
>(target
, child
);
1125 if (!target_layer
) {
1126 if (direction
== STACK_ABOVE
) {
1127 for (Layers::const_reverse_iterator i
= layers
.rbegin(),
1128 rend
= layers
.rend(); i
!= rend
; ++i
) {
1129 ancestor_layer
->StackAtBottom(*i
);
1132 for (Layers::const_iterator i
= layers
.begin(); i
!= layers
.end(); ++i
)
1133 ancestor_layer
->StackAtTop(*i
);
1138 if (direction
== STACK_ABOVE
) {
1139 for (Layers::const_reverse_iterator i
= layers
.rbegin(),
1140 rend
= layers
.rend(); i
!= rend
; ++i
) {
1141 ancestor_layer
->StackAbove(*i
, target_layer
);
1144 for (Layers::const_iterator i
= layers
.begin(); i
!= layers
.end(); ++i
)
1145 ancestor_layer
->StackBelow(*i
, target_layer
);
1149 void Window::OnStackingChanged() {
1150 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWindowStackingChanged(this));
1153 void Window::NotifyRemovingFromRootWindow(Window
* new_root
) {
1154 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1155 OnWindowRemovingFromRootWindow(this, new_root
));
1156 for (Window::Windows::const_iterator it
= children_
.begin();
1157 it
!= children_
.end(); ++it
) {
1158 (*it
)->NotifyRemovingFromRootWindow(new_root
);
1162 void Window::NotifyAddedToRootWindow() {
1163 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1164 OnWindowAddedToRootWindow(this));
1165 for (Window::Windows::const_iterator it
= children_
.begin();
1166 it
!= children_
.end(); ++it
) {
1167 (*it
)->NotifyAddedToRootWindow();
1171 void Window::NotifyWindowHierarchyChange(
1172 const WindowObserver::HierarchyChangeParams
& params
) {
1173 params
.target
->NotifyWindowHierarchyChangeDown(params
);
1174 switch (params
.phase
) {
1175 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
:
1176 if (params
.old_parent
)
1177 params
.old_parent
->NotifyWindowHierarchyChangeUp(params
);
1179 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
:
1180 if (params
.new_parent
)
1181 params
.new_parent
->NotifyWindowHierarchyChangeUp(params
);
1189 void Window::NotifyWindowHierarchyChangeDown(
1190 const WindowObserver::HierarchyChangeParams
& params
) {
1191 NotifyWindowHierarchyChangeAtReceiver(params
);
1192 for (Window::Windows::const_iterator it
= children_
.begin();
1193 it
!= children_
.end(); ++it
) {
1194 (*it
)->NotifyWindowHierarchyChangeDown(params
);
1198 void Window::NotifyWindowHierarchyChangeUp(
1199 const WindowObserver::HierarchyChangeParams
& params
) {
1200 for (Window
* window
= this; window
; window
= window
->parent())
1201 window
->NotifyWindowHierarchyChangeAtReceiver(params
);
1204 void Window::NotifyWindowHierarchyChangeAtReceiver(
1205 const WindowObserver::HierarchyChangeParams
& params
) {
1206 WindowObserver::HierarchyChangeParams local_params
= params
;
1207 local_params
.receiver
= this;
1209 switch (params
.phase
) {
1210 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
:
1211 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1212 OnWindowHierarchyChanging(local_params
));
1214 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
:
1215 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1216 OnWindowHierarchyChanged(local_params
));
1224 void Window::NotifyWindowVisibilityChanged(aura::Window
* target
,
1226 if (!NotifyWindowVisibilityChangedDown(target
, visible
)) {
1227 return; // |this| has been deleted.
1229 NotifyWindowVisibilityChangedUp(target
, visible
);
1232 bool Window::NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
1234 // |this| may be deleted during a call to OnWindowVisibilityChanged() on one
1235 // of the observers. We create an local observer for that. In that case we
1236 // exit without further access to any members.
1237 WindowTracker tracker
;
1239 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1240 OnWindowVisibilityChanged(target
, visible
));
1241 return tracker
.Contains(this);
1244 bool Window::NotifyWindowVisibilityChangedDown(aura::Window
* target
,
1246 if (!NotifyWindowVisibilityChangedAtReceiver(target
, visible
))
1247 return false; // |this| was deleted.
1248 std::set
<const Window
*> child_already_processed
;
1249 bool child_destroyed
= false;
1251 child_destroyed
= false;
1252 for (Window::Windows::const_iterator it
= children_
.begin();
1253 it
!= children_
.end(); ++it
) {
1254 if (!child_already_processed
.insert(*it
).second
)
1256 if (!(*it
)->NotifyWindowVisibilityChangedDown(target
, visible
)) {
1257 // |*it| was deleted, |it| is invalid and |children_| has changed.
1258 // We exit the current for-loop and enter a new one.
1259 child_destroyed
= true;
1263 } while (child_destroyed
);
1267 void Window::NotifyWindowVisibilityChangedUp(aura::Window
* target
,
1269 // Start with the parent as we already notified |this|
1270 // in NotifyWindowVisibilityChangedDown.
1271 for (Window
* window
= parent(); window
; window
= window
->parent()) {
1272 bool ret
= window
->NotifyWindowVisibilityChangedAtReceiver(target
, visible
);
1277 void Window::NotifyAncestorWindowTransformed(Window
* source
) {
1278 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1279 OnAncestorWindowTransformed(source
, this));
1280 for (Window::Windows::const_iterator it
= children_
.begin();
1281 it
!= children_
.end(); ++it
) {
1282 (*it
)->NotifyAncestorWindowTransformed(source
);
1286 void Window::OnWindowBoundsChanged(const gfx::Rect
& old_bounds
) {
1287 bounds_
= layer()->bounds();
1288 if (parent_
&& !parent_
->layer()) {
1289 gfx::Vector2d offset
;
1290 aura::Window
* ancestor_with_layer
= parent_
->GetAncestorWithLayer(&offset
);
1291 if (ancestor_with_layer
)
1292 bounds_
.Offset(-offset
);
1295 if (layout_manager_
)
1296 layout_manager_
->OnWindowResized();
1298 delegate_
->OnBoundsChanged(old_bounds
, bounds());
1299 FOR_EACH_OBSERVER(WindowObserver
,
1301 OnWindowBoundsChanged(this, old_bounds
, bounds()));
1304 bool Window::CleanupGestureState() {
1305 bool state_modified
= false;
1306 state_modified
|= ui::GestureRecognizer::Get()->CancelActiveTouches(this);
1308 ui::GestureRecognizer::Get()->CleanupStateForConsumer(this);
1309 for (Window::Windows::iterator iter
= children_
.begin();
1310 iter
!= children_
.end();
1312 state_modified
|= (*iter
)->CleanupGestureState();
1314 return state_modified
;
1317 void Window::OnPaintLayer(const ui::PaintContext
& context
) {
1321 void Window::OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) {
1323 FOR_EACH_OBSERVER(WindowObserver
,
1325 OnDelegatedFrameDamage(this, damage_rect_in_dip
));
1328 base::Closure
Window::PrepareForLayerBoundsChange() {
1329 return base::Bind(&Window::OnWindowBoundsChanged
, base::Unretained(this),
1333 bool Window::CanAcceptEvent(const ui::Event
& event
) {
1334 // The client may forbid certain windows from receiving events at a given
1336 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
1337 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
1340 // We need to make sure that a touch cancel event and any gesture events it
1341 // creates can always reach the window. This ensures that we receive a valid
1342 // touch / gesture stream.
1343 if (event
.IsEndingEvent())
1349 // The top-most window can always process an event.
1353 // For located events (i.e. mouse, touch etc.), an assumption is made that
1354 // windows that don't have a default event-handler cannot process the event
1355 // (see more in GetWindowForPoint()). This assumption is not made for key
1357 return event
.IsKeyEvent() || target_handler();
1360 ui::EventTarget
* Window::GetParentTarget() {
1361 if (IsRootWindow()) {
1362 return client::GetEventClient(this) ?
1363 client::GetEventClient(this)->GetToplevelEventTarget() :
1369 scoped_ptr
<ui::EventTargetIterator
> Window::GetChildIterator() const {
1370 return make_scoped_ptr(new ui::EventTargetIteratorImpl
<Window
>(children()));
1373 ui::EventTargeter
* Window::GetEventTargeter() {
1374 return targeter_
.get();
1377 void Window::ConvertEventToTarget(ui::EventTarget
* target
,
1378 ui::LocatedEvent
* event
) {
1379 event
->ConvertLocationToTarget(this,
1380 static_cast<Window
*>(target
));
1383 void Window::UpdateLayerName() {
1384 #if !defined(NDEBUG)
1387 std::string
layer_name(name_
);
1388 if (layer_name
.empty())
1389 layer_name
= "Unnamed Window";
1392 layer_name
+= " " + base::IntToString(id_
);
1394 layer()->set_name(layer_name
);
1398 const Window
* Window::GetAncestorWithLayer(gfx::Vector2d
* offset
) const {
1399 for (const aura::Window
* window
= this; window
; window
= window
->parent()) {
1400 if (window
->layer())
1403 *offset
+= window
->bounds().OffsetFromOrigin();
1406 *offset
= gfx::Vector2d();