handleExpandWire method

Future<SchematicLayoutResult?> handleExpandWire(
  1. String nodeId,
  2. String wireName
)

Handle expanding a node to reveal a specific wire (hyperedge) by name.

Partially reveals only the child modules connected by wireName inside nodeId, plus the wire itself. Used by the search routine to show a wire without fully expanding the containing module.

Implementation

Future<SchematicLayoutResult?> handleExpandWire(
  String nodeId,
  String wireName,
) async {
  if (isToggling) {
    return null;
  }
  final engine = _layoutEngine;
  if (engine == null || schematicAdapter == null) {
    return null;
  }

  setState(() {
    isToggling = true;
    togglingLabel = 'Expanding...';
    pendingToggleNodeId = nodeId;
  });

  // Let the spinner appear before heavy processing.
  final completer = Completer<void>();
  WidgetsBinding.instance.addPostFrameCallback((_) {
    completer.complete();
  });
  await completer.future;
  SchedulerBinding.instance.ensureVisualUpdate();

  // Ensure connectivity for the module containing the wire.
  await ensureConnectivity(nodeId);

  try {
    final expanded = schematicAdapter!.expandWire(nodeId, wireName);
    if (!expanded) {
      if (mounted) {
        setState(() {
          isToggling = false;
          pendingToggleNodeId = null;
        });
      }
      return null;
    }

    final elkGraph = schematicAdapter!.schematic.toJsGraph();
    final newLayout = await engine.computeLayoutFromElkGraph(
      elkGraph,
      sessionId: _sessionId,
    );

    if (newLayout.hasError || !mounted) {
      if (mounted) {
        setState(() {
          isToggling = false;
          pendingToggleNodeId = null;
        });
      }
      return null;
    }

    setState(() {
      layout = newLayout;
      isToggling = false;
      pendingToggleNodeId = null;
      focusPortId = null;
      recentlyToggledNodeId = nodeId;
    });

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        setState(() {
          recentlyToggledNodeId = null;
        });
      }
    });

    return newLayout;
  } on Exception {
    if (mounted) {
      setState(() {
        isToggling = false;
        pendingToggleNodeId = null;
      });
    }
    return null;
  }
}