forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax_event_intent_mac.mm
229 lines (206 loc) · 9.01 KB
/
ax_event_intent_mac.mm
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
// Copyright 2020 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 "ui/accessibility/platform/ax_event_intent_mac.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_event_intent.h"
namespace ui {
// static
AXTextSelection AXTextSelection::FromDirectionAndGranularity(
ax::mojom::TextBoundary text_boundary,
ax::mojom::MoveDirection move_direction) {
bool has_stayed_within_same_text_unit = false;
AXTextSelectionDirection direction = AXTextSelectionDirection::kDiscontiguous;
AXTextSelectionGranularity granularity = AXTextSelectionGranularity::kUnknown;
switch (text_boundary) {
case ax::mojom::TextBoundary::kNone:
break;
case ax::mojom::TextBoundary::kCharacter:
granularity = AXTextSelectionGranularity::kCharacter;
break;
case ax::mojom::TextBoundary::kFormat:
break; // Not supported on Mac.
case ax::mojom::TextBoundary::kLineEnd:
granularity = AXTextSelectionGranularity::kLine;
break;
case ax::mojom::TextBoundary::kLineStart:
granularity = AXTextSelectionGranularity::kLine;
break;
case ax::mojom::TextBoundary::kLineStartOrEnd:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kLine;
break;
case ax::mojom::TextBoundary::kObject:
break;
case ax::mojom::TextBoundary::kPageEnd:
granularity = AXTextSelectionGranularity::kPage;
break;
case ax::mojom::TextBoundary::kPageStart:
granularity = AXTextSelectionGranularity::kPage;
break;
case ax::mojom::TextBoundary::kPageStartOrEnd:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kPage;
break;
case ax::mojom::TextBoundary::kParagraphEnd:
granularity = AXTextSelectionGranularity::kParagraph;
break;
case ax::mojom::TextBoundary::kParagraphStart:
granularity = AXTextSelectionGranularity::kParagraph;
break;
case ax::mojom::TextBoundary::kParagraphStartOrEnd:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kParagraph;
break;
case ax::mojom::TextBoundary::kSentenceEnd:
granularity = AXTextSelectionGranularity::kSentence;
break;
case ax::mojom::TextBoundary::kSentenceStart:
granularity = AXTextSelectionGranularity::kSentence;
break;
case ax::mojom::TextBoundary::kSentenceStartOrEnd:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kSentence;
break;
case ax::mojom::TextBoundary::kWebPage:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kDocument;
break;
case ax::mojom::TextBoundary::kWordEnd:
granularity = AXTextSelectionGranularity::kWord;
break;
case ax::mojom::TextBoundary::kWordStart:
granularity = AXTextSelectionGranularity::kWord;
break;
case ax::mojom::TextBoundary::kWordStartOrEnd:
has_stayed_within_same_text_unit = true;
granularity = AXTextSelectionGranularity::kWord;
break;
}
switch (move_direction) {
case ax::mojom::MoveDirection::kNone:
break;
case ax::mojom::MoveDirection::kBackward:
if (has_stayed_within_same_text_unit) {
direction = AXTextSelectionDirection::kBeginning;
} else {
direction = AXTextSelectionDirection::kPrevious;
}
break;
case ax::mojom::MoveDirection::kForward:
if (has_stayed_within_same_text_unit) {
direction = AXTextSelectionDirection::kEnd;
} else {
direction = AXTextSelectionDirection::kNext;
}
break;
}
return AXTextSelection(direction, granularity, /* focus_change */ false);
}
AXTextSelection::AXTextSelection() = default;
AXTextSelection::AXTextSelection(AXTextSelectionDirection direction,
AXTextSelectionGranularity granularity,
bool focus_change)
: direction(direction),
granularity(granularity),
focus_change(focus_change) {}
AXTextSelection::AXTextSelection(const AXTextSelection& selection) = default;
AXTextSelection::~AXTextSelection() = default;
AXTextSelection& AXTextSelection::operator=(const AXTextSelection& selection) =
default;
// static
AXTextStateChangeIntent
AXTextStateChangeIntent::DefaultFocusTextStateChangeIntent() {
return AXTextStateChangeIntent(
AXTextStateChangeType::kSelectionMove,
AXTextSelection(AXTextSelectionDirection::kDiscontiguous,
AXTextSelectionGranularity::kUnknown,
/* focus_change */ true));
}
// static
AXTextStateChangeIntent
AXTextStateChangeIntent::DefaultSelectionChangeIntent() {
return AXTextStateChangeIntent(
AXTextStateChangeType::kSelectionMove,
AXTextSelection(AXTextSelectionDirection::kDiscontiguous,
AXTextSelectionGranularity::kUnknown,
/* focus_change */ false));
}
AXTextStateChangeIntent::AXTextStateChangeIntent() = default;
AXTextStateChangeIntent::AXTextStateChangeIntent(AXTextStateChangeType type,
AXTextSelection selection)
: type(type), selection(selection) {}
AXTextStateChangeIntent::AXTextStateChangeIntent(AXTextEditType edit)
: type(AXTextStateChangeType::kEdit), edit(edit) {}
AXTextStateChangeIntent::AXTextStateChangeIntent(
const AXTextStateChangeIntent& intent) = default;
AXTextStateChangeIntent::~AXTextStateChangeIntent() = default;
AXTextStateChangeIntent& AXTextStateChangeIntent::operator=(
const AXTextStateChangeIntent& intent) = default;
AXTextStateChangeIntent FromEventIntent(const AXEventIntent& event_intent) {
switch (event_intent.command) {
case ax::mojom::Command::kNone:
return AXTextStateChangeIntent(); // An unknown intent.
case ax::mojom::Command::kClearSelection:
return AXTextStateChangeIntent::DefaultSelectionChangeIntent();
case ax::mojom::Command::kDelete:
switch (event_intent.input_event_type) {
case ax::mojom::InputEventType::kDeleteByCut:
case ax::mojom::InputEventType::kDeleteByDrag:
return AXTextStateChangeIntent(AXTextEditType::kCut);
default:
return AXTextStateChangeIntent(AXTextEditType::kDelete);
}
break;
case ax::mojom::Command::kDictate:
return AXTextStateChangeIntent(AXTextEditType::kDictation);
case ax::mojom::Command::kExtendSelection:
return AXTextStateChangeIntent(
AXTextStateChangeType::kSelectionExtend,
AXTextSelection::FromDirectionAndGranularity(
event_intent.text_boundary, event_intent.move_direction));
case ax::mojom::Command::kFormat:
return AXTextStateChangeIntent(AXTextEditType::kAttributesChange);
case ax::mojom::Command::kHistory:
return AXTextStateChangeIntent(); // Not currently implemented on Mac.
case ax::mojom::Command::kInsert:
switch (event_intent.input_event_type) {
case ax::mojom::InputEventType::kInsertText:
case ax::mojom::InputEventType::kInsertLineBreak:
case ax::mojom::InputEventType::kInsertParagraph:
case ax::mojom::InputEventType::kInsertHorizontalRule:
return AXTextStateChangeIntent(AXTextEditType::kTyping);
case ax::mojom::InputEventType::kInsertFromPaste:
case ax::mojom::InputEventType::kInsertFromDrop:
case ax::mojom::InputEventType::kInsertFromYank:
return AXTextStateChangeIntent(AXTextEditType::kPaste);
default:
return AXTextStateChangeIntent(AXTextEditType::kInsert);
}
break;
case ax::mojom::Command::kMarker:
return AXTextStateChangeIntent(); // Not currently implemented on Mac.
case ax::mojom::Command::kMoveSelection:
// Mac calls a "kSelectionBoundary" an operation that moves the caret
// within an editable element by a given text unit, e.g. by word.
return AXTextStateChangeIntent(
AXTextStateChangeType::kSelectionBoundary,
AXTextSelection::FromDirectionAndGranularity(
event_intent.text_boundary, event_intent.move_direction));
case ax::mojom::Command::kSetSelection:
// Mac calls a "kSelectionMove" an operation that sets the selection to a
// completely new location, such as tabbing to an edit field.
if (event_intent.text_boundary == ax::mojom::TextBoundary::kNone) {
// No granularity information is available. This means that focus must
// have moved to another control, or a new selection has been made using
// the mouse or touch in the same control. In the latter case, we'll
// also pretend that focus has changed.
return AXTextStateChangeIntent::DefaultFocusTextStateChangeIntent();
}
return AXTextStateChangeIntent(
AXTextStateChangeType::kSelectionMove,
AXTextSelection::FromDirectionAndGranularity(
event_intent.text_boundary, event_intent.move_direction));
}
}
} // namespace ui