semantics.dart 20.2 KB
Newer Older
1 2 3 4
// 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.

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 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

  /// 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);

81 82 83 84
  /// 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.
85
  static const SemanticsAction showOnScreen = const SemanticsAction._(_kShowOnScreenIndex);
86

87 88 89
  /// Move the cursor forward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
90 91 92 93
  ///
  /// 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);
94 95 96 97

  /// Move the cursor backward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
  ///
  /// 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);
113

114 115 116 117 118 119 120 121 122
  /// 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);

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
  /// 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);

149 150 151 152 153 154 155 156 157 158 159 160 161
  /// The possible semantics actions.
  ///
  /// The map's key is the [index] of the action and the value is the action
  /// itself.
  static final Map<int, SemanticsAction> values = const <int, SemanticsAction>{
    _kTapIndex: tap,
    _kLongPressIndex: longPress,
    _kScrollLeftIndex: scrollLeft,
    _kScrollRightIndex: scrollRight,
    _kScrollUpIndex: scrollUp,
    _kScrollDownIndex: scrollDown,
    _kIncreaseIndex: increase,
    _kDecreaseIndex: decrease,
162 163 164 165
    _kShowOnScreenIndex: showOnScreen,
    _kMoveCursorForwardByCharacterIndex: moveCursorForwardByCharacter,
    _kMoveCursorBackwardByCharacterIndex: moveCursorBackwardByCharacter,
    _kSetSelectionIndex: setSelection,
166 167 168
    _kCopyIndex: copy,
    _kCutIndex: cut,
    _kPasteIndex: paste,
169 170
    _kDidGainAccessibilityFocusIndex: didGainAccessibilityFocus,
    _kDidLoseAccessibilityFocusIndex: didLoseAccessibilityFocus,
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  };

  @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';
192
      case _kShowOnScreenIndex:
193
        return 'SemanticsAction.showOnScreen';
194
      case _kMoveCursorForwardByCharacterIndex:
195
        return 'SemanticsAction.moveCursorForwardByCharacter';
196
      case _kMoveCursorBackwardByCharacterIndex:
M
Michael Goderbauer 已提交
197
        return 'SemanticsAction.moveCursorBackwardByCharacter';
198 199
      case _kSetSelectionIndex:
        return 'SemanticsAction.setSelection';
200 201 202 203 204 205
      case _kCopyIndex:
        return 'SemanticsAction.copy';
      case _kCutIndex:
        return 'SemanticsAction.cut';
      case _kPasteIndex:
        return 'SemanticsAction.paste';
206 207 208 209
      case _kDidGainAccessibilityFocusIndex:
        return 'SemanticsAction.didGainAccessibilityFocus';
      case _kDidLoseAccessibilityFocusIndex:
        return 'SemanticsAction.didLoseAccessibilityFocus';
210 211 212 213 214 215
    }
    return null;
  }
}

/// A Boolean value that can be associated with a semantics node.
216
class SemanticsFlag {
217 218
  static const int _kHasCheckedStateIndex = 1 << 0;
  static const int _kIsCheckedIndex = 1 << 1;
219
  static const int _kIsSelectedIndex = 1 << 2;
220
  static const int _kIsButtonIndex = 1 << 3;
221 222
  static const int _kIsTextFieldIndex = 1 << 4;
  static const int _kIsFocusedIndex = 1 << 5;
223 224
  static const int _kHasEnabledStateIndex = 1 << 6;
  static const int _kIsEnabledIndex = 1 << 7;
225
  static const int _kIsInMutuallyExclusiveGroupIndex = 1 << 8;
226
  static const int _kIsHeaderIndex = 1 << 9;
227
  static const int _kIsObscuredIndex = 1 << 10;
228

229
  const SemanticsFlag._(this.index);
230 231 232 233 234 235 236 237 238

  /// 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".
  ///
  /// For example, a checkbox or a radio button widget has checked state.
239
  static const SemanticsFlag hasCheckedState = const SemanticsFlag._(_kHasCheckedStateIndex);
240 241 242 243 244 245 246

  /// 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.
247
  static const SemanticsFlag isChecked = const SemanticsFlag._(_kIsCheckedIndex);
248

249 250 251 252 253 254 255

  /// 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.
256
  static const SemanticsFlag isSelected = const SemanticsFlag._(_kIsSelectedIndex);
257

A
amirh 已提交
258 259 260 261 262
  /// 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.
263
  static const SemanticsFlag isButton = const SemanticsFlag._(_kIsButtonIndex);
A
amirh 已提交
264

265 266 267 268
  /// Whether the semantic node represents a text field.
  ///
  /// Text fields are announced as such and allow text input via accessibility
  /// affordances.
269
  static const SemanticsFlag isTextField = const SemanticsFlag._(_kIsTextFieldIndex);
270 271 272 273

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

276 277 278 279 280 281
  /// 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.
282
  static const SemanticsFlag hasEnabledState = const SemanticsFlag._(_kHasEnabledStateIndex);
283 284

  /// Whether a semantic node that [hasEnabledState] is currently enabled.
285 286 287 288
  ///
  /// 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.
289
  static const SemanticsFlag isEnabled = const SemanticsFlag._(_kIsEnabledIndex);
290

291 292 293 294 295 296
  /// 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);

297 298 299 300 301 302 303
  /// 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);

304
  /// Whether the value of the semantics node is obscured.
305
  ///
306 307 308
  /// 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);
309

310 311 312
  /// The possible semantics flags.
  ///
  /// The map's key is the [index] of the flag and the value is the flag itself.
313
  static final Map<int, SemanticsFlag> values = const <int, SemanticsFlag>{
314 315
    _kHasCheckedStateIndex: hasCheckedState,
    _kIsCheckedIndex: isChecked,
316
    _kIsSelectedIndex: isSelected,
317 318 319
    _kIsButtonIndex: isButton,
    _kIsTextFieldIndex: isTextField,
    _kIsFocusedIndex: isFocused,
320 321
    _kHasEnabledStateIndex: hasEnabledState,
    _kIsEnabledIndex: isEnabled,
322
    _kIsInMutuallyExclusiveGroupIndex: isInMutuallyExclusiveGroup,
323
    _kIsHeaderIndex: isHeader,
324
    _kIsObscuredIndex: isObscured,
325 326 327 328 329 330
  };

