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"
7 #include "mojo/services/view_manager/server_view_delegate.h"
12 ServerView::ServerView(ServerViewDelegate
* delegate
, const ViewId
& id
)
13 : delegate_(delegate
), id_(id
), parent_(NULL
), visible_(true) {
14 DCHECK(delegate
); // Must provide a delegate.
17 ServerView::~ServerView() {
18 while (!children_
.empty())
19 children_
.front()->parent()->Remove(children_
.front());
22 parent_
->Remove(this);
24 delegate_
->OnViewDestroyed(this);
27 void ServerView::Add(ServerView
* child
) {
28 // We assume validation checks happened already.
30 DCHECK(child
!= this);
31 DCHECK(!child
->Contains(this));
32 if (child
->parent() == this) {
33 if (children_
.size() == 1)
34 return; // Already in the right position.
35 Reorder(child
, children_
.back(), ORDER_DIRECTION_ABOVE
);
39 const ServerView
* old_parent
= child
->parent();
41 child
->parent()->RemoveImpl(child
);
43 child
->parent_
= this;
44 children_
.push_back(child
);
45 child
->delegate_
->OnViewHierarchyChanged(child
, this, old_parent
);
48 void ServerView::Remove(ServerView
* child
) {
49 // We assume validation checks happened else where.
51 DCHECK(child
!= this);
52 DCHECK(child
->parent() == this);
55 child
->delegate_
->OnViewHierarchyChanged(child
, NULL
, this);
58 void ServerView::Reorder(ServerView
* child
,
60 OrderDirection direction
) {
61 // We assume validation checks happened else where.
63 DCHECK(child
->parent() == this);
64 DCHECK_GT(children_
.size(), 1u);
65 children_
.erase(std::find(children_
.begin(), children_
.end(), child
));
66 Views::iterator i
= std::find(children_
.begin(), children_
.end(), relative
);
67 if (direction
== ORDER_DIRECTION_ABOVE
) {
68 DCHECK(i
!= children_
.end());
69 children_
.insert(++i
, child
);
70 } else if (direction
== ORDER_DIRECTION_BELOW
) {
71 DCHECK(i
!= children_
.end());
72 children_
.insert(i
, child
);
76 void ServerView::SetBounds(const gfx::Rect
& bounds
) {
77 if (bounds_
== bounds
)
80 const gfx::Rect old_bounds
= bounds_
;
82 delegate_
->OnViewBoundsChanged(this, old_bounds
, bounds
);
85 const ServerView
* ServerView::GetRoot() const {
86 const ServerView
* view
= this;
87 while (view
&& view
->parent())
88 view
= view
->parent();
92 std::vector
<const ServerView
*> ServerView::GetChildren() const {
93 std::vector
<const ServerView
*> children
;
94 children
.reserve(children_
.size());
95 for (size_t i
= 0; i
< children_
.size(); ++i
)
96 children
.push_back(children_
[i
]);
100 std::vector
<ServerView
*> ServerView::GetChildren() {
101 // TODO(sky): rename to children() and fix return type.
105 bool ServerView::Contains(const ServerView
* view
) const {
106 for (const ServerView
* parent
= view
; parent
; parent
= parent
->parent_
) {
113 void ServerView::SetVisible(bool value
) {
114 if (visible_
== value
)
118 // TODO(sky): notification, including repaint.
121 void ServerView::SetBitmap(const SkBitmap
& bitmap
) {
123 delegate_
->OnViewBitmapChanged(this);
126 void ServerView::RemoveImpl(ServerView
* view
) {
127 view
->parent_
= NULL
;
128 children_
.erase(std::find(children_
.begin(), children_
.end(), view
));
131 } // namespace service