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 "ash/display/cursor_window_controller.h"
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/window_delegate.h"
14 #include "ui/aura/window_event_dispatcher.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/compositor/paint_recorder.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/gfx/image/image_skia_operations.h"
27 class CursorWindowDelegate
: public aura::WindowDelegate
{
29 CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {}
30 ~CursorWindowDelegate() override
{}
32 // aura::WindowDelegate overrides:
33 gfx::Size
GetMinimumSize() const override
{ return size_
; }
34 gfx::Size
GetMaximumSize() const override
{ return size_
; }
35 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
36 const gfx::Rect
& new_bounds
) override
{}
37 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
38 return gfx::kNullCursor
;
40 int GetNonClientComponent(const gfx::Point
& point
) const override
{
43 bool ShouldDescendIntoChildForEventHandling(
45 const gfx::Point
& location
) override
{
48 bool CanFocus() override
{ return false; }
49 void OnCaptureLost() override
{}
50 void OnPaint(const ui::PaintContext
& context
) override
{
51 // No need to cache the output here, the CursorWindow is not invalidated.
52 ui::PaintRecorder
recorder(context
);
53 recorder
.canvas()->DrawImageInt(cursor_image_
, 0, 0);
55 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
56 void OnWindowDestroying(aura::Window
* window
) override
{}
57 void OnWindowDestroyed(aura::Window
* window
) override
{}
58 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
59 bool HasHitTestMask() const override
{ return false; }
60 void GetHitTestMask(gfx::Path
* mask
) const override
{}
62 // Sets cursor compositing mode on/off.
63 void SetCursorCompositingEnabled(bool enabled
) {
64 is_cursor_compositing_enabled_
= enabled
;
67 // Sets the cursor image for the |display|'s scale factor.
68 void SetCursorImage(const gfx::ImageSkia
& image
,
69 const gfx::Display
& display
) {
70 float scale_factor
= display
.device_scale_factor();
71 const gfx::ImageSkiaRep
& image_rep
= image
.GetRepresentation(scale_factor
);
72 if (!is_cursor_compositing_enabled_
) {
73 // Note that mirror window's scale factor is always 1.0f, therefore we
74 // need to take 2x's image and paint as if it's 1x image.
75 size_
= image_rep
.pixel_size();
76 cursor_image_
= gfx::ImageSkia::CreateFrom1xBitmap(image_rep
.sk_bitmap());
79 cursor_image_
= gfx::ImageSkia(
80 gfx::ImageSkiaRep(image_rep
.sk_bitmap(), scale_factor
));
84 const gfx::Size
size() const { return size_
; }
87 bool is_cursor_compositing_enabled_
;
88 gfx::ImageSkia cursor_image_
;
91 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate
);
94 CursorWindowController::CursorWindowController()
95 : is_cursor_compositing_enabled_(false),
97 cursor_type_(ui::kCursorNone
),
99 cursor_set_(ui::CURSOR_SET_NORMAL
),
100 delegate_(new CursorWindowDelegate()) {
103 CursorWindowController::~CursorWindowController() {
107 void CursorWindowController::SetCursorCompositingEnabled(bool enabled
) {
108 if (is_cursor_compositing_enabled_
!= enabled
) {
109 is_cursor_compositing_enabled_
= enabled
;
110 delegate_
->SetCursorCompositingEnabled(enabled
);
116 void CursorWindowController::UpdateContainer() {
117 if (is_cursor_compositing_enabled_
) {
118 gfx::Screen
* screen
= Shell::GetScreen();
119 gfx::Display display
= screen
->GetDisplayNearestPoint(
120 screen
->GetCursorScreenPoint());
121 DCHECK(display
.is_valid());
122 if (display
.is_valid())
125 aura::Window
* mirror_window
= Shell::GetInstance()->
126 display_controller()->
127 mirror_window_controller()->
130 display_
= Shell::GetScreen()->GetPrimaryDisplay();
131 SetContainer(mirror_window
);
133 // Updates the hot point based on the current display.
137 void CursorWindowController::SetDisplay(const gfx::Display
& display
) {
138 if (!is_cursor_compositing_enabled_
)
142 aura::Window
* root_window
= Shell::GetInstance()->display_controller()->
143 GetRootWindowForDisplayId(display
.id());
147 SetContainer(GetRootWindowController(root_window
)->GetContainer(
148 kShellWindowId_MouseCursorContainer
));
149 SetBoundsInScreen(display
.bounds());
150 // Updates the hot point based on the current display.
154 void CursorWindowController::UpdateLocation() {
157 gfx::Point point
= aura::Env::GetInstance()->last_mouse_location();
158 if (!is_cursor_compositing_enabled_
) {
159 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point
);
161 point
.Offset(-bounds_in_screen_
.x(), -bounds_in_screen_
.y());
163 point
.Offset(-hot_point_
.x(), -hot_point_
.y());
164 gfx::Rect bounds
= cursor_window_
->bounds();
165 bounds
.set_origin(point
);
166 cursor_window_
->SetBounds(bounds
);
169 void CursorWindowController::SetCursor(gfx::NativeCursor cursor
) {
170 if (cursor_type_
== cursor
.native_type())
172 cursor_type_
= cursor
.native_type();
174 UpdateCursorVisibility();
177 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set
) {
178 cursor_set_
= cursor_set
;
182 void CursorWindowController::SetVisibility(bool visible
) {
184 UpdateCursorVisibility();
187 void CursorWindowController::SetContainer(aura::Window
* container
) {
188 if (container_
== container
)
190 container_
= container
;
192 cursor_window_
.reset();
196 // Reusing the window does not work when the display is disconnected.
197 // Just creates a new one instead. crbug.com/384218.
198 cursor_window_
.reset(new aura::Window(delegate_
.get()));
199 cursor_window_
->SetTransparent(true);
200 cursor_window_
->Init(ui::LAYER_TEXTURED
);
201 cursor_window_
->set_ignore_events(true);
202 cursor_window_
->set_owned_by_parent(false);
203 // Call UpdateCursorImage() to figure out |cursor_window_|'s desired size.
206 container
->AddChild(cursor_window_
.get());
207 UpdateCursorVisibility();
208 SetBoundsInScreen(container
->bounds());
211 void CursorWindowController::SetBoundsInScreen(const gfx::Rect
& bounds
) {
212 bounds_in_screen_
= bounds
;
216 void CursorWindowController::UpdateCursorImage() {
218 // TODO(hshi): support custom cursor set.
219 if (!ui::GetCursorDataFor(cursor_set_
,
221 display_
.device_scale_factor(),
226 const gfx::ImageSkia
* image
=
227 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id
);
228 gfx::ImageSkia rotated
= *image
;
229 if (!is_cursor_compositing_enabled_
) {
230 switch (display_
.rotation()) {
231 case gfx::Display::ROTATE_0
:
233 case gfx::Display::ROTATE_90
:
234 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
235 *image
, SkBitmapOperations::ROTATION_90_CW
);
237 rotated
.width() - hot_point_
.y(),
240 case gfx::Display::ROTATE_180
:
241 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
242 *image
, SkBitmapOperations::ROTATION_180_CW
);
244 rotated
.height() - hot_point_
.x(),
245 rotated
.width() - hot_point_
.y());
247 case gfx::Display::ROTATE_270
:
248 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
249 *image
, SkBitmapOperations::ROTATION_270_CW
);
252 rotated
.height() - hot_point_
.x());
256 hot_point_
= ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
259 delegate_
->SetCursorImage(rotated
, display_
);
260 if (cursor_window_
) {
261 cursor_window_
->SetBounds(gfx::Rect(delegate_
->size()));
262 cursor_window_
->SchedulePaintInRect(
263 gfx::Rect(cursor_window_
->bounds().size()));
268 void CursorWindowController::UpdateCursorVisibility() {
271 bool visible
= (visible_
&& cursor_type_
!= ui::kCursorNone
);
273 cursor_window_
->Show();
275 cursor_window_
->Hide();