Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / display / cursor_window_controller.cc
blob773fe5ca908b2b474728cc3898aecbcb872ab6f3
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"
26 namespace ash {
28 class CursorWindowDelegate : public aura::WindowDelegate {
29 public:
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 {
43 return HTNOWHERE;
45 bool ShouldDescendIntoChildForEventHandling(
46 aura::Window* child,
47 const gfx::Point& location) override {
48 return false;
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());
79 } else {
80 size_ = image.size();
81 cursor_image_ = gfx::ImageSkia(
82 gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor));
86 const gfx::Size size() const { return size_; }
88 private:
89 bool is_cursor_compositing_enabled_;
90 gfx::ImageSkia cursor_image_;
91 gfx::Size size_;
93 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
96 CursorWindowController::CursorWindowController()
97 : is_cursor_compositing_enabled_(false),
98 container_(NULL),
99 cursor_type_(ui::kCursorNone),
100 visible_(true),
101 cursor_set_(ui::CURSOR_SET_NORMAL),
102 delegate_(new CursorWindowDelegate()) {
105 CursorWindowController::~CursorWindowController() {
106 SetContainer(NULL);
109 void CursorWindowController::SetCursorCompositingEnabled(bool enabled) {
110 if (is_cursor_compositing_enabled_ != enabled) {
111 is_cursor_compositing_enabled_ = enabled;
112 delegate_->SetCursorCompositingEnabled(enabled);
113 UpdateCursorImage();
114 UpdateContainer();
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())
125 SetDisplay(display);
126 } else {
127 aura::Window* mirror_window = Shell::GetInstance()->
128 display_controller()->
129 mirror_window_controller()->
130 GetWindow();
131 if (mirror_window)
132 display_ = Shell::GetScreen()->GetPrimaryDisplay();
133 SetContainer(mirror_window);
135 // Updates the hot point based on the current display.
136 UpdateCursorImage();
139 void CursorWindowController::SetDisplay(const gfx::Display& display) {
140 if (!is_cursor_compositing_enabled_)
141 return;
143 display_ = display;
144 aura::Window* root_window = Shell::GetInstance()->display_controller()->
145 GetRootWindowForDisplayId(display.id());
146 if (!root_window)
147 return;
149 SetContainer(GetRootWindowController(root_window)->GetContainer(
150 kShellWindowId_MouseCursorContainer));
151 SetBoundsInScreen(display.bounds());
152 // Updates the hot point based on the current display.
153 UpdateCursorImage();
156 void CursorWindowController::UpdateLocation() {
157 if (!cursor_window_)
158 return;
159 gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
160 if (!is_cursor_compositing_enabled_) {
161 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point);
162 } else {
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())
173 return;
174 cursor_type_ = cursor.native_type();
175 UpdateCursorImage();
176 UpdateCursorVisibility();
179 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) {
180 cursor_set_ = cursor_set;
181 UpdateCursorImage();
184 void CursorWindowController::SetVisibility(bool visible) {
185 if (!cursor_window_)
186 return;
187 visible_ = visible;
188 UpdateCursorVisibility();
191 void CursorWindowController::SetContainer(aura::Window* container) {
192 if (container_ == container)
193 return;
194 container_ = container;
195 if (!container) {
196 cursor_window_.reset();
197 return;
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.
208 UpdateCursorImage();
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;
217 UpdateLocation();
220 void CursorWindowController::UpdateCursorImage() {
221 int resource_id;
222 // TODO(hshi): support custom cursor set.
223 if (!ui::GetCursorDataFor(cursor_set_,
224 cursor_type_,
225 display_.device_scale_factor(),
226 &resource_id,
227 &hot_point_)) {
228 return;
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:
236 break;
237 case gfx::Display::ROTATE_90:
238 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
239 *image, SkBitmapOperations::ROTATION_90_CW);
240 hot_point_.SetPoint(
241 rotated.width() - hot_point_.y(),
242 hot_point_.x());
243 break;
244 case gfx::Display::ROTATE_180:
245 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
246 *image, SkBitmapOperations::ROTATION_180_CW);
247 hot_point_.SetPoint(
248 rotated.height() - hot_point_.x(),
249 rotated.width() - hot_point_.y());
250 break;
251 case gfx::Display::ROTATE_270:
252 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
253 *image, SkBitmapOperations::ROTATION_270_CW);
254 hot_point_.SetPoint(
255 hot_point_.y(),
256 rotated.height() - hot_point_.x());
257 break;
259 } else {
260 hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
261 hot_point_);
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()));
268 UpdateLocation();
272 void CursorWindowController::UpdateCursorVisibility() {
273 if (!cursor_window_)
274 return;
275 bool visible = (visible_ && cursor_type_ != ui::kCursorNone);
276 if (visible)
277 cursor_window_->Show();
278 else
279 cursor_window_->Hide();
282 } // namespace ash