semantics.dart 30.5 KB
Newer Older
M
Michael Goderbauer 已提交
1
// Copyright 2013 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

A
Adam Barth 已提交
5
part of dart.ui;
6 7 8 9

/// The possible actions that can be conveyed from the operating system
/// accessibility APIs to a semantics node.
class SemanticsAction {
I
Ian Hickson 已提交
10 11
  const SemanticsAction._(this.index);

12 13 14 15 16 17 18 19
  static const int _kTapIndex = 1 << 0;
  static const int _kLongPressIndex = 1 << 1;
  static const int _kScrollLeftIndex = 1 << 2;
  static const int _kScrollRightIndex = 1 << 3;
  static const int _kScrollUpIndex = 1 << 4;
  static const int _kScrollDownIndex = 1 << 5;
  static const int _kIncreaseIndex = 1 << 6;
  static const int _kDecreaseIndex = 1 << 7;
20 21 22 23
  static const int _kShowOnScreenIndex = 1 << 8;
  static const int _kMoveCursorForwardByCharacterIndex = 1 << 9;
  static const int _kMoveCursorBackwardByCharacterIndex = 1 << 10;
  static const int _kSetSelectionIndex = 1 << 11;
24 25 26
  static const int _kCopyIndex = 1 << 12;
  static const int _kCutIndex = 1 << 13;
  static const int _kPasteIndex = 1 << 14;
27 28
  static const int _kDidGainAccessibilityFocusIndex = 1 << 15;
  static const int _kDidLoseAccessibilityFocusIndex = 1 << 16;
29
  static const int _kCustomAction = 1 << 17;
30
  static const int _kDismissIndex = 1 << 18;
31 32
  static const int _kMoveCursorForwardByWordIndex = 1 << 19;
  static const int _kMoveCursorBackwardByWordIndex = 1 << 20;
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

  /// The numerical value for this action.
  ///
  /// Each action has one bit set in this bit field.
  final int index;

  /// The equivalent of a user briefly tapping the screen with the finger
  /// without moving it.
  static const SemanticsAction tap = const SemanticsAction._(_kTapIndex);

  /// The equivalent of a user pressing and holding the screen with the finger
  /// for a few seconds without moving it.
  static const SemanticsAction longPress = const SemanticsAction._(_kLongPressIndex);

  /// The equivalent of a user moving their finger across the screen from right
  /// to left.
  ///
  /// This action should be recognized by controls that are horizontally
  /// scrollable.
  static const SemanticsAction scrollLeft = const SemanticsAction._(_kScrollLeftIndex);

  /// The equivalent of a user moving their finger across the screen from left
  /// to right.
  ///
  /// This action should be recognized by controls that are horizontally
  /// scrollable.
  static const SemanticsAction scrollRight = const SemanticsAction._(_kScrollRightIndex);

  /// The equivalent of a user moving their finger across the screen from
  /// bottom to top.
  ///
  /// This action should be recognized by controls that are vertically
  /// scrollable.
  static const SemanticsAction scrollUp = const SemanticsAction._(_kScrollUpIndex);

  /// The equivalent of a user moving their finger across the screen from top
  /// to bottom.
  ///
  /// This action should be recognized by controls that are vertically
  /// scrollable.
  static const SemanticsAction scrollDown = const SemanticsAction._(_kScrollDownIndex);

  /// A request to increase the value represented by the semantics node.
  ///
  /// For example, this action might be recognized by a slider control.
  static const SemanticsAction increase = const SemanticsAction._(_kIncreaseIndex);

  /// A request to decrease the value represented by the semantics node.
  ///
  /// For example, this action might be recognized by a slider control.
  static const SemanticsAction decrease = const SemanticsAction._(_kDecreaseIndex);

85 86 87 88
  /// A request to fully show the semantics node on screen.
  ///
  /// For example, this action might be send to a node in a scrollable list that
  /// is partially off screen to bring it on screen.
89
  static const SemanticsAction showOnScreen = const SemanticsAction._(_kShowOnScreenIndex);
90

91 92 93
  /// Move the cursor forward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
94 95 96 97
  ///
  /// The action includes a boolean argument, which indicates whether the cursor
  /// movement should extend (or start) a selection.
  static const SemanticsAction moveCursorForwardByCharacter = const SemanticsAction._(_kMoveCursorForwardByCharacterIndex);
98 99 100 101

