probeVisibleLocator method

CockpitTargetResolutionResult probeVisibleLocator(
  1. CockpitLocator locator
)

Implementation

CockpitTargetResolutionResult probeVisibleLocator(CockpitLocator locator) {
  final rootContext = _boundaryKey.currentContext;
  if (rootContext is! Element) {
    return CockpitTargetResolutionResult.failure(
      error: CockpitCommandError.targetNotFound(
        message: 'The Flutter surface is not mounted.',
      ),
    );
  }
  for (final candidate in _flatten(locator)) {
    var probe = _probeForLocator(rootContext, candidate, visibleOnly: true);
    if (probe.element == null && !probe.ambiguous) {
      final text = candidate.text;
      if (text != null &&
          candidate.signalMap.length == 1 &&
          candidate.matchMode == CockpitTextMatchMode.exact) {
        probe = _probeForLocator(
          rootContext,
          CockpitLocator(
            key: text,
            ancestor: candidate.ancestor,
            index: candidate.index,
          ),
          visibleOnly: true,
        );
      }
    }
    if (probe.ambiguous) {
      return CockpitTargetResolutionResult.failure(
        error: CockpitCommandError.ambiguousTarget(
          message: 'Multiple visible Flutter elements matched the locator.',
          details: <String, Object?>{
            'matchedKind': candidate.kind.name,
            'matchedValue': candidate.value,
            'candidateCount': probe.matchCount,
          },
        ),
      );
    }
    final element = probe.element;
    if (element != null) {
      return CockpitTargetResolutionResult.success(
        target: _probeTargetForElement(element),
        locatorResolution: CockpitLocatorResolution(
          matchedKind: candidate.kind,
          matchedValue: candidate.value,
          matchedSignals: _matchedLocatorSignals(candidate),
        ),
      );
    }
  }
  if (_requiresLogicalTargetResolution(locator)) {
    return _registry.resolve(locator);
  }
  return CockpitTargetResolutionResult.failure(
    error: CockpitCommandError.targetNotFound(
      message: 'No visible Flutter element matched the locator.',
      details: <String, Object?>{'requestedLocator': locator.toJson()},
    ),
  );
}