OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/autoclick/autoclick_controller.h" | 5 #include "ash/autoclick/autoclick_controller.h" |
6 | 6 |
7 #include "ash/aura/wm_window_aura.h" | 7 #include "ash/aura/wm_window_aura.h" |
8 #include "ash/autoclick/common/autoclick_controller_common.h" | |
9 #include "ash/common/shell_window_ids.h" | |
8 #include "ash/common/wm/root_window_finder.h" | 10 #include "ash/common/wm/root_window_finder.h" |
9 #include "ash/shell.h" | 11 #include "ash/shell.h" |
10 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
11 #include "ui/aura/env.h" | 13 #include "ui/aura/env.h" |
12 #include "ui/aura/window_tree_host.h" | 14 #include "ui/aura/window_tree_host.h" |
13 #include "ui/events/event.h" | 15 #include "ui/events/event.h" |
14 #include "ui/events/event_constants.h" | |
15 #include "ui/events/event_handler.h" | 16 #include "ui/events/event_handler.h" |
16 #include "ui/events/event_processor.h" | 17 #include "ui/events/event_processor.h" |
17 #include "ui/events/event_utils.h" | 18 #include "ui/events/event_utils.h" |
18 #include "ui/gfx/geometry/point.h" | 19 #include "ui/views/widget/widget.h" |
19 #include "ui/gfx/geometry/vector2d.h" | |
20 #include "ui/wm/core/coordinate_conversion.h" | 20 #include "ui/wm/core/coordinate_conversion.h" |
21 | 21 |
22 namespace ash { | 22 namespace ash { |
23 | 23 |
24 namespace { | |
25 | |
26 // The threshold of mouse movement measured in DIP that will | |
27 // initiate a new autoclick. | |
28 const int kMovementThreshold = 20; | |
29 | |
30 bool IsModifierKey(ui::KeyboardCode key_code) { | |
31 return key_code == ui::VKEY_SHIFT || key_code == ui::VKEY_LSHIFT || | |
32 key_code == ui::VKEY_CONTROL || key_code == ui::VKEY_LCONTROL || | |
33 key_code == ui::VKEY_RCONTROL || key_code == ui::VKEY_MENU || | |
34 key_code == ui::VKEY_LMENU || key_code == ui::VKEY_RMENU; | |
35 } | |
36 | |
37 } // namespace | |
38 | |
39 // static. | 24 // static. |
40 base::TimeDelta AutoclickController::GetDefaultAutoclickDelay() { | 25 base::TimeDelta AutoclickController::GetDefaultAutoclickDelay() { |
41 return base::TimeDelta::FromMilliseconds(int64_t{kDefaultAutoclickDelayMs}); | 26 return base::TimeDelta::FromMilliseconds(int64_t{kDefaultAutoclickDelayMs}); |
42 } | 27 } |
43 | 28 |
44 const int AutoclickController::kDefaultAutoclickDelayMs = 1000; | 29 const int AutoclickController::kDefaultAutoclickDelayMs = 1000; |
45 | 30 |
46 class AutoclickControllerImpl : public AutoclickController, | 31 class AutoclickControllerImpl : public AutoclickController, |
47 public ui::EventHandler { | 32 public ui::EventHandler { |
48 public: | 33 public: |
49 AutoclickControllerImpl(); | 34 AutoclickControllerImpl(); |
50 ~AutoclickControllerImpl() override; | 35 ~AutoclickControllerImpl() override; |
51 | 36 |
52 private: | 37 private: |
53 // AutoclickController overrides: | 38 // AutoclickController overrides: |
54 void SetDelegate(std::unique_ptr<Delegate> delegate) override; | |
55 void SetEnabled(bool enabled) override; | 39 void SetEnabled(bool enabled) override; |
56 bool IsEnabled() const override; | 40 bool IsEnabled() const override; |
57 void SetAutoclickDelay(base::TimeDelta delay) override; | 41 void SetAutoclickDelay(base::TimeDelta delay) override; |
58 base::TimeDelta GetAutoclickDelay() const override; | 42 base::TimeDelta GetAutoclickDelay() const override; |
59 | 43 |
60 // ui::EventHandler overrides: | 44 // ui::EventHandler overrides: |
61 void OnMouseEvent(ui::MouseEvent* event) override; | 45 void OnMouseEvent(ui::MouseEvent* event) override; |
62 void OnKeyEvent(ui::KeyEvent* event) override; | 46 void OnKeyEvent(ui::KeyEvent* event) override; |
63 void OnTouchEvent(ui::TouchEvent* event) override; | 47 void OnTouchEvent(ui::TouchEvent* event) override; |
64 void OnGestureEvent(ui::GestureEvent* event) override; | 48 void OnGestureEvent(ui::GestureEvent* event) override; |
65 void OnScrollEvent(ui::ScrollEvent* event) override; | 49 void OnScrollEvent(ui::ScrollEvent* event) override; |
66 | 50 |
67 void StartRingDisplay(); | |
68 void StopRingDisplay(); | |
69 void ChangeRingDisplayCenter(); | |
70 | |
71 void InitClickTimer(); | 51 void InitClickTimer(); |
72 | 52 |
73 void DoAutoclick(); | 53 void DoAutoclick(); |
74 | 54 |
75 bool enabled_; | 55 bool enabled_; |
76 base::TimeDelta delay_; | 56 base::TimeDelta delay_; |
77 int mouse_event_flags_; | 57 std::unique_ptr<AutoclickControllerCommon> autoclick_controller_common_; |
78 std::unique_ptr<base::Timer> autoclick_timer_; | |
79 std::unique_ptr<Delegate> delegate_; | |
80 // The position in screen coordinates used to determine | |
81 // the distance the mouse has moved. | |
82 gfx::Point anchor_location_; | |
83 gfx::Point current_mouse_location_; | |
84 | 58 |
85 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); | 59 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); |
86 }; | 60 }; |
87 | 61 |
88 AutoclickControllerImpl::AutoclickControllerImpl() | 62 AutoclickControllerImpl::AutoclickControllerImpl() |
89 : enabled_(false), | 63 : enabled_(false), |
90 delay_(GetDefaultAutoclickDelay()), | 64 delay_(GetDefaultAutoclickDelay()), |
91 mouse_event_flags_(ui::EF_NONE), | 65 autoclick_controller_common_(new AutoclickControllerCommon()) { |
92 delegate_(nullptr), | |
93 anchor_location_(-kMovementThreshold, -kMovementThreshold) { | |
94 InitClickTimer(); | 66 InitClickTimer(); |
95 } | 67 } |
96 | 68 |
97 AutoclickControllerImpl::~AutoclickControllerImpl() {} | 69 AutoclickControllerImpl::~AutoclickControllerImpl() {} |
98 | 70 |
99 void AutoclickControllerImpl::SetDelegate(std::unique_ptr<Delegate> delegate) { | |
100 delegate_ = std::move(delegate); | |
101 } | |
102 | |
103 void AutoclickControllerImpl::SetEnabled(bool enabled) { | 71 void AutoclickControllerImpl::SetEnabled(bool enabled) { |
104 if (enabled_ == enabled) | 72 if (enabled_ == enabled) |
105 return; | 73 return; |
106 enabled_ = enabled; | 74 enabled_ = enabled; |
107 | 75 |
108 if (enabled_) { | 76 if (enabled_) { |
109 Shell::GetInstance()->AddPreTargetHandler(this); | 77 Shell::GetInstance()->AddPreTargetHandler(this); |
110 autoclick_timer_->Stop(); | 78 autoclick_controller_common_->StopAutoclickTimer(); |
111 } else { | 79 } else { |
112 Shell::GetInstance()->RemovePreTargetHandler(this); | 80 Shell::GetInstance()->RemovePreTargetHandler(this); |
113 } | 81 } |
114 } | 82 } |
115 | 83 |
116 bool AutoclickControllerImpl::IsEnabled() const { | 84 bool AutoclickControllerImpl::IsEnabled() const { |
117 return enabled_; | 85 return enabled_; |
118 } | 86 } |
119 | 87 |
120 void AutoclickControllerImpl::SetAutoclickDelay(base::TimeDelta delay) { | 88 void AutoclickControllerImpl::SetAutoclickDelay(base::TimeDelta delay) { |
121 delay_ = delay; | 89 delay_ = delay; |
122 InitClickTimer(); | 90 InitClickTimer(); |
123 } | 91 } |
124 | 92 |
125 base::TimeDelta AutoclickControllerImpl::GetAutoclickDelay() const { | 93 base::TimeDelta AutoclickControllerImpl::GetAutoclickDelay() const { |
126 return delay_; | 94 return delay_; |
127 } | 95 } |
128 | 96 |
129 void AutoclickControllerImpl::StartRingDisplay() { | |
130 if (delegate_) | |
131 delegate_->StartGesture(delay_, anchor_location_); | |
132 } | |
133 | |
134 void AutoclickControllerImpl::StopRingDisplay() { | |
135 if (delegate_) | |
136 delegate_->StopGesture(); | |
137 } | |
138 | |
139 void AutoclickControllerImpl::ChangeRingDisplayCenter() { | |
140 if (delegate_) | |
141 delegate_->SetGestureCenter(current_mouse_location_); | |
142 } | |
143 | |
144 void AutoclickControllerImpl::InitClickTimer() { | 97 void AutoclickControllerImpl::InitClickTimer() { |
145 autoclick_timer_.reset(new base::Timer( | 98 autoclick_controller_common_->ResetAutoclickTimer(new base::Timer( |
146 FROM_HERE, delay_, | 99 FROM_HERE, delay_, |
147 base::Bind(&AutoclickControllerImpl::DoAutoclick, base::Unretained(this)), | 100 base::Bind(&AutoclickControllerImpl::DoAutoclick, base::Unretained(this)), |
148 false)); | 101 false)); |
149 } | 102 } |
150 | 103 |
151 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { | 104 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { |
152 if (event->type() == ui::ET_MOUSE_MOVED && | 105 gfx::Point mouse_location = event->location(); |
153 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { | 106 aura::Window* target = |
154 mouse_event_flags_ = event->flags(); | 107 WmWindowAura::GetAuraWindow(ash::wm::GetRootWindowAt(mouse_location)); |
108 aura::Window* root_window = target->GetRootWindow(); | |
155 | 109 |
156 gfx::Point mouse_location = event->location(); | 110 views::Widget* widget = new views::Widget; |
157 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), | 111 views::Widget::InitParams params; |
158 &mouse_location); | 112 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
113 params.accept_events = false; | |
114 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
115 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
116 params.context = root_window; | |
117 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
118 params.parent = | |
119 Shell::GetContainer(root_window, kShellWindowId_OverlayContainer); | |
120 widget->Init(params); | |
121 widget->SetOpacity(1.f); | |
sadrul
2016/07/29 20:13:35
Why do we need to create a new Widget for each mou
riajiang
2016/08/04 16:51:20
They were doing this every time StartAnimation is
| |
159 | 122 |
160 // The distance between the mouse location and the anchor location | 123 autoclick_controller_common_->HandleMouseEvent(*event, target, widget, |
161 // must exceed a certain threshold to initiate a new autoclick countdown. | 124 delay_); |
162 // This ensures that mouse jitter caused by poor motor control does not | |
163 // 1. initiate an unwanted autoclick from rest | |
164 // 2. prevent the autoclick from ever occuring when the mouse | |
165 // arrives at the target. | |
166 gfx::Vector2d delta = mouse_location - anchor_location_; | |
167 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { | |
168 anchor_location_ = mouse_location; | |
169 autoclick_timer_->Reset(); | |
170 StartRingDisplay(); | |
171 } else if (autoclick_timer_->IsRunning()) { | |
172 current_mouse_location_ = mouse_location; | |
173 ChangeRingDisplayCenter(); | |
174 } | |
175 } else if (event->type() == ui::ET_MOUSE_PRESSED) { | |
176 autoclick_timer_->Stop(); | |
177 StopRingDisplay(); | |
178 } else if (event->type() == ui::ET_MOUSEWHEEL && | |
179 autoclick_timer_->IsRunning()) { | |
180 autoclick_timer_->Reset(); | |
181 StartRingDisplay(); | |
182 } | |
183 } | 125 } |
184 | 126 |
185 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { | 127 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { |
186 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 128 autoclick_controller_common_->HandleKeyEvent(*event); |
187 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | | |
188 ui::EF_IS_EXTENDED_KEY; | |
189 int new_modifiers = event->flags() & modifier_mask; | |
190 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers; | |
191 | |
192 if (!IsModifierKey(event->key_code())) { | |
193 autoclick_timer_->Stop(); | |
194 StopRingDisplay(); | |
195 } | |
196 } | 129 } |
197 | 130 |
198 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { | 131 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { |
199 autoclick_timer_->Stop(); | 132 autoclick_controller_common_->HandleTouchEvent(*event); |
200 StopRingDisplay(); | |
201 } | 133 } |
202 | 134 |
203 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { | 135 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { |
204 autoclick_timer_->Stop(); | 136 autoclick_controller_common_->HandleGestureEvent(*event); |
205 StopRingDisplay(); | |
206 } | 137 } |
207 | 138 |
208 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { | 139 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { |
209 autoclick_timer_->Stop(); | 140 autoclick_controller_common_->HandleScrollEvent(*event); |
210 StopRingDisplay(); | |
211 } | 141 } |
212 | 142 |
213 void AutoclickControllerImpl::DoAutoclick() { | 143 void AutoclickControllerImpl::DoAutoclick() { |
214 gfx::Point screen_location = aura::Env::GetInstance()->last_mouse_location(); | 144 gfx::Point screen_location = aura::Env::GetInstance()->last_mouse_location(); |
215 aura::Window* root_window = | 145 aura::Window* root_window = |
216 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); | 146 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); |
217 DCHECK(root_window) << "Root window not found while attempting autoclick."; | 147 DCHECK(root_window) << "Root window not found while attempting autoclick."; |
218 | 148 |
219 gfx::Point click_location(screen_location); | 149 gfx::Point click_location(screen_location); |
220 anchor_location_ = click_location; | 150 autoclick_controller_common_->UpdateAnchorLocation(click_location); |
221 | 151 |
222 ::wm::ConvertPointFromScreen(root_window, &click_location); | 152 ::wm::ConvertPointFromScreen(root_window, &click_location); |
223 aura::WindowTreeHost* host = root_window->GetHost(); | 153 aura::WindowTreeHost* host = root_window->GetHost(); |
224 host->ConvertPointToHost(&click_location); | 154 host->ConvertPointToHost(&click_location); |
225 | 155 |
156 int mouse_event_flags = autoclick_controller_common_->GetMouseEventFlags(); | |
226 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, click_location, | 157 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, click_location, |
227 click_location, ui::EventTimeForNow(), | 158 click_location, ui::EventTimeForNow(), |
228 mouse_event_flags_ | ui::EF_LEFT_MOUSE_BUTTON, | 159 mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON, |
229 ui::EF_LEFT_MOUSE_BUTTON); | 160 ui::EF_LEFT_MOUSE_BUTTON); |
230 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, click_location, | 161 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, click_location, |
231 click_location, ui::EventTimeForNow(), | 162 click_location, ui::EventTimeForNow(), |
232 mouse_event_flags_ | ui::EF_LEFT_MOUSE_BUTTON, | 163 mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON, |
233 ui::EF_LEFT_MOUSE_BUTTON); | 164 ui::EF_LEFT_MOUSE_BUTTON); |
234 | 165 |
235 ui::EventDispatchDetails details = | 166 ui::EventDispatchDetails details = |
236 host->event_processor()->OnEventFromSource(&press_event); | 167 host->event_processor()->OnEventFromSource(&press_event); |
237 if (!details.dispatcher_destroyed) | 168 if (!details.dispatcher_destroyed) |
238 details = host->event_processor()->OnEventFromSource(&release_event); | 169 details = host->event_processor()->OnEventFromSource(&release_event); |
239 if (details.dispatcher_destroyed) | 170 if (details.dispatcher_destroyed) |
240 return; | 171 return; |
241 } | 172 } |
242 | 173 |
243 // static. | 174 // static. |
244 AutoclickController* AutoclickController::CreateInstance() { | 175 AutoclickController* AutoclickController::CreateInstance() { |
245 return new AutoclickControllerImpl(); | 176 return new AutoclickControllerImpl(); |
246 } | 177 } |
247 | 178 |
248 } // namespace ash | 179 } // namespace ash |
OLD | NEW |