OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/services/view_manager/server_view.h" | |
6 | |
7 #include "base/strings/stringprintf.h" | |
8 #include "mojo/services/view_manager/server_view_delegate.h" | |
9 | |
10 namespace mojo { | |
11 namespace service { | |
12 | |
13 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id) | |
14 : delegate_(delegate), | |
15 id_(id), | |
16 parent_(nullptr), | |
17 visible_(false), | |
18 opacity_(1) { | |
19 DCHECK(delegate); // Must provide a delegate. | |
20 } | |
21 | |
22 ServerView::~ServerView() { | |
23 delegate_->OnWillDestroyView(this); | |
24 | |
25 while (!children_.empty()) | |
26 children_.front()->parent()->Remove(children_.front()); | |
27 | |
28 if (parent_) | |
29 parent_->Remove(this); | |
30 | |
31 delegate_->OnViewDestroyed(this); | |
32 } | |
33 | |
34 void ServerView::Add(ServerView* child) { | |
35 // We assume validation checks happened already. | |
36 DCHECK(child); | |
37 DCHECK(child != this); | |
38 DCHECK(!child->Contains(this)); | |
39 if (child->parent() == this) { | |
40 if (children_.size() == 1) | |
41 return; // Already in the right position. | |
42 Reorder(child, children_.back(), ORDER_DIRECTION_ABOVE); | |
43 return; | |
44 } | |
45 | |
46 ServerView* old_parent = child->parent(); | |
47 child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent); | |
48 if (child->parent()) | |
49 child->parent()->RemoveImpl(child); | |
50 | |
51 child->parent_ = this; | |
52 children_.push_back(child); | |
53 child->delegate_->OnViewHierarchyChanged(child, this, old_parent); | |
54 } | |
55 | |
56 void ServerView::Remove(ServerView* child) { | |
57 // We assume validation checks happened else where. | |
58 DCHECK(child); | |
59 DCHECK(child != this); | |
60 DCHECK(child->parent() == this); | |
61 | |
62 child->delegate_->OnWillChangeViewHierarchy(child, NULL, this); | |
63 RemoveImpl(child); | |
64 child->delegate_->OnViewHierarchyChanged(child, NULL, this); | |
65 } | |
66 | |
67 void ServerView::Reorder(ServerView* child, | |
68 ServerView* relative, | |
69 OrderDirection direction) { | |
70 // We assume validation checks happened else where. | |
71 DCHECK(child); | |
72 DCHECK(child->parent() == this); | |
73 DCHECK_GT(children_.size(), 1u); | |
74 children_.erase(std::find(children_.begin(), children_.end(), child)); | |
75 Views::iterator i = std::find(children_.begin(), children_.end(), relative); | |
76 if (direction == ORDER_DIRECTION_ABOVE) { | |
77 DCHECK(i != children_.end()); | |
78 children_.insert(++i, child); | |
79 } else if (direction == ORDER_DIRECTION_BELOW) { | |
80 DCHECK(i != children_.end()); | |
81 children_.insert(i, child); | |
82 } | |
83 delegate_->OnViewReordered(this, relative, direction); | |
84 } | |
85 | |
86 void ServerView::SetBounds(const gfx::Rect& bounds) { | |
87 if (bounds_ == bounds) | |
88 return; | |
89 | |
90 const gfx::Rect old_bounds = bounds_; | |
91 bounds_ = bounds; | |
92 delegate_->OnViewBoundsChanged(this, old_bounds, bounds); | |
93 } | |
94 | |
95 const ServerView* ServerView::GetRoot() const { | |
96 const ServerView* view = this; | |
97 while (view && view->parent()) | |
98 view = view->parent(); | |
99 return view; | |
100 } | |
101 | |
102 std::vector<const ServerView*> ServerView::GetChildren() const { | |
103 std::vector<const ServerView*> children; | |
104 children.reserve(children_.size()); | |
105 for (size_t i = 0; i < children_.size(); ++i) | |
106 children.push_back(children_[i]); | |
107 return children; | |
108 } | |
109 | |
110 std::vector<ServerView*> ServerView::GetChildren() { | |
111 // TODO(sky): rename to children() and fix return type. | |
112 return children_; | |
113 } | |
114 | |
115 bool ServerView::Contains(const ServerView* view) const { | |
116 for (const ServerView* parent = view; parent; parent = parent->parent_) { | |
117 if (parent == this) | |
118 return true; | |
119 } | |
120 return false; | |
121 } | |
122 | |
123 void ServerView::SetVisible(bool value) { | |
124 if (visible_ == value) | |
125 return; | |
126 | |
127 delegate_->OnWillChangeViewVisibility(this); | |
128 visible_ = value; | |
129 } | |
130 | |
131 void ServerView::SetOpacity(float value) { | |
132 if (value == opacity_) | |
133 return; | |
134 opacity_ = value; | |
135 delegate_->OnScheduleViewPaint(this); | |
136 } | |
137 | |
138 void ServerView::SetProperty(const std::string& name, | |
139 const std::vector<uint8_t>* value) { | |
140 auto it = properties_.find(name); | |
141 if (it != properties_.end()) { | |
142 if (value && it->second == *value) | |
143 return; | |
144 } else if (!value) { | |
145 // This property isn't set in |properties_| and |value| is NULL, so there's | |
146 // no change. | |
147 return; | |
148 } | |
149 | |
150 if (value) { | |
151 properties_[name] = *value; | |
152 } else if (it != properties_.end()) { | |
153 properties_.erase(it); | |
154 } | |
155 | |
156 delegate_->OnViewSharedPropertyChanged(this, name, value); | |
157 } | |
158 | |
159 bool ServerView::IsDrawn(const ServerView* root) const { | |
160 if (!root->visible_) | |
161 return false; | |
162 const ServerView* view = this; | |
163 while (view && view != root && view->visible_) | |
164 view = view->parent_; | |
165 return view == root; | |
166 } | |
167 | |
168 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) { | |
169 surface_id_ = surface_id; | |
170 delegate_->OnViewSurfaceIdChanged(this); | |
171 } | |
172 | |
173 #if !defined(NDEBUG) | |
174 std::string ServerView::GetDebugWindowHierarchy() const { | |
175 std::string result; | |
176 BuildDebugInfo(std::string(), &result); | |
177 return result; | |
178 } | |
179 | |
180 void ServerView::BuildDebugInfo(const std::string& depth, | |
181 std::string* result) const { | |
182 *result += base::StringPrintf( | |
183 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%ld\n", | |
184 depth.c_str(), static_cast<int>(id_.connection_id), | |
185 static_cast<int>(id_.view_id), visible_ ? "true" : "false", bounds_.x(), | |
186 bounds_.y(), bounds_.width(), bounds_.height(), surface_id_.id); | |
187 for (const ServerView* child : children_) | |
188 child->BuildDebugInfo(depth + " ", result); | |
189 } | |
190 #endif | |
191 | |
192 void ServerView::RemoveImpl(ServerView* view) { | |
193 view->parent_ = NULL; | |
194 children_.erase(std::find(children_.begin(), children_.end(), view)); | |
195 } | |
196 | |
197 } // namespace service | |
198 } // namespace mojo | |
OLD | NEW |