1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "aura/window.h"
#include <algorithm>
#include "aura/desktop.h"
#include "aura/window_delegate.h"
#include "base/logging.h"
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/compositor/layer.h"
namespace aura {
Window::Window(WindowDelegate* delegate)
: delegate_(delegate),
visibility_(VISIBILITY_HIDDEN),
parent_(NULL),
id_(-1) {
}
Window::~Window() {
if (delegate_)
delegate_->OnWindowDestroyed();
if (parent_)
parent_->RemoveChild(this);
}
void Window::Init() {
layer_.reset(new ui::Layer(Desktop::GetInstance()->compositor()));
layer_->set_delegate(this);
}
void Window::SetVisibility(Visibility visibility) {
if (visibility_ == visibility)
return;
visibility_ = visibility;
if (visibility_ != VISIBILITY_HIDDEN)
SchedulePaint();
}
void Window::SetBounds(const gfx::Rect& bounds, int anim_ms) {
// TODO: support anim_ms
// TODO: funnel this through the Desktop.
bounds_ = bounds;
layer_->SetBounds(bounds);
}
void Window::SchedulePaintInRect(const gfx::Rect& rect) {
layer_->SchedulePaint(rect);
}
void Window::SetCanvas(const SkCanvas& canvas, const gfx::Point& origin) {
// TODO: figure out how this is going to work when animating the layer. In
// particular if we're animating the size then the underlying Texture is going
// to be unhappy if we try to set a texture on a size bigger than the size of
// the texture.
layer_->SetCanvas(canvas, origin);
}
void Window::SetParent(Window* parent) {
if (parent)
parent->AddChild(this);
else
Desktop::GetInstance()->window()->AddChild(this);
}
void Window::DrawTree() {
Draw();
for (Windows::iterator i = children_.begin(); i != children_.end(); ++i)
(*i)->DrawTree();
}
void Window::AddChild(Window* child) {
DCHECK(std::find(children_.begin(), children_.end(), child) ==
children_.end());
child->parent_ = this;
layer_->Add(child->layer_.get());
children_.push_back(child);
}
void Window::RemoveChild(Window* child) {
Windows::iterator i = std::find(children_.begin(), children_.end(), child);
DCHECK(i != children_.end());
child->parent_ = NULL;
layer_->Remove(child->layer_.get());
children_.erase(i);
}
// static
void Window::ConvertPointToWindow(Window* source,
Window* target,
gfx::Point* point) {
ui::Layer::ConvertPointToLayer(source->layer(), target->layer(), point);
}
bool Window::OnMouseEvent(const MouseEvent& event) {
return true;
}
bool Window::HitTest(const gfx::Point& point) {
gfx::Rect local_bounds(gfx::Point(), bounds().size());
// TODO(beng): hittest masks.
return local_bounds.Contains(point);
}
Window* Window::GetEventHandlerForPoint(const gfx::Point& point) {
Windows::const_reverse_iterator i = children_.rbegin();
for (; i != children_.rend(); ++i) {
Window* child = *i;
if (child->visibility() == Window::VISIBILITY_HIDDEN)
continue;
gfx::Point point_in_child_coords(point);
Window::ConvertPointToWindow(this, child, &point_in_child_coords);
if (child->HitTest(point_in_child_coords))
return child->GetEventHandlerForPoint(point_in_child_coords);
}
return this;
}
void Window::Draw() {
if (visibility_ != VISIBILITY_HIDDEN)
layer_->Draw();
}
void Window::SchedulePaint() {
SchedulePaintInRect(gfx::Rect(0, 0, bounds_.width(), bounds_.height()));
}
void Window::OnPaint(gfx::Canvas* canvas) {
delegate_->OnPaint(canvas);
}
} // namespace aura
|