semantics.dart 14.3 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
  static const int _kShowOnScreen = 1 << 8;
21 22
  static const int _kMoveCursorForwardByCharacter = 1 << 9;
  static const int _kMoveCursorBackwardByCharacter = 1 << 10;
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

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

75 76 77 78 79 80
  /// 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.
  static const SemanticsAction showOnScreen = const SemanticsAction._(_kShowOnScreen);

81 82 83 84 85 86 87 88 89

  /// Move the cursor forward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
  static const SemanticsAction moveCursorForwardByCharacter = const SemanticsAction._(_kMoveCursorForwardByCharacter);

  /// Move the cursor backward by one character.
  ///
  /// This is for example used by the cursor control in text fields.
M
Michael Goderbauer 已提交
90
  static const SemanticsAction moveCursorBackwardByCharacter = const SemanticsAction._(_kMoveCursorBackwardByCharacter);
91

92 93 94 95 96 97 98 99 100 101 102 103 104
  /// 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,
105
    _kShowOnScreen: showOnScreen,
106
    _kMoveCursorForwardByCharacter: moveCursorForwardByCharacter,
M
Michael Goderbauer 已提交
107
    _kMoveCursorBackwardByCharacter: moveCursorBackwardByCharacter,
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  };

  @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';
129 130
      case _kShowOnScreen:
        return 'SemanticsAction.showOnScreen';
131 132 133
      case _kMoveCursorForwardByCharacter:
        return 'SemanticsAction.moveCursorForwardByCharacter';
      case _kMoveCursorBackwardByCharacter:
M
Michael Goderbauer 已提交
134
        return 'SemanticsAction.moveCursorBackwardByCharacter';
135 136 137 138 139 140
    }
    return null;
  }
}

/// A Boolean value that can be associated with a semantics node.
141
class SemanticsFlag {
142 143
  static const int _kHasCheckedStateIndex = 1 << 0;
  static const int _kIsCheckedIndex = 1 << 1;
144
  static const int _kIsSelectedIndex = 1 << 2;
145
  static const int _kIsButtonIndex = 1 << 3;
146 147
  static const int _kIsTextFieldIndex = 1 << 4;
  static const int _kIsFocusedIndex = 1 << 5;
148 149
  static const int _kHasEnabledStateIndex = 1 << 6;
  static const int _kIsEnabledIndex = 1 << 7;
150
  static const int _kIsInMutuallyExclusiveGroupIndex = 1 << 8;
151

152
  const SemanticsFlag._(this.index);
153 154 155 156 157 158 159 160 161

  /// 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.
162
  static const SemanticsFlag hasCheckedState = const SemanticsFlag._(_kHasCheckedStateIndex);
163 164 165 166 167 168 169

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

172 173 174 175 176 177 178

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

A
amirh 已提交
181 182 183 184 185
  /// 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.
186
  static const SemanticsFlag isButton = const SemanticsFlag._(_kIsButtonIndex);
A
amirh 已提交
187

188 189 190 191
  /// Whether the semantic node represents a text field.
  ///
  /// Text fields are announced as such and allow text input via accessibility
  /// affordances.
192
  static const SemanticsFlag isTextField = const SemanticsFlag._(_kIsTextFieldIndex);
193 194 195 196

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

199 200 201 202 203 204
  /// 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.
205
  static const SemanticsFlag hasEnabledState = const SemanticsFlag._(_kHasEnabledStateIndex);
206 207

  /// Whether a semantic node that [hasEnabledState] is currently enabled.
208 209 210 211
  ///
  /// 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.
212
  static const SemanticsFlag isEnabled = const SemanticsFlag._(_kIsEnabledIndex);
213

214 215 216 217 218 219
  /// 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);

220 221 222
  /// The possible semantics flags.
  ///
  /// The map's key is the [index] of the flag and the value is the flag itself.
223
  static final Map<int, SemanticsFlag> values = const <int, SemanticsFlag>{
224 225
    _kHasCheckedStateIndex: hasCheckedState,
    _kIsCheckedIndex: isChecked,
226
    _kIsSelectedIndex: isSelected,
227 228 229
    _kIsButtonIndex: isButton,
    _kIsTextFieldIndex: isTextField,
    _kIsFocusedIndex: isFocused,
230 231
    _kHasEnabledStateIndex: hasEnabledState,
    _kIsEnabledIndex: isEnabled,
232
    _kIsInMutuallyExclusiveGroupIndex: isInMutuallyExclusiveGroup,
233 234 235 236 237 238
  };