  /// Move the cursor backward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  ///
  /// The action includes a boolean argument, which indicates whether the cursor
  /// movement should extend (or start) a selection.
  static const SemanticsAction moveCursorBackwardByCharacter = const SemanticsAction._(_kMoveCursorBackwardByCharacterIndex);

  /// Set the text selection to the given range.
  ///
  /// The provided argument is a Map<String, int> which includes the keys `base`
  /// and `extent` indicating where the selection within the `value` of the
  /// semantics node should start and where it should end. Values for both
  /// keys can range from 0 to length of `value` (inclusive).
  ///
  /// Setting `base` and `extent` to the same value will move the cursor to
  /// that position (without selecting anything).
  static const SemanticsAction setSelection = const SemanticsAction._(_kSetSelectionIndex);
117

118 119 120 121 122 123 124 125 126
  /// Copy the current selection to the clipboard.
  static const SemanticsAction copy = const SemanticsAction._(_kCopyIndex);

  /// Cut the current selection and place it in the clipboard.
  static const SemanticsAction cut = const SemanticsAction._(_kCutIndex);

  /// Paste the current content of the clipboard.
  static const SemanticsAction paste = const SemanticsAction._(_kPasteIndex);

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
  /// Indicates that the nodes has gained accessibility focus.
  ///
  /// This handler is invoked when the node annotated with this handler gains
  /// the accessibility focus. The accessibility focus is the
  /// green (on Android with TalkBack) or black (on iOS with VoiceOver)
  /// rectangle shown on screen to indicate what element an accessibility
  /// user is currently interacting with.
  ///
  /// The accessibility focus is different from the input focus. The input focus
  /// is usually held by the element that currently responds to keyboard inputs.
  /// Accessibility focus and input focus can be held by two different nodes!
  static const SemanticsAction didGainAccessibilityFocus = const SemanticsAction._(_kDidGainAccessibilityFocusIndex);

  /// Indicates that the nodes has lost accessibility focus.
  ///
  /// This handler is invoked when the node annotated with this handler
  /// loses the accessibility focus. The accessibility focus is
  /// the green (on Android with TalkBack) or black (on iOS with VoiceOver)
  /// rectangle shown on screen to indicate what element an accessibility
  /// user is currently interacting with.
  ///
  /// The accessibility focus is different from the input focus. The input focus
  /// is usually held by the element that currently responds to keyboard inputs.
  /// Accessibility focus and input focus can be held by two different nodes!
  static const SemanticsAction didLoseAccessibilityFocus = const SemanticsAction._(_kDidLoseAccessibilityFocusIndex);

153
  /// Indicates that the user has invoked a custom accessibility action.
154
  ///
155 156 157 158
  /// This handler is added automatically whenever a custom accessibility
  /// action is added to a semantics node.
  static const SemanticsAction customAction = const SemanticsAction._(_kCustomAction);

159 160 161 162 163 164 165 166 167
  /// A request that the node should be dismissed.
  ///
  /// A [Snackbar], for example, may have a dismiss action to indicate to the
  /// user that it can be removed after it is no longer relevant. On Android,
  /// (with TalkBack) special hint text is spoken when focusing the node and
  /// a custom action is availible in the local context menu. On iOS,
  /// (with VoiceOver) users can perform a standard gesture to dismiss it.
  static const SemanticsAction dismiss = const SemanticsAction._(_kDismissIndex);

168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  /// Move the cursor forward by one word.
  ///
  /// This is for example used by the cursor control in text fields.
  ///
  /// The action includes a boolean argument, which indicates whether the cursor
  /// movement should extend (or start) a selection.
  static const SemanticsAction moveCursorForwardByWord = const SemanticsAction._(_kMoveCursorForwardByWordIndex);

