| 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/node.h" | |
| 6 | |
| 7 #include "mojo/services/view_manager/node_delegate.h" | |
| 8 | |
| 9 namespace mojo { | |
| 10 namespace service { | |
| 11 | |
| 12 Node::Node(NodeDelegate* delegate, const NodeId& id) | |
| 13 : delegate_(delegate), | |
| 14 id_(id), | |
| 15 parent_(NULL), | |
| 16 visible_(true) { | |
| 17 DCHECK(delegate); // Must provide a delegate. | |
| 18 } | |
| 19 | |
| 20 Node::~Node() { | |
| 21 while (!children_.empty()) | |
| 22 children_.front()->parent()->Remove(children_.front()); | |
| 23 | |
| 24 if (parent_) | |
| 25 parent_->Remove(this); | |
| 26 | |
| 27 delegate_->OnNodeDestroyed(this); | |
| 28 } | |
| 29 | |
| 30 void Node::Add(Node* child) { | |
| 31 // We assume validation checks happened already. | |
| 32 DCHECK(child); | |
| 33 DCHECK(child != this); | |
| 34 DCHECK(!child->Contains(this)); | |
| 35 if (child->parent() == this) { | |
| 36 if (children_.size() == 1) | |
| 37 return; // Already in the right position. | |
| 38 Reorder(child, children_.back(), ORDER_DIRECTION_ABOVE); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 const Node* old_parent = child->parent(); | |
| 43 if (child->parent()) | |
| 44 child->parent()->RemoveImpl(child); | |
| 45 | |
| 46 child->parent_ = this; | |
| 47 children_.push_back(child); | |
| 48 child->delegate_->OnNodeHierarchyChanged(child, this, old_parent); | |
| 49 } | |
| 50 | |
| 51 void Node::Remove(Node* child) { | |
| 52 // We assume validation checks happened else where. | |
| 53 DCHECK(child); | |
| 54 DCHECK(child != this); | |
| 55 DCHECK(child->parent() == this); | |
| 56 | |
| 57 RemoveImpl(child); | |
| 58 child->delegate_->OnNodeHierarchyChanged(child, NULL, this); | |
| 59 } | |
| 60 | |
| 61 void Node::Reorder(Node* child, Node* relative, OrderDirection direction) { | |
| 62 // We assume validation checks happened else where. | |
| 63 DCHECK(child); | |
| 64 DCHECK(child->parent() == this); | |
| 65 DCHECK_GT(children_.size(), 1u); | |
| 66 children_.erase(std::find(children_.begin(), children_.end(), child)); | |
| 67 Nodes::iterator i = std::find(children_.begin(), children_.end(), relative); | |
| 68 if (direction == ORDER_DIRECTION_ABOVE) { | |
| 69 DCHECK(i != children_.end()); | |
| 70 children_.insert(++i, child); | |
| 71 } else if (direction == ORDER_DIRECTION_BELOW) { | |
| 72 DCHECK(i != children_.end()); | |
| 73 children_.insert(i, child); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void Node::SetBounds(const gfx::Rect& bounds) { | |
| 78 if (bounds_ == bounds) | |
| 79 return; | |
| 80 | |
| 81 const gfx::Rect old_bounds = bounds_; | |
| 82 bounds_ = bounds; | |
| 83 delegate_->OnNodeBoundsChanged(this, old_bounds, bounds); | |
| 84 } | |
| 85 | |
| 86 const Node* Node::GetRoot() const { | |
| 87 const Node* node = this; | |
| 88 while (node && node->parent()) | |
| 89 node = node->parent(); | |
| 90 return node; | |
| 91 } | |
| 92 | |
| 93 std::vector<const Node*> Node::GetChildren() const { | |
| 94 std::vector<const Node*> children; | |
| 95 children.reserve(children_.size()); | |
| 96 for (size_t i = 0; i < children_.size(); ++i) | |
| 97 children.push_back(children_[i]); | |
| 98 return children; | |
| 99 } | |
| 100 | |
| 101 std::vector<Node*> Node::GetChildren() { | |
| 102 // TODO(sky): rename to children() and fix return type. | |
| 103 return children_; | |
| 104 } | |
| 105 | |
| 106 bool Node::Contains(const Node* node) const { | |
| 107 for (const Node* parent = node; parent; parent = parent->parent_) { | |
| 108 if (parent == this) | |
| 109 return true; | |
| 110 } | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 void Node::SetVisible(bool value) { | |
| 115 if (visible_ == value) | |
| 116 return; | |
| 117 | |
| 118 visible_ = value; | |
| 119 // TODO(sky): notification, including repaint. | |
| 120 } | |
| 121 | |
| 122 void Node::SetBitmap(const SkBitmap& bitmap) { | |
| 123 bitmap_ = bitmap; | |
| 124 delegate_->OnNodeBitmapChanged(this); | |
| 125 } | |
| 126 | |
| 127 void Node::RemoveImpl(Node* node) { | |
| 128 node->parent_ = NULL; | |
| 129 children_.erase(std::find(children_.begin(), children_.end(), node)); | |
| 130 } | |
| 131 | |
| 132 } // namespace service | |
| 133 } // namespace mojo | |
| OLD | NEW |