OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/laser/laser_pointer_controller.h" | 5 #include "ash/laser/laser_pointer_controller.h" |
6 | 6 |
7 #include "ash/common/system/chromeos/palette/palette_utils.h" | 7 #include "ash/common/system/chromeos/palette/palette_utils.h" |
8 #include "ash/laser/laser_pointer_view.h" | 8 #include "ash/laser/laser_pointer_view.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ui/aura/window_event_dispatcher.h" | 10 #include "ui/aura/window_event_dispatcher.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 | 66 |
67 if (event->pointer_details().pointer_type != | 67 if (event->pointer_details().pointer_type != |
68 ui::EventPointerType::POINTER_TYPE_PEN) | 68 ui::EventPointerType::POINTER_TYPE_PEN) |
69 return; | 69 return; |
70 | 70 |
71 if (event->type() != ui::ET_MOUSE_DRAGGED && | 71 if (event->type() != ui::ET_MOUSE_DRAGGED && |
72 event->type() != ui::ET_MOUSE_PRESSED && | 72 event->type() != ui::ET_MOUSE_PRESSED && |
73 event->type() != ui::ET_MOUSE_RELEASED) | 73 event->type() != ui::ET_MOUSE_RELEASED) |
74 return; | 74 return; |
75 | 75 |
76 // Delete the LaserPointerView instance if mouse is released. | 76 aura::Window* current_window = GetCurrentRootWindow(); |
77 if (event->type() == ui::ET_MOUSE_RELEASED) { | 77 if (!current_window) { |
78 stationary_timer_->Stop(); | 78 DestroyLaserPointerView(); |
79 laser_pointer_view_->Stop(); | |
80 laser_pointer_view_.reset(); | |
81 return; | 79 return; |
82 } | 80 } |
83 | 81 |
84 // This will handle creating the initial laser pointer view on | 82 // Compute the event coordinate relative to the display it is currently on |
85 // ET_MOUSE_PRESSED events. | 83 // (and not the one the event was captured on). |
86 SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); | 84 gfx::Point event_location = event->root_location(); |
85 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
86 aura::Window* event_root = target->GetRootWindow(); | |
87 aura::Window::ConvertPointToTarget(event_root, current_window, | |
88 &event_location); | |
87 | 89 |
88 if (laser_pointer_view_) { | 90 // Start a new laser session if the mouse is pressed but not pressed over the |
89 // Remap point from where it was captured to the display it is actually on. | 91 // palette. |
90 gfx::Point event_location = event->root_location(); | 92 if (event->type() == ui::ET_MOUSE_PRESSED && |
91 aura::Window* target = static_cast<aura::Window*>(event->target()); | 93 !PaletteContainsPointInScreen(event_location)) { |
92 aura::Window* event_root = target->GetRootWindow(); | 94 DestroyLaserPointerView(); |
93 aura::Window::ConvertPointToTarget( | 95 UpdateLaserPointerView(current_window, event_location, event, false); |
94 event_root, laser_pointer_view_->GetRootWindow(), &event_location); | 96 } |
95 | 97 |
96 current_mouse_location_ = event_location; | 98 // Do not update laser if it is in the process of fading away. |
97 laser_pointer_view_->AddNewPoint(current_mouse_location_); | 99 if (event->type() == ui::ET_MOUSE_DRAGGED && laser_pointer_view_ && |
100 !is_fading_away_) { | |
101 UpdateLaserPointerView(current_window, event_location, event, true); | |
102 } | |
98 | 103 |
99 stationary_timer_repeat_count_ = 0; | 104 if (event->type() == ui::ET_MOUSE_RELEASED && laser_pointer_view_ && |
100 if (event->type() == ui::ET_MOUSE_DRAGGED) { | 105 !is_fading_away_) { |
101 // Start the timer to add stationary points if dragged. | 106 is_fading_away_ = true; |
102 if (!stationary_timer_->IsRunning()) | 107 UpdateLaserPointerView(current_window, event_location, event, true); |
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 } | 108 } |
111 } | 109 } |
112 | 110 |
113 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | 111 void LaserPointerController::OnWindowDestroying(aura::Window* window) { |
114 SwitchTargetRootWindowIfNeeded(window); | 112 SwitchTargetRootWindowIfNeeded(window); |
115 } | 113 } |
116 | 114 |
117 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | 115 void LaserPointerController::SwitchTargetRootWindowIfNeeded( |
118 aura::Window* root_window) { | 116 aura::Window* root_window) { |
119 if (!root_window) { | 117 if (!root_window) { |
120 stationary_timer_->Stop(); | 118 DestroyLaserPointerView(); |
121 laser_pointer_view_.reset(); | |
122 } else if (laser_pointer_view_) { | 119 } else if (laser_pointer_view_) { |
123 laser_pointer_view_->ReparentWidget(root_window); | 120 laser_pointer_view_->ReparentWidget(root_window); |
124 } else if (enabled_) { | 121 } else if (enabled_) { |
125 laser_pointer_view_.reset(new LaserPointerView( | 122 laser_pointer_view_.reset(new LaserPointerView( |
126 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | 123 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); |
127 } | 124 } |
128 } | 125 } |
129 | 126 |
127 void LaserPointerController::UpdateLaserPointerView( | |
128 aura::Window* current_window, | |
129 const gfx::Point& event_location, | |
130 ui::MouseEvent* event, | |
131 bool start_timer) { | |
132 SwitchTargetRootWindowIfNeeded(current_window); | |
133 current_mouse_location_ = event_location; | |
134 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
135 if (start_timer) { | |
jdufault
2016/10/14 19:13:09
Instead of having a boolean why not just extract t
sammiequon
2016/10/14 21:29:32
Done.
| |
136 stationary_timer_repeat_count_ = 0; | |
137 if (!stationary_timer_->IsRunning()) | |
138 stationary_timer_->Reset(); | |
139 } | |
140 event->StopPropagation(); | |
141 } | |
142 | |
143 void LaserPointerController::DestroyLaserPointerView() { | |
144 // |stationary_timer_| should also be stopped so that it does not attempt to | |
145 // add points when |laser_pointer_view_| is null. | |
146 stationary_timer_->Stop(); | |
147 if (laser_pointer_view_) { | |
148 is_fading_away_ = false; | |
149 laser_pointer_view_.reset(); | |
150 } | |
151 } | |
152 | |
130 void LaserPointerController::AddStationaryPoint() { | 153 void LaserPointerController::AddStationaryPoint() { |
131 laser_pointer_view_->AddNewPoint(current_mouse_location_); | 154 if (is_fading_away_) |
155 laser_pointer_view_->UpdateTime(); | |
156 else | |
157 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
158 | |
132 // We can stop repeating the timer once the mouse has been stationary for | 159 // We can stop repeating the timer once the mouse has been stationary for |
133 // longer than the life of a point. | 160 // longer than the life of a point. |
134 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= | 161 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= |
135 kPointLifeDurationMs) { | 162 kPointLifeDurationMs) { |
136 stationary_timer_->Stop(); | 163 stationary_timer_->Stop(); |
164 // Reset the view if the timer expires and the view was in process of fading | |
165 // away. | |
166 if (is_fading_away_) | |
167 DestroyLaserPointerView(); | |
137 } | 168 } |
138 stationary_timer_repeat_count_++; | 169 stationary_timer_repeat_count_++; |
139 } | 170 } |
140 } // namespace ash | 171 } // namespace ash |
OLD | NEW |