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

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 Patch Set 4 errors. 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 23 matching lines...) Expand all
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 const base::TimeDelta AutoclickController::kDefaultAutoclickDelay =
45 base::TimeDelta::FromMillisecondsD(1000.0);
45 46
46 class AutoclickControllerImpl : public AutoclickController, 47 class AutoclickControllerImpl : public AutoclickController,
47 public ui::EventHandler { 48 public ui::EventHandler {
48 public: 49 public:
49 AutoclickControllerImpl(); 50 AutoclickControllerImpl();
50 ~AutoclickControllerImpl() override; 51 ~AutoclickControllerImpl() override;
51 52
52 private: 53 private:
53 // AutoclickController overrides: 54 // AutoclickController overrides:
55 void SetDelegate(std::unique_ptr<Delegate> delegate) override;
54 void SetEnabled(bool enabled) override; 56 void SetEnabled(bool enabled) override;
55 bool IsEnabled() const override; 57 bool IsEnabled() const override;
56 void SetAutoclickDelay(int delay_ms) override; 58 void SetAutoclickDelay(base::TimeDelta delay) override;
57 int GetAutoclickDelay() const override; 59 base::TimeDelta GetAutoclickDelay() const override;
58 60
59 // ui::EventHandler overrides: 61 // ui::EventHandler overrides:
60 void OnMouseEvent(ui::MouseEvent* event) override; 62 void OnMouseEvent(ui::MouseEvent* event) override;
61 void OnKeyEvent(ui::KeyEvent* event) override; 63 void OnKeyEvent(ui::KeyEvent* event) override;
62 void OnTouchEvent(ui::TouchEvent* event) override; 64 void OnTouchEvent(ui::TouchEvent* event) override;
63 void OnGestureEvent(ui::GestureEvent* event) override; 65 void OnGestureEvent(ui::GestureEvent* event) override;
64 void OnScrollEvent(ui::ScrollEvent* event) override; 66 void OnScrollEvent(ui::ScrollEvent* event) override;
65 67
68 void StartRingDisplay();
69 void StopRingDisplay();
70 void ChangeRingDisplayCenter();
71
66 void InitClickTimer(); 72 void InitClickTimer();
67 73
68 void DoAutoclick(); 74 void DoAutoclick();
69 75
70 bool enabled_; 76 bool enabled_;
71 int delay_ms_; 77 base::TimeDelta delay_;
72 int mouse_event_flags_; 78 int mouse_event_flags_;
73 std::unique_ptr<base::Timer> autoclick_timer_; 79 std::unique_ptr<base::Timer> autoclick_timer_;
80 std::unique_ptr<Delegate> delegate_;
74 // The position in screen coordinates used to determine 81 // The position in screen coordinates used to determine
75 // the distance the mouse has moved. 82 // the distance the mouse has moved.
76 gfx::Point anchor_location_; 83 gfx::Point anchor_location_;
77 84
78 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl); 85 DISALLOW_COPY_AND_ASSIGN(AutoclickControllerImpl);
79 }; 86 };
80 87
81
82 AutoclickControllerImpl::AutoclickControllerImpl() 88 AutoclickControllerImpl::AutoclickControllerImpl()
83 : enabled_(false), 89 : enabled_(false),
84 delay_ms_(kDefaultAutoclickDelayMs), 90 delay_(kDefaultAutoclickDelay),
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698