OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "content/browser/web_contents/aura/native_view_screen_bounds_observer.h " | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "content/browser/web_contents/web_contents_view_aura.h" | |
10 #include "ui/aura/test/aura_test_base.h" | |
11 #include "ui/aura/window.h" | |
12 | |
13 | |
14 namespace content { | |
15 | |
16 NativeViewScreenBoundsObserver::NativeViewScreenBoundsObserver() | |
17 : delegate_(NULL) { | |
18 } | |
19 | |
20 void NativeViewScreenBoundsObserver::Init( | |
21 NativeViewScreenBoundsObserverDelegate* delegate) { | |
22 DCHECK(delegate); | |
23 delegate_ = delegate; | |
24 delegate_->AddDelegateWindowObserver(this); | |
25 std::set<aura::Window*> ancestors = GenerateAncestors( | |
26 delegate_->GetDelegateWindow()); | |
27 if (delegate_->GetDelegateWindow()) | |
28 ancestors.insert(delegate_->GetDelegateWindow()); | |
29 AddToObserved(ancestors); | |
30 } | |
31 | |
32 | |
33 NativeViewScreenBoundsObserver::~NativeViewScreenBoundsObserver() { | |
34 RemoveFromObserved(observed_); | |
35 delegate_->RemoveRootWindowObserver(this); | |
36 delegate_->RemoveDelegateWindowObserver(this); | |
37 } | |
38 | |
39 void NativeViewScreenBoundsObserver::OnWindowParentChanged( | |
40 aura::Window* window, | |
41 aura::Window* parent) { | |
42 std::set<aura::Window*> ancestors = GenerateAncestors( | |
43 delegate_->GetDelegateWindow()); | |
44 if (delegate_->GetDelegateWindow()) | |
45 ancestors.insert(delegate_->GetDelegateWindow()); | |
46 std::set<aura::Window*> difference; | |
47 std::set_difference( | |
48 observed_.begin(), | |
49 observed_.end(), | |
50 ancestors.begin(), | |
51 ancestors.end(), | |
52 std::inserter(difference, difference.end())); | |
53 RemoveFromObserved(difference); | |
54 | |
55 difference.clear(); | |
56 std::set_difference( | |
57 ancestors.begin(), | |
58 ancestors.end(), | |
59 observed_.begin(), | |
60 observed_.end(), | |
61 std::inserter(difference, difference.end())); | |
62 AddToObserved(difference); | |
63 } | |
64 | |
65 void NativeViewScreenBoundsObserver::OnWindowBoundsChanged( | |
66 aura::Window* window, | |
67 const gfx::Rect& old_bounds, | |
68 const gfx::Rect& new_bounds) { | |
69 if (old_bounds.size() == new_bounds.size()) { | |
70 delegate_->OnScreenPositionChanged(); | |
71 } else { | |
72 delegate_->OnScreenBoundsChanged(); | |
73 } | |
74 } | |
75 | |
76 void NativeViewScreenBoundsObserver::OnWindowAddedToRootWindow( | |
77 aura::Window* window) { | |
78 if (window == delegate_->GetDelegateWindow()) | |
79 delegate_->AddRootWindowObserver(this); | |
80 } | |
81 | |
82 void NativeViewScreenBoundsObserver::OnWindowRemovingFromRootWindow( | |
83 aura::Window* window) { | |
84 if (window == delegate_->GetDelegateWindow()) | |
85 delegate_->RemoveRootWindowObserver(this); | |
86 } | |
87 | |
88 void NativeViewScreenBoundsObserver::OnRootWindowHostMoved( | |
89 const aura::RootWindow* root, | |
90 const gfx::Point& new_origin) { | |
91 // This is for the desktop case (i.e. Aura desktop). | |
92 delegate_->OnScreenPositionChanged(); | |
93 } | |
94 | |
95 std::set<aura::Window*> NativeViewScreenBoundsObserver::GenerateAncestors( | |
sadrul
2013/11/08 02:16:26
Call this CollectAllWindowsToRoot (or something li
rharrison
2013/11/12 22:16:29
Done.
| |
96 aura::Window* window) { | |
97 std::set<aura::Window*> ret_val; | |
98 while (window && window->parent()) { | |
99 ret_val.insert(window->parent()); | |
100 window = window->parent(); | |
101 } | |
102 return ret_val; | |
103 } | |
104 | |
105 void NativeViewScreenBoundsObserver::AddToObserved( | |
106 const std::set<aura::Window*>& windows) { | |
107 for (std::set<aura::Window*>::iterator iter = windows.begin(); | |
108 iter != windows.end(); | |
109 ++iter) { | |
110 if (observed_.end() == observed_.find(*iter)) { | |
111 (*iter)->AddObserver(this); | |
112 observed_.insert(*iter); | |
113 } | |
114 } | |
115 } | |
116 | |
117 void NativeViewScreenBoundsObserver::RemoveFromObserved( | |
118 const std::set<aura::Window*>& windows) { | |
119 // Since windows can possibly be |observer_| we cannot blindly iterate and | |
120 // delete, since const & means that |windows| is a reference to | |
121 // |observer_|. Thus if we erase an entry from |observed_| it may be erased | |
122 // from |windows| which we are iterating. | |
123 for (std::set<aura::Window*>::iterator iter = windows.begin(); | |
124 iter != windows.end();) { | |
125 std::set<aura::Window*>::iterator next = iter; | |
126 next++; | |
127 if (observed_.end() != observed_.find(*iter)) { | |
128 (*iter)->RemoveObserver(this); | |
129 observed_.erase(*iter); | |
130 } | |
131 iter = next; | |
132 } | |
133 } | |
134 | |
135 } // namespace content | |
OLD | NEW |