compute static method

LiquidMorphState compute({
  1. required double rawValue,
  2. required double finalDx,
  3. required double finalDy,
  4. double horizontalOffset = 0.0,
  5. double verticalOffset = 0.0,
  6. double scaleDelta = 1.0,
  7. bool? adaptiveDamping,
  8. bool isClosing = false,
  9. double? elapsedSeconds,
  10. bool hasHandedOff = false,
  11. bool isSettled = false,
  12. bool disableAnimations = false,
  13. double phaseStartTriggerWidth = 1.0,
  14. double closeStartProgress = 1.0,
  15. double recoilHalfTravelPx = defaultRecoilHalfTravelPx,
  16. bool verticalOnlyRecoil = false,
})

Computes the full LiquidMorphState for a single animation frame.

Implementation

static LiquidMorphState compute({
  required double rawValue,
  required double finalDx,
  required double finalDy,
  double horizontalOffset = 0.0,
  double verticalOffset = 0.0,
  double scaleDelta = 1.0,
  bool? adaptiveDamping,
  bool isClosing = false,
  double? elapsedSeconds,
  bool hasHandedOff = false,
  bool isSettled = false,
  bool disableAnimations = false,
  double phaseStartTriggerWidth = 1.0,
  double closeStartProgress = 1.0,
  double recoilHalfTravelPx = defaultRecoilHalfTravelPx,
  bool verticalOnlyRecoil = false,
}) {
  final clampedValue = rawValue.clamp(0.0, 1.0);

  // Preserve close undershoot additive term for direct/stateless callers.
  final closeUndershoot = rawValue < 0.0 ? rawValue : 0.0;

  // ── Adaptive mode detection ───────────────────────────────────────────────
  final bool adaptive = adaptiveDamping ?? (scaleDelta > 1.5);

  // ── Effective J-curve amplitude (Euclidean travel) ────────────────────────
  final double amplitude = adaptive
      ? computeAdaptiveBackOutAmplitude(
          finalDx: finalDx + horizontalOffset,
          finalDy: finalDy + verticalOffset,
        )
      : _backOutAmplitude;

  // ── J-Curve / Close Position ──────────────────────────────────────────────
  final double pathT;
  if (isClosing) {
    pathT = clampedValue + closeUndershoot;
  } else {
    pathT =
        _BackOutCurve(amplitude).transform(clampedValue) + closeUndershoot;
  }

  // ── Size ─────────────────────────────────────────────────────────────────
  // On close: tightly coupled quadratic curve (`u * (0.62 + 0.38 * u)`) that
  // stays in lockstep with `pathT` all the way to landing (~280 ms) without
  // early flatlining, while opening enough mid-flight |pathT - sizeT|
  // separation to drive the SDF metaball bridge.
  final double rawSizeT;
  if (isClosing) {
    rawSizeT = clampedValue * (0.62 + 0.38 * clampedValue);
  } else if (adaptive && scaleDelta > 3.0) {
    rawSizeT = _smootherStep(clampedValue);
  } else {
    rawSizeT = Curves.linearToEaseOut.transform(clampedValue);
  }
  final sizeT = rawSizeT + closeUndershoot;

  // ── Trigger Recoil & Push Momentum ────────────────────────────────────────
  final travelDx = finalDx + horizontalOffset;
  final travelDy = finalDy + verticalOffset;
  final travelMag = math.sqrt(travelDx * travelDx + travelDy * travelDy);

  double pushDx;
  double pushDy;
  double triggerPushDx;
  double triggerPushDy;
  double triggerImpactScale;

  if (elapsedSeconds != null) {
    if (isClosing &&
        !disableAnimations &&
        !isSettled &&
        closeStartProgress > 0.001) {
      final recoilPulse =
          _recoilPulse(elapsedSeconds) * closeStartProgress.clamp(0.0, 1.0);
      final bool pureY = verticalOnlyRecoil && travelDy.abs() > 1.0;
      final effDx = pureY ? 0.0 : travelDx;
      final effMag = pureY ? travelDy.abs() : travelMag;
      final denom = effMag + recoilHalfTravelPx;
      triggerPushDx = -(_maxRecoilPx * effDx / denom) * recoilPulse;
      triggerPushDy = -(_maxRecoilPx * travelDy / denom) * recoilPulse;
      pushDx = triggerPushDx;
      pushDy = triggerPushDy;

      final impactDepth =
          0.025 + 0.015 * (travelMag / (travelMag + _impactHalfTravelPx));
      triggerImpactScale =
          (1.0 - impactDepth * recoilPulse).clamp(0.92, 1.02);
    } else {
      pushDx = 0.0;
      pushDy = 0.0;
      triggerPushDx = 0.0;
      triggerPushDy = 0.0;
      triggerImpactScale = 1.0;
    }
  } else {
    // Stateless fallback when called without a controller elapsed clock.
    final double pushFactor;
    if (adaptive) {
      pushFactor = travelMag > 0.0
          ? (_maxPushTravelPx / travelMag).clamp(0.0, 1.0)
          : 1.0;
    } else {
      pushFactor = 1.0;
    }
    pushDx = rawValue < 0.0 ? travelDx * rawValue * pushFactor : 0.0;
    pushDy = rawValue < 0.0 ? travelDy * rawValue * pushFactor : 0.0;
    triggerPushDx = pushDx;
    triggerPushDy = pushDy;
    triggerImpactScale =
        rawValue < 0.0 ? (1.0 + rawValue * 0.35).clamp(0.88, 1.0) : 1.0;
  }

  // ── Wide-Trigger Centered Width Progress (MagicMorphView #2) ─────────────
  final double triggerWidthProgress;
  if (isClosing) {
    if (disableAnimations || isSettled) {
      triggerWidthProgress = isSettled ? 1.0 : (1.0 - clampedValue);
    } else if (elapsedSeconds != null) {
      final sWidth = _stepResponse(
        elapsedSeconds,
        _closeTriggerWidthOmega0,
        _closeTriggerWidthZeta,
      );
      final startW = phaseStartTriggerWidth.clamp(0.0, 1.0);
      triggerWidthProgress = startW + (1.0 - startW) * sWidth;
    } else {
      triggerWidthProgress = 1.0 - clampedValue - closeUndershoot;
    }
  } else {
    if (clampedValue <= 0.001 &&
        (elapsedSeconds == null || elapsedSeconds <= 0.0)) {
      triggerWidthProgress = 1.0;
    } else if (disableAnimations || isSettled) {
      triggerWidthProgress = clampedValue >= 0.999 ? 0.0 : 1.0 - clampedValue;
    } else if (elapsedSeconds != null) {
      final sWidth = _stepResponse(
        elapsedSeconds,
        _openTriggerWidthOmega0,
        _openTriggerWidthZeta,
      );
      final startW = phaseStartTriggerWidth.clamp(0.0, 1.0);
      triggerWidthProgress = (startW * (1.0 - sWidth)).clamp(0.0, 1.0);
    } else {
      triggerWidthProgress = (1.0 - (clampedValue / 0.55)).clamp(0.0, 1.0);
    }
  }

  // ── Platter Scale, Opacity, & Active Window (_UIContextMenuView) ─────────
  final double platterScale;
  final double platterOpacity;
  final double contentOpacity;
  double contentBlurSigma = 0.0;
  final bool isPlatterActive;

  if (isClosing) {
    platterScale = (0.20 + 0.80 * rawValue).clamp(0.18, 1.0);
    final bool active = !hasHandedOff && !isSettled && rawValue > 0.001;
    isPlatterActive = active;
    final fade = active
        ? computeCloseContentOpacity(
            rawValue: rawValue,
            closeStartProgress: closeStartProgress,
          )
        : 0.0;
    platterOpacity = active ? (rawValue / 0.25).clamp(0.0, 1.0) : 0.0;
    contentOpacity = fade;
    contentBlurSigma = active
        ? computeCloseContentBlur(
            rawValue: rawValue,
            closeStartProgress: closeStartProgress,
          )
        : 0.0;
  } else {
    platterScale = sizeT.clamp(0.0, 1.0);
    isPlatterActive = rawValue > 0.001;
    platterOpacity = isPlatterActive ? 1.0 : 0.0;
    contentOpacity = isPlatterActive
        ? ((platterScale - 0.15) / 0.85).clamp(0.0, 1.0)
        : 0.0;
  }

  // ── Blob B Displacement ───────────────────────────────────────────────────
  final currentDx = finalDx * pathT;
  final currentDy = finalDy * pathT;

  // ── Anchor Scale ─────────────────────────────────────────────────────────
  final anchorScale =
      (1.0 - (clampedValue / _anchorEaseDuration)).clamp(0.0, 1.0);

  // ── Metaball Blend ────────────────────────────────────────────────────────
  final blendAttenuation = computeBlendAttenuation(scaleDelta);
  final separation = (pathT - sizeT).abs();
  final blend = (separation * _blendMultiplier * blendAttenuation)
      .clamp(0.0, _maxBlend);

  // ── Container Scale Pulse ─────────────────────────────────────────────────
  final containerScale = rawValue > 1.0
      ? 1.0 + (rawValue - 1.0) * 0.10
      : rawValue < 0.0
          ? 1.0 + rawValue * 0.55
          : 1.0;

  // ── Phase ─────────────────────────────────────────────────────────────────
  final phase = _derivePhase(rawValue, clampedValue);

  return LiquidMorphState(
    pathT: pathT,
    sizeT: sizeT,
    currentDx: currentDx,
    currentDy: currentDy,
    pushDx: pushDx,
    pushDy: pushDy,
    anchorScale: anchorScale,
    blend: blend,
    containerScale: containerScale,
    phase: phase,
    triggerPushDx: triggerPushDx,
    triggerPushDy: triggerPushDy,
    triggerImpactScale: triggerImpactScale,
    triggerWidthProgress: triggerWidthProgress,
    platterScale: platterScale,
    platterOpacity: platterOpacity,
    contentOpacity: contentOpacity,
    contentBlurSigma: contentBlurSigma,
    isPlatterActive: isPlatterActive,
  );
}