diff --git a/app/lib/ui/adaptive/glass_pill.dart b/app/lib/ui/adaptive/glass_pill.dart index 0beea5cf..150df5be 100644 --- a/app/lib/ui/adaptive/glass_pill.dart +++ b/app/lib/ui/adaptive/glass_pill.dart @@ -43,6 +43,24 @@ class GlassPill extends StatelessWidget { @override Widget build(BuildContext context) { + final reveal = GlassReveal.maybeOf(context); + if (reveal == null) return _build(context, 1, child); + // The fill, edge and blur fade in together, and the content with them: + // an opacity layer over the whole piece would hide the content behind + // it from the blur until the fade ends, and the blur would snap on. + final content = FadeTransition( + opacity: reveal, + alwaysIncludeSemantics: true, + child: child, + ); + return AnimatedBuilder( + animation: reveal, + builder: (context, _) => + _build(context, reveal.value.clamp(0.0, 1.0), content), + ); + } + + Widget _build(BuildContext context, double reveal, Widget child) { final solid = MediaQuery.maybeHighContrastOf(context) ?? false; final Color base; final Color edge; @@ -56,10 +74,11 @@ class GlassPill extends StatelessWidget { base = scheme.surfaceContainerHigh; edge = scheme.outlineVariant; } - final color = fill ?? (solid ? base : base.withValues(alpha: fillOpacity)); + final opaque = fill ?? (solid ? base : base.withValues(alpha: fillOpacity)); + final color = opaque.withValues(alpha: opaque.a * reveal); final border = shape.copyWith( side: BorderSide( - color: solid ? edge : edge.withValues(alpha: edge.a * 0.8), + color: edge.withValues(alpha: edge.a * (solid ? 1 : 0.8) * reveal), width: 0.5, ), ); @@ -74,7 +93,10 @@ class GlassPill extends StatelessWidget { // [BackdropGroup] (a bar's row) share one read of the content behind // them where the engine supports it (Impeller) instead of one each. surface = BackdropFilter.grouped( - filter: ImageFilter.blur(sigmaX: blurSigma, sigmaY: blurSigma), + filter: ImageFilter.blur( + sigmaX: blurSigma * reveal, + sigmaY: blurSigma * reveal, + ), child: surface, ); } @@ -87,3 +109,19 @@ class GlassPill extends StatelessWidget { ); } } + +/// Fades in every [GlassPill] below it by [opacity]: its fill, hairline, +/// blur and content together. A piece entering the screen uses it instead of +/// an opacity layer, under which the blur would read nothing behind it. +class GlassReveal extends InheritedWidget { + const GlassReveal({super.key, required this.opacity, required super.child}); + + final Animation opacity; + + static Animation? maybeOf(BuildContext context) => + context.dependOnInheritedWidgetOfExactType()?.opacity; + + @override + bool updateShouldNotify(GlassReveal oldWidget) => + opacity != oldWidget.opacity; +} diff --git a/app/lib/ui/adaptive/segmented_bar.dart b/app/lib/ui/adaptive/segmented_bar.dart index be5aa4a2..dc1ac03f 100644 --- a/app/lib/ui/adaptive/segmented_bar.dart +++ b/app/lib/ui/adaptive/segmented_bar.dart @@ -3,6 +3,8 @@ import 'dart:math' as math; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import '../motion/motion.dart'; +import 'segmented_bar_motion.dart'; import 'segmented_bar_pieces.dart'; export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill; @@ -169,6 +171,21 @@ class _SegmentedBarState extends State { final _piecesBackdrop = BackdropKey(); final _expandedTitleBackdrop = BackdropKey(); + /// Whether the bar's first frame is drawn. Islands built with the bar + /// enter one after another from the leading edge; an island that turns up + /// later (an action becoming available) enters at once. + bool _shown = false; + + /// Whether the actions pill was there at the last build: actions that + /// arrive with the pill enter with it, not again inside it. + bool _hadActions = false; + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) => _shown = true); + } + @override void didUpdateWidget(covariant SegmentedBar oldWidget) { super.didUpdateWidget(oldWidget); @@ -186,12 +203,101 @@ class _SegmentedBarState extends State { setState(() => _expanded = !_expanded); } + /// The bar's main pill: the [SegmentedBar.titleWidget] filling the row, or + /// the title pill (drawn expanded in the overlay when it is expandable). + Widget _titleIsland( + BuildContext context, + Widget Function(bool expanded) titlePill, + ) { + final titleWidget = widget.titleWidget; + if (titleWidget != null) { + // The main pill fills the row, up to its cap. + return ConstrainedBox( + constraints: BoxConstraints(maxWidth: widget.titleWidgetMaxWidth), + child: SegmentedPill( + cupertino: widget.cupertino, + fill: widget.pillColor, + child: SizedBox(width: double.infinity, child: titleWidget), + ), + ); + } + if (!widget.expandableTitle) return titlePill(false); + return LayoutBuilder( + builder: (context, titleSpace) => OverlayPortal( + controller: _expandedTitle, + overlayChildBuilder: (context) => !(_expanded && widget.expandableTitle) + ? const SizedBox.shrink() + : Positioned( + top: 0, + left: 0, + child: CompositedTransformFollower( + link: _titleLink, + showWhenUnlinked: false, + child: ConstrainedBox( + constraints: BoxConstraints(maxWidth: titleSpace.maxWidth), + child: BackdropGroup( + backdropKey: _expandedTitleBackdrop, + child: titlePill(true), + ), + ), + ), + ), + child: CompositedTransformTarget( + link: _titleLink, + child: ExcludeSemantics( + excluding: _expanded && widget.expandableTitle, + child: titlePill(false), + ), + ), + ), + ); + } + + /// The actions, each keyed by its own key (or its type and place among + /// actions of that type) so one arriving or leaving leaves the others be. + /// Only the first shrinks (ellipsises); the rest keep their natural width + /// as inflexible (flex 0) children. + List _actionIslands( + List actions, { + required bool cupertino, + required bool play, + }) { + final seen = {}; + return [ + for (final (index, action) in actions.indexed) + Flexible( + key: ValueKey(( + 'segmented.action', + action.key ?? + ( + action.runtimeType, + seen.update( + action.runtimeType, + (n) => n + 1, + ifAbsent: () => 0, + ), + ), + )), + flex: index == 0 ? 1 : 0, + child: SegmentedEntrance( + play: play, + glass: false, + child: SegmentedPress( + cupertino: cupertino, + tint: widget.foregroundColor, + enabled: actions.length > 1, + child: action, + ), + ), + ), + ]; + } + @override Widget build(BuildContext context) { final cupertino = widget.cupertino; final title = widget.title; final titleWidget = widget.titleWidget; - final titleWidgetMaxWidth = widget.titleWidgetMaxWidth; final leadingWidget = widget.leading; final automaticallyImplyLeading = widget.automaticallyImplyLeading; final subtitle = widget.subtitle; @@ -224,14 +330,33 @@ class _SegmentedBarState extends State { foreground: foregroundColor, fill: pillColor, ); + // Each island is keyed by its role, so it keeps its entrance (and plays + // none) while the bar rebuilds around it. + var order = 0; + Duration nextDelay() => + _shown ? Duration.zero : Motion.barEntranceStagger * order++; + final leadingDelay = leading == null ? Duration.zero : nextDelay(); + final hasTitle = titleWidget != null || showTitle; + final titleDelay = hasTitle ? nextDelay() : Duration.zero; + final actionsDelay = actions.isEmpty ? Duration.zero : nextDelay(); + final freshActions = !_hadActions; + _hadActions = actions.isNotEmpty; final Widget bar = LayoutBuilder( builder: (context, constraints) => Row( children: [ if (leading != null) ...[ - SegmentedPill( - cupertino: cupertino, - fill: pillColor, - child: leading, + SegmentedEntrance( + key: const ValueKey('segmented.leading'), + delay: leadingDelay, + child: SegmentedPress( + cupertino: cupertino, + tint: foregroundColor, + child: SegmentedPill( + cupertino: cupertino, + fill: pillColor, + child: leading, + ), + ), ), const SizedBox(width: 8), ], @@ -241,57 +366,17 @@ class _SegmentedBarState extends State { child: Align( alignment: AlignmentDirectional.centerStart, heightFactor: 1, - child: titleWidget != null - // The main pill fills the row, up to its cap. - ? ConstrainedBox( - constraints: BoxConstraints( - maxWidth: titleWidgetMaxWidth, + child: !hasTitle + ? const SizedBox.shrink() + : SegmentedEntrance( + key: ValueKey( + titleWidget != null + ? 'segmented.field' + : 'segmented.title', ), - child: SegmentedPill( - cupertino: cupertino, - fill: pillColor, - child: SizedBox( - width: double.infinity, - child: titleWidget, - ), - ), - ) - : showTitle && !widget.expandableTitle - ? titlePill(false) - : showTitle - ? LayoutBuilder( - builder: (context, titleSpace) => OverlayPortal( - controller: _expandedTitle, - overlayChildBuilder: (context) => - !(_expanded && widget.expandableTitle) - ? const SizedBox.shrink() - : Positioned( - top: 0, - left: 0, - child: CompositedTransformFollower( - link: _titleLink, - showWhenUnlinked: false, - child: ConstrainedBox( - constraints: BoxConstraints( - maxWidth: titleSpace.maxWidth, - ), - child: BackdropGroup( - backdropKey: _expandedTitleBackdrop, - child: titlePill(true), - ), - ), - ), - ), - child: CompositedTransformTarget( - link: _titleLink, - child: ExcludeSemantics( - excluding: _expanded && widget.expandableTitle, - child: titlePill(false), - ), - ), - ), - ) - : const SizedBox.shrink(), + delay: titleDelay, + child: _titleIsland(context, titlePill), + ), ), ), if (actions.isNotEmpty) ...[ @@ -311,15 +396,27 @@ class _SegmentedBarState extends State { constraints.maxWidth * SegmentedBar.minTitleShare, ), ), - child: SegmentedPill( - cupertino: cupertino, - fill: pillColor, - child: Row( - mainAxisSize: MainAxisSize.min, - children: [ - Flexible(child: actions.first), - ...actions.skip(1), - ], + child: SegmentedEntrance( + key: const ValueKey('segmented.actions'), + delay: actionsDelay, + // A lone action is the whole pill, so the pill sinks when it + // is pressed; beside others, each action sinks on its own. + child: SegmentedPress( + cupertino: cupertino, + tint: foregroundColor, + enabled: actions.length == 1, + child: SegmentedPill( + cupertino: cupertino, + fill: pillColor, + child: Row( + mainAxisSize: MainAxisSize.min, + children: _actionIslands( + actions, + cupertino: cupertino, + play: !freshActions, + ), + ), + ), ), ), ), diff --git a/app/lib/ui/adaptive/segmented_bar_motion.dart b/app/lib/ui/adaptive/segmented_bar_motion.dart new file mode 100644 index 00000000..313189b1 --- /dev/null +++ b/app/lib/ui/adaptive/segmented_bar_motion.dart @@ -0,0 +1,245 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/physics.dart'; + +import '../motion/motion.dart'; +import 'glass_pill.dart'; + +/// One island of a [SegmentedBar] (a pill, or an action inside one) popping +/// in: it grows from [Motion.barEntranceScaleBegin] with a gentle overshoot +/// while it fades in, after [delay]. +/// +/// It plays once, when the island is first built; rebuilding it (a new +/// title, new actions, a scroll) does not play it again, so the bar keys +/// its islands to keep them while they stay on screen. Under reduced motion, +/// or when [play] is false, the island simply appears. +/// +/// A [glass] island fades its [GlassPill] through a [GlassReveal], so the +/// blur fades in with the fill instead of snapping on at the end; anything +/// else fades as a whole. +class SegmentedEntrance extends StatefulWidget { + const SegmentedEntrance({ + super.key, + required this.child, + this.delay = Duration.zero, + this.play = true, + this.glass = true, + }); + + final Widget child; + final Duration delay; + final bool play; + final bool glass; + + @override + State createState() => _SegmentedEntranceState(); +} + +class _SegmentedEntranceState extends State + with SingleTickerProviderStateMixin { + AnimationController? _controller; + Animation _pop = kAlwaysCompleteAnimation; + Animation _fade = kAlwaysCompleteAnimation; + bool _decided = false; + + @override + void didChangeDependencies() { + super.didChangeDependencies(); + // Decided once, on the first build: later dependency changes (a theme, + // a text scale) never replay it. + if (_decided) return; + _decided = true; + final window = Motion.of(context, Motion.barEntranceDuration); + if (!widget.play || window == Duration.zero) return; + final total = (widget.delay + window).inMicroseconds; + final begin = widget.delay.inMicroseconds / total; + final faded = + (widget.delay.inMicroseconds + + window.inMicroseconds * Motion.barEntranceOpacityPortion) / + total; + final controller = AnimationController( + vsync: this, + duration: widget.delay + window, + ); + _controller = controller; + _pop = CurvedAnimation( + parent: controller, + curve: Interval(begin, 1, curve: Motion.barEntranceCurve), + ); + _fade = CurvedAnimation( + parent: controller, + curve: Interval(begin, faded, curve: Curves.easeOut), + ); + controller.forward(); + } + + @override + void dispose() { + if (_pop case final CurvedAnimation curved) curved.dispose(); + if (_fade case final CurvedAnimation curved) curved.dispose(); + _controller?.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + final child = widget.glass + ? GlassReveal(opacity: _fade, child: widget.child) + : FadeTransition( + opacity: _fade, + // Controls stay announced (and findable) while they fade in. + alwaysIncludeSemantics: true, + child: widget.child, + ); + return AnimatedBuilder( + animation: _pop, + child: child, + builder: (context, child) => Transform.scale( + scale: + Motion.barEntranceScaleBegin + + (1 - Motion.barEntranceScaleBegin) * _pop.value, + child: child, + ), + ); + } +} + +/// Press feedback for a control of a [SegmentedBar]: while a pointer is down +/// on [child] it sinks to [Motion.barPressScale] under a tint clipped to a +/// stadium over its bounds (the pill's own shape), and on release it springs +/// back. +/// +/// It only listens: the control below still takes the tap, and its own +/// feedback (an ink ripple, a Cupertino button's dimming) still shows. A +/// quick tap plays the whole press before springing back, so it is seen. +/// Under reduced motion only the tint shows. When [enabled] is false it does +/// nothing, so a bar can switch a piece's feedback on and off without +/// rebuilding the control. +class SegmentedPress extends StatefulWidget { + const SegmentedPress({ + super.key, + required this.cupertino, + required this.child, + this.enabled = true, + this.tint, + }); + + final bool cupertino; + final Widget child; + final bool enabled; + + /// The tint's colour before its opacity; defaults to the host's text + /// colour, which reads over the glass in light and dark themes. + final Color? tint; + + @override + State createState() => _SegmentedPressState(); +} + +class _SegmentedPressState extends State + with SingleTickerProviderStateMixin { + /// 0 at rest, 1 fully pressed; the release spring may pass 0 slightly. + late final AnimationController _press = AnimationController.unbounded( + vsync: this, + ); + final Set _pointers = {}; + bool _released = true; + + bool get _reduced => + Motion.of(context, Motion.barPressDuration) == Duration.zero; + + void _down(PointerDownEvent event) { + if (!widget.enabled) return; + _pointers.add(event.pointer); + _released = false; + if (_reduced) { + _press.value = 1; + return; + } + _press + .animateTo(1, duration: Motion.barPressDuration, curve: Curves.easeOut) + .then((_) { + if (_released && mounted) _springBack(); + }); + } + + void _up(PointerEvent event) { + if (!_pointers.remove(event.pointer) || _pointers.isNotEmpty) return; + _released = true; + // Still sinking: the press completes first, then springs back. + if (_press.isAnimating && _press.value < 1) return; + _springBack(); + } + + void _springBack() { + if (_reduced) { + _press.value = 0; + return; + } + _press + .animateWith( + SpringSimulation(Motion.barReleaseSpring, _press.value, 0, 0), + ) + // The spring settles within a tolerance; rest is exactly at rest. + .then((_) { + if (mounted) _press.value = 0; + }); + } + + @override + void didUpdateWidget(SegmentedPress oldWidget) { + super.didUpdateWidget(oldWidget); + if (!widget.enabled && oldWidget.enabled) { + _pointers.clear(); + _released = true; + _press.value = 0; + } + } + + @override + void dispose() { + _press.dispose(); + super.dispose(); + } + + Color _tint(BuildContext context) { + if (widget.tint case final tint?) return tint; + return widget.cupertino + ? CupertinoColors.label.resolveFrom(context) + : Theme.of(context).colorScheme.onSurface; + } + + @override + Widget build(BuildContext context) { + final tint = _tint(context); + return Listener( + onPointerDown: _down, + onPointerUp: _up, + onPointerCancel: _up, + child: AnimatedBuilder( + animation: _press, + child: widget.child, + builder: (context, child) { + final pressed = _press.value; + final reduced = _reduced; + return Transform.scale( + scale: reduced ? 1 : 1 - (1 - Motion.barPressScale) * pressed, + child: DecoratedBox( + position: DecorationPosition.foreground, + decoration: ShapeDecoration( + shape: const StadiumBorder(), + color: tint.withValues( + alpha: + tint.a * + Motion.barPressTintOpacity * + pressed.clamp(0.0, 1.0), + ), + ), + child: child, + ), + ); + }, + ), + ); + } +} diff --git a/app/lib/ui/adaptive/segmented_bar_pieces.dart b/app/lib/ui/adaptive/segmented_bar_pieces.dart index a3c4bc5a..77dd7ba6 100644 --- a/app/lib/ui/adaptive/segmented_bar_pieces.dart +++ b/app/lib/ui/adaptive/segmented_bar_pieces.dart @@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'glass_pill.dart'; +import 'segmented_bar_motion.dart'; /// The smallest height of every piece of a [SegmentedBar]: the platform's /// minimum touch target, so a round Back is a circle. @@ -216,7 +217,11 @@ class SegmentedTitlePill extends StatelessWidget { : InkWell(key: buttonKey, onTap: onPressed, child: content), ); } - return SegmentedPill( + final titleAction = this.titleAction; + // A pressable title sinks as a whole pill; beside a title action, the + // title and the action each give their own feedback. + final split = titleAction != null; + final pill = SegmentedPill( cupertino: cupertino, fill: fill, child: Row( @@ -224,11 +229,26 @@ class SegmentedTitlePill extends StatelessWidget { children: [ Flexible( fit: FlexFit.loose, - child: IntrinsicWidth(child: body), + child: IntrinsicWidth( + child: split && onPressed != null + ? SegmentedPress( + cupertino: cupertino, + tint: foreground, + child: body, + ) + : body, + ), ), - ?titleAction, + if (titleAction != null) + SegmentedPress( + cupertino: cupertino, + tint: foreground, + child: titleAction, + ), ], ), ); + if (split || onPressed == null) return pill; + return SegmentedPress(cupertino: cupertino, tint: foreground, child: pill); } } diff --git a/app/lib/ui/motion/motion.dart b/app/lib/ui/motion/motion.dart index 1a2b4142..28f73515 100644 --- a/app/lib/ui/motion/motion.dart +++ b/app/lib/ui/motion/motion.dart @@ -47,6 +47,39 @@ abstract final class Motion { /// The icon's scale at the top of its bounce. static const double tabBounceScale = 1.08; + /// Duration of each top-bar island's entrance pop (Back, title, actions). + static const Duration barEntranceDuration = Duration(milliseconds: 240); + + /// Delay between neighbouring top-bar islands entering together, from the + /// leading edge. + static const Duration barEntranceStagger = Duration(milliseconds: 40); + + /// The scale at which a top-bar island starts its entrance. + static const double barEntranceScaleBegin = 0.85; + + /// The fraction of an island's entrance spent fading in. + static const double barEntranceOpacityPortion = 0.6; + + /// The gentle overshoot of an island's entrance. + static const Curve barEntranceCurve = Curves.easeOutBack; + + /// How long a pressed top-bar control takes to sink. + static const Duration barPressDuration = Duration(milliseconds: 90); + + /// The scale of a top-bar control while it is held down. + static const double barPressScale = 0.94; + + /// The opacity of the pressed tint over a top-bar control. + static const double barPressTintOpacity = 0.14; + + /// The spring a released top-bar control returns on: quick, with a + /// barely visible overshoot. + static const SpringDescription barReleaseSpring = SpringDescription( + mass: 1, + stiffness: 520, + damping: 28, + ); + static const Curve curve = Curves.easeOutCubic; /// [duration], or zero under the system's reduce-motion setting. diff --git a/app/notes/topbar-island-motion.md b/app/notes/topbar-island-motion.md new file mode 100644 index 00000000..7e4456bc --- /dev/null +++ b/app/notes/topbar-island-motion.md @@ -0,0 +1,24 @@ +# Top bar island motion and press feedback + +- Entrance: each island of `SegmentedBar` (Back, title or field pill, actions + pill, and each action inside the pill) is a `SegmentedEntrance` + (`lib/ui/adaptive/segmented_bar_motion.dart`). It scales in from 0.85 with + `easeOutBack` over 240 ms and fades in. On the bar's first frame the islands + are staggered 40 ms apart from the leading edge. An island added later pops + in straight away. Actions that arrive together with their pill don't pop a + second time. The islands are keyed by role, and actions by their key (or + their type and position), so a rebuild, a new title or a shell tab switch + keeps the pills that are already there. Under reduced motion nothing + animates. +- Glass fades through `GlassReveal` (`glass_pill.dart`): the fill, the hairline, + the blur sigma and the content fade together. An opacity layer would hide + the backdrop from `BackdropFilter.grouped` until the fade ended. Grouping is + unchanged (checked in `test/ui/adaptive/segmented_bar_motion_test.dart`). +- Press: `SegmentedPress` listens for pointer events only, so tap targets and + hit testing are unchanged. While pressed it sinks to 0.94 in 90 ms under a + stadium tint (text colour at 14%), then springs back. A quick tap plays the + full press first. It wraps Back, a pressable title (and the title action + separately when expanded), a lone action as its whole pill, and each action + when there are several. Native ink and Cupertino dimming still show. +- Evidence: films at 560x900 in Material and Cupertino, light and dark, are in + `/tmp/topbar-evidence/` (strips `strip-*.png`, zoomed presses `zoom-press*.png`). diff --git a/app/test/ui/adaptive/segmented_bar_motion_test.dart b/app/test/ui/adaptive/segmented_bar_motion_test.dart new file mode 100644 index 00000000..8864c1f5 --- /dev/null +++ b/app/test/ui/adaptive/segmented_bar_motion_test.dart @@ -0,0 +1,268 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar_motion.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar_pieces.dart'; +import 'package:pipelines_app/ui/motion/motion.dart'; + +/// The top bar's islands pop in once, and its controls sink when pressed. +void main() { + Widget page( + bool cupertino, { + String title = 'Runs', + List actions = const [], + VoidCallback? onTitle, + bool reduced = false, + VoidCallback? onBack, + }) { + final bar = SegmentedBar( + cupertino: cupertino, + title: title, + onTitlePressed: onTitle, + leading: SegmentedBackButton( + key: const Key('back'), + cupertino: cupertino, + onPressed: onBack ?? () {}, + ), + actions: actions, + ); + final Widget home = cupertino + ? CupertinoPageScaffold( + navigationBar: bar, + child: const SizedBox.expand(), + ) + : Scaffold( + extendBodyBehindAppBar: true, + appBar: bar, + body: const SizedBox.expand(), + ); + final app = cupertino ? CupertinoApp(home: home) : MaterialApp(home: home); + return MediaQuery( + data: MediaQueryData(disableAnimations: reduced), + child: app, + ); + } + + /// The entrance scale of the island holding [of]: its pill's, or with + /// [inner] its own inside the actions pill. + double entranceScale(WidgetTester tester, Finder of, {bool inner = false}) { + final entrances = find.ancestor( + of: of, + matching: find.byType(SegmentedEntrance), + ); + final entrance = inner ? entrances.first : entrances.last; + final transform = tester.widget( + find.descendant(of: entrance, matching: find.byType(Transform)).first, + ); + return transform.transform.storage[0]; + } + + /// The press scale of the control holding [of], or with [pill] of the + /// pill holding it. + double pressScale(WidgetTester tester, Finder of, {bool pill = false}) { + final presses = find.ancestor( + of: of, + matching: find.byType(SegmentedPress), + ); + final press = pill ? presses.last : presses.first; + final transform = tester.widget( + find.descendant(of: press, matching: find.byType(Transform)).first, + ); + return transform.transform.storage[0]; + } + + Color pressTint(WidgetTester tester, Finder of) { + final press = find + .ancestor(of: of, matching: find.byType(SegmentedPress)) + .first; + final box = tester.widget( + find.descendant(of: press, matching: find.byType(DecoratedBox)).first, + ); + return (box.decoration as ShapeDecoration).color!; + } + + final back = find.byKey(const Key('back')); + final title = find.text('Runs'); + + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + final icon = cupertino ? CupertinoIcons.refresh : Icons.refresh; + Widget action(String key) => + IconButton(key: Key(key), icon: Icon(icon), onPressed: () {}); + + testWidgets('$host: the islands pop in left to right on first show', ( + tester, + ) async { + await tester.pumpWidget(page(cupertino, actions: [action('a')])); + final a = find.byKey(const Key('a')); + expect(entranceScale(tester, back), Motion.barEntranceScaleBegin); + await tester.pump(const Duration(milliseconds: 120)); + final b = entranceScale(tester, back); + final t = entranceScale(tester, title); + final r = entranceScale(tester, a); + expect(b, greaterThan(t), reason: 'Back leads the title'); + expect(t, greaterThan(r), reason: 'the title leads the actions'); + expect(r, greaterThan(Motion.barEntranceScaleBegin)); + await tester.pumpAndSettle(); + expect(entranceScale(tester, back), 1); + expect(entranceScale(tester, title), 1); + expect(entranceScale(tester, a), 1); + }); + + testWidgets('$host: a rebuild or new title does not replay it', ( + tester, + ) async { + await tester.pumpWidget(page(cupertino, actions: [action('a')])); + await tester.pumpAndSettle(); + await tester.pumpWidget( + page(cupertino, title: 'Other', actions: [action('a')]), + ); + await tester.pump(); + expect(entranceScale(tester, back), 1); + expect(entranceScale(tester, find.text('Other')), 1); + expect(entranceScale(tester, find.byKey(const Key('a'))), 1); + expect(tester.hasRunningAnimations, isFalse); + }); + + testWidgets('$host: an action that becomes available pops in alone', ( + tester, + ) async { + await tester.pumpWidget(page(cupertino, actions: [action('a')])); + await tester.pumpAndSettle(); + await tester.pumpWidget( + page(cupertino, actions: [action('b'), action('a')]), + ); + final newcomer = find.byKey(const Key('b')); + expect( + entranceScale(tester, newcomer, inner: true), + Motion.barEntranceScaleBegin, + ); + // The pill and the action already there stay put. + expect(entranceScale(tester, find.byKey(const Key('a'))), 1); + expect(entranceScale(tester, find.byKey(const Key('a')), inner: true), 1); + expect(entranceScale(tester, back), 1); + await tester.pump(Motion.barEntranceDuration ~/ 2); + expect(entranceScale(tester, newcomer, inner: true), greaterThan(0.9)); + await tester.pumpAndSettle(); + expect(entranceScale(tester, newcomer, inner: true), 1); + }); + + testWidgets('$host: reduced motion shows the bar at once', (tester) async { + await tester.pumpWidget( + page(cupertino, actions: [action('a')], reduced: true), + ); + expect(entranceScale(tester, back), 1); + expect(entranceScale(tester, title), 1); + expect(tester.hasRunningAnimations, isFalse); + }); + + testWidgets('$host: the glass fades in with its blur, not under a layer', ( + tester, + ) async { + await tester.pumpWidget(page(cupertino)); + await tester.pump(const Duration(milliseconds: 40)); + final backdrops = tester.layers.whereType().toList(); + expect(backdrops, hasLength(2), reason: 'Back and the title'); + expect(backdrops.first.backdropKey, isNotNull, reason: 'still grouped'); + expect(backdrops.last.backdropKey, same(backdrops.first.backdropKey)); + for (final layer in backdrops) { + // No opacity layer above a blur: it would read nothing behind it. + for (ContainerLayer? up = layer.parent; up != null; up = up.parent) { + if (up is OpacityLayer) expect(up.alpha, anyOf(isNull, 255)); + } + } + final mid = tester.widget( + find.ancestor(of: back, matching: find.byType(BackdropFilter)).first, + ); + await tester.pumpAndSettle(); + final rest = tester.widget( + find.ancestor(of: back, matching: find.byType(BackdropFilter)).first, + ); + expect(mid.filter, isNot(rest.filter), reason: 'the blur fades in'); + }); + + testWidgets( + '$host: Back sinks and tints while pressed, then springs back', + (tester) async { + var backs = 0; + await tester.pumpWidget(page(cupertino, onBack: () => backs++)); + await tester.pumpAndSettle(); + expect(pressScale(tester, back), 1); + expect(pressTint(tester, back).a, 0); + final gesture = await tester.startGesture(tester.getCenter(back)); + await tester.pump(); + await tester.pump(Motion.barPressDuration); + expect(pressScale(tester, back), closeTo(Motion.barPressScale, 1e-6)); + expect(pressTint(tester, back).a, greaterThan(0.1)); + await gesture.up(); + await tester.pumpAndSettle(); + expect(pressScale(tester, back), closeTo(1, 1e-3)); + expect(pressTint(tester, back).a, 0); + expect(backs, 1, reason: 'the tap still reaches Back'); + }, + ); + + testWidgets('$host: a quick tap still shows the whole press', ( + tester, + ) async { + await tester.pumpWidget(page(cupertino, actions: [action('a')])); + await tester.pumpAndSettle(); + final a = find.byKey(const Key('a')); + await tester.tap(a); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 80)); + // A lone action is the whole pill: the pill sinks, not the icon. + expect(pressScale(tester, a, pill: true), lessThan(0.95)); + expect(pressScale(tester, a), 1); + await tester.pumpAndSettle(); + expect(pressScale(tester, a, pill: true), 1); + }); + + testWidgets('$host: beside others each action sinks on its own', ( + tester, + ) async { + var taps = 0; + await tester.pumpWidget( + page( + cupertino, + onTitle: () => taps++, + actions: [action('a'), action('b')], + ), + ); + await tester.pumpAndSettle(); + final a = find.byKey(const Key('a')); + final b = find.byKey(const Key('b')); + final gesture = await tester.startGesture(tester.getCenter(b)); + await tester.pump(); + await tester.pump(Motion.barPressDuration); + expect(pressScale(tester, b), closeTo(Motion.barPressScale, 1e-6)); + expect(pressScale(tester, a), 1); + expect(pressScale(tester, back), 1); + await gesture.up(); + await tester.pumpAndSettle(); + // A pressable title sinks as a whole pill and still takes the tap. + await tester.tap(title); + await tester.pumpAndSettle(); + expect(taps, 1); + }); + + testWidgets('$host: hit testing is unchanged at rest', (tester) async { + await tester.pumpWidget(page(cupertino, actions: [action('a')])); + await tester.pumpAndSettle(); + final pill = tester.getRect( + find.ancestor(of: back, matching: find.byType(GlassPill)).first, + ); + expect(pill.left, 12); + expect(pill.height, segmentedPieceHeight(cupertino)); + // A tap between the pieces reaches no piece; on Back it reaches Back. + final backBox = tester.renderObject(back); + bool hitsBack(Offset at) => + tester.hitTestOnBinding(at).path.any((e) => e.target == backBox); + expect(hitsBack(Offset(pill.right + 4, pill.center.dy)), isFalse); + expect(hitsBack(pill.center), isTrue); + }); + } +} diff --git a/app/test/ui/adaptive/segmented_bar_title_widget_test.dart b/app/test/ui/adaptive/segmented_bar_title_widget_test.dart index f22226c0..36250f90 100644 --- a/app/test/ui/adaptive/segmented_bar_title_widget_test.dart +++ b/app/test/ui/adaptive/segmented_bar_title_widget_test.dart @@ -69,6 +69,8 @@ void main() { findsOneWidget, reason: 'one pill: the field', ); + // Measured at rest, after the pieces' entrance. + await tester.pump(const Duration(seconds: 1)); final pill = pillOf(tester, find.byKey(fieldKey)); expect(pill.left, 12); expect(pill.right, 390 - 12); @@ -94,6 +96,8 @@ void main() { ), ); + // Measured at rest, after the pieces' entrance. + await tester.pump(const Duration(seconds: 1)); final pill = pillOf(tester, find.byKey(fieldKey)); final back = pillOf(tester, find.byType(SegmentedBackButton)); expect(pill.width, 600); @@ -134,6 +138,8 @@ void main() { findsNothing, ); expect(find.byKey(const ValueKey('shell.search')), findsNothing); + // Measured at rest, after the pieces' entrance. + await tester.pump(const Duration(seconds: 1)); final pill = pillOf(tester, find.byKey(fieldKey)); expect(pill.left, 12); expect(pill.right, 560 - 12);