Update base::StartsWith calls to new form.
[chromium-blink-merge.git] / ash / display / cursor_window_controller.cc
blob3594937a55e53142831138296ead183d737a907c
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"
25 namespace ash {
27 class CursorWindowDelegate : public aura::WindowDelegate {
28 public:
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 {
41 return HTNOWHERE;
43 bool ShouldDescendIntoChildForEventHandling(
44 aura::Window* child,
45 const gfx::Point& location) override {
46 return false;
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());
77 } else {
78 size_ = image.size();
79 cursor_image_ = gfx::ImageSkia(
80 gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor));
84 const gfx::Size size() const { return size_; }
86 private:
87 bool is_cursor_compositing_enabled_;
88 gfx::ImageSkia cursor_image_;
89 gfx::Size size_;
91 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
94 CursorWindowController::CursorWindowController()
95 : is_cursor_compositing_enabled_(false),
96 container_(NULL),
97 cursor_type_(ui::kCursorNone),
98 visible_(true),
99 cursor_set_(ui::CURSOR_SET_NORMAL),
100 delegate_(new CursorWindowDelegate()) {
103 CursorWindowController::~CursorWindowController() {
104 SetContainer(NULL);
107 void CursorWindowController::SetCursorCompositingEnabled(bool enabled) {
108 if (is_cursor_compositing_enabled_ != enabled) {
109 is_cursor_compositing_enabled_ = enabled;
110 delegate_->SetCursorCompositingEnabled(enabled);
111 UpdateCursorImage();
112 UpdateContainer();
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())
123 SetDisplay(display);
124 } else {
125 aura::Window* mirror_window = Shell::GetInstance()->
126 display_controller()->
127 mirror_window_controller()->
128 GetWindow();
129 if (mirror_window)
130 display_ = Shell::GetScreen()->GetPrimaryDisplay();
131 SetContainer(mirror_window);
133 // Updates the hot point based on the current display.
134 UpdateCursorImage();
137 void CursorWindowController::SetDisplay(const gfx::Display& display) {
138 if (!is_cursor_compositing_enabled_)
139 return;
141 display_ = display;
142 aura::Window* root_window = Shell::GetInstance()->display_controller()->
143 GetRootWindowForDisplayId(display.id());
144 if (!root_window)
145 return;
147 SetContainer(GetRootWindowController(root_window)->GetContainer(
148 kShellWindowId_MouseCursorContainer));
149 SetBoundsInScreen(display.bounds());
150 // Updates the hot point based on the current display.
151 UpdateCursorImage();
154 void CursorWindowController::UpdateLocation() {
155 if (!cursor_window_)
156 return;
157 gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
158 if (!is_cursor_compositing_enabled_) {
159 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point);
160 } else {
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())
171 return;
172 cursor_type_ = cursor.native_type();
173 UpdateCursorImage();
174 UpdateCursorVisibility();
177 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) {
178 cursor_set_ = cursor_set;
179 UpdateCursorImage();
182 void CursorWindowController::SetVisibility(bool visible) {
183 visible_ = visible;
184 UpdateCursorVisibility();
187 void CursorWindowController::SetContainer(aura::Window* container) {
188 if (container_ == container)
189 return;
190 container_ = container;
191 if (!container) {
192 cursor_window_.reset();
193 return;
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.
204 UpdateCursorImage();
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;
213 UpdateLocation();
216 void CursorWindowController::UpdateCursorImage() {
217 int resource_id;
218 // TODO(hshi): support custom cursor set.
219 if (!ui::GetCursorDataFor(cursor_set_,
220 cursor_type_,
221 display_.device_scale_factor(),
222 &resource_id,
223 &hot_point_)) {
224 return;
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:
232 break;
233 case gfx::Display::ROTATE_90:
234 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
235 *image, SkBitmapOperations::ROTATION_90_CW);
236 hot_point_.SetPoint(
237 rotated.width() - hot_point_.y(),
238 hot_point_.x());
239 break;
240 case gfx::Display::ROTATE_180:
241 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
242 *image, SkBitmapOperations::ROTATION_180_CW);
243 hot_point_.SetPoint(
244 rotated.height() - hot_point_.x(),
245 rotated.width() - hot_point_.y());
246 break;
247 case gfx::Display::ROTATE_270:
248 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
249 *image, SkBitmapOperations::ROTATION_270_CW);
250 hot_point_.SetPoint(
251 hot_point_.y(),
252 rotated.height() - hot_point_.x());
253 break;
255 } else {
256 hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
257 hot_point_);
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()));
264 UpdateLocation();
268 void CursorWindowController::UpdateCursorVisibility() {
269 if (!cursor_window_)
270 return;
271 bool visible = (visible_ && cursor_type_ != ui::kCursorNone);
272 if (visible)
273 cursor_window_->Show();
274 else
275 cursor_window_->Hide();
278 } // namespace ash