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/server_view.h" |
| 6 |
| 7 #include <inttypes.h> |
| 8 |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "mojo/services/view_manager/server_view_delegate.h" |
| 11 |
| 12 namespace view_manager { |
| 13 |
| 14 ServerView::ServerView(ServerViewDelegate* delegate, const ViewId& id) |
| 15 : delegate_(delegate), |
| 16 id_(id), |
| 17 parent_(nullptr), |
| 18 visible_(false), |
| 19 opacity_(1) { |
| 20 DCHECK(delegate); // Must provide a delegate. |
| 21 } |
| 22 |
| 23 ServerView::~ServerView() { |
| 24 delegate_->OnWillDestroyView(this); |
| 25 |
| 26 while (!children_.empty()) |
| 27 children_.front()->parent()->Remove(children_.front()); |
| 28 |
| 29 if (parent_) |
| 30 parent_->Remove(this); |
| 31 |
| 32 delegate_->OnViewDestroyed(this); |
| 33 } |
| 34 |
| 35 void ServerView::Add(ServerView* child) { |
| 36 // We assume validation checks happened already. |
| 37 DCHECK(child); |
| 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); |
| 44 return; |
| 45 } |
| 46 |
| 47 ServerView* old_parent = child->parent(); |
| 48 child->delegate_->OnWillChangeViewHierarchy(child, this, old_parent); |
| 49 if (child->parent()) |
| 50 child->parent()->RemoveImpl(child); |
| 51 |
| 52 child->parent_ = this; |
| 53 children_.push_back(child); |
| 54 child->delegate_->OnViewHierarchyChanged(child, this, old_parent); |
| 55 } |
| 56 |
| 57 void ServerView::Remove(ServerView* child) { |
| 58 // We assume validation checks happened else where. |
| 59 DCHECK(child); |
| 60 DCHECK(child != this); |
| 61 DCHECK(child->parent() == this); |
| 62 |
| 63 child->delegate_->OnWillChangeViewHierarchy(child, NULL, this); |
| 64 RemoveImpl(child); |
| 65 child->delegate_->OnViewHierarchyChanged(child, NULL, this); |
| 66 } |
| 67 |
| 68 void ServerView::Reorder(ServerView* child, |
| 69 ServerView* relative, |
| 70 mojo::OrderDirection direction) { |
| 71 // We assume validation checks happened else where. |
| 72 DCHECK(child); |
| 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); |
| 83 } |
| 84 delegate_->OnViewReordered(this, relative, direction); |
| 85 } |
| 86 |
| 87 void ServerView::SetBounds(const gfx::Rect& bounds) { |
| 88 if (bounds_ == bounds) |
| 89 return; |
| 90 |
| 91 const gfx::Rect old_bounds = bounds_; |
| 92 bounds_ = bounds; |
| 93 delegate_->OnViewBoundsChanged(this, old_bounds, bounds); |
| 94 } |
| 95 |
| 96 const ServerView* ServerView::GetRoot() const { |
| 97 const ServerView* view = this; |
| 98 while (view && view->parent()) |
| 99 view = view->parent(); |
| 100 return view; |
| 101 } |
| 102 |
| 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]); |
| 108 return children; |
| 109 } |
| 110 |
| 111 std::vector<ServerView*> ServerView::GetChildren() { |
| 112 // TODO(sky): rename to children() and fix return type. |
| 113 return children_; |
| 114 } |
| 115 |
| 116 bool ServerView::Contains(const ServerView* view) const { |
| 117 for (const ServerView* parent = view; parent; parent = parent->parent_) { |
| 118 if (parent == this) |
| 119 return true; |
| 120 } |
| 121 return false; |
| 122 } |
| 123 |
| 124 void ServerView::SetVisible(bool value) { |
| 125 if (visible_ == value) |
| 126 return; |
| 127 |
| 128 delegate_->OnWillChangeViewVisibility(this); |
| 129 visible_ = value; |
| 130 } |
| 131 |
| 132 void ServerView::SetOpacity(float value) { |
| 133 if (value == opacity_) |
| 134 return; |
| 135 opacity_ = value; |
| 136 delegate_->OnScheduleViewPaint(this); |
| 137 } |
| 138 |
| 139 void ServerView::SetTransform(const gfx::Transform& transform) { |
| 140 if (transform_ == transform) |
| 141 return; |
| 142 |
| 143 transform_ = transform; |
| 144 delegate_->OnScheduleViewPaint(this); |
| 145 } |
| 146 |
| 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) |
| 152 return; |
| 153 } else if (!value) { |
| 154 // This property isn't set in |properties_| and |value| is NULL, so there's |
| 155 // no change. |
| 156 return; |
| 157 } |
| 158 |
| 159 if (value) { |
| 160 properties_[name] = *value; |
| 161 } else if (it != properties_.end()) { |
| 162 properties_.erase(it); |
| 163 } |
| 164 |
| 165 delegate_->OnViewSharedPropertyChanged(this, name, value); |
| 166 } |
| 167 |
| 168 bool ServerView::IsDrawn(const ServerView* root) const { |
| 169 if (!root->visible_) |
| 170 return false; |
| 171 const ServerView* view = this; |
| 172 while (view && view != root && view->visible_) |
| 173 view = view->parent_; |
| 174 return view == root; |
| 175 } |
| 176 |
| 177 void ServerView::SetSurfaceId(cc::SurfaceId surface_id) { |
| 178 surface_id_ = surface_id; |
| 179 delegate_->OnViewSurfaceIdChanged(this); |
| 180 } |
| 181 |
| 182 #if !defined(NDEBUG) |
| 183 std::string ServerView::GetDebugWindowHierarchy() const { |
| 184 std::string result; |
| 185 BuildDebugInfo(std::string(), &result); |
| 186 return result; |
| 187 } |
| 188 |
| 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); |
| 198 } |
| 199 #endif |
| 200 |
| 201 void ServerView::RemoveImpl(ServerView* view) { |
| 202 view->parent_ = NULL; |
| 203 children_.erase(std::find(children_.begin(), children_.end(), view)); |
| 204 } |
| 205 |
| 206 } // namespace view_manager |
OLD | NEW |