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