  @override
  String toString() {
    switch (index) {
      case _kHasCheckedStateIndex:
239
        return 'SemanticsFlag.hasCheckedState';
240
      case _kIsCheckedIndex:
241
        return 'SemanticsFlag.isChecked';
242
      case _kIsSelectedIndex:
243
        return 'SemanticsFlag.isSelected';
244
      case _kIsButtonIndex:
245
        return 'SemanticsFlag.isButton';
246
      case _kIsTextFieldIndex:
247
        return 'SemanticsFlag.isTextField';
248
      case _kIsFocusedIndex:
249
        return 'SemanticsFlag.isFocused';
250
      case _kHasEnabledStateIndex:
251
        return 'SemanticsFlag.hasEnabledState';
252
      case _kIsEnabledIndex:
253
        return 'SemanticsFlag.isEnabled';
254 255
      case _kIsInMutuallyExclusiveGroupIndex:
        return 'SemanticsFlag.isInMutuallyExclusiveGroup';
256 257 258 259 260 261 262 263 264 265 266 267
    }
    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(); }
268
  void _constructor() native 'SemanticsUpdateBuilder_constructor';
269 270 271 272 273 274 275 276 277 278

  /// 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.
  ///
279
  /// The `flags` are a bit field of [SemanticsFlag]s that apply to this node.
280
  ///
I
Ian Hickson 已提交
281
  /// The `actions` are a bit field of [SemanticsAction]s that can be undertaken
282 283
  /// 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 已提交
284
  /// the possible [SemanticsAction]s. Because the semantics tree is maintained
285 286 287
  /// asynchronously, the [Window.onSemanticsAction] callback might be called
  /// with an action that is no longer possible.
  ///
288
  /// The `label` is a string that describes this node. The `value` property
289 290 291 292 293 294
  /// 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`.
295 296 297 298 299
  ///
  /// The `rect` is the region occupied by this node in its own coordinate
  /// system.
  ///
  /// The `transform` is a matrix that maps this node's coodinate system into
300
  /// its parent's coordinate system.
301 302 303 304 305 306
  void updateNode({
    int id,
    int flags,
    int actions,
    Rect rect,
    String label,
307 308
    String hint,
    String value,
309 310
    String increasedValue,
    String decreasedValue,
311
    TextDirection textDirection,
312
    Float64List transform,
313
    Int32List children,
314 315
  }) {
    if (transform.length != 16)
316
      throw new ArgumentError('transform argument must have 16 entries.');
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    _updateNode(
      id,
      flags,
      actions,
      rect.left,
      rect.top,
      rect.right,
      rect.bottom,
      label,
      hint,
      value,
      increasedValue,
      decreasedValue,
      textDirection != null ? textDirection.index + 1 : 0,
      transform,
      children,
    );
334 335 336 337 338 339 340 341 342 343
  }
  void _updateNode(
    int id,
    int flags,
    int actions,
    double left,
    double top,
    double right,
    double bottom,
    String label,
344 345
    String hint,
    String value,
346 347
    String increasedValue,
    String decreasedValue,
348
    int textDirection,
349
    Float64List transform,
350 351
    Int32List children,
  ) native 'SemanticsUpdateBuilder_updateNode';
352 353 354 355 356 357

  /// 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.
358
  SemanticsUpdate build() native 'SemanticsUpdateBuilder_build';
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
}

/// 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 {
  /// Creates an uninitialized SemanticsUpdate object.
  ///
  /// Calling the SemanticsUpdate constructor directly will not create a useable
  /// object. To create a SemanticsUpdate object, use a [SemanticsUpdateBuilder].
  SemanticsUpdate(); // (this constructor is here just so we can document it)

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