expressiveSearchViewGeometry function

  1. @visibleForTesting
({Rect content, double contentAlpha, double corner, Rect field, Rect surface, double surfaceAlpha}) expressiveSearchViewGeometry({
  1. required Size screen,
  2. required Rect? collapsed,
  3. required double progress,
  4. double backProgress = 0,
  5. bool swipeFromLeft = true,
  6. double backDeltaY = 0,
  7. double topInset = 0,
  8. bool contained = false,
})

The search view's frame at progress (0 collapsed, 1 expanded) and a predictive back gesture's transformed backProgress, on a screen whose collapsed bar stood at collapsed (Compose's FullScreenSearchBarLayout measure and placement, point for point): the surface, the field, the content, the surface's corner radius and the surface's and content's alpha.

Implementation

@visibleForTesting
({
  Rect surface,
  Rect field,
  Rect content,
  double corner,
  double surfaceAlpha,
  double contentAlpha,
})
expressiveSearchViewGeometry({
  required Size screen,
  required Rect? collapsed,
  required double progress,
  double backProgress = 0,
  bool swipeFromLeft = true,
  double backDeltaY = 0,
  double topInset = 0,
  bool contained = false,
}) {
  final bounds =
      collapsed ??
      const Rect.fromLTWH(
        0,
        0,
        SearchBarBehavior.minWidth,
        SearchBarTokens.containerHeight,
      );
  final collapsedWidth = bounds.width;
  final collapsedHeight = bounds.height;
  final p = progress;
  final pb = backProgress;
  final backEndWidth = math.max(
    screen.width * _predictiveBackMinScale,
    collapsedWidth,
  );
  final backEndHeight = math.max(
    screen.height * _predictiveBackMinScale,
    collapsedHeight,
  );
  final endWidth = lerpDouble(screen.width, backEndWidth, pb)!;
  final endHeight = lerpDouble(screen.height, backEndHeight, pb)!;
  final width = contained
      ? endWidth
      : lerpDouble(collapsedWidth, endWidth, p)!.clamp(0.0, screen.width);
  final height = contained
      ? endHeight
      : lerpDouble(collapsedHeight, endHeight, p)!.clamp(0.0, screen.height);

  final fieldPadding = contained
      ? SearchBarBehavior.fullScreenExpandedHorizontalPadding
      : 0.0;
  final fieldWidth = lerpDouble(collapsedWidth, width - 2 * fieldPadding, p)!;
  final topPadding =
      topInset +
      (contained
          ? SearchBarBehavior.appBarVerticalPadding
          : SearchBarBehavior.verticalPadding);
  final animatedTopPadding = lerpDouble(0, topPadding, math.min(p, 1 - pb))!;
  final bottomPadding = contained
      ? SearchBarBehavior.verticalPadding
      : lerpDouble(0, SearchBarBehavior.verticalPadding, p)!;

  const margin = SearchBarBehavior.predictiveBackMinMargin;
  // `coerceAtLeast(right - endWidth).coerceAtMost(left)`.
  final endOffsetX = math.min(
    math.max(
      swipeFromLeft ? screen.width - margin - backEndWidth : margin,
      bounds.right - backEndWidth,
    ),
    bounds.left,
  );
  final available = math.max(0.0, (screen.height - backEndHeight) / 2 - margin);
  final totalOffsetY = math.min(
    available,
    SearchBarBehavior.predictiveBackMaxOffsetY,
  );
  final relativeDeltaY = screen.height <= 0
      ? 0.0
      : backDeltaY.abs() / screen.height;
  final endOffsetY = math.min(
    lerpDouble(0, totalOffsetY, relativeDeltaY)! * backDeltaY.sign + topPadding,
    bounds.top,
  );
  final backOffsetX = lerpDouble(0, endOffsetX, pb)!;
  final offsetX = contained
      ? backOffsetX
      : lerpDouble(bounds.left, backOffsetX, p)!;
  final centerX = lerpDouble(bounds.center.dx, offsetX + width / 2, p)!;
  final offsetY = lerpDouble(0, endOffsetY, pb)!;
  final animatedOffsetY = lerpDouble(bounds.top, offsetY, p)!;

  final surfaceTop = contained ? offsetY : animatedOffsetY;
  final fieldTop = animatedOffsetY + animatedTopPadding;
  final contentTop = fieldTop + collapsedHeight + bottomPadding;
  return (
    surface: Rect.fromLTWH(offsetX, surfaceTop, width, height),
    field: Rect.fromLTWH(
      centerX - fieldWidth * 0.5,
      fieldTop,
      fieldWidth,
      collapsedHeight,
    ),
    content: Rect.fromLTWH(
      offsetX,
      contentTop,
      width,
      math.max(0.0, height - (contentTop - surfaceTop)),
    ),
    corner: SearchBarTokens.containerHeight / 2 * math.max(1 - p, pb),
    surfaceAlpha: contained ? p : 1.0,
    contentAlpha: p,
  );
}