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 ui::TextInputClient
* GetFocusedTextInputClient() override
{ return nullptr; }
38 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
39 return gfx::kNullCursor
;
41 int GetNonClientComponent(const gfx::Point
& point
) const override
{
44 bool ShouldDescendIntoChildForEventHandling(
46 const gfx::Point
& location
) override
{
49 bool CanFocus() override
{ return false; }
50 void OnCaptureLost() override
{}
51 void OnPaint(const ui::PaintContext
& context
) override
{
52 // No need to cache the output here, the CursorWindow is not invalidated.
53 ui::PaintRecorder
recorder(context
);
54 recorder
.canvas()->DrawImageInt(cursor_image_
, 0, 0);
56 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
57 void OnWindowDestroying(aura::Window
* window
) override
{}
58 void OnWindowDestroyed(aura::Window
* window
) override
{}
59 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
60 bool HasHitTestMask() const override
{ return false; }
61 void GetHitTestMask(gfx::Path
* mask
) const override
{}
63 // Sets cursor compositing mode on/off.
64 void SetCursorCompositingEnabled(bool enabled
) {
65 is_cursor_compositing_enabled_
= enabled
;
68 // Sets the cursor image for the |display|'s scale factor.
69 void SetCursorImage(const gfx::ImageSkia
& image
,
70 const gfx::Display
& display
) {
71 float scale_factor
= display
.device_scale_factor();
72 const gfx::ImageSkiaRep
& image_rep
= image
.GetRepresentation(scale_factor
);
73 if (!is_cursor_compositing_enabled_
) {
74 // Note that mirror window's scale factor is always 1.0f, therefore we
75 // need to take 2x's image and paint as if it's 1x image.
76 size_
= image_rep
.pixel_size();
77 cursor_image_
= gfx::ImageSkia::CreateFrom1xBitmap(image_rep
.sk_bitmap());
80 cursor_image_
= gfx::ImageSkia(
81 gfx::ImageSkiaRep(image_rep
.sk_bitmap(), scale_factor
));
85 const gfx::Size
size() const { return size_
; }
88 bool is_cursor_compositing_enabled_
;
89 gfx::ImageSkia cursor_image_
;
92 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate
);
95 CursorWindowController::CursorWindowController()
96 : is_cursor_compositing_enabled_(false),
98 cursor_type_(ui::kCursorNone
),
100 cursor_set_(ui::CURSOR_SET_NORMAL
),
101 delegate_(new CursorWindowDelegate()) {
104 CursorWindowController::~CursorWindowController() {
108 void CursorWindowController::SetCursorCompositingEnabled(bool enabled
) {
109 if (is_cursor_compositing_enabled_
!= enabled
) {
110 is_cursor_compositing_enabled_
= enabled
;
111 delegate_
->SetCursorCompositingEnabled(enabled
);
117 void CursorWindowController::UpdateContainer() {
118 if (is_cursor_compositing_enabled_
) {
119 gfx::Screen
* screen
= Shell::GetScreen();
120 gfx::Display display
= screen
->GetDisplayNearestPoint(
121 screen
->GetCursorScreenPoint());
122 DCHECK(display
.is_valid());
123 if (display
.is_valid())
126 aura::Window
* mirror_window
= Shell::GetInstance()->
127 display_controller()->
128 mirror_window_controller()->
131 display_
= Shell::GetScreen()->GetPrimaryDisplay();
132 SetContainer(mirror_window
);
134 // Updates the hot point based on the current display.
138 void CursorWindowController::SetDisplay(const gfx::Display
& display
) {
139 if (!is_cursor_compositing_enabled_
)
143 aura::Window
* root_window
= Shell::GetInstance()->display_controller()->
144 GetRootWindowForDisplayId(display
.id());
148 SetContainer(GetRootWindowController(root_window
)->GetContainer(
149 kShellWindowId_MouseCursorContainer
));
150 SetBoundsInScreen(display
.bounds());
151 // Updates the hot point based on the current display.
155 void CursorWindowController::UpdateLocation() {
158 gfx::Point point
= aura::Env::GetInstance()->last_mouse_location();
159 if (!is_cursor_compositing_enabled_
) {
160 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point
);
162 point
.Offset(-bounds_in_screen_
.x(), -bounds_in_screen_
.y());
164 point
.Offset(-hot_point_
.x(), -hot_point_
.y());
165 gfx::Rect bounds
= cursor_window_
->bounds();
166 bounds
.set_origin(point
);
167 cursor_window_
->SetBounds(bounds
);
170 void CursorWindowController::SetCursor(gfx::NativeCursor cursor
) {
171 if (cursor_type_
== cursor
.native_type())
173 cursor_type_
= cursor
.native_type();
175 UpdateCursorVisibility();
178 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set
) {
179 cursor_set_
= cursor_set
;
183 void CursorWindowController::SetVisibility(bool visible
) {
185 UpdateCursorVisibility();
188 void CursorWindowController::SetContainer(aura::Window
* container
) {
189 if (container_
== container
)
191 container_
= container
;
193 cursor_window_
.reset();
197 // Reusing the window does not work when the display is disconnected.
198 // Just creates a new one instead. crbug.com/384218.
199 cursor_window_
.reset(new aura::Window(delegate_
.get()));
200 cursor_window_
->SetTransparent(true);
201 cursor_window_
->Init(ui::LAYER_TEXTURED
);
202 cursor_window_
->set_ignore_events(true);
203 cursor_window_
->set_owned_by_parent(false);
204 // Call UpdateCursorImage() to figure out |cursor_window_|'s desired size.
207 container
->AddChild(cursor_window_
.get());
208 UpdateCursorVisibility();
209 SetBoundsInScreen(container
->bounds());
212 void CursorWindowController::SetBoundsInScreen(const gfx::Rect
& bounds
) {
213 bounds_in_screen_
= bounds
;
217 void CursorWindowController::UpdateCursorImage() {
219 // TODO(hshi): support custom cursor set.
220 if (!ui::GetCursorDataFor(cursor_set_
,
222 display_
.device_scale_factor(),
227 const gfx::ImageSkia
* image
=
228 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id
);
229 gfx::ImageSkia rotated
= *image
;
230 if (!is_cursor_compositing_enabled_
) {
231 switch (display_
.rotation()) {
232 case gfx::Display::ROTATE_0
:
234 case gfx::Display::ROTATE_90
:
235 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
236 *image
, SkBitmapOperations::ROTATION_90_CW
);
238 rotated
.width() - hot_point_
.y(),
241 case gfx::Display::ROTATE_180
:
242 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
243 *image
, SkBitmapOperations::ROTATION_180_CW
);
245 rotated
.height() - hot_point_
.x(),
246 rotated
.width() - hot_point_
.y());
248 case gfx::Display::ROTATE_270
:
249 rotated
= gfx::ImageSkiaOperations::CreateRotatedImage(
250 *image
, SkBitmapOperations::ROTATION_270_CW
);
253 rotated
.height() - hot_point_
.x());
257 hot_point_
= ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
260 delegate_
->SetCursorImage(rotated
, display_
);
261 if (cursor_window_
) {
262 cursor_window_
->SetBounds(gfx::Rect(delegate_
->size()));
263 cursor_window_
->SchedulePaintInRect(
264 gfx::Rect(cursor_window_
->bounds().size()));
269 void CursorWindowController::UpdateCursorVisibility() {
272 bool visible
= (visible_
&& cursor_type_
!= ui::kCursorNone
);
274 cursor_window_
->Show();
276 cursor_window_
->Hide();