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/root_window.h"
14 #include "ui/aura/window_delegate.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/gfx/canvas.h"
20 #include "ui/gfx/display.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/image/image_skia_operations.h"
27 class CursorWindowDelegate : public aura::WindowDelegate {
29 CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {}
30 virtual ~CursorWindowDelegate() {}
32 // aura::WindowDelegate overrides:
33 virtual gfx::Size GetMinimumSize() const OVERRIDE { return size_; }
34 virtual gfx::Size GetMaximumSize() const OVERRIDE { return size_; }
35 virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
36 const gfx::Rect& new_bounds) OVERRIDE {}
37 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
38 return gfx::kNullCursor;
40 virtual int GetNonClientComponent(
41 const gfx::Point& point) const OVERRIDE {
44 virtual bool ShouldDescendIntoChildForEventHandling(
46 const gfx::Point& location) OVERRIDE {
49 virtual bool CanFocus() OVERRIDE { return false; }
50 virtual void OnCaptureLost() OVERRIDE {}
51 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
52 canvas->DrawImageInt(cursor_image_, 0, 0);
54 virtual void OnDeviceScaleFactorChanged(
55 float device_scale_factor) OVERRIDE {}
56 virtual void OnWindowDestroying() OVERRIDE {}
57 virtual void OnWindowDestroyed() OVERRIDE {}
58 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
59 virtual bool HasHitTestMask() const OVERRIDE { return false; }
60 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
61 virtual void DidRecreateLayer(ui::Layer* old_layer,
62 ui::Layer* new_layer) 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),
100 cursor_set_(ui::CURSOR_SET_NORMAL),
101 cursor_rotation_(gfx::Display::ROTATE_0),
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 display_ = Shell::GetScreen()->GetPrimaryDisplay();
120 if (is_cursor_compositing_enabled_) {
121 SetDisplay(display_);
123 aura::RootWindow* mirror_root_window = Shell::GetInstance()->
124 display_controller()->mirror_window_controller()->root_window();
125 SetContainer(mirror_root_window ? mirror_root_window->window() : NULL);
129 void CursorWindowController::SetDisplay(const gfx::Display& display) {
130 if (!is_cursor_compositing_enabled_)
134 aura::Window* root_window = Shell::GetInstance()->display_controller()->
135 GetRootWindowForDisplayId(display.id());
139 SetContainer(GetRootWindowController(root_window)->GetContainer(
140 kShellWindowId_OverlayContainer));
141 SetBoundsInScreen(display.bounds());
144 void CursorWindowController::UpdateLocation() {
148 gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
149 if (!is_cursor_compositing_enabled_) {
150 Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->ConvertPointToHost(
153 point.Offset(-bounds_in_screen_.x(), -bounds_in_screen_.y());
155 point.Offset(-hot_point_.x(), -hot_point_.y());
156 gfx::Rect bounds = cursor_window_->bounds();
157 bounds.set_origin(point);
158 cursor_window_->SetBounds(bounds);
161 void CursorWindowController::SetCursor(gfx::NativeCursor cursor) {
162 if (cursor_type_ == cursor.native_type() &&
163 cursor_rotation_ == display_.rotation())
165 cursor_type_ = cursor.native_type();
166 cursor_rotation_ = display_.rotation();
170 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) {
171 cursor_set_ = cursor_set;
175 void CursorWindowController::SetVisibility(bool visible) {
179 cursor_window_->Show();
181 cursor_window_->Hide();
184 void CursorWindowController::SetContainer(aura::Window* container) {
185 if (container_ == container)
188 container_ = container;
190 cursor_window_.reset();
194 if (!cursor_window_) {
195 cursor_window_.reset(new aura::Window(delegate_.get()));
196 cursor_window_->SetTransparent(true);
197 cursor_window_->Init(aura::WINDOW_LAYER_TEXTURED);
198 cursor_window_->set_ignore_events(true);
199 cursor_window_->set_owned_by_parent(false);
202 container->AddChild(cursor_window_.get());
203 cursor_window_->Show();
204 SetBoundsInScreen(container->bounds());
207 void CursorWindowController::SetBoundsInScreen(const gfx::Rect& bounds) {
208 bounds_in_screen_ = bounds;
212 void CursorWindowController::UpdateCursorImage() {
214 // TODO(hshi): support custom cursor set.
215 if (!ui::GetCursorDataFor(cursor_set_,
217 display_.device_scale_factor(),
222 const gfx::ImageSkia* image =
223 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
224 gfx::ImageSkia rotated = *image;
225 if (!is_cursor_compositing_enabled_) {
226 switch (cursor_rotation_) {
227 case gfx::Display::ROTATE_0:
229 case gfx::Display::ROTATE_90:
230 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
231 *image, SkBitmapOperations::ROTATION_90_CW);
233 rotated.width() - hot_point_.y(),
236 case gfx::Display::ROTATE_180:
237 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
238 *image, SkBitmapOperations::ROTATION_180_CW);
240 rotated.height() - hot_point_.x(),
241 rotated.width() - hot_point_.y());
243 case gfx::Display::ROTATE_270:
244 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
245 *image, SkBitmapOperations::ROTATION_270_CW);
248 rotated.height() - hot_point_.x());
252 hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
255 delegate_->SetCursorImage(rotated, display_);
256 if (cursor_window_) {
257 cursor_window_->SetBounds(gfx::Rect(delegate_->size()));
258 cursor_window_->SchedulePaintInRect(
259 gfx::Rect(cursor_window_->bounds().size()));
264 } // namespace internal