  /// Move the cursor backward by one word.
  ///
  /// This is for example used by the cursor control in text fields.
  ///
  /// The action includes a boolean argument, which indicates whether the cursor
  /// movement should extend (or start) a selection.
  static const SemanticsAction moveCursorBackwardByWord = const SemanticsAction._(_kMoveCursorBackwardByWordIndex);

184 185 186 187
  /// The possible semantics actions.
  ///
  /// The map's key is the [index] of the action and the value is the action
  /// itself.
188
  static const Map<int, SemanticsAction> values = const <int, SemanticsAction>{
189 190 191 192 193 194 195 196
    _kTapIndex: tap,
    _kLongPressIndex: longPress,
    _kScrollLeftIndex: scrollLeft,
    _kScrollRightIndex: scrollRight,
    _kScrollUpIndex: scrollUp,
    _kScrollDownIndex: scrollDown,
    _kIncreaseIndex: increase,
    _kDecreaseIndex: decrease,
197 198 199 200
    _kShowOnScreenIndex: showOnScreen,
    _kMoveCursorForwardByCharacterIndex: moveCursorForwardByCharacter,
    _kMoveCursorBackwardByCharacterIndex: moveCursorBackwardByCharacter,
    _kSetSelectionIndex: setSelection,
201 202 203
    _kCopyIndex: copy,
    _kCutIndex: cut,
    _kPasteIndex: paste,
204 205
    _kDidGainAccessibilityFocusIndex: didGainAccessibilityFocus,
    _kDidLoseAccessibilityFocusIndex: didLoseAccessibilityFocus,
206
    _kCustomAction: customAction,
207
    _kDismissIndex: dismiss,
208 209
    _kMoveCursorForwardByWordIndex: moveCursorForwardByWord,
    _kMoveCursorBackwardByWordIndex: moveCursorBackwardByWord,
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  };

  @override
  String toString() {
    switch (index) {
      case _kTapIndex:
        return 'SemanticsAction.tap';
      case _kLongPressIndex:
        return 'SemanticsAction.longPress';
      case _kScrollLeftIndex:
        return 'SemanticsAction.scrollLeft';
      case _kScrollRightIndex:
        return 'SemanticsAction.scrollRight';
      case _kScrollUpIndex:
        return 'SemanticsAction.scrollUp';
      case _kScrollDownIndex:
        return 'SemanticsAction.scrollDown';
      case _kIncreaseIndex:
        return 'SemanticsAction.increase';
      case _kDecreaseIndex:
        return 'SemanticsAction.decrease';
231
      case _kShowOnScreenIndex:
232
        return 'SemanticsAction.showOnScreen';
233
      case _kMoveCursorForwardByCharacterIndex:
234
        return 'SemanticsAction.moveCursorForwardByCharacter';
235
      case _kMoveCursorBackwardByCharacterIndex:
M
Michael Goderbauer 已提交
236
        return 'SemanticsAction.moveCursorBackwardByCharacter';
237 238
      case _kSetSelectionIndex:
        return 'SemanticsAction.setSelection';
239 240 241 242 243 244
      case _kCopyIndex:
        return 'SemanticsAction.copy';
      case _kCutIndex:
        return 'SemanticsAction.cut';
      case _kPasteIndex:
        return 'SemanticsAction.paste';
245 246 247 248
      case _kDidGainAccessibilityFocusIndex:
        return 'SemanticsAction.didGainAccessibilityFocus';
      case _kDidLoseAccessibilityFocusIndex:
        return 'SemanticsAction.didLoseAccessibilityFocus';
249 250
      case _kCustomAction:
        return 'SemanticsAction.customAction';
251 252
      case _kDismissIndex:
        return 'SemanticsAction.dismiss';
253 254 255 256
      case _kMoveCursorForwardByWordIndex:
        return 'SemanticsAction.moveCursorForwardByWord';
      case _kMoveCursorBackwardByWordIndex:
        return 'SemanticsAction.moveCursorBackwardByWord';
257 258 259 260 261 262
    }
    return null;
  }
}

