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/window_observer.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, |
33 public AutoclickControllerCommon::Delegate, | |
34 public aura::WindowObserver { | |
48 public: | 35 public: |
49 AutoclickControllerImpl(); | 36 AutoclickControllerImpl(); |
50 ~AutoclickControllerImpl() override; | 37 ~AutoclickControllerImpl() override; |
51 | 38 |
52 private: | 39 private: |
53 // AutoclickController overrides: | 40 // AutoclickController overrides: |
54 void SetDelegate(std::unique_ptr<Delegate> delegate) override; | |
55 void SetEnabled(bool enabled) override; | 41 void SetEnabled(bool enabled) override; |
56 bool IsEnabled() const override; | 42 bool IsEnabled() const override; |
57 void SetAutoclickDelay(base::TimeDelta delay) override; | 43 void SetAutoclickDelay(base::TimeDelta delay) override; |
58 base::TimeDelta GetAutoclickDelay() const override; | |
59 | 44 |
60 // ui::EventHandler overrides: | 45 // ui::EventHandler overrides: |
61 void OnMouseEvent(ui::MouseEvent* event) override; | 46 void OnMouseEvent(ui::MouseEvent* event) override; |
62 void OnKeyEvent(ui::KeyEvent* event) override; | 47 void OnKeyEvent(ui::KeyEvent* event) override; |
63 void OnTouchEvent(ui::TouchEvent* event) override; | 48 void OnTouchEvent(ui::TouchEvent* event) override; |
64 void OnGestureEvent(ui::GestureEvent* event) override; | 49 void OnGestureEvent(ui::GestureEvent* event) override; |
65 void OnScrollEvent(ui::ScrollEvent* event) override; | 50 void OnScrollEvent(ui::ScrollEvent* event) override; |
66 | 51 |
67 void StartRingDisplay(); | 52 // AutoclickControllerCommon::Delegate overrides: |
68 void StopRingDisplay(); | 53 std::unique_ptr<views::Widget> CreateAutoclickRingWidget( |
69 void ChangeRingDisplayCenter(); | 54 const gfx::Point& event_location) override; |
55 void UpdateAutoclickRingWidget(views::Widget* widget, | |
56 const gfx::Point& event_location) override; | |
57 void DoAutoclick(const gfx::Point& event_location, | |
58 const int mouse_event_flags) override; | |
59 void OnAutoclickCanceled() override; | |
70 | 60 |
71 void InitClickTimer(); | 61 // aura::WindowObserver overrides: |
62 void OnWindowDestroying(aura::Window* window) override; | |
72 | 63 |
73 void DoAutoclick(); | 64 void SetTapDownTarget(aura::Window* target); |
sadrul
2016/08/15 17:23:04
Non-override before overrides.
riajiang
2016/08/16 00:44:24
Done.
| |
74 | 65 |
75 bool enabled_; | 66 bool enabled_; |
76 base::TimeDelta delay_; | 67 // The target window is observed by AutoclickControllerImpl for the duration |
77 int mouse_event_flags_; | 68 // of a autoclick gesture. |
78 std::unique_ptr<base::Timer> autoclick_timer_; | 69 aura::Window* tap_down_target_; |
79 std::unique_ptr<Delegate> delegate_; | 70 std::unique_ptr<AutoclickControllerCommon> autoclick_controller_common_; |
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 | 71 |
85 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); | 72 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); |
86 }; | 73 }; |
87 | 74 |
88 AutoclickControllerImpl::AutoclickControllerImpl() | 75 AutoclickControllerImpl::AutoclickControllerImpl() |
89 : enabled_(false), | 76 : enabled_(false), |
90 delay_(GetDefaultAutoclickDelay()), | 77 tap_down_target_(nullptr), |
91 mouse_event_flags_(ui::EF_NONE), | 78 autoclick_controller_common_( |
92 delegate_(nullptr), | 79 new AutoclickControllerCommon(GetDefaultAutoclickDelay(), this)) {} |
93 anchor_location_(-kMovementThreshold, -kMovementThreshold) { | |
94 InitClickTimer(); | |
95 } | |
96 | 80 |
97 AutoclickControllerImpl::~AutoclickControllerImpl() {} | 81 AutoclickControllerImpl::~AutoclickControllerImpl() { |
98 | 82 SetTapDownTarget(nullptr); |
99 void AutoclickControllerImpl::SetDelegate(std::unique_ptr<Delegate> delegate) { | |
100 delegate_ = std::move(delegate); | |
101 } | 83 } |
102 | 84 |
103 void AutoclickControllerImpl::SetEnabled(bool enabled) { | 85 void AutoclickControllerImpl::SetEnabled(bool enabled) { |
104 if (enabled_ == enabled) | 86 if (enabled_ == enabled) |
105 return; | 87 return; |
106 enabled_ = enabled; | 88 enabled_ = enabled; |
107 | 89 |
108 if (enabled_) { | 90 if (enabled_) { |
109 Shell::GetInstance()->AddPreTargetHandler(this); | 91 Shell::GetInstance()->AddPreTargetHandler(this); |
110 autoclick_timer_->Stop(); | 92 autoclick_controller_common_->CancelAutoclick(); |
111 } else { | 93 } else { |
112 Shell::GetInstance()->RemovePreTargetHandler(this); | 94 Shell::GetInstance()->RemovePreTargetHandler(this); |
113 } | 95 } |
114 } | 96 } |
115 | 97 |
116 bool AutoclickControllerImpl::IsEnabled() const { | 98 bool AutoclickControllerImpl::IsEnabled() const { |
117 return enabled_; | 99 return enabled_; |
118 } | 100 } |
119 | 101 |
120 void AutoclickControllerImpl::SetAutoclickDelay(base::TimeDelta delay) { | 102 void AutoclickControllerImpl::SetAutoclickDelay(base::TimeDelta delay) { |
121 delay_ = delay; | 103 autoclick_controller_common_->SetAutoclickDelay(delay); |
122 InitClickTimer(); | |
123 } | |
124 | |
125 base::TimeDelta AutoclickControllerImpl::GetAutoclickDelay() const { | |
126 return delay_; | |
127 } | |
128 | |
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() { | |
145 autoclick_timer_.reset(new base::Timer( | |
146 FROM_HERE, delay_, | |
147 base::Bind(&AutoclickControllerImpl::DoAutoclick, base::Unretained(this)), | |
148 false)); | |
149 } | 104 } |
150 | 105 |
151 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { | 106 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { |
152 if (event->type() == ui::ET_MOUSE_MOVED && | 107 autoclick_controller_common_->HandleMouseEvent(*event); |
153 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { | 108 } |
154 mouse_event_flags_ = event->flags(); | |
155 | 109 |
156 gfx::Point mouse_location = event->location(); | 110 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { |
157 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), | 111 autoclick_controller_common_->HandleKeyEvent(*event); |
158 &mouse_location); | 112 } |
159 | 113 |
160 // The distance between the mouse location and the anchor location | 114 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { |
161 // must exceed a certain threshold to initiate a new autoclick countdown. | 115 autoclick_controller_common_->HandleTouchEvent(*event); |
162 // This ensures that mouse jitter caused by poor motor control does not | 116 } |
163 // 1. initiate an unwanted autoclick from rest | 117 |
164 // 2. prevent the autoclick from ever occuring when the mouse | 118 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { |
165 // arrives at the target. | 119 autoclick_controller_common_->HandleGestureEvent(*event); |
166 gfx::Vector2d delta = mouse_location - anchor_location_; | 120 } |
167 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { | 121 |
168 anchor_location_ = mouse_location; | 122 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { |
169 autoclick_timer_->Reset(); | 123 autoclick_controller_common_->HandleScrollEvent(*event); |
170 StartRingDisplay(); | 124 } |
171 } else if (autoclick_timer_->IsRunning()) { | 125 |
172 current_mouse_location_ = mouse_location; | 126 std::unique_ptr<views::Widget> |
173 ChangeRingDisplayCenter(); | 127 AutoclickControllerImpl::CreateAutoclickRingWidget( |
174 } | 128 const gfx::Point& event_location) { |
175 } else if (event->type() == ui::ET_MOUSE_PRESSED) { | 129 aura::Window* target = |
176 autoclick_timer_->Stop(); | 130 WmWindowAura::GetAuraWindow(ash::wm::GetRootWindowAt(event_location)); |
177 StopRingDisplay(); | 131 SetTapDownTarget(target); |
178 } else if (event->type() == ui::ET_MOUSEWHEEL && | 132 aura::Window* root_window = target->GetRootWindow(); |
179 autoclick_timer_->IsRunning()) { | 133 std::unique_ptr<views::Widget> widget(new views::Widget); |
180 autoclick_timer_->Reset(); | 134 views::Widget::InitParams params; |
181 StartRingDisplay(); | 135 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
136 params.accept_events = false; | |
137 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; | |
138 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
139 params.context = root_window; | |
140 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
141 params.parent = | |
142 Shell::GetContainer(root_window, kShellWindowId_OverlayContainer); | |
143 widget->Init(params); | |
144 widget->SetOpacity(1.f); | |
145 return widget; | |
146 } | |
147 | |
148 void AutoclickControllerImpl::UpdateAutoclickRingWidget( | |
149 views::Widget* widget, | |
150 const gfx::Point& event_location) { | |
151 aura::Window* target = | |
152 WmWindowAura::GetAuraWindow(ash::wm::GetRootWindowAt(event_location)); | |
153 SetTapDownTarget(target); | |
154 aura::Window* root_window = target->GetRootWindow(); | |
155 if (widget->GetNativeView()->GetRootWindow() != root_window) { | |
156 views::Widget::ReparentNativeView( | |
157 widget->GetNativeView(), | |
158 Shell::GetContainer(root_window, kShellWindowId_OverlayContainer)); | |
182 } | 159 } |
183 } | 160 } |
184 | 161 |
185 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { | 162 void AutoclickControllerImpl::DoAutoclick(const gfx::Point& event_location, |
186 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 163 const int mouse_event_flags) { |
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 } | |
197 | |
198 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { | |
199 autoclick_timer_->Stop(); | |
200 StopRingDisplay(); | |
201 } | |
202 | |
203 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { | |
204 autoclick_timer_->Stop(); | |
205 StopRingDisplay(); | |
206 } | |
207 | |
208 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { | |
209 autoclick_timer_->Stop(); | |
210 StopRingDisplay(); | |
211 } | |
212 | |
213 void AutoclickControllerImpl::DoAutoclick() { | |
214 gfx::Point screen_location = aura::Env::GetInstance()->last_mouse_location(); | |
215 aura::Window* root_window = | 164 aura::Window* root_window = |
216 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); | 165 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(event_location)); |
217 DCHECK(root_window) << "Root window not found while attempting autoclick."; | 166 DCHECK(root_window) << "Root window not found while attempting autoclick."; |
218 | 167 |
219 gfx::Point click_location(screen_location); | 168 gfx::Point click_location(event_location); |
220 anchor_location_ = click_location; | |
221 | |
222 ::wm::ConvertPointFromScreen(root_window, &click_location); | 169 ::wm::ConvertPointFromScreen(root_window, &click_location); |
223 aura::WindowTreeHost* host = root_window->GetHost(); | 170 aura::WindowTreeHost* host = root_window->GetHost(); |
224 host->ConvertPointToHost(&click_location); | 171 host->ConvertPointToHost(&click_location); |
225 | 172 |
226 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, click_location, | 173 ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, click_location, |
227 click_location, ui::EventTimeForNow(), | 174 click_location, ui::EventTimeForNow(), |
228 mouse_event_flags_ | ui::EF_LEFT_MOUSE_BUTTON, | 175 mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON, |
229 ui::EF_LEFT_MOUSE_BUTTON); | 176 ui::EF_LEFT_MOUSE_BUTTON); |
230 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, click_location, | 177 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, click_location, |
231 click_location, ui::EventTimeForNow(), | 178 click_location, ui::EventTimeForNow(), |
232 mouse_event_flags_ | ui::EF_LEFT_MOUSE_BUTTON, | 179 mouse_event_flags | ui::EF_LEFT_MOUSE_BUTTON, |
233 ui::EF_LEFT_MOUSE_BUTTON); | 180 ui::EF_LEFT_MOUSE_BUTTON); |
234 | 181 |
235 ui::EventDispatchDetails details = | 182 ui::EventDispatchDetails details = |
236 host->event_processor()->OnEventFromSource(&press_event); | 183 host->event_processor()->OnEventFromSource(&press_event); |
237 if (!details.dispatcher_destroyed) | 184 if (!details.dispatcher_destroyed) |
238 details = host->event_processor()->OnEventFromSource(&release_event); | 185 details = host->event_processor()->OnEventFromSource(&release_event); |
239 if (details.dispatcher_destroyed) | 186 if (details.dispatcher_destroyed) |
240 return; | 187 return; |
241 } | 188 } |
242 | 189 |
190 void AutoclickControllerImpl::OnAutoclickCanceled() { | |
191 SetTapDownTarget(nullptr); | |
192 } | |
193 | |
194 void AutoclickControllerImpl::OnWindowDestroying(aura::Window* window) { | |
195 DCHECK_EQ(tap_down_target_, window); | |
196 autoclick_controller_common_->CancelAutoclick(); | |
197 } | |
198 | |
199 void AutoclickControllerImpl::SetTapDownTarget(aura::Window* target) { | |
200 if (tap_down_target_ == target) | |
201 return; | |
202 | |
203 if (tap_down_target_) | |
204 tap_down_target_->RemoveObserver(this); | |
205 tap_down_target_ = target; | |
206 if (tap_down_target_) | |
207 tap_down_target_->AddObserver(this); | |
208 } | |
209 | |
243 // static. | 210 // static. |
244 AutoclickController* AutoclickController::CreateInstance() { | 211 AutoclickController* AutoclickController::CreateInstance() { |
245 return new AutoclickControllerImpl(); | 212 return new AutoclickControllerImpl(); |
246 } | 213 } |
247 | 214 |
248 } // namespace ash | 215 } // namespace ash |
OLD | NEW |