| 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/common/wm/root_window_finder.h" | 8 #include "ash/common/wm/root_window_finder.h" |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "base/timer/timer.h" | 10 #include "base/timer/timer.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 key_code == ui::VKEY_LCONTROL || | 34 key_code == ui::VKEY_LCONTROL || |
| 35 key_code == ui::VKEY_RCONTROL || | 35 key_code == ui::VKEY_RCONTROL || |
| 36 key_code == ui::VKEY_MENU || | 36 key_code == ui::VKEY_MENU || |
| 37 key_code == ui::VKEY_LMENU || | 37 key_code == ui::VKEY_LMENU || |
| 38 key_code == ui::VKEY_RMENU; | 38 key_code == ui::VKEY_RMENU; |
| 39 } | 39 } |
| 40 | 40 |
| 41 } // namespace | 41 } // namespace |
| 42 | 42 |
| 43 // static. | 43 // static. |
| 44 const int AutoclickController::kDefaultAutoclickDelayMs = 400; | 44 base::TimeDelta AutoclickController::GetDefaultAutoclickDelay() { |
| 45 return base::TimeDelta::FromMilliseconds(int64_t{kDefaultAutoclickDelayMs}); |
| 46 } |
| 47 |
| 48 const int AutoclickController::kDefaultAutoclickDelayMs = 1000; |
| 45 | 49 |
| 46 class AutoclickControllerImpl : public AutoclickController, | 50 class AutoclickControllerImpl : public AutoclickController, |
| 47 public ui::EventHandler { | 51 public ui::EventHandler { |
| 48 public: | 52 public: |
| 49 AutoclickControllerImpl(); | 53 AutoclickControllerImpl(); |
| 50 ~AutoclickControllerImpl() override; | 54 ~AutoclickControllerImpl() override; |
| 51 | 55 |
| 52 private: | 56 private: |
| 53 // AutoclickController overrides: | 57 // AutoclickController overrides: |
| 58 void SetDelegate(std::unique_ptr<Delegate> delegate) override; |
| 54 void SetEnabled(bool enabled) override; | 59 void SetEnabled(bool enabled) override; |
| 55 bool IsEnabled() const override; | 60 bool IsEnabled() const override; |
| 56 void SetAutoclickDelay(int delay_ms) override; | 61 void SetAutoclickDelay(base::TimeDelta delay) override; |
| 57 int GetAutoclickDelay() const override; | 62 base::TimeDelta GetAutoclickDelay() const override; |
| 58 | 63 |
| 59 // ui::EventHandler overrides: | 64 // ui::EventHandler overrides: |
| 60 void OnMouseEvent(ui::MouseEvent* event) override; | 65 void OnMouseEvent(ui::MouseEvent* event) override; |
| 61 void OnKeyEvent(ui::KeyEvent* event) override; | 66 void OnKeyEvent(ui::KeyEvent* event) override; |
| 62 void OnTouchEvent(ui::TouchEvent* event) override; | 67 void OnTouchEvent(ui::TouchEvent* event) override; |
| 63 void OnGestureEvent(ui::GestureEvent* event) override; | 68 void OnGestureEvent(ui::GestureEvent* event) override; |
| 64 void OnScrollEvent(ui::ScrollEvent* event) override; | 69 void OnScrollEvent(ui::ScrollEvent* event) override; |
| 65 | 70 |
| 71 void StartRingDisplay(); |
| 72 void StopRingDisplay(); |
| 73 void ChangeRingDisplayCenter(); |
| 74 |
| 66 void InitClickTimer(); | 75 void InitClickTimer(); |
| 67 | 76 |
| 68 void DoAutoclick(); | 77 void DoAutoclick(); |
| 69 | 78 |
| 70 bool enabled_; | 79 bool enabled_; |
| 71 int delay_ms_; | 80 base::TimeDelta delay_; |
| 72 int mouse_event_flags_; | 81 int mouse_event_flags_; |
| 73 std::unique_ptr<base::Timer> autoclick_timer_; | 82 std::unique_ptr<base::Timer> autoclick_timer_; |
| 83 std::unique_ptr<Delegate> delegate_; |
| 74 // The position in screen coordinates used to determine | 84 // The position in screen coordinates used to determine |
| 75 // the distance the mouse has moved. | 85 // the distance the mouse has moved. |
| 76 gfx::Point anchor_location_; | 86 gfx::Point anchor_location_; |
| 87 gfx::Point current_mouse_location_; |
| 77 | 88 |
| 78 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); | 89 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); |
| 79 }; | 90 }; |
| 80 | 91 |
| 81 | |
| 82 AutoclickControllerImpl::AutoclickControllerImpl() | 92 AutoclickControllerImpl::AutoclickControllerImpl() |
| 83 : enabled_(false), | 93 : enabled_(false), |
| 84 delay_ms_(kDefaultAutoclickDelayMs), | 94 delay_(GetDefaultAutoclickDelay()), |
| 85 mouse_event_flags_(ui::EF_NONE), | 95 mouse_event_flags_(ui::EF_NONE), |
| 96 delegate_(nullptr), |
| 86 anchor_location_(-kMovementThreshold, -kMovementThreshold) { | 97 anchor_location_(-kMovementThreshold, -kMovementThreshold) { |
| 87 InitClickTimer(); | 98 InitClickTimer(); |
| 88 } | 99 } |
| 89 | 100 |
| 90 AutoclickControllerImpl::~AutoclickControllerImpl() { | 101 AutoclickControllerImpl::~AutoclickControllerImpl() { |
| 91 } | 102 } |
| 92 | 103 |
| 104 void AutoclickControllerImpl::SetDelegate(std::unique_ptr<Delegate> delegate) { |
| 105 delegate_ = std::move(delegate); |
| 106 } |
| 107 |
| 93 void AutoclickControllerImpl::SetEnabled(bool enabled) { | 108 void AutoclickControllerImpl::SetEnabled(bool enabled) { |
| 94 if (enabled_ == enabled) | 109 if (enabled_ == enabled) |
| 95 return; | 110 return; |
| 96 enabled_ = enabled; | 111 enabled_ = enabled; |
| 97 | 112 |
| 98 if (enabled_) { | 113 if (enabled_) { |
| 99 Shell::GetInstance()->AddPreTargetHandler(this); | 114 Shell::GetInstance()->AddPreTargetHandler(this); |
| 100 autoclick_timer_->Stop(); | 115 autoclick_timer_->Stop(); |
| 101 } else { | 116 } else { |
| 102 Shell::GetInstance()->RemovePreTargetHandler(this); | 117 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 103 } | 118 } |
| 104 } | 119 } |
| 105 | 120 |
| 106 bool AutoclickControllerImpl::IsEnabled() const { | 121 bool AutoclickControllerImpl::IsEnabled() const { |
| 107 return enabled_; | 122 return enabled_; |
| 108 } | 123 } |
| 109 | 124 |
| 110 void AutoclickControllerImpl::SetAutoclickDelay(int delay_ms) { | 125 void AutoclickControllerImpl::SetAutoclickDelay(base::TimeDelta delay) { |
| 111 delay_ms_ = delay_ms; | 126 delay_ = delay; |
| 112 InitClickTimer(); | 127 InitClickTimer(); |
| 113 } | 128 } |
| 114 | 129 |
| 115 int AutoclickControllerImpl::GetAutoclickDelay() const { | 130 base::TimeDelta AutoclickControllerImpl::GetAutoclickDelay() const { |
| 116 return delay_ms_; | 131 return delay_; |
| 132 } |
| 133 |
| 134 void AutoclickControllerImpl::StartRingDisplay() { |
| 135 if (delegate_) |
| 136 delegate_->StartGesture(delay_, anchor_location_); |
| 137 } |
| 138 |
| 139 void AutoclickControllerImpl::StopRingDisplay() { |
| 140 if (delegate_) |
| 141 delegate_->StopGesture(); |
| 142 } |
| 143 |
| 144 void AutoclickControllerImpl::ChangeRingDisplayCenter() { |
| 145 if (delegate_) |
| 146 delegate_->SetGestureCenter(current_mouse_location_); |
| 117 } | 147 } |
| 118 | 148 |
| 119 void AutoclickControllerImpl::InitClickTimer() { | 149 void AutoclickControllerImpl::InitClickTimer() { |
| 120 autoclick_timer_.reset(new base::Timer( | 150 autoclick_timer_.reset(new base::Timer( |
| 121 FROM_HERE, | 151 FROM_HERE, delay_, |
| 122 base::TimeDelta::FromMilliseconds(delay_ms_), | 152 base::Bind(&AutoclickControllerImpl::DoAutoclick, base::Unretained(this)), |
| 123 base::Bind(&AutoclickControllerImpl::DoAutoclick, | |
| 124 base::Unretained(this)), | |
| 125 false)); | 153 false)); |
| 126 } | 154 } |
| 127 | 155 |
| 128 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { | 156 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { |
| 129 if (event->type() == ui::ET_MOUSE_MOVED && | 157 if (event->type() == ui::ET_MOUSE_MOVED && |
| 130 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { | 158 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { |
| 131 mouse_event_flags_ = event->flags(); | 159 mouse_event_flags_ = event->flags(); |
| 132 | 160 |
| 133 gfx::Point mouse_location = event->location(); | 161 gfx::Point mouse_location = event->location(); |
| 134 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), | 162 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), |
| 135 &mouse_location); | 163 &mouse_location); |
| 136 | 164 |
| 137 // The distance between the mouse location and the anchor location | 165 // The distance between the mouse location and the anchor location |
| 138 // must exceed a certain threshold to initiate a new autoclick countdown. | 166 // must exceed a certain threshold to initiate a new autoclick countdown. |
| 139 // This ensures that mouse jitter caused by poor motor control does not | 167 // This ensures that mouse jitter caused by poor motor control does not |
| 140 // 1. initiate an unwanted autoclick from rest | 168 // 1. initiate an unwanted autoclick from rest |
| 141 // 2. prevent the autoclick from ever occuring when the mouse | 169 // 2. prevent the autoclick from ever occuring when the mouse |
| 142 // arrives at the target. | 170 // arrives at the target. |
| 143 gfx::Vector2d delta = mouse_location - anchor_location_; | 171 gfx::Vector2d delta = mouse_location - anchor_location_; |
| 144 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { | 172 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { |
| 145 anchor_location_ = mouse_location; | 173 anchor_location_ = mouse_location; |
| 146 autoclick_timer_->Reset(); | 174 autoclick_timer_->Reset(); |
| 175 StartRingDisplay(); |
| 176 } else if (autoclick_timer_->IsRunning()) { |
| 177 current_mouse_location_ = mouse_location; |
| 178 ChangeRingDisplayCenter(); |
| 147 } | 179 } |
| 148 } else if (event->type() == ui::ET_MOUSE_PRESSED) { | 180 } else if (event->type() == ui::ET_MOUSE_PRESSED) { |
| 149 autoclick_timer_->Stop(); | 181 autoclick_timer_->Stop(); |
| 182 StopRingDisplay(); |
| 150 } else if (event->type() == ui::ET_MOUSEWHEEL && | 183 } else if (event->type() == ui::ET_MOUSEWHEEL && |
| 151 autoclick_timer_->IsRunning()) { | 184 autoclick_timer_->IsRunning()) { |
| 152 autoclick_timer_->Reset(); | 185 autoclick_timer_->Reset(); |
| 186 StartRingDisplay(); |
| 153 } | 187 } |
| 154 } | 188 } |
| 155 | 189 |
| 156 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { | 190 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { |
| 157 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | | 191 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | |
| 158 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | | 192 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | |
| 159 ui::EF_IS_EXTENDED_KEY; | 193 ui::EF_IS_EXTENDED_KEY; |
| 160 int new_modifiers = event->flags() & modifier_mask; | 194 int new_modifiers = event->flags() & modifier_mask; |
| 161 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers; | 195 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers; |
| 162 | 196 |
| 163 if (!IsModifierKey(event->key_code())) | 197 if (!IsModifierKey(event->key_code())) { |
| 164 autoclick_timer_->Stop(); | 198 autoclick_timer_->Stop(); |
| 199 StopRingDisplay(); |
| 200 } |
| 165 } | 201 } |
| 166 | 202 |
| 167 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { | 203 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { |
| 168 autoclick_timer_->Stop(); | 204 autoclick_timer_->Stop(); |
| 205 StopRingDisplay(); |
| 169 } | 206 } |
| 170 | 207 |
| 171 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { | 208 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { |
| 172 autoclick_timer_->Stop(); | 209 autoclick_timer_->Stop(); |
| 210 StopRingDisplay(); |
| 173 } | 211 } |
| 174 | 212 |
| 175 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { | 213 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { |
| 176 autoclick_timer_->Stop(); | 214 autoclick_timer_->Stop(); |
| 215 StopRingDisplay(); |
| 177 } | 216 } |
| 178 | 217 |
| 179 void AutoclickControllerImpl::DoAutoclick() { | 218 void AutoclickControllerImpl::DoAutoclick() { |
| 180 gfx::Point screen_location = | 219 gfx::Point screen_location = |
| 181 aura::Env::GetInstance()->last_mouse_location(); | 220 aura::Env::GetInstance()->last_mouse_location(); |
| 182 aura::Window* root_window = | 221 aura::Window* root_window = |
| 183 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); | 222 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); |
| 184 DCHECK(root_window) << "Root window not found while attempting autoclick."; | 223 DCHECK(root_window) << "Root window not found while attempting autoclick."; |
| 185 | 224 |
| 186 gfx::Point click_location(screen_location); | 225 gfx::Point click_location(screen_location); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 206 if (details.dispatcher_destroyed) | 245 if (details.dispatcher_destroyed) |
| 207 return; | 246 return; |
| 208 } | 247 } |
| 209 | 248 |
| 210 // static. | 249 // static. |
| 211 AutoclickController* AutoclickController::CreateInstance() { | 250 AutoclickController* AutoclickController::CreateInstance() { |
| 212 return new AutoclickControllerImpl(); | 251 return new AutoclickControllerImpl(); |
| 213 } | 252 } |
| 214 | 253 |
| 215 } // namespace ash | 254 } // namespace ash |
| OLD | NEW |