/// A Boolean value that can be associated with a semantics node.
263
class SemanticsFlag {
264 265
  static const int _kHasCheckedStateIndex = 1 << 0;
  static const int _kIsCheckedIndex = 1 << 1;
266
  static const int _kIsSelectedIndex = 1 << 2;
267
  static const int _kIsButtonIndex = 1 << 3;
268 269
  static const int _kIsTextFieldIndex = 1 << 4;
  static const int _kIsFocusedIndex = 1 << 5;
270 271
  static const int _kHasEnabledStateIndex = 1 << 6;
  static const int _kIsEnabledIndex = 1 << 7;
272
  static const int _kIsInMutuallyExclusiveGroupIndex = 1 << 8;
273
  static const int _kIsHeaderIndex = 1 << 9;
274
  static const int _kIsObscuredIndex = 1 << 10;
275 276
  static const int _kScopesRouteIndex= 1 << 11;
  static const int _kNamesRouteIndex = 1 << 12;
277
  static const int _kIsHiddenIndex = 1 << 13;
278 279 280 281
  static const int _kIsImageIndex = 1 << 14;
  static const int _kIsLiveRegionIndex = 1 << 15;
  static const int _kHasToggledStateIndex = 1 << 16;
  static const int _kIsToggledIndex = 1 << 17;
282
  static const int _kHasImplicitScrollingIndex = 1 << 18;
283

284
  const SemanticsFlag._(this.index);
285 286 287 288 289 290 291 292

  /// The numerical value for this flag.
  ///
  /// Each flag has one bit set in this bit field.
  final int index;

  /// The semantics node has the quality of either being "checked" or "unchecked".
  ///
293 294
  /// This flag is mutually exclusive with [hasToggledState].
  ///
295
  /// For example, a checkbox or a radio button widget has checked state.
296 297 298 299
  ///
  /// See also:
  ///
  ///   * [SemanticsFlag.isChecked], which controls whether the node is "checked" or "unchecked".
300
  static const SemanticsFlag hasCheckedState = const SemanticsFlag._(_kHasCheckedStateIndex);
301 302 303 304 305 306 307

  /// Whether a semantics node that [hasCheckedState] is checked.
  ///
  /// If true, the semantics node is "checked". If false, the semantics node is
  /// "unchecked".
  ///
  /// For example, if a checkbox has a visible checkmark, [isChecked] is true.
308 309 310 311
  ///
  /// See also:
  ///
  ///   * [SemanticsFlag.hasCheckedState], which enables a checked state.
312
  static const SemanticsFlag isChecked = const SemanticsFlag._(_kIsCheckedIndex);
313

314 315 316 317 318 319 320

  /// Whether a semantics node is selected.
  ///
  /// If true, the semantics node is "selected". If false, the semantics node is
  /// "unselected".
  ///
  /// For example, the active tab in a tab bar has [isSelected] set to true.
321
  static const SemanticsFlag isSelected = const SemanticsFlag._(_kIsSelectedIndex);
322

A
amirh 已提交
323 324 325 326 327
  /// Whether the semantic node represents a button.
  ///
  /// Platforms has special handling for buttons, for example Android's TalkBack
  /// and iOS's VoiceOver provides an additional hint when the focused object is
  /// a button.
328
  static const SemanticsFlag isButton = const SemanticsFlag._(_kIsButtonIndex);
A
amirh 已提交
329

330 331 332 333
  /// Whether the semantic node represents a text field.
  ///
  /// Text fields are announced as such and allow text input via accessibility
  /// affordances.
334
  static const SemanticsFlag isTextField = const SemanticsFlag._(_kIsTextFieldIndex);
335 336 337 338

  /// Whether the semantic node currently holds the user's focus.
  ///
  /// The focused element is usually the current receiver of keyboard inputs.
339
  static const SemanticsFlag isFocused = const SemanticsFlag._(_kIsFocusedIndex);
340

341 342 343 344 345 346
  /// The semantics node has the quality of either being "enabled" or
  /// "disabled".
  ///
  /// For example, a button can be enabled or disabled and therefore has an
  /// "enabled" state. Static text is usually neither enabled nor disabled and
  /// therefore does not have an "enabled" state.
347
  static const SemanticsFlag hasEnabledState = const SemanticsFlag._(_kHasEnabledStateIndex);
348 349

