Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1306)

Side by Side Diff: ash/laser/laser_pointer_controller.cc

Issue 2311393004: Laser tool blocks events from propagating. (Closed)
Patch Set: Fixed patch set 1 errors. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
James Cook 2016/09/14 17:52:26 Since this feature only works on chromeos, how abo
sammiequon 2016/09/14 21:44:08 Done.
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
James Cook 2016/09/14 17:52:26 nit: received
sammiequon 2016/09/14 21:44:08 Done.
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();
James Cook 2016/09/14 17:52:25 nit: no ash
sammiequon 2016/09/14 21:44:09 Done.
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
49
50 LaserPointerController::LaserPointerController() {
51 timer_.reset(new base::Timer(
James Cook 2016/09/14 17:52:26 do this in constructor initializer list : timer_(n
sammiequon 2016/09/14 21:44:09 Done.
52 FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs),
53 base::Bind(&LaserPointerController::AddStationaryPoint,
James Cook 2016/09/14 17:52:25 So if I instantiate a LaserPointerController, but
sammiequon 2016/09/14 21:44:08 The timer currently only runs once a drag is detec
54 base::Unretained(this)),
55 true));
James Cook 2016/09/14 17:52:26 nit: document /* is_repeating */ or put in const b
sammiequon 2016/09/14 21:44:09 Done.
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 Shell::GetInstance()->AddPreTargetHandler(this);
69 else
70 Shell::GetInstance()->RemovePreTargetHandler(this);
71 }
72
73 void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) {
74 if (!enabled_)
75 return;
76
77 if (event->pointer_details().pointer_type !=
78 ui::EventPointerType::POINTER_TYPE_PEN)
79 return;
80
81 if (event->type() != ui::ET_MOUSE_DRAGGED &&
82 event->type() != ui::ET_MOUSE_PRESSED &&
83 event->type() != ui::ET_MOUSE_RELEASED)
84 return;
85
86 // Delete the LaserPointerView instance if mouse is released.
87 if (event->type() == ui::ET_MOUSE_RELEASED) {
88 StopTimer();
89 laser_pointer_view_->Stop();
90 laser_pointer_view_.reset();
91 return;
92 }
93
94 if (!laser_pointer_view_) {
95 laser_pointer_view_.reset(new LaserPointerView(
96 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs),
97 GetCurrentRootWindow()));
98 }
99
100 SwitchTargetRootWindowIfNeeded();
James Cook 2016/09/14 17:52:26 please document what this is doing, either in the
sammiequon 2016/09/14 21:44:08 Done.
101
102 // Remap point from where it was captured to the display it is actually on.
103 gfx::Point event_location = event->root_location();
104 aura::Window* target = static_cast<aura::Window*>(event->target());
105 aura::Window* event_root = target->GetRootWindow();
106 aura::Window::ConvertPointToTarget(
107 event_root, laser_pointer_view_->GetRootWindow(), &event_location);
108
109 current_mouse_location_ = event_location;
James Cook 2016/09/14 17:52:26 Could you use ui/display/screen.h Screen::GetCurso
sammiequon 2016/09/14 21:44:09 We would need to compute the display origin for wh
James Cook 2016/09/14 22:53:37 This is fine.
110 laser_pointer_view_->AddNewPoint(current_mouse_location_);
111
112 timer_repeat_count_ = 0;
113 if (event->type() == ui::ET_MOUSE_DRAGGED) {
114 // Start the timer to add stationary points if dragged.
115 if (!timer_->IsRunning())
116 timer_->Reset();
117 }
118
119 // Consume the event unless it is over the palette tray or palette shelf
120 // button.
121 if (!ShouldSkipEventFiltering(current_mouse_location_))
122 event->StopPropagation();
123 }
124
125 void LaserPointerController::OnWindowDestroying(aura::Window* window) {
126 SwitchTargetRootWindowIfNeeded();
127 }
128
129 void LaserPointerController::StopTimer() {
130 timer_repeat_count_ = 0;
131 timer_->Stop();
132 }
133
134 void LaserPointerController::SwitchTargetRootWindowIfNeeded() {
135 if (enabled_) {
136 // If the current root window has changed, remake the laser pointer view
137 // with the new root window.
138 if (laser_pointer_view_->GetRootWindow() != GetCurrentRootWindow()) {
139 laser_pointer_view_.reset(new LaserPointerView(
140 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs),
141 GetCurrentRootWindow()));
142 }
143 } else {
144 laser_pointer_view_.reset();
145 }
146 }
147
148 void LaserPointerController::AddStationaryPoint() {
149 laser_pointer_view_->AddNewPoint(current_mouse_location_);
150 // We can stop repeating the timer once the mouse has been stationary for
151 // longer than the life of a point.
152 if (timer_repeat_count_++ * kAddStationaryPointsDelayMs >=
153 kPointLifeDurationMs) {
154 StopTimer();
155 }
156 }
157 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698