OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "ash/laser/laser_pointer_controller.h" | |
6 | |
7 #include "ash/laser/laser_pointer_view.h" | |
8 #include "ash/shell.h" | |
9 #include "ui/aura/window_event_dispatcher.h" | |
10 #include "ui/aura/window_tree_host.h" | |
11 #include "ui/views/widget/widget.h" | |
12 | |
13 #if defined(OS_CHROMEOS) | |
14 #include "ash/common/system/chromeos/palette/palette_utils.h" | |
15 #endif | |
16 | |
17 namespace ash { | |
18 namespace { | |
19 | |
20 // A point gets removed from the collection if it is older than | |
21 // |kPointLifeDurationMs|. | |
22 const int kPointLifeDurationMs = 200; | |
23 | |
24 // When no move events are being recieved we add a new point every | |
25 // |kAddStationaryPointsDelayMs| so that points older than | |
26 // |kPointLifeDurationMs| can get removed. | |
27 const int kAddStationaryPointsDelayMs = 5; | |
28 | |
29 // Returns true if the event should be processed normally, ie, the stylus is | |
30 // over the palette icon or widget. | |
31 bool ShouldSkipEventFiltering(const gfx::Point& point) { | |
32 #if defined(OS_CHROMEOS) | |
33 return PaletteContainsPointInScreen(point); | |
34 #else | |
35 return false; | |
36 #endif | |
37 } | |
38 | |
39 aura::Window* GetCurrentRootWindow() { | |
40 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | |
41 for (aura::Window* root_window : root_windows) { | |
42 if (root_window->ContainsPointInRoot( | |
43 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) | |
44 return root_window; | |
45 } | |
46 return nullptr; | |
47 } | |
48 } // namespace | |
jdufault
2016/09/12 21:10:11
newline above
sammiequon
2016/09/13 01:09:56
Done.
| |
49 | |
50 LaserPointerController::LaserPointerController() { | |
51 timer_.reset(new base::Timer( | |
52 FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), | |
53 base::Bind(&LaserPointerController::AddStationaryPoint, | |
54 base::Unretained(this)), | |
55 true)); | |
56 } | |
57 | |
58 LaserPointerController::~LaserPointerController() { | |
59 Shell::GetInstance()->RemovePreTargetHandler(this); | |
60 } | |
61 | |
62 void LaserPointerController::SetEnabled(bool enabled) { | |
63 if (enabled == enabled_) | |
64 return; | |
65 | |
66 enabled_ = enabled; | |
67 if (enabled_) { | |
68 laser_pointer_view_.reset(new LaserPointerView( | |
69 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), | |
70 GetCurrentRootWindow())); | |
71 Shell::GetInstance()->AddPreTargetHandler(this); | |
72 } else { | |
73 Shell::GetInstance()->RemovePreTargetHandler(this); | |
74 StopTimer(); | |
75 laser_pointer_view_.reset(); | |
76 } | |
77 } | |
78 | |
79 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | |
80 SwitchTargetRootWindowIfNeeded(); | |
81 } | |
82 | |
83 void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { | |
84 OnLocatedEvent(event, event->pointer_details()); | |
85 } | |
86 | |
87 void LaserPointerController::OnTouchEvent(ui::TouchEvent* event) { | |
jdufault
2016/09/12 21:10:11
We can probably remove the OnTouchEvent override,
sammiequon
2016/09/13 01:09:56
Done.
| |
88 OnLocatedEvent(event, event->pointer_details()); | |
89 } | |
90 | |
91 void LaserPointerController::OnLocatedEvent( | |
92 ui::LocatedEvent* event, | |
93 const ui::PointerDetails& pointer_details) { | |
94 if (!enabled_) | |
95 return; | |
96 | |
97 if (pointer_details.pointer_type != ui::EventPointerType::POINTER_TYPE_PEN) | |
98 return; | |
99 | |
100 if (event->type() != ui::ET_MOUSE_DRAGGED && | |
101 event->type() != ui::ET_MOUSE_PRESSED && | |
102 event->type() != ui::ET_MOUSE_RELEASED) | |
103 return; | |
104 | |
105 SwitchTargetRootWindowIfNeeded(); | |
106 | |
107 // Remap point from where it was captured to the display it is actually on. | |
108 gfx::Point event_location = event->root_location(); | |
109 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
110 aura::Window* event_root = target->GetRootWindow(); | |
111 aura::Window::ConvertPointToTarget( | |
112 event_root, laser_pointer_view_->GetRootWindow(), &event_location); | |
113 | |
114 current_mouse_location_ = event_location; | |
115 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
116 | |
117 timer_repeat_count_ = 0; | |
118 if (event->type() == ui::ET_MOUSE_DRAGGED) { | |
119 // Start the timer to add stationary points if dragged. | |
120 if (!timer_->IsRunning()) | |
121 timer_->Reset(); | |
122 } else if (event->type() == ui::ET_MOUSE_RELEASED) { | |
123 StopTimer(); | |
124 laser_pointer_view_->Stop(); | |
125 } | |
126 | |
127 // Consume the event unless it is over the palette tray or palette shelf | |
128 // button. | |
129 if (!ShouldSkipEventFiltering(current_mouse_location_)) | |
130 event->StopPropagation(); | |
131 } | |
132 | |
133 void LaserPointerController::StopTimer() { | |
134 timer_repeat_count_ = 0; | |
135 timer_->Stop(); | |
136 } | |
137 | |
138 void LaserPointerController::SwitchTargetRootWindowIfNeeded() { | |
139 if (enabled_) { | |
140 // If the current root window has changed, remake the laser pointer view | |
141 // with the new root window. | |
142 if (laser_pointer_view_->GetRootWindow() != GetCurrentRootWindow()) { | |
143 laser_pointer_view_.reset(new LaserPointerView( | |
144 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), | |
145 GetCurrentRootWindow())); | |
146 } | |
147 } else { | |
148 laser_pointer_view_.reset(); | |
149 } | |
150 } | |
151 | |
152 void LaserPointerController::AddStationaryPoint() { | |
153 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
154 // We can stop repeating the timer once the mouse has been stationary for | |
155 // longer than the life of a point. | |
156 if (timer_repeat_count_++ * kAddStationaryPointsDelayMs >= | |
157 kPointLifeDurationMs) { | |
158 StopTimer(); | |
159 } | |
160 } | |
161 } // namespace ash | |
OLD | NEW |