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 aura::Window* GetCurrentRootWindow() { | |
28 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | |
29 for (aura::Window* root_window : root_windows) { | |
30 if (root_window->ContainsPointInRoot( | |
31 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) | |
32 return root_window; | |
33 } | |
34 return nullptr; | |
35 } | |
36 } // namespace | |
37 | |
38 LaserPointerController::LaserPointerController() | |
39 : stationary_timer_(new base::Timer( | |
40 FROM_HERE, | |
41 base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), | |
42 base::Bind(&LaserPointerController::AddStationaryPoint, | |
43 base::Unretained(this)), | |
44 true /* is_repeating */)) {} | |
45 | |
46 LaserPointerController::~LaserPointerController() { | |
47 Shell::GetInstance()->RemovePreTargetHandler(this); | |
48 } | |
49 | |
50 void LaserPointerController::SetEnabled(bool enabled) { | |
51 if (enabled == enabled_) | |
52 return; | |
53 | |
54 enabled_ = enabled; | |
55 if (enabled_) | |
56 Shell::GetInstance()->AddPreTargetHandler(this); | |
57 else | |
58 Shell::GetInstance()->RemovePreTargetHandler(this); | |
59 } | |
60 | |
61 void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { | |
62 if (!enabled_) | |
63 return; | |
64 | |
65 if (event->pointer_details().pointer_type != | |
66 ui::EventPointerType::POINTER_TYPE_PEN) | |
67 return; | |
68 | |
69 if (event->type() != ui::ET_MOUSE_DRAGGED && | |
70 event->type() != ui::ET_MOUSE_PRESSED && | |
71 event->type() != ui::ET_MOUSE_RELEASED) | |
72 return; | |
73 | |
74 // Delete the LaserPointerView instance if mouse is released. | |
75 if (event->type() == ui::ET_MOUSE_RELEASED) { | |
76 StopTimer(); | |
77 laser_pointer_view_->Stop(); | |
78 laser_pointer_view_.reset(); | |
79 return; | |
80 } | |
81 | |
82 gfx::Point event_location = event->root_location(); | |
James Cook
2016/09/16 18:13:31
Can this go inside the if() block below?
sammiequon
2016/09/16 19:21:03
Done.
| |
83 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
84 aura::Window* event_root = target->GetRootWindow(); | |
85 | |
86 SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); | |
jdufault
2016/09/16 18:43:53
Add a comment in line 85 that this will function c
sammiequon
2016/09/16 19:21:03
Done.
| |
87 | |
88 if (laser_pointer_view_) { | |
89 // Remap point from where it was captured to the display it is actually on. | |
90 aura::Window::ConvertPointToTarget( | |
91 event_root, laser_pointer_view_->GetRootWindow(), &event_location); | |
92 | |
93 current_mouse_location_ = event_location; | |
94 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
95 | |
96 stationary_timer_repeat_count_ = 0; | |
James Cook
2016/09/16 18:13:31
Do you ever read this variable? If not, remove it.
jdufault
2016/09/16 18:43:53
It is used inside of AddStationaryPoint
sammiequon
2016/09/16 19:21:03
This is used at line 135 to determine when to stop
| |
97 if (event->type() == ui::ET_MOUSE_DRAGGED) { | |
98 // Start the timer to add stationary points if dragged. | |
99 if (!stationary_timer_->IsRunning()) | |
100 stationary_timer_->Reset(); | |
101 } | |
102 } | |
103 | |
104 // If the stylus is over the palette icon or widget, do not consume the event. | |
105 if (!PaletteContainsPointInScreen(current_mouse_location_)) | |
106 event->StopPropagation(); | |
107 } | |
108 | |
109 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | |
110 SwitchTargetRootWindowIfNeeded(window); | |
111 } | |
112 | |
113 void LaserPointerController::StopTimer() { | |
114 stationary_timer_repeat_count_ = 0; | |
jdufault
2016/09/16 18:43:53
This value gets reset every time the timer is rese
sammiequon
2016/09/16 19:21:03
Done.
| |
115 stationary_timer_->Stop(); | |
116 } | |
117 | |
118 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | |
119 aura::Window* root_window) { | |
120 if (root_window == nullptr) { | |
James Cook
2016/09/16 18:13:31
nit: if (!root_window) is more idiomatic in chrome
sammiequon
2016/09/16 19:21:03
Done.
| |
121 stationary_timer_->Stop(); | |
122 laser_pointer_view_.reset(); | |
123 } else if (laser_pointer_view_) { | |
124 laser_pointer_view_->ReparentWidget(root_window); | |
125 } else if (enabled_) { | |
126 laser_pointer_view_.reset(new LaserPointerView( | |
127 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | |
128 } | |
129 } | |
130 | |
131 void LaserPointerController::AddStationaryPoint() { | |
132 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
133 // We can stop repeating the timer once the mouse has been stationary for | |
134 // longer than the life of a point. | |
135 if (stationary_timer_repeat_count_++ * kAddStationaryPointsDelayMs >= | |
136 kPointLifeDurationMs) { | |
137 StopTimer(); | |
138 } | |
139 } | |
140 } // namespace ash | |
OLD | NEW |