OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/exo/pointer.h" | 5 #include "components/exo/pointer.h" |
6 | 6 |
7 #include "ash/common/shell_window_ids.h" | 7 #include "ash/common/shell_window_ids.h" |
8 #include "ash/display/display_info.h" | 8 #include "ash/display/display_info.h" |
9 #include "ash/display/display_manager.h" | 9 #include "ash/display/display_manager.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "components/exo/pointer_delegate.h" | 11 #include "components/exo/pointer_delegate.h" |
12 #include "components/exo/surface.h" | 12 #include "components/exo/surface.h" |
13 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
15 #include "ui/events/event.h" | 15 #include "ui/events/event.h" |
16 #include "ui/gfx/geometry/vector2d_conversions.h" | 16 #include "ui/gfx/geometry/vector2d_conversions.h" |
17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
18 | 18 |
19 namespace exo { | 19 namespace exo { |
20 namespace { | 20 namespace { |
21 | 21 |
| 22 static constexpr float kLargeCursorScale = 2.8; |
| 23 |
22 // Synthesized events typically lack floating point precision so to avoid | 24 // Synthesized events typically lack floating point precision so to avoid |
23 // generating mouse event jitter we consider the location of these events | 25 // generating mouse event jitter we consider the location of these events |
24 // to be the same as |location| if floored values match. | 26 // to be the same as |location| if floored values match. |
25 bool SameLocation(const ui::LocatedEvent* event, const gfx::PointF& location) { | 27 bool SameLocation(const ui::LocatedEvent* event, const gfx::PointF& location) { |
26 if (event->flags() & ui::EF_IS_SYNTHESIZED) | 28 if (event->flags() & ui::EF_IS_SYNTHESIZED) |
27 return event->location() == gfx::ToFlooredPoint(location); | 29 return event->location() == gfx::ToFlooredPoint(location); |
28 | 30 |
29 return event->location_f() == location; | 31 return event->location_f() == location; |
30 } | 32 } |
31 | 33 |
32 } // namespace | 34 } // namespace |
33 | 35 |
34 //////////////////////////////////////////////////////////////////////////////// | 36 //////////////////////////////////////////////////////////////////////////////// |
35 // Pointer, public: | 37 // Pointer, public: |
36 | 38 |
37 Pointer::Pointer(PointerDelegate* delegate) | 39 Pointer::Pointer(PointerDelegate* delegate) |
38 : delegate_(delegate), | 40 : delegate_(delegate), |
39 surface_(nullptr), | 41 surface_(nullptr), |
40 focus_(nullptr), | 42 focus_(nullptr), |
41 cursor_scale_(1.0f) { | 43 cursor_scale_(1.0f) { |
42 ash::Shell::GetInstance()->AddPreTargetHandler(this); | 44 ash::Shell* ash_shell = ash::Shell::GetInstance(); |
| 45 ash_shell->AddPreTargetHandler(this); |
| 46 |
| 47 wm::CursorManager* cursor_manager = ash_shell->cursor_manager(); |
| 48 DCHECK(cursor_manager); |
| 49 cursor_manager->AddObserver(this); |
43 } | 50 } |
44 | 51 |
45 Pointer::~Pointer() { | 52 Pointer::~Pointer() { |
46 delegate_->OnPointerDestroying(this); | 53 delegate_->OnPointerDestroying(this); |
47 if (surface_) | 54 if (surface_) |
48 surface_->RemoveSurfaceObserver(this); | 55 surface_->RemoveSurfaceObserver(this); |
49 if (focus_) { | 56 if (focus_) { |
50 focus_->RemoveSurfaceObserver(this); | 57 focus_->RemoveSurfaceObserver(this); |
51 focus_->UnregisterCursorProvider(this); | 58 focus_->UnregisterCursorProvider(this); |
52 } | 59 } |
53 if (widget_) | 60 if (widget_) |
54 widget_->CloseNow(); | 61 widget_->CloseNow(); |
55 ash::Shell::GetInstance()->RemovePreTargetHandler(this); | 62 |
| 63 ash::Shell* ash_shell = ash::Shell::GetInstance(); |
| 64 DCHECK(ash_shell->cursor_manager()); |
| 65 ash_shell->cursor_manager()->RemoveObserver(this); |
| 66 ash_shell->RemovePreTargetHandler(this); |
56 } | 67 } |
57 | 68 |
58 void Pointer::SetCursor(Surface* surface, const gfx::Point& hotspot) { | 69 void Pointer::SetCursor(Surface* surface, const gfx::Point& hotspot) { |
59 // Early out if the pointer doesn't have a surface in focus. | 70 // Early out if the pointer doesn't have a surface in focus. |
60 if (!focus_) | 71 if (!focus_) |
61 return; | 72 return; |
62 | 73 |
63 // If surface is different than the current pointer surface then remove the | 74 // If surface is different than the current pointer surface then remove the |
64 // current surface and add the new surface. | 75 // current surface and add the new surface. |
65 if (surface != surface_) { | 76 if (surface != surface_) { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 CreatePointerWidget(); | 215 CreatePointerWidget(); |
205 | 216 |
206 // Update cursor location if mouse event caused it to change. | 217 // Update cursor location if mouse event caused it to change. |
207 gfx::Point mouse_location = aura::Env::GetInstance()->last_mouse_location(); | 218 gfx::Point mouse_location = aura::Env::GetInstance()->last_mouse_location(); |
208 if (mouse_location != widget_->GetNativeWindow()->bounds().origin()) { | 219 if (mouse_location != widget_->GetNativeWindow()->bounds().origin()) { |
209 gfx::Rect bounds = widget_->GetNativeWindow()->bounds(); | 220 gfx::Rect bounds = widget_->GetNativeWindow()->bounds(); |
210 bounds.set_origin(mouse_location); | 221 bounds.set_origin(mouse_location); |
211 widget_->GetNativeWindow()->SetBounds(bounds); | 222 widget_->GetNativeWindow()->SetBounds(bounds); |
212 } | 223 } |
213 | 224 |
214 // Update cursor scale if the effective UI scale has changed since last | 225 UpdateCursorScale(); |
215 // mouse event. | |
216 display::Display display = | |
217 display::Screen::GetScreen()->GetDisplayNearestWindow( | |
218 widget_->GetNativeWindow()); | |
219 float ui_scale = ash::Shell::GetInstance() | |
220 ->display_manager() | |
221 ->GetDisplayInfo(display.id()) | |
222 .GetEffectiveUIScale(); | |
223 if (ui_scale != cursor_scale_) { | |
224 gfx::Transform transform; | |
225 transform.Scale(ui_scale, ui_scale); | |
226 widget_->GetNativeWindow()->SetTransform(transform); | |
227 cursor_scale_ = ui_scale; | |
228 } | |
229 } else { | 226 } else { |
230 if (widget_ && widget_->IsVisible()) | 227 if (widget_ && widget_->IsVisible()) |
231 widget_->Hide(); | 228 widget_->Hide(); |
232 } | 229 } |
233 } | 230 } |
234 | 231 |
235 void Pointer::OnScrollEvent(ui::ScrollEvent* event) { | 232 void Pointer::OnScrollEvent(ui::ScrollEvent* event) { |
236 OnMouseEvent(event); | 233 OnMouseEvent(event); |
237 } | 234 } |
238 | 235 |
| 236 void Pointer::OnCursorSetChanged(ui::CursorSetType cursor_set) { |
| 237 UpdateCursorScale(); |
| 238 } |
| 239 |
239 //////////////////////////////////////////////////////////////////////////////// | 240 //////////////////////////////////////////////////////////////////////////////// |
240 // SurfaceDelegate overrides: | 241 // SurfaceDelegate overrides: |
241 | 242 |
242 void Pointer::OnSurfaceCommit() { | 243 void Pointer::OnSurfaceCommit() { |
243 surface_->CheckIfSurfaceHierarchyNeedsCommitToNewSurfaces(); | 244 surface_->CheckIfSurfaceHierarchyNeedsCommitToNewSurfaces(); |
244 surface_->CommitSurfaceHierarchy(); | 245 surface_->CommitSurfaceHierarchy(); |
245 surface_->window()->SetBounds( | 246 surface_->window()->SetBounds( |
246 gfx::Rect(gfx::Point() - hotspot_.OffsetFromOrigin(), | 247 gfx::Rect(gfx::Point() - hotspot_.OffsetFromOrigin(), |
247 surface_->window()->layer()->size())); | 248 surface_->window()->layer()->size())); |
248 } | 249 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 | 288 |
288 Surface* Pointer::GetEffectiveTargetForEvent(ui::Event* event) const { | 289 Surface* Pointer::GetEffectiveTargetForEvent(ui::Event* event) const { |
289 Surface* target = | 290 Surface* target = |
290 Surface::AsSurface(static_cast<aura::Window*>(event->target())); | 291 Surface::AsSurface(static_cast<aura::Window*>(event->target())); |
291 if (!target) | 292 if (!target) |
292 return nullptr; | 293 return nullptr; |
293 | 294 |
294 return delegate_->CanAcceptPointerEventsForSurface(target) ? target : nullptr; | 295 return delegate_->CanAcceptPointerEventsForSurface(target) ? target : nullptr; |
295 } | 296 } |
296 | 297 |
| 298 void Pointer::UpdateCursorScale() { |
| 299 if (!focus_) |
| 300 return; |
| 301 |
| 302 // Update cursor scale if the effective UI scale has changed. |
| 303 display::Display display = |
| 304 display::Screen::GetScreen()->GetDisplayNearestWindow( |
| 305 widget_->GetNativeWindow()); |
| 306 float ui_scale = ash::Shell::GetInstance() |
| 307 ->display_manager() |
| 308 ->GetDisplayInfo(display.id()) |
| 309 .GetEffectiveUIScale(); |
| 310 |
| 311 ash::Shell* ash_shell = ash::Shell::GetInstance(); |
| 312 if (ash_shell->cursor_manager()->GetCursorSet() == ui::CURSOR_SET_LARGE) |
| 313 ui_scale *= kLargeCursorScale; |
| 314 |
| 315 if (ui_scale != cursor_scale_) { |
| 316 gfx::Transform transform; |
| 317 transform.Scale(ui_scale, ui_scale); |
| 318 widget_->GetNativeWindow()->SetTransform(transform); |
| 319 cursor_scale_ = ui_scale; |
| 320 } |
| 321 } |
| 322 |
297 } // namespace exo | 323 } // namespace exo |
OLD | NEW |