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_(base::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 base::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 {
444 return layer() ? layer()->GetTargetBounds() : bounds();
447 void Window::SchedulePaintInRect(const gfx::Rect
& rect
) {
448 layer()->SchedulePaint(rect
);
451 void Window::StackChildAtTop(Window
* child
) {
452 if (children_
.size() <= 1 || child
== children_
.back())
453 return; // In the front already.
454 StackChildAbove(child
, children_
.back());
457 void Window::StackChildAbove(Window
* child
, Window
* target
) {
458 StackChildRelativeTo(child
, target
, STACK_ABOVE
);
461 void Window::StackChildAtBottom(Window
* child
) {
462 if (children_
.size() <= 1 || child
== children_
.front())
463 return; // At the bottom already.
464 StackChildBelow(child
, children_
.front());
467 void Window::StackChildBelow(Window
* child
, Window
* target
) {
468 StackChildRelativeTo(child
, target
, STACK_BELOW
);
471 void Window::AddChild(Window
* child
) {
472 DCHECK(layer()) << "Parent has not been Init()ed yet.";
473 DCHECK(child
->layer()) << "Child has not been Init()ed yt.";
474 WindowObserver::HierarchyChangeParams params
;
475 params
.target
= child
;
476 params
.new_parent
= this;
477 params
.old_parent
= child
->parent();
478 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
;
479 NotifyWindowHierarchyChange(params
);
481 Window
* old_root
= child
->GetRootWindow();
483 DCHECK(std::find(children_
.begin(), children_
.end(), child
) ==
486 child
->parent()->RemoveChildImpl(child
, this);
488 child
->parent_
= this;
489 layer()->Add(child
->layer());
491 children_
.push_back(child
);
493 layout_manager_
->OnWindowAddedToLayout(child
);
494 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWindowAdded(child
));
495 child
->OnParentChanged();
497 Window
* root_window
= GetRootWindow();
498 if (root_window
&& old_root
!= root_window
) {
499 root_window
->GetHost()->dispatcher()->OnWindowAddedToRootWindow(child
);
500 child
->NotifyAddedToRootWindow();
503 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
;
504 NotifyWindowHierarchyChange(params
);
507 void Window::RemoveChild(Window
* child
) {
508 WindowObserver::HierarchyChangeParams params
;
509 params
.target
= child
;
510 params
.new_parent
= NULL
;
511 params
.old_parent
= this;
512 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
;
513 NotifyWindowHierarchyChange(params
);
515 RemoveChildImpl(child
, NULL
);
517 params
.phase
= WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
;
518 NotifyWindowHierarchyChange(params
);
521 bool Window::Contains(const Window
* other
) const {
522 for (const Window
* parent
= other
; parent
; parent
= parent
->parent_
) {
529 Window
* Window::GetChildById(int id
) {
530 return const_cast<Window
*>(const_cast<const Window
*>(this)->GetChildById(id
));
533 const Window
* Window::GetChildById(int id
) const {
534 Windows::const_iterator i
;
535 for (i
= children_
.begin(); i
!= children_
.end(); ++i
) {
536 if ((*i
)->id() == id
)
538 const Window
* result
= (*i
)->GetChildById(id
);
546 void Window::ConvertPointToTarget(const Window
* source
,
547 const Window
* target
,
551 if (source
->GetRootWindow() != target
->GetRootWindow()) {
552 client::ScreenPositionClient
* source_client
=
553 client::GetScreenPositionClient(source
->GetRootWindow());
554 // |source_client| can be NULL in tests.
556 source_client
->ConvertPointToScreen(source
, point
);
558 client::ScreenPositionClient
* target_client
=
559 client::GetScreenPositionClient(target
->GetRootWindow());
560 // |target_client| can be NULL in tests.
562 target_client
->ConvertPointFromScreen(target
, point
);
564 ui::Layer::ConvertPointToLayer(source
->layer(), target
->layer(), point
);
569 void Window::ConvertRectToTarget(const Window
* source
,
570 const Window
* target
,
573 gfx::Point origin
= rect
->origin();
574 ConvertPointToTarget(source
, target
, &origin
);
575 rect
->set_origin(origin
);
578 void Window::MoveCursorTo(const gfx::Point
& point_in_window
) {
579 Window
* root_window
= GetRootWindow();
581 gfx::Point
point_in_root(point_in_window
);
582 ConvertPointToTarget(this, root_window
, &point_in_root
);
583 root_window
->GetHost()->MoveCursorTo(point_in_root
);
586 gfx::NativeCursor
Window::GetCursor(const gfx::Point
& point
) const {
587 return delegate_
? delegate_
->GetCursor(point
) : gfx::kNullCursor
;
590 void Window::AddObserver(WindowObserver
* observer
) {
591 observer
->OnObservingWindow(this);
592 observers_
.AddObserver(observer
);
595 void Window::RemoveObserver(WindowObserver
* observer
) {
596 observer
->OnUnobservingWindow(this);
597 observers_
.RemoveObserver(observer
);
600 bool Window::HasObserver(const WindowObserver
* observer
) const {
601 return observers_
.HasObserver(observer
);
604 bool Window::ContainsPointInRoot(const gfx::Point
& point_in_root
) const {
605 const Window
* root_window
= GetRootWindow();
608 gfx::Point
local_point(point_in_root
);
609 ConvertPointToTarget(root_window
, this, &local_point
);
610 return gfx::Rect(GetTargetBounds().size()).Contains(local_point
);
613 bool Window::ContainsPoint(const gfx::Point
& local_point
) const {
614 return gfx::Rect(bounds().size()).Contains(local_point
);
617 Window
* Window::GetEventHandlerForPoint(const gfx::Point
& local_point
) {
618 return GetWindowForPoint(local_point
, true, true);
621 Window
* Window::GetTopWindowContainingPoint(const gfx::Point
& local_point
) {
622 return GetWindowForPoint(local_point
, false, false);
625 Window
* Window::GetToplevelWindow() {
626 Window
* topmost_window_with_delegate
= NULL
;
627 for (aura::Window
* window
= this; window
!= NULL
; window
= window
->parent()) {
628 if (window
->delegate())
629 topmost_window_with_delegate
= window
;
631 return topmost_window_with_delegate
;
634 void Window::Focus() {
635 client::FocusClient
* client
= client::GetFocusClient(this);
637 client
->FocusWindow(this);
640 bool Window::HasFocus() const {
641 client::FocusClient
* client
= client::GetFocusClient(this);
642 return client
&& client
->GetFocusedWindow() == this;
645 bool Window::CanFocus() const {
649 // NOTE: as part of focusing the window the ActivationClient may make the
650 // window visible (by way of making a hidden ancestor visible). For this
651 // reason we can't check visibility here and assume the client is doing it.
652 if (!parent_
|| (delegate_
&& !delegate_
->CanFocus()))
655 // The client may forbid certain windows from receiving focus at a given point
657 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
658 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
661 return parent_
->CanFocus();
664 bool Window::CanReceiveEvents() const {
668 // The client may forbid certain windows from receiving events at a given
670 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
671 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
674 return parent_
&& IsVisible() && parent_
->CanReceiveEvents();
677 void Window::SetCapture() {
681 Window
* root_window
= GetRootWindow();
684 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
687 client::GetCaptureClient(root_window
)->SetCapture(this);
690 void Window::ReleaseCapture() {
691 Window
* root_window
= GetRootWindow();
694 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
697 client::GetCaptureClient(root_window
)->ReleaseCapture(this);
700 bool Window::HasCapture() {
701 Window
* root_window
= GetRootWindow();
704 client::CaptureClient
* capture_client
= client::GetCaptureClient(root_window
);
705 return capture_client
&& capture_client
->GetCaptureWindow() == this;
708 void Window::SuppressPaint() {
709 layer()->SuppressPaint();
712 // {Set,Get,Clear}Property are implemented in window_property.h.
714 void Window::SetNativeWindowProperty(const char* key
, void* value
) {
716 key
, key
, NULL
, reinterpret_cast<int64
>(value
), 0);
719 void* Window::GetNativeWindowProperty(const char* key
) const {
720 return reinterpret_cast<void*>(GetPropertyInternal(key
, 0));
723 void Window::OnDeviceScaleFactorChanged(float device_scale_factor
) {
724 ScopedCursorHider
hider(this);
726 delegate_
->OnDeviceScaleFactorChanged(device_scale_factor
);
730 std::string
Window::GetDebugInfo() const {
731 return base::StringPrintf(
732 "%s<%d> bounds(%d, %d, %d, %d) %s %s opacity=%.1f",
733 name().empty() ? "Unknown" : name().c_str(), id(),
734 bounds().x(), bounds().y(), bounds().width(), bounds().height(),
735 visible_
? "WindowVisible" : "WindowHidden",
737 (layer()->GetTargetVisibility() ? "LayerVisible" : "LayerHidden") :
739 layer() ? layer()->opacity() : 1.0f
);
742 void Window::PrintWindowHierarchy(int depth
) const {
743 VLOG(0) << base::StringPrintf(
744 "%*s%s", depth
* 2, "", GetDebugInfo().c_str());
745 for (Windows::const_iterator it
= children_
.begin();
746 it
!= children_
.end(); ++it
) {
748 child
->PrintWindowHierarchy(depth
+ 1);
753 void Window::RemoveOrDestroyChildren() {
754 while (!children_
.empty()) {
755 Window
* child
= children_
[0];
756 if (child
->owned_by_parent_
) {
758 // Deleting the child so remove it from out children_ list.
759 DCHECK(std::find(children_
.begin(), children_
.end(), child
) ==
762 // Even if we can't delete the child, we still need to remove it from the
763 // parent so that relevant bookkeeping (parent_ back-pointers etc) are
770 ///////////////////////////////////////////////////////////////////////////////
773 int64
Window::SetPropertyInternal(const void* key
,
775 PropertyDeallocator deallocator
,
777 int64 default_value
) {
778 int64 old
= GetPropertyInternal(key
, default_value
);
779 if (value
== default_value
) {
780 prop_map_
.erase(key
);
783 prop_value
.name
= name
;
784 prop_value
.value
= value
;
785 prop_value
.deallocator
= deallocator
;
786 prop_map_
[key
] = prop_value
;
788 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
789 OnWindowPropertyChanged(this, key
, old
));
793 int64
Window::GetPropertyInternal(const void* key
,
794 int64 default_value
) const {
795 std::map
<const void*, Value
>::const_iterator iter
= prop_map_
.find(key
);
796 if (iter
== prop_map_
.end())
797 return default_value
;
798 return iter
->second
.value
;
801 bool Window::HitTest(const gfx::Point
& local_point
) {
802 gfx::Rect
local_bounds(bounds().size());
803 if (!delegate_
|| !delegate_
->HasHitTestMask())
804 return local_bounds
.Contains(local_point
);
807 delegate_
->GetHitTestMask(&mask
);
809 SkRegion clip_region
;
810 clip_region
.setRect(local_bounds
.x(), local_bounds
.y(),
811 local_bounds
.width(), local_bounds
.height());
812 SkRegion mask_region
;
813 return mask_region
.setPath(mask
, clip_region
) &&
814 mask_region
.contains(local_point
.x(), local_point
.y());
817 void Window::SetBoundsInternal(const gfx::Rect
& new_bounds
) {
818 gfx::Rect old_bounds
= GetTargetBounds();
820 // Always need to set the layer's bounds -- even if it is to the same thing.
821 // This may cause important side effects such as stopping animation.
822 layer()->SetBounds(new_bounds
);
824 // If we are currently not the layer's delegate, we will not get bounds
825 // changed notification from the layer (this typically happens after animating
826 // hidden). We must notify ourselves.
827 if (layer()->delegate() != this)
828 OnWindowBoundsChanged(old_bounds
);
831 void Window::SetVisible(bool visible
) {
832 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
833 tracked_objects::ScopedTracker
tracking_profile(
834 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 Window::SetVisible"));
836 if ((layer() && visible
== layer()->GetTargetVisibility()) ||
837 (!layer() && visible
== visible_
))
838 return; // No change.
840 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
841 OnWindowVisibilityChanging(this, visible
));
843 client::VisibilityClient
* visibility_client
=
844 client::GetVisibilityClient(this);
845 if (visibility_client
)
846 visibility_client
->UpdateLayerVisibility(this, visible
);
848 layer()->SetVisible(visible
);
851 if (parent_
&& parent_
->layout_manager_
)
852 parent_
->layout_manager_
->OnChildWindowVisibilityChanged(this, visible
);
855 delegate_
->OnWindowTargetVisibilityChanged(visible
);
857 NotifyWindowVisibilityChanged(this, visible
);
860 void Window::SchedulePaint() {
861 SchedulePaintInRect(gfx::Rect(0, 0, bounds().width(), bounds().height()));
864 void Window::Paint(const ui::PaintContext
& context
) {
866 delegate_
->OnPaint(context
);
869 Window
* Window::GetWindowForPoint(const gfx::Point
& local_point
,
870 bool return_tightest
,
871 bool for_event_handling
) {
875 if ((for_event_handling
&& !HitTest(local_point
)) ||
876 (!for_event_handling
&& !ContainsPoint(local_point
)))
879 // Check if I should claim this event and not pass it to my children because
880 // the location is inside my hit test override area. For details, see
881 // set_hit_test_bounds_override_inner().
882 if (for_event_handling
&& !hit_test_bounds_override_inner_
.empty()) {
883 gfx::Rect
inset_local_bounds(gfx::Point(), bounds().size());
884 inset_local_bounds
.Inset(hit_test_bounds_override_inner_
);
885 // We know we're inside the normal local bounds, so if we're outside the
886 // inset bounds we must be in the special hit test override area.
887 DCHECK(HitTest(local_point
));
888 if (!inset_local_bounds
.Contains(local_point
))
889 return delegate_
? this : NULL
;
892 if (!return_tightest
&& delegate_
)
895 for (Windows::const_reverse_iterator it
= children_
.rbegin(),
896 rend
= children_
.rend();
900 if (for_event_handling
) {
901 if (child
->ignore_events_
)
903 // The client may not allow events to be processed by certain subtrees.
904 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
905 if (client
&& !client
->CanProcessEventsWithinSubtree(child
))
907 if (delegate_
&& !delegate_
->ShouldDescendIntoChildForEventHandling(
912 gfx::Point
point_in_child_coords(local_point
);
913 ConvertPointToTarget(this, child
, &point_in_child_coords
);
914 Window
* match
= child
->GetWindowForPoint(point_in_child_coords
,
921 return delegate_
? this : NULL
;
924 void Window::RemoveChildImpl(Window
* child
, Window
* new_parent
) {
926 layout_manager_
->OnWillRemoveWindowFromLayout(child
);
927 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWillRemoveWindow(child
));
928 Window
* root_window
= child
->GetRootWindow();
929 Window
* new_root_window
= new_parent
? new_parent
->GetRootWindow() : NULL
;
930 if (root_window
&& root_window
!= new_root_window
)
931 child
->NotifyRemovingFromRootWindow(new_root_window
);
933 if (child
->OwnsLayer())
934 layer()->Remove(child
->layer());
935 child
->parent_
= NULL
;
936 Windows::iterator i
= std::find(children_
.begin(), children_
.end(), child
);
937 DCHECK(i
!= children_
.end());
939 child
->OnParentChanged();
941 layout_manager_
->OnWindowRemovedFromLayout(child
);
944 void Window::ReparentLayers(ui::Layer
* parent_layer
,
945 const gfx::Vector2d
& offset
) {
947 for (size_t i
= 0; i
< children_
.size(); ++i
) {
948 children_
[i
]->ReparentLayers(
950 offset
+ children_
[i
]->bounds().OffsetFromOrigin());
953 const gfx::Rect
real_bounds(bounds());
954 parent_layer
->Add(layer());
955 gfx::Rect
layer_bounds(layer()->bounds().size());
956 layer_bounds
+= offset
;
957 layer()->SetBounds(layer_bounds
);
958 bounds_
= real_bounds
;
962 void Window::OffsetLayerBounds(const gfx::Vector2d
& offset
) {
964 for (size_t i
= 0; i
< children_
.size(); ++i
)
965 children_
[i
]->OffsetLayerBounds(offset
);
967 gfx::Rect
layer_bounds(layer()->bounds());
968 layer_bounds
+= offset
;
969 layer()->SetBounds(layer_bounds
);
973 void Window::OnParentChanged() {
975 WindowObserver
, observers_
, OnWindowParentChanged(this, parent_
));
978 void Window::StackChildRelativeTo(Window
* child
,
980 StackDirection direction
) {
981 DCHECK_NE(child
, target
);
984 DCHECK_EQ(this, child
->parent());
985 DCHECK_EQ(this, target
->parent());
987 client::WindowStackingClient
* stacking_client
=
988 client::GetWindowStackingClient();
989 if (stacking_client
&&
990 !stacking_client
->AdjustStacking(&child
, &target
, &direction
))
993 const size_t child_i
=
994 std::find(children_
.begin(), children_
.end(), child
) - children_
.begin();
995 const size_t target_i
=
996 std::find(children_
.begin(), children_
.end(), target
) - children_
.begin();
998 // Don't move the child if it is already in the right place.
999 if ((direction
== STACK_ABOVE
&& child_i
== target_i
+ 1) ||
1000 (direction
== STACK_BELOW
&& child_i
+ 1 == target_i
))
1003 const size_t dest_i
=
1004 direction
== STACK_ABOVE
?
1005 (child_i
< target_i
? target_i
: target_i
+ 1) :
1006 (child_i
< target_i
? target_i
- 1 : target_i
);
1007 children_
.erase(children_
.begin() + child_i
);
1008 children_
.insert(children_
.begin() + dest_i
, child
);
1010 StackChildLayerRelativeTo(child
, target
, direction
);
1012 child
->OnStackingChanged();
1015 void Window::StackChildLayerRelativeTo(Window
* child
,
1017 StackDirection direction
) {
1019 if (child
->layer() && target
->layer()) {
1020 if (direction
== STACK_ABOVE
)
1021 layer()->StackAbove(child
->layer(), target
->layer());
1023 layer()->StackBelow(child
->layer(), target
->layer());
1026 typedef std::vector
<ui::Layer
*> Layers
;
1028 GetLayersToStack(child
, &layers
);
1032 ui::Layer
* target_layer
;
1033 if (direction
== STACK_ABOVE
) {
1035 FindStackingTargetLayer
<Windows::const_reverse_iterator
>(target
, child
);
1038 FindStackingTargetLayer
<Windows::const_iterator
>(target
, child
);
1041 if (!target_layer
) {
1042 if (direction
== STACK_ABOVE
) {
1043 for (Layers::const_reverse_iterator i
= layers
.rbegin(),
1044 rend
= layers
.rend(); i
!= rend
; ++i
) {
1045 layer()->StackAtBottom(*i
);
1048 for (Layers::const_iterator i
= layers
.begin(); i
!= layers
.end(); ++i
)
1049 layer()->StackAtTop(*i
);
1054 if (direction
== STACK_ABOVE
) {
1055 for (Layers::const_reverse_iterator i
= layers
.rbegin(),
1056 rend
= layers
.rend(); i
!= rend
; ++i
) {
1057 layer()->StackAbove(*i
, target_layer
);
1060 for (Layers::const_iterator i
= layers
.begin(); i
!= layers
.end(); ++i
)
1061 layer()->StackBelow(*i
, target_layer
);
1065 void Window::OnStackingChanged() {
1066 FOR_EACH_OBSERVER(WindowObserver
, observers_
, OnWindowStackingChanged(this));
1069 void Window::NotifyRemovingFromRootWindow(Window
* new_root
) {
1070 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1071 OnWindowRemovingFromRootWindow(this, new_root
));
1072 for (Window::Windows::const_iterator it
= children_
.begin();
1073 it
!= children_
.end(); ++it
) {
1074 (*it
)->NotifyRemovingFromRootWindow(new_root
);
1078 void Window::NotifyAddedToRootWindow() {
1079 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1080 OnWindowAddedToRootWindow(this));
1081 for (Window::Windows::const_iterator it
= children_
.begin();
1082 it
!= children_
.end(); ++it
) {
1083 (*it
)->NotifyAddedToRootWindow();
1087 void Window::NotifyWindowHierarchyChange(
1088 const WindowObserver::HierarchyChangeParams
& params
) {
1089 params
.target
->NotifyWindowHierarchyChangeDown(params
);
1090 switch (params
.phase
) {
1091 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
:
1092 if (params
.old_parent
)
1093 params
.old_parent
->NotifyWindowHierarchyChangeUp(params
);
1095 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
:
1096 if (params
.new_parent
)
1097 params
.new_parent
->NotifyWindowHierarchyChangeUp(params
);
1105 void Window::NotifyWindowHierarchyChangeDown(
1106 const WindowObserver::HierarchyChangeParams
& params
) {
1107 NotifyWindowHierarchyChangeAtReceiver(params
);
1108 for (Window::Windows::const_iterator it
= children_
.begin();
1109 it
!= children_
.end(); ++it
) {
1110 (*it
)->NotifyWindowHierarchyChangeDown(params
);
1114 void Window::NotifyWindowHierarchyChangeUp(
1115 const WindowObserver::HierarchyChangeParams
& params
) {
1116 for (Window
* window
= this; window
; window
= window
->parent())
1117 window
->NotifyWindowHierarchyChangeAtReceiver(params
);
1120 void Window::NotifyWindowHierarchyChangeAtReceiver(
1121 const WindowObserver::HierarchyChangeParams
& params
) {
1122 WindowObserver::HierarchyChangeParams local_params
= params
;
1123 local_params
.receiver
= this;
1125 switch (params
.phase
) {
1126 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGING
:
1127 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1128 OnWindowHierarchyChanging(local_params
));
1130 case WindowObserver::HierarchyChangeParams::HIERARCHY_CHANGED
:
1131 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1132 OnWindowHierarchyChanged(local_params
));
1140 void Window::NotifyWindowVisibilityChanged(aura::Window
* target
,
1142 if (!NotifyWindowVisibilityChangedDown(target
, visible
)) {
1143 return; // |this| has been deleted.
1145 NotifyWindowVisibilityChangedUp(target
, visible
);
1148 bool Window::NotifyWindowVisibilityChangedAtReceiver(aura::Window
* target
,
1150 // |this| may be deleted during a call to OnWindowVisibilityChanged() on one
1151 // of the observers. We create an local observer for that. In that case we
1152 // exit without further access to any members.
1153 WindowTracker tracker
;
1155 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1156 OnWindowVisibilityChanged(target
, visible
));
1157 return tracker
.Contains(this);
1160 bool Window::NotifyWindowVisibilityChangedDown(aura::Window
* target
,
1162 if (!NotifyWindowVisibilityChangedAtReceiver(target
, visible
))
1163 return false; // |this| was deleted.
1164 std::set
<const Window
*> child_already_processed
;
1165 bool child_destroyed
= false;
1167 child_destroyed
= false;
1168 for (Window::Windows::const_iterator it
= children_
.begin();
1169 it
!= children_
.end(); ++it
) {
1170 if (!child_already_processed
.insert(*it
).second
)
1172 if (!(*it
)->NotifyWindowVisibilityChangedDown(target
, visible
)) {
1173 // |*it| was deleted, |it| is invalid and |children_| has changed.
1174 // We exit the current for-loop and enter a new one.
1175 child_destroyed
= true;
1179 } while (child_destroyed
);
1183 void Window::NotifyWindowVisibilityChangedUp(aura::Window
* target
,
1185 // Start with the parent as we already notified |this|
1186 // in NotifyWindowVisibilityChangedDown.
1187 for (Window
* window
= parent(); window
; window
= window
->parent()) {
1188 bool ret
= window
->NotifyWindowVisibilityChangedAtReceiver(target
, visible
);
1193 void Window::NotifyAncestorWindowTransformed(Window
* source
) {
1194 FOR_EACH_OBSERVER(WindowObserver
, observers_
,
1195 OnAncestorWindowTransformed(source
, this));
1196 for (Window::Windows::const_iterator it
= children_
.begin();
1197 it
!= children_
.end(); ++it
) {
1198 (*it
)->NotifyAncestorWindowTransformed(source
);
1202 void Window::OnWindowBoundsChanged(const gfx::Rect
& old_bounds
) {
1203 bounds_
= layer()->bounds();
1204 if (layout_manager_
)
1205 layout_manager_
->OnWindowResized();
1207 delegate_
->OnBoundsChanged(old_bounds
, bounds());
1208 FOR_EACH_OBSERVER(WindowObserver
,
1210 OnWindowBoundsChanged(this, old_bounds
, bounds()));
1213 bool Window::CleanupGestureState() {
1214 bool state_modified
= false;
1215 state_modified
|= ui::GestureRecognizer::Get()->CancelActiveTouches(this);
1217 ui::GestureRecognizer::Get()->CleanupStateForConsumer(this);
1218 for (Window::Windows::iterator iter
= children_
.begin();
1219 iter
!= children_
.end();
1221 state_modified
|= (*iter
)->CleanupGestureState();
1223 return state_modified
;
1226 void Window::OnPaintLayer(const ui::PaintContext
& context
) {
1230 void Window::OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) {
1232 FOR_EACH_OBSERVER(WindowObserver
,
1234 OnDelegatedFrameDamage(this, damage_rect_in_dip
));
1237 base::Closure
Window::PrepareForLayerBoundsChange() {
1238 return base::Bind(&Window::OnWindowBoundsChanged
, base::Unretained(this),
1242 bool Window::CanAcceptEvent(const ui::Event
& event
) {
1243 // The client may forbid certain windows from receiving events at a given
1245 client::EventClient
* client
= client::GetEventClient(GetRootWindow());
1246 if (client
&& !client
->CanProcessEventsWithinSubtree(this))
1249 // We need to make sure that a touch cancel event and any gesture events it
1250 // creates can always reach the window. This ensures that we receive a valid
1251 // touch / gesture stream.
1252 if (event
.IsEndingEvent())
1258 // The top-most window can always process an event.
1262 // For located events (i.e. mouse, touch etc.), an assumption is made that
1263 // windows that don't have a default event-handler cannot process the event
1264 // (see more in GetWindowForPoint()). This assumption is not made for key
1266 return event
.IsKeyEvent() || target_handler();
1269 ui::EventTarget
* Window::GetParentTarget() {
1270 if (IsRootWindow()) {
1271 return client::GetEventClient(this) ?
1272 client::GetEventClient(this)->GetToplevelEventTarget() :
1278 scoped_ptr
<ui::EventTargetIterator
> Window::GetChildIterator() const {
1279 return make_scoped_ptr(new ui::EventTargetIteratorImpl
<Window
>(children()));
1282 ui::EventTargeter
* Window::GetEventTargeter() {
1283 return targeter_
.get();
1286 void Window::ConvertEventToTarget(ui::EventTarget
* target
,
1287 ui::LocatedEvent
* event
) {
1288 event
->ConvertLocationToTarget(this,
1289 static_cast<Window
*>(target
));
1292 void Window::UpdateLayerName() {
1293 #if !defined(NDEBUG)
1296 std::string
layer_name(name_
);
1297 if (layer_name
.empty())
1298 layer_name
= "Unnamed Window";
1301 layer_name
+= " " + base::IntToString(id_
);
1303 layer()->set_name(layer_name
);