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); |
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); |
| 102 RestartTimer(); |
| 103 } |
98 | 104 |
99 stationary_timer_repeat_count_ = 0; | 105 if (event->type() == ui::ET_MOUSE_RELEASED && laser_pointer_view_ && |
100 if (event->type() == ui::ET_MOUSE_DRAGGED) { | 106 !is_fading_away_) { |
101 // Start the timer to add stationary points if dragged. | 107 is_fading_away_ = true; |
102 if (!stationary_timer_->IsRunning()) | 108 UpdateLaserPointerView(current_window, event_location, event); |
103 stationary_timer_->Reset(); | 109 RestartTimer(); |
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 } | 110 } |
111 } | 111 } |
112 | 112 |
113 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | 113 void LaserPointerController::OnWindowDestroying(aura::Window* window) { |
114 SwitchTargetRootWindowIfNeeded(window); | 114 SwitchTargetRootWindowIfNeeded(window); |
115 } | 115 } |
116 | 116 |
117 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | 117 void LaserPointerController::SwitchTargetRootWindowIfNeeded( |
118 aura::Window* root_window) { | 118 aura::Window* root_window) { |
119 if (!root_window) { | 119 if (!root_window) { |
120 stationary_timer_->Stop(); | 120 DestroyLaserPointerView(); |
121 laser_pointer_view_.reset(); | |
122 } else if (laser_pointer_view_) { | 121 } else if (laser_pointer_view_) { |
123 laser_pointer_view_->ReparentWidget(root_window); | 122 laser_pointer_view_->ReparentWidget(root_window); |
124 } else if (enabled_) { | 123 } else if (enabled_) { |
125 laser_pointer_view_.reset(new LaserPointerView( | 124 laser_pointer_view_.reset(new LaserPointerView( |
126 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | 125 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); |
127 } | 126 } |
128 } | 127 } |
129 | 128 |
| 129 void LaserPointerController::UpdateLaserPointerView( |
| 130 aura::Window* current_window, |
| 131 const gfx::Point& event_location, |
| 132 ui::MouseEvent* event) { |
| 133 SwitchTargetRootWindowIfNeeded(current_window); |
| 134 current_mouse_location_ = event_location; |
| 135 laser_pointer_view_->AddNewPoint(current_mouse_location_); |
| 136 event->StopPropagation(); |
| 137 } |
| 138 |
| 139 void LaserPointerController::DestroyLaserPointerView() { |
| 140 // |stationary_timer_| should also be stopped so that it does not attempt to |
| 141 // add points when |laser_pointer_view_| is null. |
| 142 stationary_timer_->Stop(); |
| 143 if (laser_pointer_view_) { |
| 144 is_fading_away_ = false; |
| 145 laser_pointer_view_.reset(); |
| 146 } |
| 147 } |
| 148 |
| 149 void LaserPointerController::RestartTimer() { |
| 150 stationary_timer_repeat_count_ = 0; |
| 151 if (!stationary_timer_->IsRunning()) |
| 152 stationary_timer_->Reset(); |
| 153 } |
| 154 |
130 void LaserPointerController::AddStationaryPoint() { | 155 void LaserPointerController::AddStationaryPoint() { |
131 laser_pointer_view_->AddNewPoint(current_mouse_location_); | 156 if (is_fading_away_) |
| 157 laser_pointer_view_->UpdateTime(); |
| 158 else |
| 159 laser_pointer_view_->AddNewPoint(current_mouse_location_); |
| 160 |
132 // We can stop repeating the timer once the mouse has been stationary for | 161 // We can stop repeating the timer once the mouse has been stationary for |
133 // longer than the life of a point. | 162 // longer than the life of a point. |
134 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= | 163 if (stationary_timer_repeat_count_ * kAddStationaryPointsDelayMs >= |
135 kPointLifeDurationMs) { | 164 kPointLifeDurationMs) { |
136 stationary_timer_->Stop(); | 165 stationary_timer_->Stop(); |
| 166 // Reset the view if the timer expires and the view was in process of fading |
| 167 // away. |
| 168 if (is_fading_away_) |
| 169 DestroyLaserPointerView(); |
137 } | 170 } |
138 stationary_timer_repeat_count_++; | 171 stationary_timer_repeat_count_++; |
139 } | 172 } |
140 } // namespace ash | 173 } // namespace ash |
OLD | NEW |