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