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();
40 child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent);
42 child->parent()->RemoveImpl(child);
44 child->parent_ = this;
45 children_.push_back(child);
46 child->delegate_->OnViewHierarchyChanged(child, this, old_parent);
49 void ServerView::Remove(ServerView* child) {
50 // We assume validation checks happened else where.
52 DCHECK(child != this);
53 DCHECK(child->parent() == this);
55 child->delegate_->OnWillChangeViewHierarchy(child, NULL, this);
57 child->delegate_->OnViewHierarchyChanged(child, NULL, this);
60 void ServerView::Reorder(ServerView* child,
62 OrderDirection direction) {
63 // We assume validation checks happened else where.
65 DCHECK(child->parent() == this);
66 DCHECK_GT(children_.size(), 1u);
67 children_.erase(std::find(children_.begin(), children_.end(), child));
68 Views::iterator i = std::find(children_.begin(), children_.end(), relative);
69 if (direction == ORDER_DIRECTION_ABOVE) {
70 DCHECK(i != children_.end());
71 children_.insert(++i, child);
72 } else if (direction == ORDER_DIRECTION_BELOW) {
73 DCHECK(i != children_.end());
74 children_.insert(i, child);
78 void ServerView::SetBounds(const gfx::Rect& bounds) {
79 if (bounds_ == bounds)
82 const gfx::Rect old_bounds = bounds_;
84 delegate_->OnViewBoundsChanged(this, old_bounds, bounds);
87 const ServerView* ServerView::GetRoot() const {
88 const ServerView* view = this;
89 while (view && view->parent())
90 view = view->parent();
94 std::vector<const ServerView*> ServerView::GetChildren() const {
95 std::vector<const ServerView*> children;
96 children.reserve(children_.size());
97 for (size_t i = 0; i < children_.size(); ++i)
98 children.push_back(children_[i]);
102 std::vector<ServerView*> ServerView::GetChildren() {
103 // TODO(sky): rename to children() and fix return type.
107 bool ServerView::Contains(const ServerView* view) const {
108 for (const ServerView* parent = view; parent; parent = parent->parent_) {
115 void ServerView::SetVisible(bool value) {
116 if (visible_ == value)
119 delegate_->OnWillChangeViewVisibility(this);
123 bool ServerView::IsDrawn(const ServerView* root) const {
126 const ServerView* view = this;
127 while (view && view != root && view->visible_)
128 view = view->parent_;
132 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) {
133 surface_id_ = surface_id;
134 delegate_->OnViewSurfaceIdChanged(this);
137 void ServerView::RemoveImpl(ServerView* view) {
138 view->parent_ = NULL;
139 children_.erase(std::find(children_.begin(), children_.end(), view));
142 } // namespace service