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