Chromium Code Reviews| 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 |
| 78 stationary_timer_->Stop(); | 78 if (!current_window) { |
| 79 laser_pointer_view_->Stop(); | 79 DestroyLaserPointerView(); |
| 80 laser_pointer_view_.reset(); | |
| 81 return; | 80 return; |
| 82 } | 81 } |
| 83 | 82 |
| 84 // This will handle creating the initial laser pointer view on | 83 // Compute the event coordinate relative to the display it is currently on |
| 85 // ET_MOUSE_PRESSED events. | 84 // (and not the one the event was captured on). |
| 86 SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); | 85 gfx::Point event_location = event->root_location(); |
| 86 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
| 87 aura::Window* event_root = target->GetRootWindow(); | |
| 88 aura::Window::ConvertPointToTarget(event_root, current_window, | |
| 89 &event_location); | |
| 90 | |
| 91 // Do not start a new laser session if the event is over the palette, or is in | |
| 92 // process of fading away. | |
| 93 if (!laser_pointer_view_ && | |
| 94 (is_fading_away_ || PaletteContainsPointInScreen(event_location))) { | |
|
jdufault
2016/10/13 00:12:30
We should allow a new session even if the laser is
sammiequon
2016/10/14 18:48:50
Yeah that expression never happens, so it was work
| |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 // If the stylus is pressed and the laser pointer view is currently active, | |
| 99 // create a new view. | |
| 100 if (event->type() == ui::ET_MOUSE_PRESSED) | |
| 101 DestroyLaserPointerView(); | |
| 102 | |
| 103 SwitchTargetRootWindowIfNeeded(current_window); | |
|
jdufault
2016/10/13 00:12:30
I think this code would be much easier if the impl
sammiequon
2016/10/14 18:48:50
Done.
| |
| 87 | 104 |
| 88 if (laser_pointer_view_) { | 105 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; | 106 current_mouse_location_ = event_location; |
| 97 laser_pointer_view_->AddNewPoint(current_mouse_location_); | 107 // Signal the view to start fading away if the mouse has been released. |
| 108 is_fading_away_ = event->type() == ui::ET_MOUSE_RELEASED; | |
| 109 if (is_fading_away_) | |
| 110 laser_pointer_view_->UpdateTime(); | |
| 111 else | |
| 112 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 98 | 113 |
| 99 stationary_timer_repeat_count_ = 0; | 114 stationary_timer_repeat_count_ = 0; |
| 100 if (event->type() == ui::ET_MOUSE_DRAGGED) { | 115 if (event->type() == ui::ET_MOUSE_DRAGGED || |
| 101 // Start the timer to add stationary points if dragged. | 116 event->type() == ui::ET_MOUSE_RELEASED) { |
| 117 // Start the timer to add stationary points if dragged, or to advance the | |
| 118 // internal collections timestamps if released. | |
| 102 if (!stationary_timer_->IsRunning()) | 119 if (!stationary_timer_->IsRunning()) |
| 103 stationary_timer_->Reset(); | 120 stationary_timer_->Reset(); |
| 104 } | 121 } |
| 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 } | 122 } |
| 123 event->StopPropagation(); | |
| 111 } | 124 } |
| 112 | 125 |
| 113 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | 126 void LaserPointerController::OnWindowDestroying(aura::Window* window) { |
| 114 SwitchTargetRootWindowIfNeeded(window); | 127 SwitchTargetRootWindowIfNeeded(window); |
| 115 } | 128 } |
| 116 | 129 |
| 117 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | 130 void LaserPointerController::SwitchTargetRootWindowIfNeeded( |
| 118 aura::Window* root_window) { | 131 aura::Window* root_window) { |
| 119 if (!root_window) { | 132 if (!root_window) { |
| 120 stationary_timer_->Stop(); | 133 DestroyLaserPointerView(); |
| 121 laser_pointer_view_.reset(); | |
| 122 } else if (laser_pointer_view_) { | 134 } else if (laser_pointer_view_) { |
| 123 laser_pointer_view_->ReparentWidget(root_window); | 135 laser_pointer_view_->ReparentWidget(root_window); |
| 124 } else if (enabled_) { | 136 } else if (enabled_) { |
| 125 laser_pointer_view_.reset(new LaserPointerView( | 137 laser_pointer_view_.reset(new LaserPointerView( |
| 126 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | 138 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); |
| 127 } | 139 } |
| 128 } | 140 } |
| 129 | 141 |
| 142 void LaserPointerController::DestroyLaserPointerView() { | |
| 143 // |stationary_timer_| should also be stopped so that it does not attempt to | |
| 144 // add points when |laser_pointer_view_| is null. | |
| 145 stationary_timer_->Stop(); | |
| 146 if (laser_pointer_view_) { | |
| 147 is_fading_away_ = false; | |
| 148 laser_pointer_view_.reset(); | |
| 149 } | |
| 150 } | |
| 151 | |
| 130 void LaserPointerController::AddStationaryPoint() { | 152 void LaserPointerController::AddStationaryPoint() { |
| 131 laser_pointer_view_->AddNewPoint(current_mouse_location_); | 153 if (is_fading_away_) |
| 154 laser_pointer_view_->UpdateTime(); | |
| 155 else | |
| 156 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 157 | |
| 132 // We can stop repeating the timer once the mouse has been stationary for | 158 // We can stop repeating the timer once the mouse has been stationary for |
| 133 // longer than the life of a point. | 159 // longer than the life of a point. |
| 134 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= | 160 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= |
| 135 kPointLifeDurationMs) { | 161 kPointLifeDurationMs) { |
| 136 stationary_timer_->Stop(); | 162 stationary_timer_->Stop(); |
| 163 // Reset the view if the timer expires and the view was in process of fading | |
| 164 // away. | |
| 165 if (is_fading_away_) | |
| 166 DestroyLaserPointerView(); | |
| 137 } | 167 } |
| 138 stationary_timer_repeat_count_++; | 168 stationary_timer_repeat_count_++; |
| 139 } | 169 } |
| 140 } // namespace ash | 170 } // namespace ash |
| OLD | NEW |