probeVisibleLocator method

CockpitTargetResolutionResult probeVisibleLocator(
  1. CockpitLocator locator, {
  2. CockpitCommandType? requiredCommand,
})

Implementation

CockpitTargetResolutionResult probeVisibleLocator(
  CockpitLocator locator, {
  CockpitCommandType? requiredCommand,
}) {
  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)) {
    if (requiredCommand != null || candidate.index != null) {
      final registryResolution = _registry.resolve(
        candidate,
        requiredCommand: requiredCommand,
      );
      final resolvedTargetSupportsCommand =
          requiredCommand != null &&
          registryResolution.target?.supportedCommands.contains(
                requiredCommand,
              ) ==
              true;
      final hasCommandMatches =
          requiredCommand != null &&
          registryResolution.matches.any(
            (target) => target.supportedCommands.contains(requiredCommand),
          );
      if ((requiredCommand == null && candidate.index != null) ||
          resolvedTargetSupportsCommand ||
          hasCommandMatches) {
        return registryResolution;
      }
    }
    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) {
      final targetResolution = _probeTargetForElement(
        element,
        requiredCommand: requiredCommand,
      );
      if (!targetResolution.isSuccess) {
        return targetResolution;
      }
      return CockpitTargetResolutionResult.success(
        target: targetResolution.target!,
        locatorResolution: CockpitLocatorResolution(
          matchedKind: candidate.kind,
          matchedValue: candidate.value,
          matchedSignals: _matchedLocatorSignals(candidate),
        ),
        matches: targetResolution.matches,
      );
    }
  }
  return _registry.resolve(locator, requiredCommand: requiredCommand);
}