  /// Whether a semantic node that [hasEnabledState] is currently enabled.
350 351 352 353
  ///
  /// A disabled element does not respond to user interaction. For example, a
  /// button that currently does not respond to user interaction should be
  /// marked as disabled.
354
  static const SemanticsFlag isEnabled = const SemanticsFlag._(_kIsEnabledIndex);
355

356 357 358 359 360 361
  /// Whether a semantic node is in a mutually exclusive group.
  ///
  /// For example, a radio button is in a mutually exclusive group because
  /// only one radio button in that group can be marked as [isChecked].
  static const SemanticsFlag isInMutuallyExclusiveGroup = const SemanticsFlag._(_kIsInMutuallyExclusiveGroupIndex);

362 363 364 365 366 367 368
  /// Whether a semantic node is a header that divides content into sections.
  ///
  /// For example, headers can be used to divide a list of alphabetically
  /// sorted words into the sections A, B, C, etc. as can be found in many
  /// address book applications.
  static const SemanticsFlag isHeader = const SemanticsFlag._(_kIsHeaderIndex);

369
  /// Whether the value of the semantics node is obscured.
370
  ///
371 372 373
  /// This is usually used for text fields to indicate that its content
  /// is a password or contains other sensitive information.
  static const SemanticsFlag isObscured = const SemanticsFlag._(_kIsObscuredIndex);
374

375 376
  /// Whether the semantics node is the root of a subtree for which a route name
  /// should be announced.
377 378
  ///
  /// When a node with this flag is removed from the semantics tree, the
379 380 381 382 383
  /// framework will select the last in depth-first, paint order node with this
  /// flag.  When a node with this flag is added to the semantics tree, it is
  /// selected automatically, unless there were multiple nodes with this flag
  /// added.  In this case, the last added node in depth-first, paint order
  /// will be selected.
384
  ///
385
  /// From this selected node, the framework will search in depth-first, paint
386 387 388
  /// order for the first node with a [namesRoute] flag and a non-null,
  /// non-empty label. The [namesRoute] and [scopesRoute] flags may be on the
  /// same node. The label of the found node will be announced as an edge
389
  /// transition. If no non-empty, non-null label is found then:
390
  ///
391 392 393 394
  ///   * VoiceOver will make a chime announcement.
  ///   * TalkBack will make no announcement
  ///
  /// Semantic nodes annotated with this flag are generally not a11y focusable.
395 396
  ///
  /// This is used in widgets such as Routes, Drawers, and Dialogs to
397 398 399 400 401
  /// communicate significant changes in the visible screen.
  static const SemanticsFlag scopesRoute = const SemanticsFlag._(_kScopesRouteIndex);

  /// Whether the semantics node label is the name of a visually distinct
  /// route.
402
  ///
403 404 405 406 407
  /// This is used by certain widgets like Drawers and Dialogs, to indicate
  /// that the node's semantic label can be used to announce an edge triggered
  /// semantics update.
  ///
  /// Semantic nodes annotated with this flag will still recieve a11y focus.
408 409
  ///
  /// Updating this label within the same active route subtree will not cause
410 411 412
  /// additional announcements.
  static const SemanticsFlag namesRoute = const SemanticsFlag._(_kNamesRouteIndex);

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
  /// Whether the semantics node is considered hidden.
  ///
  /// Hidden elements are currently not visible on screen. They may be covered
  /// by other elements or positioned outside of the visible area of a viewport.
  ///
  /// Hidden elements cannot gain accessibility focus though regular touch. The
  /// only way they can be focused is by moving the focus to them via linear
  /// navigation.
  ///
  /// Platforms are free to completely ignore hidden elements and new platforms
  /// are encouraged to do so.
  ///
  /// Instead of marking an element as hidden it should usually be excluded from
  /// the semantics tree altogether. Hidden elements are only included in the
  /// semantics tree to work around platform limitations and they are mainly
  /// used to implement accessibility scrolling on iOS.
  static const SemanticsFlag isHidden = const SemanticsFlag._(_kIsHiddenIndex);

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  /// Whether the semantics node represents an image.
  ///
  /// Both TalkBack and VoiceOver will inform the user the the semantics node
  /// represents an image.
  static const SemanticsFlag isImage = const SemanticsFlag._(_kIsImageIndex);

  /// Whether the semantics node is a live region.
  ///
  /// A live region indicates that updates to semantics node are important.
  /// Platforms may use this information to make polite announcements to the
  /// user to inform them of updates to this node.
  ///
  /// An example of a live region is a [SnackBar] widget. On Android, A live
  /// region causes a polite announcement to be generated automatically, even
  /// if the user does not have focus of the widget.
  static const SemanticsFlag isLiveRegion = const SemanticsFlag._(_kIsLiveRegionIndex);

