forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoclick_ring_handler.cc
355 lines (306 loc) · 12.1 KB
/
autoclick_ring_handler.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/autoclick/autoclick_ring_handler.h"
#include "base/command_line.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/accessibility/accessibility_switches.h"
#include "ui/aura/window.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/transform.h"
#include "ui/views/view.h"
namespace ash {
namespace {
// The default values of the autoclick ring widget size.
const int kAutoclickRingOuterRadius = 30;
const int kAutoclickRingInnerRadius = 20;
// Angles from x-axis at which the outer and inner circles start.
const int kAutoclickRingInnerStartAngle = -90;
const int kAutoclickRingGlowWidth = 20;
// The following is half width to avoid division by 2.
const int kAutoclickRingArcWidth = 2;
// Start and end values for various animations.
const double kAutoclickRingScaleStartValue = 1.0;
const double kAutoclickRingScaleEndValue = 1.0;
const double kAutoclickRingShrinkScaleEndValue = 0.5;
const double kAutoclickRingOpacityStartValue = 0.1;
const double kAutoclickRingOpacityEndValue = 0.5;
const int kAutoclickRingAngleStartValue = -90;
// The sweep angle is a bit greater than 360 to make sure the circle
// completes at the end of the animation.
const int kAutoclickRingAngleEndValue = 360;
// Visual constants.
const SkColor kAutoclickRingArcColor = SkColorSetARGB(255, 0, 255, 0);
const SkColor kAutoclickRingCircleColor = SkColorSetARGB(255, 0, 0, 255);
// Constants for colors in V2, which has different UX.
const SkColor kAutoclickRingArcColorV2 = SkColorSetARGB(255, 255, 255, 255);
const SkColor kAutoclickRingCircleColorV2 = SkColorSetARGB(128, 0, 0, 0);
const SkColor kAutoclickRingUnderArcColor = SkColorSetARGB(100, 128, 134, 139);
void PaintAutoclickRingCircle(gfx::Canvas* canvas,
gfx::Point& center,
int radius) {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(2 * kAutoclickRingArcWidth);
flags.setColor(kAutoclickRingCircleColor);
flags.setAntiAlias(true);
canvas->DrawCircle(center, radius, flags);
}
void PaintAutoclickRingArc(gfx::Canvas* canvas,
const gfx::Point& center,
int radius,
int start_angle,
int end_angle) {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(2 * kAutoclickRingArcWidth);
flags.setColor(kAutoclickRingArcColor);
flags.setAntiAlias(true);
SkPath arc_path;
arc_path.addArc(SkRect::MakeXYWH(center.x() - radius, center.y() - radius,
2 * radius, 2 * radius),
start_angle, end_angle - start_angle);
canvas->DrawPath(arc_path, flags);
}
// Paints the full Autoclick ring.
void PaintAutoclickRingV2(gfx::Canvas* canvas,
const gfx::Point& center,
int radius,
int start_angle,
int end_angle) {
cc::PaintFlags flags;
flags.setAntiAlias(true);
// Draw the point being selected.
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(kAutoclickRingArcColorV2);
canvas->DrawCircle(center, kAutoclickRingArcWidth / 2, flags);
// Draw the outline of the point being selected.
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kAutoclickRingArcWidth);
flags.setColor(kAutoclickRingCircleColorV2);
canvas->DrawCircle(center, kAutoclickRingArcWidth * 3 / 2, flags);
// Draw the outline of the arc.
flags.setColor(kAutoclickRingCircleColorV2);
canvas->DrawCircle(center, radius + kAutoclickRingArcWidth, flags);
canvas->DrawCircle(center, radius - kAutoclickRingArcWidth, flags);
// Draw the background of the arc.
flags.setColor(kAutoclickRingUnderArcColor);
canvas->DrawCircle(center, radius, flags);
// Draw the arc.
SkPath arc_path;
arc_path.addArc(SkRect::MakeXYWH(center.x() - radius, center.y() - radius,
2 * radius, 2 * radius),
start_angle, end_angle - start_angle);
flags.setStrokeWidth(kAutoclickRingArcWidth);
flags.setColor(kAutoclickRingArcColorV2);
canvas->DrawPath(arc_path, flags);
}
} // namespace
// View of the AutoclickRingHandler. Draws the actual contents and updates as
// the animation proceeds. It also maintains the views::Widget that the
// animation is shown in.
class AutoclickRingHandler::AutoclickRingView : public views::View {
public:
AutoclickRingView(const gfx::Point& event_location,
views::Widget* ring_widget,
int outer_radius,
int inner_radius)
: views::View(),
widget_(ring_widget),
current_angle_(kAutoclickRingAngleStartValue),
current_scale_(kAutoclickRingScaleStartValue),
outer_radius_(outer_radius),
inner_radius_(inner_radius),
is_v2_enabled_(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableExperimentalAccessibilityAutoclick)) {
widget_->SetContentsView(this);
// We are owned by the AutoclickRingHandler.
set_owned_by_client();
SetNewLocation(event_location);
}
~AutoclickRingView() override = default;
void SetNewLocation(const gfx::Point& new_event_location) {
gfx::Point point = new_event_location;
widget_->SetBounds(
gfx::Rect(point.x() - (outer_radius_ + kAutoclickRingGlowWidth),
point.y() - (outer_radius_ + kAutoclickRingGlowWidth),
GetPreferredSize().width(), GetPreferredSize().height()));
widget_->Show();
if (is_v2_enabled_) {
widget_->GetNativeView()->layer()->SetOpacity(1.0);
} else {
widget_->GetNativeView()->layer()->SetOpacity(
kAutoclickRingOpacityStartValue);
}
}
void UpdateWithGrowAnimation(gfx::Animation* animation) {
// Update the portion of the circle filled so far and re-draw.
current_angle_ = animation->CurrentValueBetween(
kAutoclickRingInnerStartAngle, kAutoclickRingAngleEndValue);
if (!is_v2_enabled_) {
current_scale_ = animation->CurrentValueBetween(
kAutoclickRingScaleStartValue, kAutoclickRingScaleEndValue);
widget_->GetNativeView()->layer()->SetOpacity(
animation->CurrentValueBetween(kAutoclickRingOpacityStartValue,
kAutoclickRingOpacityEndValue));
}
SchedulePaint();
}
void UpdateWithShrinkAnimation(gfx::Animation* animation) {
current_angle_ = animation->CurrentValueBetween(
kAutoclickRingInnerStartAngle, kAutoclickRingAngleEndValue);
if (!is_v2_enabled_) {
current_scale_ = animation->CurrentValueBetween(
kAutoclickRingScaleEndValue, kAutoclickRingShrinkScaleEndValue);
widget_->GetNativeView()->layer()->SetOpacity(
animation->CurrentValueBetween(kAutoclickRingOpacityStartValue,
kAutoclickRingOpacityEndValue));
}
SchedulePaint();
}
void SetSize(int outer_radius, int inner_radius) {
outer_radius_ = outer_radius;
inner_radius_ = inner_radius;
}
private:
// Overridden from views::View.
gfx::Size CalculatePreferredSize() const override {
return gfx::Size(2 * (outer_radius_ + kAutoclickRingGlowWidth),
2 * (outer_radius_ + kAutoclickRingGlowWidth));
}
void OnPaint(gfx::Canvas* canvas) override {
gfx::Point center(GetPreferredSize().width() / 2,
GetPreferredSize().height() / 2);
canvas->Save();
gfx::Transform scale;
scale.Scale(current_scale_, current_scale_);
// We want to scale from the center.
canvas->Translate(center.OffsetFromOrigin());
canvas->Transform(scale);
canvas->Translate(-center.OffsetFromOrigin());
if (is_v2_enabled_) {
PaintAutoclickRingV2(canvas, center, inner_radius_,
kAutoclickRingInnerStartAngle, current_angle_);
} else {
// Paint inner circle.
PaintAutoclickRingArc(canvas, center, inner_radius_,
kAutoclickRingInnerStartAngle, current_angle_);
// Paint outer circle.
PaintAutoclickRingCircle(canvas, center, outer_radius_);
}
canvas->Restore();
}
views::Widget* widget_;
int current_angle_;
double current_scale_;
int outer_radius_;
int inner_radius_;
// Autoclick UX is being updated. This bool tracks whether the feature flag
// was set to view the new UX, or whether the old UX should be shown.
// TODO(crbug.com/894907): Remove this flag and the old UX when launching.
bool is_v2_enabled_;
DISALLOW_COPY_AND_ASSIGN(AutoclickRingView);
};
////////////////////////////////////////////////////////////////////////////////
// AutoclickRingHandler, public
AutoclickRingHandler::AutoclickRingHandler()
: gfx::LinearAnimation(nullptr),
ring_widget_(nullptr),
current_animation_type_(AnimationType::NONE),
outer_radius_(kAutoclickRingOuterRadius),
inner_radius_(kAutoclickRingInnerRadius) {}
AutoclickRingHandler::~AutoclickRingHandler() {
StopAutoclickRing();
}
void AutoclickRingHandler::StartGesture(
base::TimeDelta duration,
const gfx::Point& center_point_in_screen,
views::Widget* widget) {
StopAutoclickRing();
tap_down_location_ = center_point_in_screen;
ring_widget_ = widget;
current_animation_type_ = AnimationType::GROW_ANIMATION;
animation_duration_ = duration;
StartAnimation(base::TimeDelta());
}
void AutoclickRingHandler::StopGesture() {
StopAutoclickRing();
}
void AutoclickRingHandler::SetGestureCenter(
const gfx::Point& center_point_in_screen,
views::Widget* widget) {
tap_down_location_ = center_point_in_screen;
ring_widget_ = widget;
}
void AutoclickRingHandler::SetSize(int outer_radius, int inner_radius) {
outer_radius_ = outer_radius;
inner_radius_ = inner_radius;
if (view_)
view_->SetSize(outer_radius, inner_radius);
}
////////////////////////////////////////////////////////////////////////////////
// AutoclickRingHandler, private
void AutoclickRingHandler::StartAnimation(base::TimeDelta delay) {
switch (current_animation_type_) {
case AnimationType::GROW_ANIMATION: {
view_.reset(new AutoclickRingView(tap_down_location_, ring_widget_,
outer_radius_, inner_radius_));
SetDuration(delay);
Start();
break;
}
case AnimationType::SHRINK_ANIMATION: {
view_.reset(new AutoclickRingView(tap_down_location_, ring_widget_,
outer_radius_, inner_radius_));
SetDuration(delay);
Start();
break;
}
case AnimationType::NONE:
NOTREACHED();
break;
}
}
void AutoclickRingHandler::StopAutoclickRing() {
// Since, Animation::Stop() calls AnimationStopped(), we need to reset the
// |current_animation_type_| before Stop(), otherwise AnimationStopped() may
// start the timer again.
current_animation_type_ = AnimationType::NONE;
Stop();
view_.reset();
}
void AutoclickRingHandler::AnimateToState(double state) {
DCHECK(view_.get());
switch (current_animation_type_) {
case AnimationType::GROW_ANIMATION:
view_->SetNewLocation(tap_down_location_);
view_->UpdateWithGrowAnimation(this);
break;
case AnimationType::SHRINK_ANIMATION:
view_->SetNewLocation(tap_down_location_);
view_->UpdateWithShrinkAnimation(this);
break;
case AnimationType::NONE:
NOTREACHED();
break;
}
}
void AutoclickRingHandler::AnimationStopped() {
switch (current_animation_type_) {
case AnimationType::GROW_ANIMATION:
current_animation_type_ = AnimationType::SHRINK_ANIMATION;
StartAnimation(animation_duration_);
break;
case AnimationType::SHRINK_ANIMATION:
current_animation_type_ = AnimationType::NONE;
break;
case AnimationType::NONE:
// fall through to reset the view.
view_.reset();
break;
}
}
} // namespace ash