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 "ash/display/cursor_window_controller.h" | |
6 | |
7 #include "ash/display/display_controller.h" | |
8 #include "ash/display/mirror_window_controller.h" | |
9 #include "ash/root_window_controller.h" | |
10 #include "ash/shell.h" | |
11 #include "ash/shell_window_ids.h" | |
12 #include "ui/aura/env.h" | |
13 #include "ui/aura/root_window.h" | |
14 #include "ui/aura/window_delegate.h" | |
15 #include "ui/base/cursor/cursors_aura.h" | |
16 #include "ui/base/hit_test.h" | |
17 #include "ui/base/resource/resource_bundle.h" | |
18 #include "ui/compositor/dip_util.h" | |
19 #include "ui/gfx/canvas.h" | |
20 #include "ui/gfx/display.h" | |
21 #include "ui/gfx/image/image_skia.h" | |
22 #include "ui/gfx/image/image_skia_operations.h" | |
23 | |
24 namespace ash { | |
25 namespace internal { | |
26 | |
27 class CursorWindowDelegate : public aura::WindowDelegate { | |
28 public: | |
29 CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {} | |
30 virtual ~CursorWindowDelegate() {} | |
31 | |
32 // aura::WindowDelegate overrides: | |
33 virtual gfx::Size GetMinimumSize() const OVERRIDE { return size_; } | |
34 virtual gfx::Size GetMaximumSize() const OVERRIDE { return size_; } | |
35 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
36 const gfx::Rect& new_bounds) OVERRIDE {} | |
37 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
38 return gfx::kNullCursor; | |
39 } | |
40 virtual int GetNonClientComponent( | |
41 const gfx::Point& point) const OVERRIDE { | |
42 return HTNOWHERE; | |
43 } | |
44 virtual bool ShouldDescendIntoChildForEventHandling( | |
45 aura::Window* child, | |
46 const gfx::Point& location) OVERRIDE { | |
47 return false; | |
48 } | |
49 virtual bool CanFocus() OVERRIDE { return false; } | |
50 virtual void OnCaptureLost() OVERRIDE {} | |
51 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
52 canvas->DrawImageInt(cursor_image_, 0, 0); | |
53 } | |
54 virtual void OnDeviceScaleFactorChanged( | |
55 float device_scale_factor) OVERRIDE {} | |
56 virtual void OnWindowDestroying() OVERRIDE {} | |
57 virtual void OnWindowDestroyed() OVERRIDE {} | |
58 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} | |
59 virtual bool HasHitTestMask() const OVERRIDE { return false; } | |
60 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} | |
61 virtual void DidRecreateLayer(ui::Layer* old_layer, | |
62 ui::Layer* new_layer) OVERRIDE {} | |
63 | |
64 // Sets cursor compositing mode on/off. | |
65 void SetCursorCompositingEnabled(bool enabled) { | |
66 is_cursor_compositing_enabled_ = enabled; | |
67 } | |
68 | |
69 // Sets the cursor image for the |display|'s scale factor. | |
70 void SetCursorImage(const gfx::ImageSkia& image, | |
71 const gfx::Display& display) { | |
72 float scale_factor = display.device_scale_factor(); | |
73 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor); | |
74 if (!is_cursor_compositing_enabled_) { | |
75 // Note that mirror window's scale factor is always 1.0f, therefore we | |
76 // need to take 2x's image and paint as if it's 1x image. | |
77 size_ = image_rep.pixel_size(); | |
78 cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap()); | |
79 } else { | |
80 size_ = image.size(); | |
81 cursor_image_ = gfx::ImageSkia( | |
82 gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor)); | |
83 } | |
84 } | |
85 | |
86 const gfx::Size size() const { return size_; } | |
87 | |
88 private: | |
89 bool is_cursor_compositing_enabled_; | |
90 gfx::ImageSkia cursor_image_; | |
91 gfx::Size size_; | |
92 | |
93 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate); | |
94 }; | |
95 | |
96 CursorWindowController::CursorWindowController() | |
97 : is_cursor_compositing_enabled_(false), | |
98 container_(NULL), | |
99 cursor_type_(ui::kCursorNone), | |
100 cursor_set_(ui::CURSOR_SET_NORMAL), | |
101 cursor_rotation_(gfx::Display::ROTATE_0), | |
102 delegate_(new CursorWindowDelegate()) { | |
103 } | |
104 | |
105 CursorWindowController::~CursorWindowController() { | |
106 SetContainer(NULL); | |
107 } | |
108 | |
109 void CursorWindowController::SetCursorCompositingEnabled(bool enabled) { | |
110 if (is_cursor_compositing_enabled_ != enabled) { | |
111 is_cursor_compositing_enabled_ = enabled; | |
112 delegate_->SetCursorCompositingEnabled(enabled); | |
113 UpdateCursorImage(); | |
114 UpdateContainer(); | |
115 } | |
116 } | |
117 | |
118 void CursorWindowController::UpdateContainer() { | |
119 display_ = Shell::GetScreen()->GetPrimaryDisplay(); | |
120 if (is_cursor_compositing_enabled_) { | |
121 SetDisplay(display_); | |
122 } else { | |
123 aura::RootWindow* mirror_root_window = Shell::GetInstance()-> | |
124 display_controller()->mirror_window_controller()->root_window(); | |
125 SetContainer(mirror_root_window ? mirror_root_window->window() : NULL); | |
126 } | |
127 } | |
128 | |
129 void CursorWindowController::SetDisplay(const gfx::Display& display) { | |
130 if (!is_cursor_compositing_enabled_) | |
131 return; | |
132 | |
133 display_ = display; | |
134 aura::Window* root_window = Shell::GetInstance()->display_controller()-> | |
135 GetRootWindowForDisplayId(display.id()); | |
136 if (!root_window) | |
137 return; | |
138 | |
139 SetContainer(GetRootWindowController(root_window)->GetContainer( | |
140 kShellWindowId_OverlayContainer)); | |
141 SetBoundsInScreen(display.bounds()); | |
142 } | |
143 | |
144 void CursorWindowController::UpdateLocation() { | |
145 if (!cursor_window_) | |
146 return; | |
147 | |
148 gfx::Point point = aura::Env::GetInstance()->last_mouse_location(); | |
149 if (!is_cursor_compositing_enabled_) { | |
150 Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->ConvertPointToHost( | |
151 &point); | |
152 } else { | |
153 point.Offset(-bounds_in_screen_.x(), -bounds_in_screen_.y()); | |
154 } | |
155 point.Offset(-hot_point_.x(), -hot_point_.y()); | |
156 gfx::Rect bounds = cursor_window_->bounds(); | |
157 bounds.set_origin(point); | |
158 cursor_window_->SetBounds(bounds); | |
159 } | |
160 | |
161 void CursorWindowController::SetCursor(gfx::NativeCursor cursor) { | |
162 if (cursor_type_ == cursor.native_type() && | |
163 cursor_rotation_ == display_.rotation()) | |
164 return; | |
165 cursor_type_ = cursor.native_type(); | |
166 cursor_rotation_ = display_.rotation(); | |
167 UpdateCursorImage(); | |
168 } | |
169 | |
170 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) { | |
171 cursor_set_ = cursor_set; | |
172 UpdateCursorImage(); | |
173 } | |
174 | |
175 void CursorWindowController::SetVisibility(bool visible) { | |
176 if (!cursor_window_) | |
177 return; | |
178 if (visible) | |
179 cursor_window_->Show(); | |
180 else | |
181 cursor_window_->Hide(); | |
182 } | |
183 | |
184 void CursorWindowController::SetContainer(aura::Window* container) { | |
185 if (container_ == container) | |
186 return; | |
187 | |
188 container_ = container; | |
189 if (!container) { | |
190 cursor_window_.reset(); | |
191 return; | |
192 } | |
193 | |
194 if (!cursor_window_) { | |
195 cursor_window_.reset(new aura::Window(delegate_.get())); | |
196 cursor_window_->SetTransparent(true); | |
197 cursor_window_->Init(aura::WINDOW_LAYER_TEXTURED); | |
198 cursor_window_->set_ignore_events(true); | |
199 cursor_window_->set_owned_by_parent(false); | |
200 } | |
201 | |
202 container->AddChild(cursor_window_.get()); | |
203 cursor_window_->Show(); | |
204 SetBoundsInScreen(container->bounds()); | |
205 } | |
206 | |
207 void CursorWindowController::SetBoundsInScreen(const gfx::Rect& bounds) { | |
208 bounds_in_screen_ = bounds; | |
209 UpdateLocation(); | |
210 } | |
211 | |
212 void CursorWindowController::UpdateCursorImage() { | |
213 int resource_id; | |
214 // TODO(hshi): support custom cursor set. | |
215 bool success = ui::GetCursorDataFor( | |
216 cursor_set_, | |
oshima
2014/02/06 01:00:48
nit: move arguments to align (
hshi1
2014/02/06 01:07:42
Done.
| |
217 cursor_type_, | |
218 display_.device_scale_factor(), | |
219 &resource_id, | |
220 &hot_point_); | |
221 if (!success) | |
222 return; | |
oshima
2014/02/06 01:00:48
nit:
if (ui::GetCursor...) {
return;
}
hshi1
2014/02/06 01:07:42
Done.
| |
223 const gfx::ImageSkia* image = | |
224 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); | |
225 gfx::ImageSkia rotated = *image; | |
226 if (!is_cursor_compositing_enabled_) { | |
227 switch (cursor_rotation_) { | |
228 case gfx::Display::ROTATE_0: | |
229 break; | |
230 case gfx::Display::ROTATE_90: | |
231 rotated = gfx::ImageSkiaOperations::CreateRotatedImage( | |
232 *image, SkBitmapOperations::ROTATION_90_CW); | |
233 hot_point_.SetPoint( | |
234 rotated.width() - hot_point_.y(), | |
235 hot_point_.x()); | |
236 break; | |
237 case gfx::Display::ROTATE_180: | |
238 rotated = gfx::ImageSkiaOperations::CreateRotatedImage( | |
239 *image, SkBitmapOperations::ROTATION_180_CW); | |
240 hot_point_.SetPoint( | |
241 rotated.height() - hot_point_.x(), | |
242 rotated.width() - hot_point_.y()); | |
243 break; | |
244 case gfx::Display::ROTATE_270: | |
245 rotated = gfx::ImageSkiaOperations::CreateRotatedImage( | |
246 *image, SkBitmapOperations::ROTATION_270_CW); | |
247 hot_point_.SetPoint( | |
248 hot_point_.y(), | |
249 rotated.height() - hot_point_.x()); | |
250 break; | |
251 } | |
252 } else { | |
253 hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(), | |
254 hot_point_); | |
255 } | |
256 delegate_->SetCursorImage(rotated, display_); | |
257 if (cursor_window_) { | |
258 cursor_window_->SetBounds(gfx::Rect(delegate_->size())); | |
259 cursor_window_->SchedulePaintInRect( | |
260 gfx::Rect(cursor_window_->bounds().size())); | |
261 UpdateLocation(); | |
262 } | |
263 } | |
264 | |
265 } // namespace internal | |
266 } // namespace ash | |
OLD | NEW |