sendSemanticsUpdate method

void sendSemanticsUpdate()

Update the semantics using onSemanticsUpdate.

Implementation

void sendSemanticsUpdate() {
  // Once the tree is up-to-date, verify that every node is visible.
  assert(() {
    final List<SemanticsNode> invisibleNodes = <SemanticsNode>[];
    // Finds the invisible nodes in the tree rooted at `node` and adds them to
    // the invisibleNodes list. If a node is itself invisible, all its
    // descendants will be skipped.
    bool findInvisibleNodes(SemanticsNode node) {
      if (node.rect.isEmpty) {
        invisibleNodes.add(node);
      } else if (!node.mergeAllDescendantsIntoThisNode) {
        node.visitChildren(findInvisibleNodes);
      }
      return true;
    }

    final SemanticsNode? rootSemanticsNode = this.rootSemanticsNode;
    if (rootSemanticsNode != null) {
      // The root node is allowed to be invisible when it has no children.
      if (rootSemanticsNode.childrenCount > 0 && rootSemanticsNode.rect.isEmpty) {
        invisibleNodes.add(rootSemanticsNode);
      } else if (!rootSemanticsNode.mergeAllDescendantsIntoThisNode) {
        rootSemanticsNode.visitChildren(findInvisibleNodes);
      }
    }

    if (invisibleNodes.isEmpty) {
      return true;
    }

    List<DiagnosticsNode> nodeToMessage(SemanticsNode invisibleNode) {
      final SemanticsNode? parent = invisibleNode.parent;
      return<DiagnosticsNode>[
        invisibleNode.toDiagnosticsNode(style: DiagnosticsTreeStyle.errorProperty),
        parent?.toDiagnosticsNode(name: 'which was added as a child of', style: DiagnosticsTreeStyle.errorProperty) ?? ErrorDescription('which was added as the root SemanticsNode'),
      ];
    }

    throw FlutterError.fromParts(<DiagnosticsNode>[
      ErrorSummary('Invisible SemanticsNodes should not be added to the tree.'),
      ErrorDescription('The following invisible SemanticsNodes were added to the tree:'),
      ...invisibleNodes.expand(nodeToMessage),
      ErrorHint(
        'An invisible SemanticsNode is one whose rect is not on screen hence not reachable for users, '
        'and its semantic information is not merged into a visible parent.'
      ),
      ErrorHint(
        'An invisible SemanticsNode makes the accessibility experience confusing, '
        'as it does not provide any visual indication when the user selects it '
        'via accessibility technologies.'
      ),
      ErrorHint(
        'Consider removing the above invisible SemanticsNodes if they were added by your '
        'RenderObject.assembleSemanticsNode implementation, or filing a bug on GitHub:\n'
        '  https://github.com/flutter/flutter/issues/new?template=2_bug.yml',
      ),
    ]);
  }());

  if (_dirtyNodes.isEmpty) {
    return;
  }
  final Set<int> customSemanticsActionIds = <int>{};
  final List<SemanticsNode> visitedNodes = <SemanticsNode>[];
  while (_dirtyNodes.isNotEmpty) {
    final List<SemanticsNode> localDirtyNodes = _dirtyNodes.where((SemanticsNode node) => !_detachedNodes.contains(node)).toList();
    _dirtyNodes.clear();
    _detachedNodes.clear();
    localDirtyNodes.sort((SemanticsNode a, SemanticsNode b) => a.depth - b.depth);
    visitedNodes.addAll(localDirtyNodes);
    for (final SemanticsNode node in localDirtyNodes) {
      assert(node._dirty);
      assert(node.parent == null || !node.parent!.isPartOfNodeMerging || node.isMergedIntoParent);
      if (node.isPartOfNodeMerging) {
        assert(node.mergeAllDescendantsIntoThisNode || node.parent != null);
        // If child node is merged into its parent, make sure the parent is marked as dirty
        if (node.parent != null && node.parent!.isPartOfNodeMerging) {
          node.parent!._markDirty(); // this can add the node to the dirty list
          node._dirty = false; // Do not send update for this node, as it's now part of its parent
        }
      }
    }
  }
  visitedNodes.sort((SemanticsNode a, SemanticsNode b) => a.depth - b.depth);
  final SemanticsUpdateBuilder builder = SemanticsBinding.instance.createSemanticsUpdateBuilder();
  for (final SemanticsNode node in visitedNodes) {
    assert(node.parent?._dirty != true); // could be null (no parent) or false (not dirty)
    // The _serialize() method marks the node as not dirty, and
    // recurses through the tree to do a deep serialization of all
    // contiguous dirty nodes. This means that when we return here,
    // it's quite possible that subsequent nodes are no longer
    // dirty. We skip these here.
    // We also skip any nodes that were reset and subsequently
    // dropped entirely (RenderObject.markNeedsSemanticsUpdate()
    // calls reset() on its SemanticsNode if onlyChanges isn't set,
    // which happens e.g. when the node is no longer contributing
    // semantics).
    if (node._dirty && node.attached) {
      node._addToUpdate(builder, customSemanticsActionIds);
    }
  }
  _dirtyNodes.clear();
  for (final int actionId in customSemanticsActionIds) {
    final CustomSemanticsAction action = CustomSemanticsAction.getAction(actionId)!;
    builder.updateCustomAction(id: actionId, label: action.label, hint: action.hint, overrideId: action.action?.index ?? -1);
  }
  onSemanticsUpdate(builder.build());
  notifyListeners();
}