Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: services/view_manager/server_view.cc

Issue 1082943002: Adds a ServerViewObserver interface (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: cleanup Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/view_manager/server_view.h" 5 #include "services/view_manager/server_view.h"
6 6
7 #include <inttypes.h> 7 #include <inttypes.h>
8 8
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "services/view_manager/server_view_delegate.h" 10 #include "services/view_manager/server_view_delegate.h"
11 #include "services/view_manager/server_view_observer.h"
11 12
12 namespace view_manager { 13 namespace view_manager {
13 14
14 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id) 15 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id)
15 : delegate_(delegate), 16 : delegate_(delegate),
16 id_(id), 17 id_(id),
17 parent_(nullptr), 18 parent_(nullptr),
18 visible_(false), 19 visible_(false),
19 opacity_(1) { 20 opacity_(1) {
20 DCHECK(delegate); // Must provide a delegate. 21 DCHECK(delegate); // Must provide a delegate.
21 } 22 }
22 23
23 ServerView::~ServerView() { 24 ServerView::~ServerView() {
24 delegate_->OnWillDestroyView(this); 25 delegate_->PrepareToDestroyView(this);
26 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnWillDestroyView(this));
25 27
26 while (!children_.empty()) 28 while (!children_.empty())
27 children_.front()->parent()->Remove(children_.front()); 29 children_.front()->parent()->Remove(children_.front());
28 30
29 if (parent_) 31 if (parent_)
30 parent_->Remove(this); 32 parent_->Remove(this);
31 33
32 delegate_->OnViewDestroyed(this); 34 FOR_EACH_OBSERVER(ServerViewObserver, observers_, OnViewDestroyed(this));
35 }
36
37 void ServerView::AddObserver(ServerViewObserver* observer) {
38 observers_.AddObserver(observer);
39 }
40
41 void ServerView::RemoveObserver(ServerViewObserver* observer) {
42 observers_.RemoveObserver(observer);
33 } 43 }
34 44
35 void ServerView::Add(ServerView* child) { 45 void ServerView::Add(ServerView* child) {
36 // We assume validation checks happened already. 46 // We assume validation checks happened already.
37 DCHECK(child); 47 DCHECK(child);
38 DCHECK(child != this); 48 DCHECK(child != this);
39 DCHECK(!child->Contains(this)); 49 DCHECK(!child->Contains(this));
40 if (child->parent() == this) { 50 if (child->parent() == this) {
41 if (children_.size() == 1) 51 if (children_.size() == 1)
42 return; // Already in the right position. 52 return; // Already in the right position.
43 Reorder(child, children_.back(), mojo::ORDER_DIRECTION_ABOVE); 53 Reorder(child, children_.back(), mojo::ORDER_DIRECTION_ABOVE);
44 return; 54 return;
45 } 55 }
46 56
47 ServerView* old_parent = child->parent(); 57 ServerView* old_parent = child->parent();
48 child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent); 58 child->delegate_->PrepareToChangeViewHierarchy(child, this, old_parent);
59 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
60 OnWillChangeViewHierarchy(child, this, old_parent));
61
49 if (child->parent()) 62 if (child->parent())
50 child->parent()->RemoveImpl(child); 63 child->parent()->RemoveImpl(child);
51 64
52 child->parent_ = this; 65 child->parent_ = this;
53 children_.push_back(child); 66 children_.push_back(child);
54 child->delegate_->OnViewHierarchyChanged(child, this, old_parent); 67 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
68 OnViewHierarchyChanged(child, this, old_parent));
55 } 69 }
56 70
57 void ServerView::Remove(ServerView* child) { 71 void ServerView::Remove(ServerView* child) {
58 // We assume validation checks happened else where. 72 // We assume validation checks happened else where.
59 DCHECK(child); 73 DCHECK(child);
60 DCHECK(child != this); 74 DCHECK(child != this);
61 DCHECK(child->parent() == this); 75 DCHECK(child->parent() == this);
62 76
63 child->delegate_->OnWillChangeViewHierarchy(child, NULL, this); 77 child->delegate_->PrepareToChangeViewHierarchy(child, NULL, this);
78 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
79 OnWillChangeViewHierarchy(child, nullptr, this));
64 RemoveImpl(child); 80 RemoveImpl(child);
65 child->delegate_->OnViewHierarchyChanged(child, NULL, this); 81 FOR_EACH_OBSERVER(ServerViewObserver, child->observers_,
82 OnViewHierarchyChanged(child, nullptr, this));
66 } 83 }
67 84
68 void ServerView::Reorder(ServerView* child, 85 void ServerView::Reorder(ServerView* child,
69 ServerView* relative, 86 ServerView* relative,
70 mojo::OrderDirection direction) { 87 mojo::OrderDirection direction) {
71 // We assume validation checks happened else where. 88 // We assume validation checks happened else where.
72 DCHECK(child); 89 DCHECK(child);
73 DCHECK(child->parent() == this); 90 DCHECK(child->parent() == this);
74 DCHECK_GT(children_.size(), 1u); 91 DCHECK_GT(children_.size(), 1u);
75 children_.erase(std::find(children_.begin(), children_.end(), child)); 92 children_.erase(std::find(children_.begin(), children_.end(), child));
76 Views::iterator i = std::find(children_.begin(), children_.end(), relative); 93 Views::iterator i = std::find(children_.begin(), children_.end(), relative);
77 if (direction == mojo::ORDER_DIRECTION_ABOVE) { 94 if (direction == mojo::ORDER_DIRECTION_ABOVE) {
78 DCHECK(i != children_.end()); 95 DCHECK(i != children_.end());
79 children_.insert(++i, child); 96 children_.insert(++i, child);
80 } else if (direction == mojo::ORDER_DIRECTION_BELOW) { 97 } else if (direction == mojo::ORDER_DIRECTION_BELOW) {
81 DCHECK(i != children_.end()); 98 DCHECK(i != children_.end());
82 children_.insert(i, child); 99 children_.insert(i, child);
83 } 100 }
84 delegate_->OnViewReordered(this, relative, direction); 101 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
102 OnViewReordered(this, relative, direction));
85 } 103 }
86 104
87 void ServerView::SetBounds(const gfx::Rect& bounds) { 105 void ServerView::SetBounds(const gfx::Rect& bounds) {
88 if (bounds_ == bounds) 106 if (bounds_ == bounds)
89 return; 107 return;
90 108
91 const gfx::Rect old_bounds = bounds_; 109 const gfx::Rect old_bounds = bounds_;
92 bounds_ = bounds; 110 bounds_ = bounds;
93 delegate_->OnViewBoundsChanged(this, old_bounds, bounds); 111 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
112 OnViewBoundsChanged(this, old_bounds, bounds));
94 } 113 }
95 114
96 const ServerView* ServerView::GetRoot() const { 115 const ServerView* ServerView::GetRoot() const {
97 const ServerView* view = this; 116 const ServerView* view = this;
98 while (view && view->parent()) 117 while (view && view->parent())
99 view = view->parent(); 118 view = view->parent();
100 return view; 119 return view;
101 } 120 }
102 121
103 std::vector<const ServerView*> ServerView::GetChildren() const { 122 std::vector<const ServerView*> ServerView::GetChildren() const {
(...skipping 14 matching lines...) Expand all
118 if (parent == this) 137 if (parent == this)
119 return true; 138 return true;
120 } 139 }
121 return false; 140 return false;
122 } 141 }
123 142
124 void ServerView::SetVisible(bool value) { 143 void ServerView::SetVisible(bool value) {
125 if (visible_ == value) 144 if (visible_ == value)
126 return; 145 return;
127 146
128 delegate_->OnWillChangeViewVisibility(this); 147 delegate_->PrepareToChangeViewVisibility(this);
148 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
149 OnWillChangeViewVisibility(this));
129 visible_ = value; 150 visible_ = value;
130 } 151 }
131 152
132 void ServerView::SetOpacity(float value) { 153 void ServerView::SetOpacity(float value) {
133 if (value == opacity_) 154 if (value == opacity_)
134 return; 155 return;
135 opacity_ = value; 156 opacity_ = value;
136 delegate_->OnScheduleViewPaint(this); 157 delegate_->OnScheduleViewPaint(this);
137 } 158 }
138 159
(...skipping 16 matching lines...) Expand all
155 // no change. 176 // no change.
156 return; 177 return;
157 } 178 }
158 179
159 if (value) { 180 if (value) {
160 properties_[name] = *value; 181 properties_[name] = *value;
161 } else if (it != properties_.end()) { 182 } else if (it != properties_.end()) {
162 properties_.erase(it); 183 properties_.erase(it);
163 } 184 }
164 185
165 delegate_->OnViewSharedPropertyChanged(this, name, value); 186 FOR_EACH_OBSERVER(ServerViewObserver, observers_,
187 OnViewSharedPropertyChanged(this, name, value));
166 } 188 }
167 189
168 bool ServerView::IsDrawn(const ServerView* root) const { 190 bool ServerView::IsDrawn(const ServerView* root) const {
169 if (!root->visible_) 191 if (!root->visible_)
170 return false; 192 return false;
171 const ServerView* view = this; 193 const ServerView* view = this;
172 while (view && view != root && view->visible_) 194 while (view && view != root && view->visible_)
173 view = view->parent_; 195 view = view->parent_;
174 return view == root; 196 return view == root;
175 } 197 }
176 198
177 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) { 199 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) {
178 surface_id_ = surface_id; 200 surface_id_ = surface_id;
179 delegate_->OnViewSurfaceIdChanged(this); 201 delegate_->OnScheduleViewPaint(this);
180 } 202 }
181 203
182 #if !defined(NDEBUG) 204 #if !defined(NDEBUG)
183 std::string ServerView::GetDebugWindowHierarchy() const { 205 std::string ServerView::GetDebugWindowHierarchy() const {
184 std::string result; 206 std::string result;
185 BuildDebugInfo(std::string(), &result); 207 BuildDebugInfo(std::string(), &result);
186 return result; 208 return result;
187 } 209 }
188 210
189 void ServerView::BuildDebugInfo(const std::string& depth, 211 void ServerView::BuildDebugInfo(const std::string& depth,
190 std::string* result) const { 212 std::string* result) const {
191 *result += base::StringPrintf( 213 *result += base::StringPrintf(
192 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64 "\n", 214 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64 "\n",
193 depth.c_str(), static_cast<int>(id_.connection_id), 215 depth.c_str(), static_cast<int>(id_.connection_id),
194 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(), 216 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(),
195 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id); 217 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id);
196 for (const ServerView* child : children_) 218 for (const ServerView* child : children_)
197 child->BuildDebugInfo(depth + " ", result); 219 child->BuildDebugInfo(depth + " ", result);
198 } 220 }
199 #endif 221 #endif
200 222
201 void ServerView::RemoveImpl(ServerView* view) { 223 void ServerView::RemoveImpl(ServerView* view) {
202 view->parent_ = NULL; 224 view->parent_ = NULL;
203 children_.erase(std::find(children_.begin(), children_.end(), view)); 225 children_.erase(std::find(children_.begin(), children_.end(), view));
204 } 226 }
205 227
206 } // namespace view_manager 228 } // namespace view_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698