  /// The semantics node has the quality of either being "on" or "off".
  ///
  /// This flag is mutually exclusive with [hasCheckedState].
  ///
  /// For example, a switch has toggled state.
  ///
  /// See also:
  ///
  ///    * [SemanticsFlag.isToggled], which controls whether the node is "on" or "off".
  static const SemanticsFlag hasToggledState = const SemanticsFlag._(_kHasToggledStateIndex);

  /// If true, the semantics node is "on". If false, the semantics node is
  /// "off".
  ///
  /// For example, if a switch is in the on position, [isToggled] is true.
  ///
  /// See also:
  ///
  ///   * [SemanticsFlag.hasToggledState], which enables a toggled state.
  static const SemanticsFlag isToggled = const SemanticsFlag._(_kIsToggledIndex);

469 470 471 472 473 474 475 476 477
  /// Whether the platform can scroll the semantics node when the user attempts
  /// to move focus to an offscreen child.
  ///
  /// For example, a [ListView] widget has implicit scrolling so that users can
  /// easily move to the next visible set of children. A [TabBar] widget does
  /// not have implicit scrolling, so that users can navigate into the tab
  /// body when reaching the end of the tab bar.
  static const SemanticsFlag hasImplicitScrolling = const SemanticsFlag._(_kHasImplicitScrollingIndex);

478 479 480
  /// The possible semantics flags.
  ///
  /// The map's key is the [index] of the flag and the value is the flag itself.
481
  static const Map<int, SemanticsFlag> values = const <int, SemanticsFlag>{
482 483
    _kHasCheckedStateIndex: hasCheckedState,
    _kIsCheckedIndex: isChecked,
484
    _kIsSelectedIndex: isSelected,
485 486 487
    _kIsButtonIndex: isButton,
    _kIsTextFieldIndex: isTextField,
    _kIsFocusedIndex: isFocused,
488 489
    _kHasEnabledStateIndex: hasEnabledState,
    _kIsEnabledIndex: isEnabled,
490
    _kIsInMutuallyExclusiveGroupIndex: isInMutuallyExclusiveGroup,
491
    _kIsHeaderIndex: isHeader,
492
    _kIsObscuredIndex: isObscured,
493 494
    _kScopesRouteIndex: scopesRoute,
    _kNamesRouteIndex: namesRoute,
495
    _kIsHiddenIndex: isHidden,
496 497 498 499
    _kIsImageIndex: isImage,
    _kIsLiveRegionIndex: isLiveRegion,
    _kHasToggledStateIndex: hasToggledState,
    _kIsToggledIndex: isToggled,
500
    _kHasImplicitScrollingIndex: hasImplicitScrolling,
501 502 503 504 505 506
  };

  @override
  String toString() {
    switch (index) {
      case _kHasCheckedStateIndex:
507
        return 'SemanticsFlag.hasCheckedState';
508
      case _kIsCheckedIndex:
509
        return 'SemanticsFlag.isChecked';
510
      case _kIsSelectedIndex:
511
        return 'SemanticsFlag.isSelected';
512
      case _kIsButtonIndex:
513
        return 'SemanticsFlag.isButton';
514
      case _kIsTextFieldIndex:
515
        return 'SemanticsFlag.isTextField';
516
      case _kIsFocusedIndex:
517
        return 'SemanticsFlag.isFocused';
518
      case _kHasEnabledStateIndex:
519
        return 'SemanticsFlag.hasEnabledState';
520
      case _kIsEnabledIndex:
521
        return 'SemanticsFlag.isEnabled';
522 523
      case _kIsInMutuallyExclusiveGroupIndex:
        return 'SemanticsFlag.isInMutuallyExclusiveGroup';
524 525
      case _kIsHeaderIndex:
        return 'SemanticsFlag.isHeader';
526 527
      case _kIsObscuredIndex:
        return 'SemanticsFlag.isObscured';
528 529 530 531
      case _kScopesRouteIndex:
        return 'SemanticsFlag.scopesRoute';
      case _kNamesRouteIndex:
        return 'SemanticsFlag.namesRoute';
532 533
      case _kIsHiddenIndex:
        return 'SemanticsFlag.isHidden';
534 535 536 537 538 539 540 541
      case _kIsImageIndex:
        return 'SemanticsFlag.isImage';
      case _kIsLiveRegionIndex:
        return 'SemanticsFlag.isLiveRegion';
      case _kHasToggledStateIndex:
        return 'SemanticsFlag.hasToggledState';
      case _kIsToggledIndex:
        return 'SemanticsFlag.isToggled';
542 543
      case _kHasImplicitScrollingIndex:
        return 'SemanticsFlag.hasImplicitScrolling';
544 545 546 547 548 549 550 551 552
    }
    return null;
  }
}

