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_context.h"
20 #include "ui/compositor/paint_recorder.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/gfx/display.h"
23 #include "ui/gfx/image/image_skia.h"
24 #include "ui/gfx/image/image_skia_operations.h"
28 class CursorWindowDelegate
: public aura::WindowDelegate
{
30 CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {}
31 ~CursorWindowDelegate() override
{}
33 // aura::WindowDelegate overrides:
34 gfx::Size
GetMinimumSize() const override
{ return size_
; }
35 gfx::Size
GetMaximumSize() const override
{ return size_
; }
36 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
37 const gfx::Rect
& new_bounds
) override
{}
38 ui::TextInputClient
* GetFocusedTextInputClient() override
{ return nullptr; }
39 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
40 return gfx::kNullCursor
;
42 int GetNonClientComponent(const gfx::Point
& point
) const override
{
45 bool ShouldDescendIntoChildForEventHandling(
47 const gfx::Point
& location
) override
{
50 bool CanFocus() override
{ return false; }
51 void OnCaptureLost() override
{}
52 void OnPaint(const ui::PaintContext
& context
) override
{
53 // No need to cache the output here, the CursorWindow is not invalidated.
54 ui::PaintRecorder
recorder(context
);
55 recorder
.canvas()->DrawImageInt(cursor_image_
, 0, 0);
57 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
58 void OnWindowDestroying(aura::Window
* window
) override
{}
59 void OnWindowDestroyed(aura::Window
* window
) override
{}
60 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
61 bool HasHitTestMask() const override
{ return false; }
62 void GetHitTestMask(gfx::Path
* mask
) const override
{}
64 // Sets cursor compositing mode on/off.
65 void SetCursorCompositingEnabled(bool enabled
) {
66 is_cursor_compositing_enabled_
= enabled
;
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());
81 cursor_image_
= gfx::ImageSkia(
82 gfx::ImageSkiaRep(image_rep
.sk_bitmap(), scale_factor
));
86 const gfx::Size
size() const { return size_
; }
89 bool is_cursor_compositing_enabled_
;
90 gfx::ImageSkia cursor_image_
;
93 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate
);
96 CursorWindowController::CursorWindowController()
97 : is_cursor_compositing_enabled_(false),
99 cursor_type_(ui::kCursorNone
),
101 cursor_set_(ui::CURSOR_SET_NORMAL
),
102 delegate_(new CursorWindowDelegate()) {
105 CursorWindowController::~CursorWindowController() {
109 void CursorWindowController::SetCursorCompositingEnabled(bool enabled
) {
110 if (is_cursor_compositing_enabled_
!= enabled
) {
111 is_cursor_compositing_enabled_
= enabled
;
112 delegate_
->SetCursorCompositingEnabled(enabled
);
118 void CursorWindowController::UpdateContainer() {
119 if (is_cursor_compositing_enabled_
) {
120 gfx::Screen
* screen
= Shell::GetScreen();
121 gfx::Display display
= screen
->GetDisplayNearestPoint(
122 screen
->GetCursorScreenPoint());
123 DCHECK(display
.is_valid());
124 if (display
.is_valid())
127 aura::Window
* mirror_window
= Shell::GetInstance()->
128 display_controller()->
129 mirror_window_controller()->
132 display_
= Shell::GetScreen()->GetPrimaryDisplay();
133 SetContainer(mirror_window
);
135 // Updates the hot point based on the current display.
139 void CursorWindowController::SetDisplay(const gfx::Display
& display
) {
140 if (!is_cursor_compositing_enabled_
)
144 aura::Window
* root_window
= Shell::GetInstance()->display_controller()->
145 GetRootWindowForDisplayId(display
.id());
149 SetContainer(GetRootWindowController(root_window
)->GetContainer(
150 kShellWindowId_MouseCursorContainer
));
151 SetBoundsInScreen(display
.bounds());
152 // Updates the hot point based on the current display.
156 void CursorWindowController::UpdateLocation() {
159 gfx::Point point
= aura::Env::GetInstance()->last_mouse_location();
160 if (!is_cursor_compositing_enabled_
) {
161 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point
);
163 point
.Offset(-bounds_in_screen_
.x(), -bounds_in_screen_
.y());
165 point
.Offset(-hot_point_
.x(), -hot_point_
.y());
166 gfx::Rect bounds
= cursor_window_
->bounds();
167 bounds
.set_origin(point
);
168 cursor_window_
->SetBounds(bounds
);
171 void CursorWindowController::SetCursor(gfx::NativeCursor cursor
) {
172 if (cursor_type_
== cursor
.native_type())
174 cursor_type_
= cursor
.native_type();
176 UpdateCursorVisibility();
179 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set
) {
180 cursor_set_
= cursor_set
;
184 void CursorWindowController::SetVisibility(bool visible
) {
188 UpdateCursorVisibility();
191 void CursorWindowController::SetContainer(aura::Window
* container
) {
192 if (container_
== container
)
194 container_
= container
;
196 cursor_window_
.reset();
200 // Reusing the window does not work when the display is disconnected.
201 // Just creates a new one instead. crbug.com/384218.
202 cursor_window_
.reset(new aura::Window(delegate_
.get()));
203 cursor_window_
->SetTransparent(true);
204 cursor_window_
->Init(ui::LAYER_TEXTURED
);
205 cursor_window_
->set_ignore_events(true);
206 cursor_window_
->set_owned_by_parent(false);
207 // Call UpdateCursorImage() to figure out |cursor_window_|'s desired size.
210 container
->AddChild(cursor_window_
.get());
211 UpdateCursorVisibility();
212 SetBoundsInScreen(container
->bounds());
215 void CursorWindowController::SetBoundsInScreen(const gfx::Rect
& bounds
) {
216 bounds_in_screen_
= bounds
;
220 void CursorWindowController::UpdateCursorImage() {
222 // TODO(hshi): support custom cursor set.
223 if (!ui::GetCursorDataFor(cursor_set_
,
225 display_
.device_scale_factor(),
230 const gfx::ImageSkia
* image
=
231 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id
);
232 gfx::ImageSkia rotated
= *image
;
233 if (!is_cursor_compositing_enabled_
) {
234 switch (display_
.rotation()) {
235 case gfx::Display::ROTATE_0
:
237 case gfx::Display::ROTATE_90
:
238 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
239 *image
, SkBitmapOperations::ROTATION_90_CW
);
241 rotated
.width() - hot_point_
.y(),
244 case gfx::Display::ROTATE_180
:
245 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
246 *image
, SkBitmapOperations::ROTATION_180_CW
);
248 rotated
.height() - hot_point_
.x(),
249 rotated
.width() - hot_point_
.y());
251 case gfx::Display::ROTATE_270
:
252 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
253 *image
, SkBitmapOperations::ROTATION_270_CW
);
256 rotated
.height() - hot_point_
.x());
260 hot_point_
= ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
263 delegate_
->SetCursorImage(rotated
, display_
);
264 if (cursor_window_
) {
265 cursor_window_
->SetBounds(gfx::Rect(delegate_
->size()));
266 cursor_window_
->SchedulePaintInRect(
267 gfx::Rect(cursor_window_
->bounds().size()));
272 void CursorWindowController::UpdateCursorVisibility() {
275 bool visible
= (visible_
&& cursor_type_
!= ui::kCursorNone
);
277 cursor_window_
->Show();
279 cursor_window_
->Hide();