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
ash / system / accessibility / select_to_speak / select_to_speak_menu_view.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/accessibility/select_to_speak/select_to_speak_menu_view.h"
#include "ash/public/cpp/accessibility_controller_enums.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_provider.h"
#include "ash/system/accessibility/floating_menu_button.h"
#include "ash/system/accessibility/select_to_speak/select_to_speak_constants.h"
#include "ash/system/accessibility/select_to_speak/select_to_speak_metrics_utils.h"
#include "ash/system/tray/tray_constants.h"
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/metrics/histogram_functions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/box_layout.h"
namespace ash {
namespace {
constexpr int kButtonSize = 36;
constexpr int kStopButtonPadding = 14;
constexpr int kSeparatorHeight = 16;
void RecordButtonMetric(SelectToSpeakPanelAction action) {
base::UmaHistogramEnumeration(
"Accessibility.CrosSelectToSpeak.BubbleButtonPress", action);
}
void RecordKeyPressMetric(SelectToSpeakPanelAction action) {
base::UmaHistogramEnumeration(
"Accessibility.CrosSelectToSpeak.BubbleKeyPress", action);
}
// Histograms in which user action statistics are recorded. These values
// correspond to their respective entries in histograms.xml, so if they are
// changed, please deprecate the corresponding histograms there.
const char kParagraphNavigationMethodHistogramName[] =
"Accessibility.CrosSelectToSpeak.ParagraphNavigationMethod";
const char kSentenceNavigationMethodHistogramName[] =
"Accessibility.CrosSelectToSpeak.SentenceNavigationMethod";
const char kBubbleDismissMethodHistogramName[] =
"Accessibility.CrosSelectToSpeak.BubbleDismissMethod";
SelectToSpeakPanelAction PanelActionForButtonID(int button_id, bool is_paused) {
auto button_enum = static_cast<SelectToSpeakMenuView::ButtonId>(button_id);
switch (button_enum) {
case SelectToSpeakMenuView::ButtonId::kPrevParagraph:
return SelectToSpeakPanelAction::kPreviousParagraph;
case SelectToSpeakMenuView::ButtonId::kPrevSentence:
return SelectToSpeakPanelAction::kPreviousSentence;
case SelectToSpeakMenuView::ButtonId::kPause:
// Pause button toggles pause/resume state.
if (is_paused)
return SelectToSpeakPanelAction::kResume;
else
return SelectToSpeakPanelAction::kPause;
case SelectToSpeakMenuView::ButtonId::kNextParagraph:
return SelectToSpeakPanelAction::kNextParagraph;
case SelectToSpeakMenuView::ButtonId::kNextSentence:
return SelectToSpeakPanelAction::kNextSentence;
case SelectToSpeakMenuView::ButtonId::kStop:
return SelectToSpeakPanelAction::kExit;
case SelectToSpeakMenuView::ButtonId::kSpeed:
return SelectToSpeakPanelAction::kChangeSpeed;
}
NOTREACHED();
}
} // namespace
SelectToSpeakMenuView::SelectToSpeakMenuView(Delegate* delegate)
: delegate_(delegate) {
int total_height = kUnifiedTopShortcutSpacing * 2 + kTrayItemSize;
int separator_spacing = (total_height - kSeparatorHeight) / 2;
views::Builder<SelectToSpeakMenuView>(this)
.SetCrossAxisAlignment(views::BoxLayout::CrossAxisAlignment::kEnd)
.AddChildren(
views::Builder<views::BoxLayoutView>()
.SetInsideBorderInsets(kUnifiedMenuItemPadding)
.SetBetweenChildSpacing(kUnifiedTopShortcutSpacing)
.AddChildren(
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&prev_paragraph_button_)
.SetID(static_cast<int>(ButtonId::kPrevParagraph))
.SetVectorIcon(kSelectToSpeakPrevParagraphIcon)
.SetFlipCanvasOnPaintForRTLUI(true)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_PREV_PARAGRAPH))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(prev_paragraph_button_))),
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&prev_sentence_button_)
.SetID(static_cast<int>(ButtonId::kPrevSentence))
.SetVectorIcon(kSelectToSpeakPrevSentenceIcon)
.SetFlipCanvasOnPaintForRTLUI(true)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_PREV_SENTENCE))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(prev_sentence_button_))),
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&pause_button_)
.SetID(static_cast<int>(ButtonId::kPause))
.SetVectorIcon(kSelectToSpeakPauseIcon)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_PAUSE))
// Setting the accessible name means that ChromeVox will
// read this rather than the play/pause tooltip. This
// ensures that ChromeVox doesn't immediately interrupt
// reading to announce that the button tooltip text
// changed.
.SetAccessibleName(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_TOGGLE_PLAYBACK))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(pause_button_))),
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&next_sentence_button_)
.SetID(static_cast<int>(ButtonId::kNextSentence))
.SetVectorIcon(kSelectToSpeakNextSentenceIcon)
.SetFlipCanvasOnPaintForRTLUI(true)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_NEXT_SENTENCE))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(next_sentence_button_))),
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&next_paragraph_button_)
.SetID(static_cast<int>(ButtonId::kNextParagraph))
.SetVectorIcon(kSelectToSpeakNextParagraphIcon)
.SetFlipCanvasOnPaintForRTLUI(true)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_NEXT_PARAGRAPH))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(next_paragraph_button_))),
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&speed_button_)
.SetID(static_cast<int>(ButtonId::kSpeed))
.SetVectorIcon(kSelectToSpeakReadingSpeedNormalIcon)
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_READING_SPEED))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(speed_button_)))),
views::Builder<views::Separator>()
.SetColorId(ui::kColorAshSystemUIMenuSeparator)
.SetPreferredLength(kSeparatorHeight)
.SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(
separator_spacing - kUnifiedTopShortcutSpacing, 0,
separator_spacing, 0))),
views::Builder<views::BoxLayoutView>()
.SetInsideBorderInsets(gfx::Insets::TLBR(0, kStopButtonPadding,
kStopButtonPadding,
kStopButtonPadding))
.SetBetweenChildSpacing(kStopButtonPadding)
.AddChildren(
views::Builder<FloatingMenuButton>()
.CopyAddressTo(&stop_button_)
.SetID(static_cast<int>(ButtonId::kStop))
.SetVectorIcon(kSelectToSpeakStopIcon)
.SetDrawHighlight(false)
.SetPreferredSize(gfx::Size(kButtonSize, kButtonSize))
.SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_SELECT_TO_SPEAK_EXIT))
.SetCallback(base::BindRepeating(
&SelectToSpeakMenuView::OnButtonPressed,
base::Unretained(this),
base::Unretained(stop_button_)))))
.BuildChildren();
}
void SelectToSpeakMenuView::SetInitialSpeechRate(double initial_speech_rate) {
const gfx::VectorIcon* speed_icon = &kSelectToSpeakReadingSpeedNormalIcon;
if (initial_speech_rate == kSelectToSpeakSpeechRateSlow) {
speed_icon = &kSelectToSpeakReadingSpeedSlowIcon;
} else if (initial_speech_rate == kSelectToSpeakSpeechRatePeppy) {
speed_icon = &kSelectToSpeakReadingSpeedPeppyIcon;
} else if (initial_speech_rate == kSelectToSpeakSpeechRateFast) {
speed_icon = &kSelectToSpeakReadingSpeedFastIcon;
} else if (initial_speech_rate == kSelectToSpeakSpeechRateFaster) {
speed_icon = &kSelectToSpeakReadingSpeedFasterIcon;
}
speed_button_->SetVectorIcon(*speed_icon);
}
void SelectToSpeakMenuView::OnKeyEvent(ui::KeyEvent* key_event) {
if (key_event->type() != ui::EventType::kKeyPressed ||
key_event->is_repeat()) {
// Only process key when first pressed.
return;
}
auto action = SelectToSpeakPanelAction::kNone;
switch (key_event->key_code()) {
case ui::KeyboardCode::VKEY_LEFT:
if (base::i18n::IsRTL()) {
action = SelectToSpeakPanelAction::kNextSentence;
} else {
action = SelectToSpeakPanelAction::kPreviousSentence;
}
base::UmaHistogramEnumeration(
kSentenceNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kKeyboardShortcut);
break;
case ui::KeyboardCode::VKEY_RIGHT:
if (base::i18n::IsRTL()) {
action = SelectToSpeakPanelAction::kPreviousSentence;
} else {
action = SelectToSpeakPanelAction::kNextSentence;
}
base::UmaHistogramEnumeration(
kSentenceNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kKeyboardShortcut);
break;
case ui::KeyboardCode::VKEY_UP:
action = SelectToSpeakPanelAction::kPreviousParagraph;
base::UmaHistogramEnumeration(
kParagraphNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kKeyboardShortcut);
break;
case ui::KeyboardCode::VKEY_DOWN:
action = SelectToSpeakPanelAction::kNextParagraph;
base::UmaHistogramEnumeration(
kParagraphNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kKeyboardShortcut);
break;
case ui::KeyboardCode::VKEY_ESCAPE:
action = SelectToSpeakPanelAction::kExit;
base::UmaHistogramEnumeration(
kBubbleDismissMethodHistogramName,
CrosSelectToSpeakActivationMethod::kKeyboardShortcut);
break;
default:
// Unhandled key.
return;
}
RecordKeyPressMetric(action);
delegate_->OnActionSelected(action);
key_event->SetHandled();
key_event->StopPropagation();
}
void SelectToSpeakMenuView::SetPaused(bool is_paused) {
pause_button_->SetVectorIcon(is_paused ? kSelectToSpeakPlayIcon
: kSelectToSpeakPauseIcon);
pause_button_->SetTooltipText(
l10n_util::GetStringUTF16(is_paused ? IDS_ASH_SELECT_TO_SPEAK_RESUME
: IDS_ASH_SELECT_TO_SPEAK_PAUSE));
is_paused_ = is_paused;
}
void SelectToSpeakMenuView::SetInitialFocus() {
pause_button_->RequestFocus();
}
void SelectToSpeakMenuView::SetSpeedButtonFocused() {
speed_button_->RequestFocus();
}
void SelectToSpeakMenuView::SetSpeedButtonToggled(bool toggled) {
speed_button_->SetToggled(toggled);
}
void SelectToSpeakMenuView::OnButtonPressed(views::Button* sender) {
SelectToSpeakPanelAction action =
PanelActionForButtonID(sender->GetID(), is_paused_);
RecordButtonMetric(action);
switch (action) {
case SelectToSpeakPanelAction::kPreviousParagraph:
[[fallthrough]];
case SelectToSpeakPanelAction::kNextParagraph:
base::UmaHistogramEnumeration(
kParagraphNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kMenuButton);
break;
case SelectToSpeakPanelAction::kPreviousSentence:
[[fallthrough]];
case SelectToSpeakPanelAction::kNextSentence:
base::UmaHistogramEnumeration(
kSentenceNavigationMethodHistogramName,
CrosSelectToSpeakActivationMethod::kMenuButton);
break;
case SelectToSpeakPanelAction::kExit:
base::UmaHistogramEnumeration(
kBubbleDismissMethodHistogramName,
CrosSelectToSpeakActivationMethod::kMenuButton);
break;
default:
break; // Nothing to record
}
delegate_->OnActionSelected(action);
}
BEGIN_METADATA(SelectToSpeakMenuView)
END_METADATA
} // namespace ash