1 // Copyright (c) 2012 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/wm/image_cursors.h"
9 #include "base/logging.h"
10 #include "base/string16.h"
11 #include "ui/base/cursor/cursor.h"
12 #include "ui/base/cursor/cursor_loader.h"
13 #include "ui/base/cursor/cursors_aura.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/point.h"
19 const int kImageCursorIds
[] = {
23 ui::kCursorNotAllowed
,
27 ui::kCursorNorthEastResize
,
28 ui::kCursorSouthWestResize
,
29 ui::kCursorSouthEastResize
,
30 ui::kCursorNorthWestResize
,
31 ui::kCursorNorthResize
,
32 ui::kCursorSouthResize
,
33 ui::kCursorEastResize
,
34 ui::kCursorWestResize
,
38 ui::kCursorContextMenu
,
41 ui::kCursorVerticalText
,
45 ui::kCursorColumnResize
,
46 ui::kCursorEastWestResize
,
47 ui::kCursorNorthSouthResize
,
48 ui::kCursorNorthEastSouthWestResize
,
49 ui::kCursorNorthWestSouthEastResize
,
54 const int kAnimatedCursorIds
[] = {
59 ImageCursors::ImageCursors() : scale_(1.f
) {
62 ImageCursors::~ImageCursors() {
65 gfx::Display
ImageCursors::GetDisplay() const {
66 if (!cursor_loader_
) {
68 // Returning default on release build as it's not serious enough to crash
69 // even if this ever happens.
70 return gfx::Display();
72 return cursor_loader_
->display();
75 bool ImageCursors::SetDisplay(const gfx::Display
& display
) {
76 float device_scale_factor
= display
.device_scale_factor();
77 if (!cursor_loader_
) {
78 cursor_loader_
.reset(ui::CursorLoader::Create());
79 cursor_loader_
->set_scale(scale_
);
80 } else if (cursor_loader_
->display().rotation() == display
.rotation() &&
81 cursor_loader_
->display().device_scale_factor() ==
82 device_scale_factor
) {
86 cursor_loader_
->set_display(display
);
91 void ImageCursors::ReloadCursors() {
92 const gfx::Display
& display
= cursor_loader_
->display();
93 float device_scale_factor
= display
.device_scale_factor();
95 cursor_loader_
->UnloadAll();
97 for (size_t i
= 0; i
< arraysize(kImageCursorIds
); ++i
) {
100 bool success
= ui::GetCursorDataFor(kImageCursorIds
[i
],
105 cursor_loader_
->LoadImageCursor(kImageCursorIds
[i
], resource_id
, hot_point
);
107 for (size_t i
= 0; i
< arraysize(kAnimatedCursorIds
); ++i
) {
108 int resource_id
= -1;
109 gfx::Point hot_point
;
110 bool success
= ui::GetAnimatedCursorDataFor(kAnimatedCursorIds
[i
],
115 cursor_loader_
->LoadAnimatedCursor(kAnimatedCursorIds
[i
],
118 ui::kAnimatedCursorFrameDelayMs
);
122 void ImageCursors::SetScale(float scale
) {
123 if (scale
< FLT_EPSILON
) {
124 NOTREACHED() << "Scale must be bigger than 0.";
130 if (cursor_loader_
.get()) {
131 cursor_loader_
->set_scale(scale
);
136 void ImageCursors::SetPlatformCursor(gfx::NativeCursor
* cursor
) {
137 cursor_loader_
->SetPlatformCursor(cursor
);
140 void ImageCursors::SetCursorResourceModule(const base::string16
& module_name
) {
141 cursor_loader_
->SetCursorResourceModule(module_name
);