diff --git a/app/lib/ui/adaptive/floating_destination_bar.dart b/app/lib/ui/adaptive/floating_destination_bar.dart index f19303e2..053a4602 100644 --- a/app/lib/ui/adaptive/floating_destination_bar.dart +++ b/app/lib/ui/adaptive/floating_destination_bar.dart @@ -3,6 +3,7 @@ import 'dart:ui' show SemanticsRole; import 'package:flutter/widgets.dart'; +import '../motion/motion.dart'; import 'floating_bar_layout.dart'; import 'floating_tab_button.dart'; import 'floating_tab_motion.dart'; @@ -71,10 +72,22 @@ class FloatingDestinationBar extends StatefulWidget { static double buttonGap({required bool cupertino}) => cupertino ? cupertinoButtonGap : materialButtonGap; - /// The width of a row of [tabs] buttons, when the space allows it. + /// The selected button's size, about [Motion.tabSelectedScale] times the + /// others (whole pixels, so layout stays exact). + static const double materialSelectedButtonSize = 62; + static const double cupertinoSelectedButtonSize = 60; + + static double selectedButtonSize({required bool cupertino}) => + cupertino ? cupertinoSelectedButtonSize : materialSelectedButtonSize; + + /// The width of a row of [tabs] buttons (one of them selected), when the + /// space allows it. static double rowWidth({required bool cupertino, required int tabs}) => - buttonSize(cupertino: cupertino) * tabs + - buttonGap(cupertino: cupertino) * math.max(0, tabs - 1); + tabs == 0 + ? 0 + : buttonSize(cupertino: cupertino) * (tabs - 1) + + selectedButtonSize(cupertino: cupertino) + + buttonGap(cupertino: cupertino) * math.max(0, tabs - 1); @override State createState() => _FloatingDestinationBarState(); @@ -119,6 +132,12 @@ class _FloatingDestinationBarState extends State items.map((item) => '${item.key ?? item.label}:${item.label}').join('|'); double get _size => FloatingDestinationBar.buttonSize(cupertino: cupertino); + double get _selectedSize => + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino); + + /// The buttons' natural width side by side, the selected one larger. + double get _buttonsWidth => + items.isEmpty ? 0 : _size * (items.length - 1) + _selectedSize; @override Widget build(BuildContext context) { @@ -165,14 +184,17 @@ class _FloatingDestinationBarState extends State final gap = FloatingDestinationBar.buttonGap(cupertino: cupertino); final count = items.length; if (count < 2 || !width.isFinite) return gap; - return (width - count * _size).clamp(0.0, gap * (count - 1)) / (count - 1); + return (width - _buttonsWidth).clamp(0.0, gap * (count - 1)) / (count - 1); } Widget _row(BuildContext context, double width) { final gap = _gap(width); + // As tall as the selected (largest) button; the others are centred on + // it, and every gap stays the same while a button grows or shrinks. Widget row = Row( key: FloatingDestinationBar.rowKey, mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, children: [ for (var index = 0; index < items.length; index += 1) ...[ if (index > 0) SizedBox(width: gap), @@ -181,10 +203,10 @@ class _FloatingDestinationBarState extends State ], ); // Narrower than the buttons themselves: the whole row shrinks. - if (width.isFinite && width < items.length * _size) { + if (width.isFinite && width < _buttonsWidth) { row = SizedBox( width: math.max(0.0, width), - height: _size, + height: _selectedSize, child: FittedBox(fit: BoxFit.scaleDown, child: row), ); } @@ -204,23 +226,31 @@ class _FloatingDestinationBarState extends State Widget _button(BuildContext context, int index) { final item = items[index]; final selected = index == currentIndex; + final iconSize = cupertino + ? FloatingDestinationBar.cupertinoIconSize + : FloatingDestinationBar.materialIconSize; + // The selected button grows (and a deselected one shrinks back) in the + // layout, so its neighbours move over and keep their gaps. return FloatingTabEntrance( motion: _motion, index: index, - child: FloatingTabButton( - key: item.key, - cupertino: cupertino, - motion: _motion, - index: index, - count: items.length, - selected: selected, - size: _size, - iconSize: cupertino - ? FloatingDestinationBar.cupertinoIconSize - : FloatingDestinationBar.materialIconSize, - label: item.label, - icon: selected ? item.selectedIcon ?? item.icon : item.icon, - onTap: () => widget.onTap(index), + child: TweenAnimationBuilder( + tween: Tween(end: selected ? _selectedSize : _size), + duration: Motion.of(context, Motion.tabSelectDuration), + curve: Motion.curve, + builder: (context, size, _) => FloatingTabButton( + key: item.key, + cupertino: cupertino, + motion: _motion, + index: index, + count: items.length, + selected: selected, + size: size, + iconSize: iconSize * size / _size, + label: item.label, + icon: selected ? item.selectedIcon ?? item.icon : item.icon, + onTap: () => widget.onTap(index), + ), ), ); } diff --git a/app/lib/ui/adaptive/floating_tab_button.dart b/app/lib/ui/adaptive/floating_tab_button.dart index 02b4ea64..375e215d 100644 --- a/app/lib/ui/adaptive/floating_tab_button.dart +++ b/app/lib/ui/adaptive/floating_tab_button.dart @@ -156,11 +156,6 @@ class FloatingTabButton extends StatelessWidget { ), child: FloatingTabBounce(motion: motion, index: index, child: icon), ); - // The selected icon is a little larger, growing with the selection. - glyph = Transform.scale( - scale: 1 + (Motion.tabSelectedIconScale - 1) * t, - child: glyph, - ); final reveal = GlassReveal.maybeOf(context); if (reveal != null) { // The icon sits above the glass, so it fades with it here. diff --git a/app/lib/ui/layout/content_width.dart b/app/lib/ui/layout/content_width.dart index 001d0272..fcdbbc3d 100644 --- a/app/lib/ui/layout/content_width.dart +++ b/app/lib/ui/layout/content_width.dart @@ -8,7 +8,11 @@ const double kContentMaxWidth = 1100; /// Centres [child] and caps its width, keeping the incoming height constraints. class ContentWidth extends StatelessWidget { - const ContentWidth({super.key, required this.child, this.maxWidth = kContentMaxWidth}); + const ContentWidth({ + super.key, + required this.child, + this.maxWidth = kContentMaxWidth, + }); final Widget child; final double maxWidth; diff --git a/app/lib/ui/motion/motion.dart b/app/lib/ui/motion/motion.dart index 57ca6b02..b666c827 100644 --- a/app/lib/ui/motion/motion.dart +++ b/app/lib/ui/motion/motion.dart @@ -43,9 +43,10 @@ abstract final class Motion { /// The icon's scale at the top of its bounce. static const double tabBounceScale = 1.08; - /// How much larger the selected tab's icon is than the others; it grows - /// and shrinks with the selection tint. - static const double tabSelectedIconScale = 1.15; + /// How much larger the selected tab's button (and its icon) is than the + /// others; it grows and shrinks with the selection tint, and its + /// neighbours keep their gaps to it. + static const double tabSelectedScale = 1.2; /// Duration of each top-bar island's entrance pop (Back, title, actions). static const Duration barEntranceDuration = Duration(milliseconds: 240); diff --git a/app/test/ui/adaptive/floating_bar_geometry_test.dart b/app/test/ui/adaptive/floating_bar_geometry_test.dart index 8cac0967..e5e7f54f 100644 --- a/app/test/ui/adaptive/floating_bar_geometry_test.dart +++ b/app/test/ui/adaptive/floating_bar_geometry_test.dart @@ -1,7 +1,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:pipelines_app/ui/motion/motion.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'package:pipelines_app/ui/adaptive/glass_pill.dart'; @@ -116,7 +115,12 @@ void main() { ); for (var index = 0; index < 3; index += 1) { final circle = button(tester, index); - expect(circle.size, Size(diameter, diameter)); + // Tab 0 is selected: its whole button is larger than the others. + final across = index == 0 + ? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) + : diameter; + expect(circle.width, moreOrLessEquals(across)); + expect(circle.height, moreOrLessEquals(across)); expect( tester .widget( @@ -129,9 +133,12 @@ void main() { isA(), ); final icon = tester.getRect(find.byIcon(icons[index])); - // Tab 0 is selected: its icon is a little larger than the others. final side = index == 0 - ? iconSize * Motion.tabSelectedIconScale + ? iconSize * + FloatingDestinationBar.selectedButtonSize( + cupertino: cupertino, + ) / + FloatingDestinationBar.buttonSize(cupertino: cupertino) : iconSize; expect(icon.width, moreOrLessEquals(side)); expect(icon.height, moreOrLessEquals(side)); @@ -156,10 +163,18 @@ void main() { final box = row(tester); expect(box.right, size.width - 16); expect(box.bottom, size.height - 16); - expect(box.height, diameter); + // As tall as the selected (larger) button. + expect( + box.height, + moreOrLessEquals( + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino), + ), + ); expect( box.width, - FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count), + moreOrLessEquals( + FloatingDestinationBar.rowWidth(cupertino: cupertino, tabs: count), + ), ); expect(button(tester, 0).left, box.left); expect(button(tester, count - 1).right, box.right); @@ -168,7 +183,11 @@ void main() { button(tester, index).left - button(tester, index - 1).right, moreOrLessEquals(gap), ); - expect(button(tester, index).top, box.top); + // Every button is centred on the row, whatever its size. + expect( + button(tester, index).center.dy, + moreOrLessEquals(box.center.dy), + ); } }); } @@ -229,9 +248,16 @@ void main() { final box = row(tester); expect(box.right, 320 - 16); expect(box.left, greaterThanOrEqualTo(16)); - final narrowed = (288 - 5 * diameter) / 4; + // Tab 4 is selected and larger; the gaps narrow around it. + final selected = FloatingDestinationBar.selectedButtonSize( + cupertino: cupertino, + ); + final narrowed = (288 - 4 * diameter - selected) / 4; for (var index = 1; index < 5; index += 1) { - expect(button(tester, index).width, diameter); + expect( + button(tester, index).width, + moreOrLessEquals(index == 4 ? selected : diameter), + ); expect( button(tester, index).left - button(tester, index - 1).right, moreOrLessEquals(narrowed < gap ? narrowed : gap), diff --git a/app/test/ui/adaptive/floating_tab_motion_test.dart b/app/test/ui/adaptive/floating_tab_motion_test.dart index 8af0e701..32db1326 100644 --- a/app/test/ui/adaptive/floating_tab_motion_test.dart +++ b/app/test/ui/adaptive/floating_tab_motion_test.dart @@ -1,7 +1,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:pipelines_app/ui/motion/motion.dart'; import 'package:pipelines_app/shell/adaptive_shell.dart'; import 'package:pipelines_app/shell/destination.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; @@ -117,7 +116,10 @@ void main() { for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 0); - expect(widthOf(tester, tab), moreOrLessEquals(size * 0.6)); + final rest = index == 0 + ? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) + : size; + expect(widthOf(tester, tab), moreOrLessEquals(rest * 0.6)); } await tester.pump(const Duration(milliseconds: 130)); final first = widthOf(tester, find.byKey(tabKey(0))); @@ -129,7 +131,13 @@ void main() { for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 1); - expect(widthOf(tester, tab), size); + // Tab 0 is selected, so it settles at the larger size. + expect( + widthOf(tester, tab), + index == 0 + ? FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) + : size, + ); } }); @@ -145,13 +153,12 @@ void main() { for (var index = 0; index < 3; index += 1) { final tab = find.byKey(tabKey(index)); expect(opacityOf(tester, tab), 1, reason: 'tab $index faded'); - // Only the press may shrink the tapped button, and only a - // little; a replayed pop starts at 0.6. + // The selection grows and shrinks buttons between their two + // sizes and the press sinks the tapped one a little; a replayed + // pop would start at 0.6. expect( widthOf(tester, tab), - index == target - ? greaterThan(size * 0.9) - : moreOrLessEquals(size), + greaterThan(size * 0.9), reason: 'tab $index popped again', ); } @@ -222,7 +229,11 @@ void main() { // At rest the selected icon is a little larger than the others. expect( iconWidth(tester, 2), - moreOrLessEquals(iconSize * Motion.tabSelectedIconScale), + moreOrLessEquals( + iconSize * + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) / + FloatingDestinationBar.buttonSize(cupertino: cupertino), + ), ); expect(iconWidth(tester, 0), moreOrLessEquals(iconSize)); expect(iconWidth(tester, 1), moreOrLessEquals(iconSize)); @@ -251,10 +262,18 @@ void main() { await tester.pump(); await tester.pump(const Duration(milliseconds: 120)); expect(widthOf(tester, tab), lessThan(size * 0.97)); - expect(widthOf(tester, find.byKey(tabKey(0))), size); + // Tab 0 is selected, so it is the larger button; the press leaves it. + expect( + widthOf(tester, find.byKey(tabKey(0))), + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino), + ); await gesture.up(); await tester.pumpAndSettle(); - expect(widthOf(tester, tab), size); + // The tap selected it: it springs back to the selected size. + expect( + widthOf(tester, tab), + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino), + ); }); testWidgets('$host: under reduced motion the tint jumps and the buttons ' @@ -273,7 +292,11 @@ void main() { // No bounce, and the selected icon jumps straight to its larger size. expect( iconWidth(tester, 2), - moreOrLessEquals(iconSize * Motion.tabSelectedIconScale), + moreOrLessEquals( + iconSize * + FloatingDestinationBar.selectedButtonSize(cupertino: cupertino) / + FloatingDestinationBar.buttonSize(cupertino: cupertino), + ), ); expect(iconWidth(tester, 0), moreOrLessEquals(iconSize)); }); @@ -313,7 +336,13 @@ void main() { reason: '${tab.name} faded switching to ${next.name}', ); if (tab != next) { - expect(widthOf(tester, finder), size, reason: tab.name); + // The previous selection shrinks back to its size; nothing + // pops again from 0.6. + expect( + widthOf(tester, finder), + greaterThan(size * 0.9), + reason: tab.name, + ); } } } diff --git a/app/test/ui/adaptive/top_bar_clearance_test.dart b/app/test/ui/adaptive/top_bar_clearance_test.dart index d8f4c342..60524023 100644 --- a/app/test/ui/adaptive/top_bar_clearance_test.dart +++ b/app/test/ui/adaptive/top_bar_clearance_test.dart @@ -187,7 +187,7 @@ void main() { final pill = tester.getRect(find.byKey(FloatingDestinationBar.rowKey)); expect(pill.top, 64); // The pill is the bar's own height: leftover media padding is not added. - expect(pill.height, FloatingDestinationBar.materialButtonSize); + expect(pill.height, FloatingDestinationBar.materialSelectedButtonSize); // Wide: sized to its tabs at the leading edge, under the title. expect(pill.left, FloatingDestinationBar.topMargin); expect(pill.width, lessThan(400));