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/node.h"
7 #include "mojo/services/view_manager/node_delegate.h"
8 #include "ui/aura/window_property.h"
9 #include "ui/base/cursor/cursor.h"
10 #include "ui/base/hit_test.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/image/image_skia.h"
13 #include "ui/gfx/native_widget_types.h"
15 DECLARE_WINDOW_PROPERTY_TYPE(mojo::service::Node*);
20 DEFINE_WINDOW_PROPERTY_KEY(Node*, kNodeKey, NULL);
22 Node::Node(NodeDelegate* delegate, const NodeId& id)
23 : delegate_(delegate),
26 DCHECK(delegate); // Must provide a delegate.
27 window_.set_owned_by_parent(false);
28 window_.AddObserver(this);
29 window_.SetProperty(kNodeKey, this);
30 window_.Init(aura::WINDOW_LAYER_TEXTURED);
32 // TODO(sky): this likely needs to be false and add a visibility API.
37 // This is implicitly done during deletion of the window, but we do it here so
38 // that we're in a known state.
40 window_.parent()->RemoveChild(&window_);
42 delegate_->OnNodeDestroyed(this);
46 Node* Node::NodeForWindow(aura::Window* window) {
47 return window->GetProperty(kNodeKey);
50 const Node* Node::GetParent() const {
51 if (!window_.parent())
53 return window_.parent()->GetProperty(kNodeKey);
56 void Node::Add(Node* child) {
57 window_.AddChild(&child->window_);
60 void Node::Remove(Node* child) {
61 window_.RemoveChild(&child->window_);
64 void Node::Reorder(Node* child, Node* relative, OrderDirection direction) {
65 if (direction == ORDER_DIRECTION_ABOVE)
66 window_.StackChildAbove(child->window(), relative->window());
67 else if (direction == ORDER_DIRECTION_BELOW)
68 window_.StackChildBelow(child->window(), relative->window());
71 const Node* Node::GetRoot() const {
72 const aura::Window* window = &window_;
73 while (window && window->parent())
74 window = window->parent();
75 return window->GetProperty(kNodeKey);
78 std::vector<const Node*> Node::GetChildren() const {
79 std::vector<const Node*> children;
80 children.reserve(window_.children().size());
81 for (size_t i = 0; i < window_.children().size(); ++i)
82 children.push_back(window_.children()[i]->GetProperty(kNodeKey));
86 std::vector<Node*> Node::GetChildren() {
87 std::vector<Node*> children;
88 children.reserve(window_.children().size());
89 for (size_t i = 0; i < window_.children().size(); ++i)
90 children.push_back(window_.children()[i]->GetProperty(kNodeKey));
94 bool Node::Contains(const Node* node) const {
95 return node && window_.Contains(&(node->window_));
98 bool Node::IsVisible() const {
99 return window_.TargetVisibility();
102 void Node::SetVisible(bool value) {
109 void Node::SetBitmap(const SkBitmap& bitmap) {
111 window_.SchedulePaintInRect(gfx::Rect(window_.bounds().size()));
114 void Node::OnWindowHierarchyChanged(
115 const aura::WindowObserver::HierarchyChangeParams& params) {
116 if (params.target != &window_ || params.receiver != &window_)
118 const Node* new_parent = params.new_parent ?
119 params.new_parent->GetProperty(kNodeKey) : NULL;
120 const Node* old_parent = params.old_parent ?
121 params.old_parent->GetProperty(kNodeKey) : NULL;
122 // This check is needed because even the root Node's aura::Window has a
123 // parent, but the Node itself has no parent (so it's possible for us to
124 // receive this notification from aura when no logical Node hierarchy change
125 // has actually ocurred).
126 if (new_parent != old_parent)
127 delegate_->OnNodeHierarchyChanged(this, new_parent, old_parent);
130 gfx::Size Node::GetMinimumSize() const {
134 gfx::Size Node::GetMaximumSize() const {
138 void Node::OnBoundsChanged(const gfx::Rect& old_bounds,
139 const gfx::Rect& new_bounds) {
140 delegate_->OnNodeBoundsChanged(this, old_bounds, new_bounds);
143 gfx::NativeCursor Node::GetCursor(const gfx::Point& point) {
144 return gfx::kNullCursor;
147 int Node::GetNonClientComponent(const gfx::Point& point) const {
151 bool Node::ShouldDescendIntoChildForEventHandling(
153 const gfx::Point& location) {
157 bool Node::CanFocus() {
161 void Node::OnCaptureLost() {
164 void Node::OnPaint(gfx::Canvas* canvas) {
165 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(bitmap_), 0, 0);
168 void Node::OnDeviceScaleFactorChanged(float device_scale_factor) {
171 void Node::OnWindowDestroying(aura::Window* window) {
174 void Node::OnWindowDestroyed(aura::Window* window) {
177 void Node::OnWindowTargetVisibilityChanged(bool visible) {
180 bool Node::HasHitTestMask() const {
184 void Node::GetHitTestMask(gfx::Path* mask) const {
187 } // namespace service