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.',
      ),
    );
  }
  var needsNativeDiscoveryFallback = false;
  for (final candidate in _flatten(locator)) {
    if (candidate.index != null) {
      return _registry.resolve(candidate, requiredCommand: requiredCommand);
    }
    if (requiredCommand != null && _registry.registeredTargets.isNotEmpty) {
      final registryResolution = _registry.resolveRegistered(
        candidate,
        requiredCommand: requiredCommand,
      );
      final resolvedTargetSupportsCommand =
          registryResolution.target?.supportedCommands.contains(
            requiredCommand,
          ) ==
          true;
      final hasCommandMatches = registryResolution.matches.any(
        (target) => target.supportedCommands.contains(requiredCommand),
      );
      if (resolvedTargetSupportsCommand || hasCommandMatches) {
        return registryResolution;
      }
    }
    final probe = _probeForLocator(
      rootContext,
      candidate,
      visibleOnly: true,
      fallbackLocator: _stableTextKeyFallback(candidate),
    );
    if (probe.ambiguous) {
      if (requiredCommand != null &&
          !_allowsExplicitGestureFallback(candidate, requiredCommand)) {
        return _registry.resolve(locator, requiredCommand: requiredCommand);
      }
      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,
        rootContext: rootContext,
        requiredCommand: requiredCommand,
        allowGestureFallback: _allowsExplicitGestureFallback(
          candidate,
          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,
      );
    }
    if (!_canFastFailDirectElementProbe(candidate, requiredCommand)) {
      needsNativeDiscoveryFallback = true;
    }
  }
  if (!needsNativeDiscoveryFallback) {
    return CockpitTargetResolutionResult.failure(
      error: CockpitCommandError.targetNotFound(
        message: 'No visible target matched the requested locator chain.',
        details: <String, Object?>{'requestedLocator': locator.toJson()},
      ),
    );
  }
  return _registry.resolve(locator, requiredCommand: requiredCommand);
}