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.
5 #include "mojo/services/view_manager/server_view.h"
9 #include "base/strings/stringprintf.h"
10 #include "mojo/services/view_manager/server_view_delegate.h"
12 namespace view_manager
{
14 ServerView::ServerView(ServerViewDelegate
* delegate
, const ViewId
& id
)
15 : delegate_(delegate
),
20 DCHECK(delegate
); // Must provide a delegate.
23 ServerView::~ServerView() {
24 delegate_
->OnWillDestroyView(this);
26 while (!children_
.empty())
27 children_
.front()->parent()->Remove(children_
.front());
30 parent_
->Remove(this);
32 delegate_
->OnViewDestroyed(this);
35 void ServerView::Add(ServerView
* child
) {
36 // We assume validation checks happened already.
38 DCHECK(child
!= this);
39 DCHECK(!child
->Contains(this));
40 if (child
->parent() == this) {
41 if (children_
.size() == 1)
42 return; // Already in the right position.
43 Reorder(child
, children_
.back(), mojo::ORDER_DIRECTION_ABOVE
);
47 ServerView
* old_parent
= child
->parent();
48 child
->delegate_
->OnWillChangeViewHierarchy(child
, this, old_parent
);
50 child
->parent()->RemoveImpl(child
);
52 child
->parent_
= this;
53 children_
.push_back(child
);
54 child
->delegate_
->OnViewHierarchyChanged(child
, this, old_parent
);
57 void ServerView::Remove(ServerView
* child
) {
58 // We assume validation checks happened else where.
60 DCHECK(child
!= this);
61 DCHECK(child
->parent() == this);
63 child
->delegate_
->OnWillChangeViewHierarchy(child
, NULL
, this);
65 child
->delegate_
->OnViewHierarchyChanged(child
, NULL
, this);
68 void ServerView::Reorder(ServerView
* child
,
70 mojo::OrderDirection direction
) {
71 // We assume validation checks happened else where.
73 DCHECK(child
->parent() == this);
74 DCHECK_GT(children_
.size(), 1u);
75 children_
.erase(std::find(children_
.begin(), children_
.end(), child
));
76 Views::iterator i
= std::find(children_
.begin(), children_
.end(), relative
);
77 if (direction
== mojo::ORDER_DIRECTION_ABOVE
) {
78 DCHECK(i
!= children_
.end());
79 children_
.insert(++i
, child
);
80 } else if (direction
== mojo::ORDER_DIRECTION_BELOW
) {
81 DCHECK(i
!= children_
.end());
82 children_
.insert(i
, child
);
84 delegate_
->OnViewReordered(this, relative
, direction
);
87 void ServerView::SetBounds(const gfx::Rect
& bounds
) {
88 if (bounds_
== bounds
)
91 const gfx::Rect old_bounds
= bounds_
;
93 delegate_
->OnViewBoundsChanged(this, old_bounds
, bounds
);
96 const ServerView
* ServerView::GetRoot() const {
97 const ServerView
* view
= this;
98 while (view
&& view
->parent())
99 view
= view
->parent();
103 std::vector
<const ServerView
*> ServerView::GetChildren() const {
104 std::vector
<const ServerView
*> children
;
105 children
.reserve(children_
.size());
106 for (size_t i
= 0; i
< children_
.size(); ++i
)
107 children
.push_back(children_
[i
]);
111 std::vector
<ServerView
*> ServerView::GetChildren() {
112 // TODO(sky): rename to children() and fix return type.
116 bool ServerView::Contains(const ServerView
* view
) const {
117 for (const ServerView
* parent
= view
; parent
; parent
= parent
->parent_
) {
124 void ServerView::SetVisible(bool value
) {
125 if (visible_
== value
)
128 delegate_
->OnWillChangeViewVisibility(this);
132 void ServerView::SetOpacity(float value
) {
133 if (value
== opacity_
)
136 delegate_
->OnScheduleViewPaint(this);
139 void ServerView::SetTransform(const gfx::Transform
& transform
) {
140 if (transform_
== transform
)
143 transform_
= transform
;
144 delegate_
->OnScheduleViewPaint(this);
147 void ServerView::SetProperty(const std::string
& name
,
148 const std::vector
<uint8_t>* value
) {
149 auto it
= properties_
.find(name
);
150 if (it
!= properties_
.end()) {
151 if (value
&& it
->second
== *value
)
154 // This property isn't set in |properties_| and |value| is NULL, so there's
160 properties_
[name
] = *value
;
161 } else if (it
!= properties_
.end()) {
162 properties_
.erase(it
);
165 delegate_
->OnViewSharedPropertyChanged(this, name
, value
);
168 bool ServerView::IsDrawn(const ServerView
* root
) const {
171 const ServerView
* view
= this;
172 while (view
&& view
!= root
&& view
->visible_
)
173 view
= view
->parent_
;
177 void ServerView::SetSurfaceId(cc::SurfaceId surface_id
) {
178 surface_id_
= surface_id
;
179 delegate_
->OnViewSurfaceIdChanged(this);
183 std::string
ServerView::GetDebugWindowHierarchy() const {
185 BuildDebugInfo(std::string(), &result
);
189 void ServerView::BuildDebugInfo(const std::string
& depth
,
190 std::string
* result
) const {
191 *result
+= base::StringPrintf(
192 "%sid=%d,%d visible=%s bounds=%d,%d %dx%d surface_id=%" PRIu64
"\n",
193 depth
.c_str(), static_cast<int>(id_
.connection_id
),
194 static_cast<int>(id_
.view_id
), visible_
? "true" : "false", bounds_
.x(),
195 bounds_
.y(), bounds_
.width(), bounds_
.height(), surface_id_
.id
);
196 for (const ServerView
* child
: children_
)
197 child
->BuildDebugInfo(depth
+ " ", result
);
201 void ServerView::RemoveImpl(ServerView
* view
) {
202 view
->parent_
= NULL
;
203 children_
.erase(std::find(children_
.begin(), children_
.end(), view
));
206 } // namespace view_manager