handleCollapsePartial method

Future<SchematicLayoutResult?> handleCollapsePartial(
  1. String nodeId
)

Handle collapsing a partially expanded node back to fully collapsed.

Clears the partial expansion state and re-runs layout so the node returns to its collapsed appearance.

Implementation

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

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

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

  try {
    final collapsed = schematicAdapter!.collapsePartialExpansion(nodeId);
    if (!collapsed) {
      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;
  }
}