/// An object that creates [SemanticsUpdate] objects.
///
/// Once created, the [SemanticsUpdate] objects can be passed to
/// [Window.updateSemantics] to update the semantics conveyed to the user.
553
@pragma('vm:entry-point')
554 555
class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
  /// Creates an empty [SemanticsUpdateBuilder] object.
556
  @pragma('vm:entry-point')
557
  SemanticsUpdateBuilder() { _constructor(); }
558
  void _constructor() native 'SemanticsUpdateBuilder_constructor';
559 560 561 562

  /// Update the information associated with the node with the given `id`.
  ///
  /// The semantics nodes form a tree, with the root of the tree always having
563 564 565 566 567 568 569
  /// an id of zero. The `childrenInTraversalOrder` and `childrenInHitTestOrder`
  /// are the ids of the nodes that are immediate children of this node. The
  /// former enumerates children in traversal order, and the latter enumerates
  /// the same children in the hit test order. The two lists must have the same
  /// length and contain the same ids. They may only differ in the order the
  /// ids are listed in. For more information about different child orders, see
  /// [DebugSemanticsDumpOrder].
L
liyuqian 已提交
570
  ///
571 572 573 574
  /// The system retains the nodes that are currently reachable from the root.
  /// A given update need not contain information for nodes that do not change
  /// in the update. If a node is not reachable from the root after an update,
  /// the node will be discarded from the tree.
575
  ///
576
  /// The `flags` are a bit field of [SemanticsFlag]s that apply to this node.
577
  ///
I
Ian Hickson 已提交
578
  /// The `actions` are a bit field of [SemanticsAction]s that can be undertaken
579 580
  /// by this node. If the user wishes to undertake one of these actions on this
  /// node, the [Window.onSemanticsAction] will be called with `id` and one of
I
Ian Hickson 已提交
581
  /// the possible [SemanticsAction]s. Because the semantics tree is maintained
582 583 584
  /// asynchronously, the [Window.onSemanticsAction] callback might be called
  /// with an action that is no longer possible.
  ///
585
  /// The `label` is a string that describes this node. The `value` property
586 587 588 589 590 591
  /// describes the current value of the node as a string. The `increasedValue`
  /// string will become the `value` string after a [SemanticsAction.increase]
  /// action is performed. The `decreasedValue` string will become the `value`
  /// string after a [SemanticsAction.decrease] action is performed. The `hint`
  /// string describes what result an action performed on this node has. The
  /// reading direction of all these strings is given by `textDirection`.
592
  ///
593
  /// The fields 'textSelectionBase' and 'textSelectionExtent' describe the
594 595
  /// currently selected text within `value`.
  ///
596 597 598 599 600
  /// For scrollable nodes `scrollPosition` describes the current scroll
  /// position in logical pixel. `scrollExtentMax` and `scrollExtentMin`
  /// describe the maximum and minimum in-rage values that `scrollPosition` can
  /// be. Both or either may be infinity to indicate unbound scrolling. The
  /// value for `scrollPosition` can (temporarily) be outside this range, for
601 602 603
  /// example during an overscroll. `scrollChildren` is the count of the
  /// total number of child nodes that contribute semantics and `scrollIndex`
  /// is the index of the first visible child node that contributes semantics.
604
  ///
605 606 607
  /// The `rect` is the region occupied by this node in its own coordinate
  /// system.
  ///
608
  /// The `transform` is a matrix that maps this node's coordinate system into
609
  /// its parent's coordinate system.
