Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / mojo / services / view_manager / server_view.cc
blob33507c25882e52f75737e37ff3315334437254f4
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"
9 namespace mojo {
10 namespace service {
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());
21 if (parent_)
22 parent_->Remove(this);
24 delegate_->OnViewDestroyed(this);
27 void ServerView::Add(ServerView* child) {
28 // We assume validation checks happened already.
29 DCHECK(child);
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);
36 return;
39 const ServerView* old_parent = child->parent();
40 if (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.
50 DCHECK(child);
51 DCHECK(child != this);
52 DCHECK(child->parent() == this);
54 RemoveImpl(child);
55 child->delegate_->OnViewHierarchyChanged(child, NULL, this);
58 void ServerView::Reorder(ServerView* child,
59 ServerView* relative,
60 OrderDirection direction) {
61 // We assume validation checks happened else where.
62 DCHECK(child);
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)
78 return;
80 const gfx::Rect old_bounds = bounds_;
81 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();
89 return view;
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]);
97 return children;
100 std::vector<ServerView*> ServerView::GetChildren() {
101 // TODO(sky): rename to children() and fix return type.
102 return children_;
105 bool ServerView::Contains(const ServerView* view) const {
106 for (const ServerView* parent = view; parent; parent = parent->parent_) {
107 if (parent == this)
108 return true;
110 return false;
113 void ServerView::SetVisible(bool value) {
114 if (visible_ == value)
115 return;
117 visible_ = value;
118 // TODO(sky): notification, including repaint.
121 void ServerView::SetBitmap(const SkBitmap& bitmap) {
122 bitmap_ = 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
132 } // namespace mojo