  @override
  String toString() {
    switch (index) {
      case _kHasCheckedStateIndex:
331
        return 'SemanticsFlag.hasCheckedState';
332
      case _kIsCheckedIndex:
333
        return 'SemanticsFlag.isChecked';
334
      case _kIsSelectedIndex:
335
        return 'SemanticsFlag.isSelected';
336
      case _kIsButtonIndex:
337
        return 'SemanticsFlag.isButton';
338
      case _kIsTextFieldIndex:
339
        return 'SemanticsFlag.isTextField';
340
      case _kIsFocusedIndex:
341
        return 'SemanticsFlag.isFocused';
342
      case _kHasEnabledStateIndex:
343
        return 'SemanticsFlag.hasEnabledState';
344
      case _kIsEnabledIndex:
345
        return 'SemanticsFlag.isEnabled';
346 347
      case _kIsInMutuallyExclusiveGroupIndex:
        return 'SemanticsFlag.isInMutuallyExclusiveGroup';
348 349
      case _kIsHeaderIndex:
        return 'SemanticsFlag.isHeader';
350 351
      case _kIsObscuredIndex:
        return 'SemanticsFlag.isObscured';
352 353 354 355 356 357 358 359 360 361 362 363
    }
    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.
class SemanticsUpdateBuilder extends NativeFieldWrapperClass2 {
  /// Creates an empty [SemanticsUpdateBuilder] object.
  SemanticsUpdateBuilder() { _constructor(); }
364
  void _constructor() native 'SemanticsUpdateBuilder_constructor';
365 366 367 368 369 370 371 372 373 374

  /// 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
  /// an id of zero. The `children` are the ids of the nodes that are immediate
  /// children of this node. 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.
  ///
375
  /// The `flags` are a bit field of [SemanticsFlag]s that apply to this node.
376
  ///
I
Ian Hickson 已提交
377
  /// The `actions` are a bit field of [SemanticsAction]s that can be undertaken
378 379
  /// 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 已提交
380
  /// the possible [SemanticsAction]s. Because the semantics tree is maintained
381 382 383
  /// asynchronously, the [Window.onSemanticsAction] callback might be called
  /// with an action that is no longer possible.
  ///
384
  /// The `label` is a string that describes this node. The `value` property
385 386 387 388 389 390
  /// 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`.
391
  ///
392
  /// The fields 'textSelectionBase' and 'textSelectionExtent' describe the
393 394
  /// currently selected text within `value`.
  ///
395 396 397 398 399 400 401
  /// 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
  /// example during an overscroll.
  ///
402 403 404
  /// The `rect` is the region occupied by this node in its own coordinate
  /// system.
  ///
405
  /// The `transform` is a matrix that maps this node's coordinate system into
406
  /// its parent's coordinate system.
407 408 409 410
  void updateNode({
    int id,
    int flags,
    int actions,
411 412
    int textSelectionBase,
    int textSelectionExtent,
413 414 415
    double scrollPosition,
    double scrollExtentMax,
    double scrollExtentMin,
416 417
    Rect rect,
    String label,
418 419
    String hint,
    String value,
420 421
    String increasedValue,
    String decreasedValue,
422
    TextDirection textDirection,
423
    int nextNodeId,
424
    int previousNodeId,
425
    Float64List transform,
426
    Int32List children,
427 428
  }) {
    if (transform.length != 16)
429
      throw new ArgumentError('transform argument must have 16 entries.');
430 431 432 433 434
    _updateNode(id,
                flags,
                actions,
                textSelectionBase,
                textSelectionExtent,
435 436 437
                scrollPosition,
                scrollExtentMax,
                scrollExtentMin,
438 439 440 441 442 443 444 445 446 447
                rect.left,
                rect.top,
                rect.right,
                rect.bottom,
                label,
                hint,
                value,
                increasedValue,
                decreasedValue,
                textDirection != null ? textDirection.index + 1 : 0,
448
                nextNodeId ?? -1,
449
                previousNodeId ?? -1,
450 451
                transform,
                children,);
452 453 454 455 456
  }
  void _updateNode(
    int id,
    int flags,
    int actions,
457 458
    int textSelectionBase,
    int textSelectionExtent,
459 460 461
    double scrollPosition,
    double scrollExtentMax,
    double scrollExtentMin,
462 463 464 465 466
    double left,
    double top,
    double right,
    double bottom,
    String label,
467 468
    String hint,
    String value,
469 470
    String increasedValue,
    String decreasedValue,
471
    int textDirection,
472
    int nextNodeId,
473
    int previousNodeId,
474
    Float64List transform,
475 476
    Int32List children,
  ) native 'SemanticsUpdateBuilder_updateNode';
477 478 479 480 481 482

  /// 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.
483
  SemanticsUpdate build() native 'SemanticsUpdateBuilder_build';
484 485 486 487 488 489 490 491 492
}

/// 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.
class SemanticsUpdate extends NativeFieldWrapperClass2 {
493 494
  /// This class is created by the engine, and should not be instantiated
  /// or extended directly.
495
  ///
496 497
  /// To create a SemanticsUpdate object, use a [SemanticsUpdateBuilder].
  SemanticsUpdate._();
498 499 500 501 502

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