610 611 612 613
  void updateNode({
    int id,
    int flags,
    int actions,
614 615
    int textSelectionBase,
    int textSelectionExtent,
616 617
    int scrollChildren,
    int scrollIndex,
618 619 620
    double scrollPosition,
    double scrollExtentMax,
    double scrollExtentMin,
621 622
    Rect rect,
    String label,
623 624
    String hint,
    String value,
625 626
    String increasedValue,
    String decreasedValue,
627
    TextDirection textDirection,
628
    Float64List transform,
629 630
    Int32List childrenInTraversalOrder,
    Int32List childrenInHitTestOrder,
631
    @Deprecated('use additionalActions instead')
632
    Int32List customAcccessibilityActions,
633
    Int32List additionalActions,
634 635
  }) {
    if (transform.length != 16)
636
      throw new ArgumentError('transform argument must have 16 entries.');
637 638 639 640 641 642
    _updateNode(
      id,
      flags,
      actions,
      textSelectionBase,
      textSelectionExtent,
643 644
      scrollChildren,
      scrollIndex,
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
      scrollPosition,
      scrollExtentMax,
      scrollExtentMin,
      rect.left,
      rect.top,
      rect.right,
      rect.bottom,
      label,
      hint,
      value,
      increasedValue,
      decreasedValue,
      textDirection != null ? textDirection.index + 1 : 0,
      transform,
      childrenInTraversalOrder,
      childrenInHitTestOrder,
661
      additionalActions ?? customAcccessibilityActions,
662
    );
663 664 665 666 667
  }
  void _updateNode(
    int id,
    int flags,
    int actions,
668 669
    int textSelectionBase,
    int textSelectionExtent,
670 671
    int scrollChildren,
    int scrollIndex,
672 673 674
    double scrollPosition,
    double scrollExtentMax,
    double scrollExtentMin,
675 676 677 678 679
    double left,
    double top,
    double right,
    double bottom,
    String label,
680 681
    String hint,
    String value,
682 683
    String increasedValue,
    String decreasedValue,
684
    int textDirection,
685
    Float64List transform,
686 687
    Int32List childrenInTraversalOrder,
    Int32List childrenInHitTestOrder,
688
    Int32List additionalActions,
689
  ) native 'SemanticsUpdateBuilder_updateNode';
690

691 692 693 694 695 696 697 698 699 700 701
  /// Update the custom semantics action associated with the given `id`.
  ///
  /// The name of the action exposed to the user is the `label`. For overriden
  /// standard actions this value is ignored.
  ///
  /// The `hint` should describe what happens when an action occurs, not the
  /// manner in which a tap is accomplished. For example, use "delete" instead
  /// of "double tap to delete".
  ///
  /// The text direction of the `hint` and `label` is the same as the global
  /// window.
702
  ///
703 704 705 706
  /// For overriden standard actions, `overrideId` corresponds with a
  /// [SemanticsAction.index] value. For custom actions this argument should not be
  /// provided.
  void updateCustomAction({int id, String label, String hint, int overrideId = -1}) {
707
    assert(id != null);
708 709
    assert(overrideId != null);
    _updateCustomAction(id, label, hint, overrideId);
710
  }
711
  void _updateCustomAction(int id, String label, String hint, int overrideId) native 'SemanticsUpdateBuilder_updateCustomAction';
712

713 714 715 716 717
  /// Creates a [SemanticsUpdate] object that encapsulates the updates recorded
  /// by this object.
  ///
  /// The returned object can be passed to [Window.updateSemantics] to actually
  /// update the semantics retained by the system.
718
  SemanticsUpdate build() native 'SemanticsUpdateBuilder_build';
719 720 721 722 723 724 725 726
}

/// An opaque object representing a batch of semantics updates.
///
/// To create a SemanticsUpdate object, use a [SemanticsUpdateBuilder].
///
/// Semantics updates can be applied to the system's retained semantics tree
/// using the [Window.updateSemantics] method.
727
@pragma('vm:entry-point')
728
class SemanticsUpdate extends NativeFieldWrapperClass2 {
729 730
  /// This class is created by the engine, and should not be instantiated
  /// or extended directly.
731
  ///
732
  /// To create a SemanticsUpdate object, use a [SemanticsUpdateBuilder].
733
  @pragma('vm:entry-point')
734
  SemanticsUpdate._();
735 736 737 738 739

  /// Releases the resources used by this semantics update.
  ///
  /// After calling this function, the semantics update is cannot be used
  /// further.
740
  void dispose() native 'SemanticsUpdate_dispose';
741
}