Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(580)

Side by Side Diff: ash/autoclick/autoclick_controller.cc

Issue 2016073004: Show a visual indicator for the progress of auto-click. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Fixed errors from patch set 2. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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.
jdufault 2016/06/08 17:50:19 Move this into the other AutoclickController overr
sammiequon 2016/06/09 04:03:46 Done.
53 void SetDelegate(
54 std::unique_ptr<Delegate> autoclickgesture_delegate) override;
jdufault 2016/06/08 17:50:19 Just delegate
sammiequon 2016/06/09 04:03:46 Done.
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<Delegate> autoclick_gesture_delegate_;
jdufault 2016/06/08 17:50:19 Just delegate_
sammiequon 2016/06/09 04:03:46 Done.
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()
jdufault 2016/06/08 17:50:19 Restore newline
sammiequon 2016/06/09 04:03:46 Done.
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::SetDelegate(
103 std::unique_ptr<Delegate> 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 (autoclick_gesture_delegate_)
135 autoclick_gesture_delegate_->StartGesture(delay_ms_, anchor_location_);
136 }
137
138 void AutoclickControllerImpl::StopRingDisplay() {
139 if (autoclick_gesture_delegate_)
140 autoclick_gesture_delegate_->StopGesture();
141 }
142
143 void AutoclickControllerImpl::ChangeRingDisplayCenter() {
144 if (autoclick_gesture_delegate_)
145 autoclick_gesture_delegate_->SetGestureCenter(anchor_location_);
146 }
147
119 void AutoclickControllerImpl::InitClickTimer() { 148 void AutoclickControllerImpl::InitClickTimer() {
120 autoclick_timer_.reset(new base::Timer( 149 autoclick_timer_.reset(new base::Timer(
121 FROM_HERE, 150 FROM_HERE,
122 base::TimeDelta::FromMilliseconds(delay_ms_), 151 base::TimeDelta::FromMilliseconds(delay_ms_),
123 base::Bind(&AutoclickControllerImpl::DoAutoclick, 152 base::Bind(&AutoclickControllerImpl::DoAutoclick,
124 base::Unretained(this)), 153 base::Unretained(this)),
125 false)); 154 false));
126 } 155 }
127 156
128 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) { 157 void AutoclickControllerImpl::OnMouseEvent(ui::MouseEvent* event) {
129 if (event->type() == ui::ET_MOUSE_MOVED && 158 if (event->type() == ui::ET_MOUSE_MOVED &&
130 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { 159 !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
131 mouse_event_flags_ = event->flags(); 160 mouse_event_flags_ = event->flags();
132 161
133 gfx::Point mouse_location = event->location(); 162 gfx::Point mouse_location = event->location();
134 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), 163 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()),
135 &mouse_location); 164 &mouse_location);
136 165
137 // The distance between the mouse location and the anchor location 166 // The distance between the mouse location and the anchor location
138 // must exceed a certain threshold to initiate a new autoclick countdown. 167 // must exceed a certain threshold to initiate a new autoclick countdown.
139 // This ensures that mouse jitter caused by poor motor control does not 168 // This ensures that mouse jitter caused by poor motor control does not
140 // 1. initiate an unwanted autoclick from rest 169 // 1. initiate an unwanted autoclick from rest
141 // 2. prevent the autoclick from ever occuring when the mouse 170 // 2. prevent the autoclick from ever occuring when the mouse
142 // arrives at the target. 171 // arrives at the target.
143 gfx::Vector2d delta = mouse_location - anchor_location_; 172 gfx::Vector2d delta = mouse_location - anchor_location_;
144 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) { 173 if (delta.LengthSquared() >= kMovementThreshold * kMovementThreshold) {
145 anchor_location_ = mouse_location; 174 anchor_location_ = mouse_location;
146 autoclick_timer_->Reset(); 175 autoclick_timer_->Reset();
176 StartRingDisplay();
177 } else if (autoclick_timer_->IsRunning()) {
178 anchor_location_ = mouse_location;
179 ChangeRingDisplayCenter();
147 } 180 }
148 } else if (event->type() == ui::ET_MOUSE_PRESSED) { 181 } else if (event->type() == ui::ET_MOUSE_PRESSED) {
149 autoclick_timer_->Stop(); 182 autoclick_timer_->Stop();
183 StopRingDisplay();
150 } else if (event->type() == ui::ET_MOUSEWHEEL && 184 } else if (event->type() == ui::ET_MOUSEWHEEL &&
151 autoclick_timer_->IsRunning()) { 185 autoclick_timer_->IsRunning()) {
152 autoclick_timer_->Reset(); 186 autoclick_timer_->Reset();
187 StartRingDisplay();
153 } 188 }
154 } 189 }
155 190
156 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) { 191 void AutoclickControllerImpl::OnKeyEvent(ui::KeyEvent* event) {
157 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | 192 int modifier_mask = ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
158 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN | 193 ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN |
159 ui::EF_IS_EXTENDED_KEY; 194 ui::EF_IS_EXTENDED_KEY;
160 int new_modifiers = event->flags() & modifier_mask; 195 int new_modifiers = event->flags() & modifier_mask;
161 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers; 196 mouse_event_flags_ = (mouse_event_flags_ & ~modifier_mask) | new_modifiers;
162 197
163 if (!IsModifierKey(event->key_code())) 198 if (!IsModifierKey(event->key_code())) {
164 autoclick_timer_->Stop(); 199 autoclick_timer_->Stop();
200 StopRingDisplay();
201 }
165 } 202 }
166 203
167 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) { 204 void AutoclickControllerImpl::OnTouchEvent(ui::TouchEvent* event) {
168 autoclick_timer_->Stop(); 205 autoclick_timer_->Stop();
206 StopRingDisplay();
169 } 207 }
170 208
171 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) { 209 void AutoclickControllerImpl::OnGestureEvent(ui::GestureEvent* event) {
172 autoclick_timer_->Stop(); 210 autoclick_timer_->Stop();
211 StopRingDisplay();
173 } 212 }
174 213
175 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) { 214 void AutoclickControllerImpl::OnScrollEvent(ui::ScrollEvent* event) {
176 autoclick_timer_->Stop(); 215 autoclick_timer_->Stop();
216 StopRingDisplay();
177 } 217 }
178 218
179 void AutoclickControllerImpl::DoAutoclick() { 219 void AutoclickControllerImpl::DoAutoclick() {
180 gfx::Point screen_location = 220 gfx::Point screen_location =
181 aura::Env::GetInstance()->last_mouse_location(); 221 aura::Env::GetInstance()->last_mouse_location();
182 aura::Window* root_window = 222 aura::Window* root_window =
183 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location)); 223 WmWindowAura::GetAuraWindow(wm::GetRootWindowAt(screen_location));
184 DCHECK(root_window) << "Root window not found while attempting autoclick."; 224 DCHECK(root_window) << "Root window not found while attempting autoclick.";
185 225
186 gfx::Point click_location(screen_location); 226 gfx::Point click_location(screen_location);
(...skipping 19 matching lines...) Expand all
206 if (details.dispatcher_destroyed) 246 if (details.dispatcher_destroyed)
207 return; 247 return;
208 } 248 }
209 249
210 // static. 250 // static.
211 AutoclickController* AutoclickController::CreateInstance() { 251 AutoclickController* AutoclickController::CreateInstance() {
212 return new AutoclickControllerImpl(); 252 return new AutoclickControllerImpl();
213 } 253 }
214 254
215 } // namespace ash 255 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698