LCOV - code coverage report
Current view: top level - components/combobox - _foss_combobox_popup.dart Coverage Total Hit
Test: fossui coverage Lines: 100.0 % 103 103
Test Date: 2026-11-29 09:15:47 Functions: - 0 0

            Line data    Source code
       1              : part of 'foss_combobox.dart';
       2              : 
       3              : /// Frames the popup [body] in the shared surface: a superellipse border, fill,
       4              : /// and shadow, clipped to the same superellipse so a highlighted first or last
       5              : /// row never pokes past the corner.
       6            2 : Widget _popupChrome(_ComboboxVisuals v, Widget body) {
       7            4 :   final radius = BorderRadius.circular(v.borderRadius);
       8            2 :   return DecoratedBox(
       9            2 :     decoration: ShapeDecoration(
      10            2 :       color: v.popupColor,
      11            2 :       shape: RoundedSuperellipseBorder(
      12            4 :         side: BorderSide(color: v.popupBorderColor),
      13              :         borderRadius: radius,
      14              :       ),
      15            2 :       shadows: v.popupShadow,
      16              :     ),
      17            2 :     child: ClipPath(
      18            2 :       clipper: ShapeBorderClipper(
      19            2 :         shape: RoundedSuperellipseBorder(borderRadius: radius),
      20              :       ),
      21              :       child: body,
      22              :     ),
      23              :   );
      24              : }
      25              : 
      26              : /// The shared overlay machinery behind every combobox field: the anchored popup
      27              : /// portal, its open and close animation, the roving highlight and keyboard
      28              : /// navigation, the query-driven highlight reset, and dismiss-on-scroll. The
      29              : /// single-select, autocomplete, and multi-select fields mix this in and differ
      30              : /// only in the hooks below.
      31              : mixin _ComboboxPopup<T, W extends StatefulWidget>
      32              :     on State<W>, SingleTickerProviderStateMixin<W> {
      33              :   final OverlayPortalController _portal = OverlayPortalController();
      34              :   final GlobalKey _anchorKey = GlobalKey();
      35              :   final List<ScrollPosition> _scrollPositions = [];
      36              : 
      37              :   late final AnimationController _animation;
      38              :   late final CurvedAnimation _curve;
      39              :   late final Animation<double> _scale;
      40              : 
      41              :   bool _open = false;
      42              :   int _highlight = -1;
      43              : 
      44              :   /// Whether the field accepts interaction; a disabled field never opens.
      45              :   bool get popupEnabled;
      46              : 
      47              :   /// The options currently visible, after the active query filter.
      48              :   List<FossComboboxItem<T>> get filteredOptions;
      49              : 
      50              :   /// The row to highlight when the popup opens.
      51              :   int initialHighlight();
      52              : 
      53              :   /// Commits an item: a select for the single field, a toggle for the multi.
      54              :   void onActivate(FossComboboxItem<T> item);
      55              : 
      56              :   /// Builds the decorated popup surface, its rows or the empty state.
      57              :   Widget buildPopupSurface(BuildContext context);
      58              : 
      59            6 :   @override
      60              :   void initState() {
      61            6 :     super.initState();
      62           12 :     _animation = AnimationController(vsync: this);
      63           18 :     _curve = CurvedAnimation(parent: _animation, curve: Curves.easeOut);
      64           24 :     _scale = Tween<double>(begin: _openScale, end: 1).animate(_curve);
      65              :   }
      66              : 
      67            6 :   @override
      68              :   void dispose() {
      69            6 :     _detachScrollDismiss();
      70           12 :     _animation.dispose();
      71           12 :     _curve.dispose();
      72            6 :     super.dispose();
      73              :   }
      74              : 
      75            3 :   bool get _reduceMotion =>
      76            9 :       MediaQuery.maybeOf(context)?.disableAnimations ?? false;
      77              : 
      78           15 :   Duration get _duration => context.fossTheme.motion.overlay;
      79              : 
      80            3 :   int _firstEnabled(List<FossComboboxItem<T>> options) =>
      81            9 :       options.indexWhere((o) => o.enabled);
      82              : 
      83            3 :   void _openPopup() {
      84            6 :     if (!popupEnabled || _open) return;
      85            6 :     setState(() {
      86            3 :       _open = true;
      87            6 :       _highlight = initialHighlight();
      88              :     });
      89            6 :     _portal.show();
      90            3 :     _attachScrollDismiss();
      91           12 :     _animation.duration = _reduceMotion ? Duration.zero : _duration;
      92            9 :     _animation.forward(from: _reduceMotion ? 1 : 0);
      93              :   }
      94              : 
      95            3 :   void _closePopup() {
      96            3 :     if (!_open) return;
      97            3 :     _detachScrollDismiss();
      98            9 :     setState(() => _open = false);
      99            3 :     if (_reduceMotion) {
     100            4 :       _animation.value = 0;
     101            4 :       _portal.hide();
     102              :       return;
     103              :     }
     104            9 :     _animation.duration = _duration;
     105            3 :     unawaited(
     106           12 :       _animation.reverse().whenComplete(() {
     107           12 :         if (mounted && _animation.status == AnimationStatus.dismissed) {
     108            6 :           _portal.hide();
     109              :         }
     110              :       }),
     111              :     );
     112              :   }
     113              : 
     114              :   // Close the popup when any enclosing scrollable moves, so it never floats
     115              :   // detached from its field. Every ancestor is tracked, not just the nearest,
     116              :   // so a page swipe dismisses it even when the page has its own scroll view.
     117            3 :   void _attachScrollDismiss() {
     118              :     for (
     119            6 :       var scrollable = Scrollable.maybeOf(context);
     120              :       scrollable != null;
     121            2 :       scrollable = Scrollable.maybeOf(scrollable.context)
     122              :     ) {
     123            3 :       scrollable.position.addListener(_closePopup);
     124            3 :       _scrollPositions.add(scrollable.position);
     125              :     }
     126              :   }
     127              : 
     128            6 :   void _detachScrollDismiss() {
     129            7 :     for (final position in _scrollPositions) {
     130            2 :       position.removeListener(_closePopup);
     131              :     }
     132           12 :     _scrollPositions.clear();
     133              :   }
     134              : 
     135            2 :   void _moveHighlight(int delta) {
     136            2 :     final options = filteredOptions;
     137            2 :     final count = options.length;
     138            2 :     if (count == 0) return;
     139            2 :     var next = _highlight;
     140            3 :     for (var step = 0; step < count; step++) {
     141            4 :       next = (next + delta) % count;
     142            2 :       if (next < 0) next += count;
     143            4 :       if (options[next].enabled) {
     144            6 :         setState(() => _highlight = next);
     145              :         return;
     146              :       }
     147              :     }
     148              :   }
     149              : 
     150              :   // On a query change, drop the highlight back to the first enabled match.
     151            3 :   void _resetHighlight() {
     152           18 :     if (_open) setState(() => _highlight = _firstEnabled(filteredOptions));
     153              :   }
     154              : 
     155            2 :   void _highlightRow(int index) {
     156           10 :     if (_highlight != index) setState(() => _highlight = index);
     157              :   }
     158              : 
     159            2 :   void _activateHighlighted() {
     160            2 :     final options = filteredOptions;
     161           10 :     if (_highlight >= 0 && _highlight < options.length) {
     162            6 :       onActivate(options[_highlight]);
     163              :     }
     164              :   }
     165              : 
     166            2 :   KeyEventResult _handlePopupKey(KeyEvent event) {
     167            2 :     if (event is KeyUpEvent) return KeyEventResult.ignored;
     168            2 :     final key = event.logicalKey;
     169            2 :     if (key == LogicalKeyboardKey.escape) {
     170            2 :       if (!_open) return KeyEventResult.ignored;
     171            2 :       _closePopup();
     172              :       return KeyEventResult.handled;
     173              :     }
     174            2 :     if (key == LogicalKeyboardKey.arrowDown) {
     175            4 :       _open ? _moveHighlight(1) : _openPopup();
     176              :       return KeyEventResult.handled;
     177              :     }
     178            2 :     if (key == LogicalKeyboardKey.arrowUp) {
     179            6 :       if (_open) _moveHighlight(-1);
     180              :       return KeyEventResult.handled;
     181              :     }
     182              :     return KeyEventResult.ignored;
     183              :   }
     184              : 
     185            3 :   Widget _buildOverlay(BuildContext context) {
     186            3 :     final anchor = _anchorRect(context);
     187              :     if (anchor == null) return const SizedBox.shrink();
     188              :     // The on-screen keyboard eats the bottom of the overlay; subtract it so the
     189              :     // popup lays out against the visible area, not behind the keyboard.
     190            6 :     final bottomInset = MediaQuery.viewInsetsOf(context).bottom;
     191            3 :     return TextFieldTapRegion(
     192            3 :       child: Stack(
     193            3 :         children: [
     194            3 :           Positioned.fill(
     195            3 :             child: CustomSingleChildLayout(
     196            3 :               delegate: _PopupLayout(anchor: anchor, bottomInset: bottomInset),
     197            3 :               child: FadeTransition(
     198            3 :                 opacity: _curve,
     199            3 :                 child: ScaleTransition(
     200            3 :                   scale: _scale,
     201              :                   alignment: Alignment.topCenter,
     202            3 :                   child: buildPopupSurface(context),
     203              :                 ),
     204              :               ),
     205              :             ),
     206              :           ),
     207              :         ],
     208              :       ),
     209              :     );
     210              :   }
     211              : 
     212            3 :   Rect? _anchorRect(BuildContext overlayContext) {
     213            9 :     final anchor = _anchorKey.currentContext?.findRenderObject();
     214            9 :     final overlay = Overlay.of(overlayContext).context.findRenderObject();
     215            9 :     if (anchor is! RenderBox || overlay is! RenderBox || !anchor.attached) {
     216              :       return null;
     217              :     }
     218            9 :     return anchor.localToGlobal(Offset.zero, ancestor: overlay) & anchor.size;
     219              :   }
     220              : }
        

Generated by: LCOV version 2.0-1