assembleSemanticsNode method

  1. @override
void assembleSemanticsNode(
  1. SemanticsNode node,
  2. SemanticsConfiguration config,
  3. Iterable<SemanticsNode> children
)
override

Assemble the SemanticsNode for this RenderObject.

If describeSemanticsConfiguration sets SemanticsConfiguration.isSemanticBoundary to true, this method is called with the node created for this RenderObject, the config to be applied to that node and the children SemanticsNodes that descendants of this RenderObject have generated.

By default, the method will annotate node with config and add the children to it.

Subclasses can override this method to add additional SemanticsNodes to the tree. If new SemanticsNodes are instantiated in this method they must be disposed in clearSemantics.

Implementation

@override
void assembleSemanticsNode(
    SemanticsNode node, SemanticsConfiguration config, Iterable<SemanticsNode> children) {
  assert(_semanticsInfo != null && _semanticsInfo!.isNotEmpty);
  final List<SemanticsNode> newChildren = <SemanticsNode>[];
  TextDirection currentDirection = textDirection;
  Rect currentRect;
  double ordinal = 0.0;
  int start = 0;
  int placeholderIndex = 0;
  RenderBox? child = firstChild;
  for (InlineSpanSemanticsInformation info in _combineSemanticsInfo()) {
    final TextDirection initialDirection = currentDirection;
    final TextSelection selection = TextSelection(
      baseOffset: start,
      extentOffset: start + info.text.length,
    );
    final List<ui.TextBox> rects = getBoxesForSelection(selection);
    if (rects.isEmpty) {
      continue;
    }
    Rect rect = rects.first.toRect();
    currentDirection = rects.first.direction;
    for (ui.TextBox textBox in rects.skip(1)) {
      rect = rect.expandToInclude(textBox.toRect());
      currentDirection = textBox.direction;
    }
    // Any of the text boxes may have had infinite dimensions.
    // We shouldn't pass infinite dimensions up to the bridges.
    rect = Rect.fromLTWH(
      math.max(0.0, rect.left),
      math.max(0.0, rect.top),
      math.min(rect.width, constraints.maxWidth),
      math.min(rect.height, constraints.maxHeight),
    );
    // round the current rectangle to make this API testable and add some
    // padding so that the accessibility rects do not overlap with the text.
    currentRect = Rect.fromLTRB(
      rect.left.floorToDouble() - 4.0,
      rect.top.floorToDouble() - 4.0,
      rect.right.ceilToDouble() + 4.0,
      rect.bottom.ceilToDouble() + 4.0,
    );

    if (info.isPlaceholder) {
      final SemanticsNode childNode = children.elementAt(placeholderIndex++);
      final TextParentData parentData = child!.parentData as TextParentData;
      childNode.rect = Rect.fromLTWH(
        childNode.rect.left,
        childNode.rect.top,
        childNode.rect.width * parentData.scale!,
        childNode.rect.height * parentData.scale!,
      );
      newChildren.add(childNode);
      child = childAfter(child);
    } else {
      final SemanticsConfiguration configuration = SemanticsConfiguration()
        ..sortKey = OrdinalSortKey(ordinal++)
        ..textDirection = initialDirection
        ..label = info.semanticsLabel ?? info.text;
      if (info.recognizer != null) {
        if (info.recognizer is TapGestureRecognizer) {
          final TapGestureRecognizer recognizer = info.recognizer as TapGestureRecognizer;
          configuration.onTap = recognizer.onTap;
          configuration.isLink = true;
        } else if (info.recognizer is LongPressGestureRecognizer) {
          final LongPressGestureRecognizer recognizer =
              info.recognizer as LongPressGestureRecognizer;
          configuration.onLongPress = recognizer.onLongPress;
        } else {
          assert(false);
        }
      }
      newChildren.add(
        SemanticsNode()
          ..updateWith(config: configuration)
          ..rect = currentRect,
      );
    }
    start += info.text.length;
  }
  node.updateWith(config: config, childrenInInversePaintOrder: newChildren);
}