LCOV - code coverage report
Current view: top level - foundation - anchored_overlay.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 84 84
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : import 'dart:math' as math;
       2              : 
       3              : import 'package:flutter/widgets.dart';
       4              : import 'package:fossui/src/theme/colors/foss_colors.dart';
       5              : 
       6              : /// The physical side of an anchor a floating overlay opens on, after any RTL
       7              : /// resolution. The overlay flips to the opposite side when it would overflow.
       8              : enum AnchorSide {
       9              :   /// Above the anchor.
      10              :   top,
      11              : 
      12              :   /// Below the anchor.
      13              :   bottom,
      14              : 
      15              :   /// The left of the anchor.
      16              :   left,
      17              : 
      18              :   /// The right of the anchor.
      19              :   right,
      20              : }
      21              : 
      22              : /// How a floating overlay aligns to its anchor along the cross axis of its
      23              : /// [AnchorSide]: the horizontal axis for [AnchorSide.top] / [AnchorSide.bottom],
      24              : /// the vertical axis for [AnchorSide.left] / [AnchorSide.right].
      25              : enum AnchorAlign {
      26              :   /// Flush with the anchor's leading edge on the cross axis.
      27              :   start,
      28              : 
      29              :   /// Centered on the anchor's cross axis.
      30              :   center,
      31              : 
      32              :   /// Flush with the anchor's trailing edge on the cross axis.
      33              :   end,
      34              : }
      35              : 
      36              : /// Default gap between an anchored overlay and its anchor, in logical pixels.
      37              : const double kAnchorSideOffset = 4;
      38              : 
      39              : /// Keeps an anchored overlay this far off the viewport edge on overflow.
      40              : const double kAnchorViewportMargin = 8;
      41              : 
      42              : /// The least room beside an anchor worth shrinking an overlay into. With less
      43              : /// on both sides, a tooltip on a full-screen image for instance, the overlay
      44              : /// keeps its size and overlaps the anchor rather than collapsing to nothing.
      45              : const double kAnchorMinRoom = 48;
      46              : 
      47              : /// Positions a floating overlay against [anchor] on the preferred [side] and
      48              : /// [align], flipping and clamping so it never overflows the screen or covers
      49              : /// its anchor. All values are physical: callers resolve the reading direction
      50              : /// before constructing the delegate.
      51              : ///
      52              : /// The overlay stays [kAnchorViewportMargin] inside the area left after
      53              : /// [padding], typically [overlaySafeArea]. It keeps [side] while the child
      54              : /// fits there and otherwise opens on whichever side has more room. Its extent
      55              : /// along the main axis is capped to that larger room, so a child too big for
      56              : /// either side shrinks rather than sliding over the anchor, unless that room
      57              : /// is under [kAnchorMinRoom].
      58              : class AnchoredLayout extends SingleChildLayoutDelegate {
      59              :   /// Creates a layout delegate anchored to [anchor].
      60            8 :   AnchoredLayout({
      61              :     required this.anchor,
      62              :     required this.side,
      63              :     this.align = AnchorAlign.center,
      64              :     this.sideOffset = kAnchorSideOffset,
      65              :     this.alignOffset = 0,
      66              :     this.padding = EdgeInsets.zero,
      67              :   });
      68              : 
      69              :   /// The anchor rectangle in the overlay's coordinate space.
      70              :   final Rect anchor;
      71              : 
      72              :   /// The preferred side to open on.
      73              :   final AnchorSide side;
      74              : 
      75              :   /// The cross-axis alignment against the anchor.
      76              :   final AnchorAlign align;
      77              : 
      78              :   /// The gap from the anchor along the main axis.
      79              :   final double sideOffset;
      80              : 
      81              :   /// The shift along the cross axis, in the physical direction of [align].
      82              :   final double alignOffset;
      83              : 
      84              :   /// Insets from the overlay's edges the child never enters, such as the
      85              :   /// system bars and the keyboard.
      86              :   final EdgeInsets padding;
      87              : 
      88              :   /// The area the child may occupy within an overlay of [size].
      89            8 :   Rect _bounds(Size size) =>
      90           32 :       padding.deflateRect(Offset.zero & size).deflate(kAnchorViewportMargin);
      91              : 
      92              :   /// The room between the anchor and the edge of [bounds] on [side].
      93            8 :   double _room(Rect bounds, AnchorSide side) => switch (side) {
      94           56 :     AnchorSide.top => anchor.top - sideOffset - bounds.top,
      95           56 :     AnchorSide.bottom => bounds.bottom - anchor.bottom - sideOffset,
      96           28 :     AnchorSide.left => anchor.left - sideOffset - bounds.left,
      97           28 :     AnchorSide.right => bounds.right - anchor.right - sideOffset,
      98              :   };
      99              : 
     100            8 :   static AnchorSide _opposite(AnchorSide side) => switch (side) {
     101            8 :     AnchorSide.top => AnchorSide.bottom,
     102            8 :     AnchorSide.bottom => AnchorSide.top,
     103            4 :     AnchorSide.left => AnchorSide.right,
     104            4 :     AnchorSide.right => AnchorSide.left,
     105              :   };
     106              : 
     107            7 :   @override
     108              :   BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
     109           14 :     final bounds = _bounds(constraints.biggest);
     110            7 :     final room = math.max(
     111           14 :       _room(bounds, side),
     112           21 :       _room(bounds, _opposite(side)),
     113              :     );
     114           28 :     final vertical = side == AnchorSide.top || side == AnchorSide.bottom;
     115           11 :     final full = vertical ? bounds.height : bounds.width;
     116            7 :     final main = room < kAnchorMinRoom ? full : room;
     117            7 :     return BoxConstraints(
     118           14 :       maxWidth: math.max(0, vertical ? bounds.width : main),
     119           11 :       maxHeight: math.max(0, vertical ? main : bounds.height),
     120              :     );
     121              :   }
     122              : 
     123            8 :   @override
     124              :   Offset getPositionForChild(Size size, Size childSize) {
     125            8 :     final bounds = _bounds(size);
     126           16 :     final position = _place(childSize, _resolveSide(bounds, childSize));
     127            8 :     return Offset(
     128           48 :       _clamp(position.dx, bounds.left, bounds.right - childSize.width),
     129           48 :       _clamp(position.dy, bounds.top, bounds.bottom - childSize.height),
     130              :     );
     131              :   }
     132              : 
     133              :   /// Keeps [value] within `[lo, hi]`, collapsing to [lo] when the child is
     134              :   /// larger than the available room.
     135            8 :   static double _clamp(double value, double lo, double hi) =>
     136           16 :       hi < lo ? lo : value.clamp(lo, hi);
     137              : 
     138            8 :   Offset _place(Size child, AnchorSide side) {
     139              :     switch (side) {
     140            8 :       case AnchorSide.top:
     141            8 :       case AnchorSide.bottom:
     142              :         final dx =
     143           64 :             _crossStart(child.width, anchor.left, anchor.right) + alignOffset;
     144            8 :         final dy = side == AnchorSide.top
     145           36 :             ? anchor.top - sideOffset - child.height
     146           32 :             : anchor.bottom + sideOffset;
     147            8 :         return Offset(dx, dy);
     148            4 :       case AnchorSide.left:
     149            4 :       case AnchorSide.right:
     150              :         final dy =
     151           32 :             _crossStart(child.height, anchor.top, anchor.bottom) + alignOffset;
     152            4 :         final dx = side == AnchorSide.left
     153           24 :             ? anchor.left - sideOffset - child.width
     154           16 :             : anchor.right + sideOffset;
     155            4 :         return Offset(dx, dy);
     156              :     }
     157              :   }
     158              : 
     159              :   /// The cross-axis origin for [extent] between the anchor's [lead] and [trail]
     160              :   /// edges, resolved against [align].
     161            8 :   double _crossStart(double extent, double lead, double trail) =>
     162            8 :       switch (align) {
     163            8 :         AnchorAlign.start => lead,
     164           26 :         AnchorAlign.center => lead + (trail - lead) / 2 - extent / 2,
     165           10 :         AnchorAlign.end => trail - extent,
     166              :       };
     167              : 
     168              :   /// Keeps [side] while the child fits there, else takes the roomier side.
     169            8 :   AnchorSide _resolveSide(Rect bounds, Size child) {
     170            8 :     final extent = switch (side) {
     171           24 :       AnchorSide.top || AnchorSide.bottom => child.height,
     172           12 :       AnchorSide.left || AnchorSide.right => child.width,
     173              :     };
     174           16 :     final preferred = _room(bounds, side);
     175           16 :     final opposite = _opposite(side);
     176           22 :     return extent <= preferred || preferred >= _room(bounds, opposite)
     177            8 :         ? side
     178              :         : opposite;
     179              :   }
     180              : 
     181            7 :   @override
     182              :   bool shouldRelayout(AnchoredLayout old) =>
     183           21 :       old.anchor != anchor ||
     184           21 :       old.side != side ||
     185           18 :       old.align != align ||
     186           18 :       old.sideOffset != sideOffset ||
     187           18 :       old.alignOffset != alignOffset ||
     188           18 :       old.padding != padding;
     189              : }
     190              : 
     191              : /// The insets an overlay in the nearest [Overlay] keeps clear: the system
     192              : /// padding (status bar, notch, home indicator) and the keyboard.
     193              : ///
     194              : /// Read from the [Overlay]'s own media query rather than the caller's, since a
     195              : /// trigger inside a safe area sees that padding already removed while the
     196              : /// overlay still spans the full screen. Registers [context] as a dependent, so
     197              : /// it rebuilds when the insets change.
     198            7 : EdgeInsets overlaySafeArea(BuildContext context) {
     199            7 :   final element = Overlay.of(
     200              :     context,
     201           14 :   ).context.getElementForInheritedWidgetOfExactType<MediaQuery>();
     202              :   if (element == null) return EdgeInsets.zero;
     203            7 :   context.dependOnInheritedElement(element);
     204            7 :   return switch (element.widget) {
     205           35 :     MediaQuery(:final data) => data.padding + data.viewInsets,
     206              :     _ => EdgeInsets.zero,
     207              :   };
     208              : }
     209              : 
     210              : /// Softens a shadow set to the 5% tint a floating overlay wears, a fainter drop
     211              : /// than a surface at the same elevation.
     212           12 : List<BoxShadow> overlaySoftShadow(List<BoxShadow> base) => <BoxShadow>[
     213            6 :   for (final shadow in base)
     214           18 :     shadow.copyWith(color: shadow.color.withValues(alpha: 0.05)),
     215              : ];
     216              : 
     217              : /// The raised-edge highlight inside an overlay surface border: a top line in
     218              : /// light mode, a bottom line in dark mode, derived from the foreground role at
     219              : /// low alpha. Fill a surface's clipped bounds with it via [Positioned.fill].
     220              : class OverlayInnerRing extends StatelessWidget {
     221              :   /// Creates the inner highlight ring for [colors].
     222            6 :   const OverlayInnerRing({required this.colors, super.key});
     223              : 
     224              :   /// The active color roles, read for the foreground tint and brightness.
     225              :   final FossColors colors;
     226              : 
     227            6 :   @override
     228              :   Widget build(BuildContext context) {
     229           12 :     final dark = colors.isDark;
     230           18 :     final highlight = colors.foreground.withValues(alpha: dark ? 0.06 : 0.04);
     231            6 :     final edge = BorderSide(color: highlight);
     232            6 :     return DecoratedBox(
     233            6 :       decoration: BoxDecoration(
     234            6 :         border: Border(
     235              :           top: dark ? BorderSide.none : edge,
     236              :           bottom: dark ? edge : BorderSide.none,
     237              :         ),
     238              :       ),
     239              :     );
     240              :   }
     241              : }
        

Generated by: LCOV version 2.0-1