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 laser_pointer_view_.reset(); | |
59 Shell::GetInstance()->RemovePreTargetHandler(this); | |
60 } | |
61 } | |
62 | |
63 void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { | |
64 if (!enabled_) | |
65 return; | |
66 | |
67 if (event->pointer_details().pointer_type != | |
68 ui::EventPointerType::POINTER_TYPE_PEN) | |
69 return; | |
70 | |
71 if (event->type() != ui::ET_MOUSE_DRAGGED && | |
72 event->type() != ui::ET_MOUSE_PRESSED && | |
73 event->type() != ui::ET_MOUSE_RELEASED) | |
74 return; | |
75 | |
76 // Delete the LaserPointerView instance if mouse is released. | |
77 if (event->type() == ui::ET_MOUSE_RELEASED) { | |
78 stationary_timer_->Stop(); | |
79 laser_pointer_view_->Stop(); | |
80 laser_pointer_view_.reset(); | |
81 return; | |
82 } | |
83 | |
84 // This will handle creating the initial laser pointer view on | |
85 // ET_MOUSE_PRESSED events. | |
86 SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); | |
87 | |
88 if (laser_pointer_view_) { | |
89 // Remap point from where it was captured to the display it is actually on. | |
90 gfx::Point event_location = event->root_location(); | |
91 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
92 aura::Window* event_root = target->GetRootWindow(); | |
93 aura::Window::ConvertPointToTarget( | |
94 event_root, laser_pointer_view_->GetRootWindow(), &event_location); | |
95 | |
96 current_mouse_location_ = event_location; | |
97 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
98 | |
99 stationary_timer_repeat_count_ = 0; | |
100 if (event->type() == ui::ET_MOUSE_DRAGGED) { | |
101 // Start the timer to add stationary points if dragged. | |
102 if (!stationary_timer_->IsRunning()) | |
103 stationary_timer_->Reset(); | |
104 } | |
105 | |
106 // If the stylus is over the palette icon or widget, do not consume the | |
107 // event. | |
108 if (!PaletteContainsPointInScreen(current_mouse_location_)) | |
109 event->StopPropagation(); | |
110 } | |
111 } | |
112 | |
113 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | |
114 SwitchTargetRootWindowIfNeeded(window); | |
115 } | |
116 | |
117 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | |
118 aura::Window* root_window) { | |
119 if (!root_window) { | |
120 stationary_timer_->Stop(); | |
121 laser_pointer_view_.reset(); | |
122 } else if (laser_pointer_view_) { | |
123 laser_pointer_view_->ReparentWidget(root_window); | |
124 } else if (enabled_) { | |
125 laser_pointer_view_.reset(new LaserPointerView( | |
126 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | |
127 } | |
128 } | |
129 | |
130 void LaserPointerController::AddStationaryPoint() { | |
131 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
132 // We can stop repeating the timer once the mouse has been stationary for | |
133 // longer than the life of a point. | |
134 if (stationary_timer_repeat_count_++ * kAddStationaryPointsDelayMs >= | |
James Cook
2016/09/16 23:47:00
nit: This would be clearer if you did the incremen
sammiequon
2016/09/17 00:04:24
Done.
| |
135 kPointLifeDurationMs) { | |
136 stationary_timer_->Stop(); | |
137 } | |
138 } | |
139 } // namespace ash | |
OLD | NEW |