diff --git a/app/lib/features/chat/ui/sessions/chat_adaptive.dart b/app/lib/features/chat/ui/sessions/chat_adaptive.dart index 4402b55c..2a94ce95 100644 --- a/app/lib/features/chat/ui/sessions/chat_adaptive.dart +++ b/app/lib/features/chat/ui/sessions/chat_adaptive.dart @@ -1,3 +1,4 @@ +import '../../../../ui/adaptive/segmented_bar_overflow.dart'; import '../../../../ui/theme/app_theme.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -13,7 +14,7 @@ ChatControlsStyle chatControlsStyleOf(BuildContext context) => ? ChatControlsStyle.cupertino : ChatControlsStyle.material; -class ChatBarAction extends StatelessWidget { +class ChatBarAction extends StatelessWidget implements SegmentedOverflowable { const ChatBarAction({ super.key, required this.label, @@ -27,6 +28,14 @@ class ChatBarAction extends StatelessWidget { final IconData materialIcon; final IconData cupertinoIcon; + /// A narrow bar moves it into its More menu, under the bar's host's icon. + @override + SegmentedOverflowEntry get overflowEntry => SegmentedOverflowEntry( + label: label, + icon: materialIcon, + onPressed: onPressed, + ); + @override Widget build(BuildContext context) { if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) { diff --git a/app/lib/features/chat/ui/sessions/session_row_content.dart b/app/lib/features/chat/ui/sessions/session_row_content.dart index 7dbdc00d..4bfdea62 100644 --- a/app/lib/features/chat/ui/sessions/session_row_content.dart +++ b/app/lib/features/chat/ui/sessions/session_row_content.dart @@ -62,7 +62,6 @@ class ChatSessionRow extends StatelessWidget { selected: selected, emphasized: unread, divider: divider, - keepMenuWhenCompact: true, onTap: () => onOpen(session), onLongPress: onLongPress, accent: chatColorShade(context, chatSessionColor(session)), diff --git a/app/lib/features/pipelines/screen.dart b/app/lib/features/pipelines/screen.dart index dbdc5aa5..b445dad9 100644 --- a/app/lib/features/pipelines/screen.dart +++ b/app/lib/features/pipelines/screen.dart @@ -327,24 +327,14 @@ class _PipelinesScreenState extends State { Widget _settingsButton(BuildContext context) => pipelineSettingsButton(context, widget.onOpenSettings); - Widget _refreshButton(BuildContext context) { - if (useCupertinoControls(context)) { - return Semantics( - button: true, - label: 'Refresh', - child: CupertinoButton( - padding: EdgeInsets.zero, - onPressed: _controller.load, - child: const Icon(CupertinoIcons.refresh), - ), - ); - } - return IconButton( - tooltip: 'Refresh', - onPressed: _controller.load, - icon: const Icon(Icons.refresh), - ); - } + Widget _refreshButton(BuildContext context) => pipelineBarIconButton( + context, + key: const Key('pipelines-refresh'), + label: 'Refresh', + icon: Icons.refresh, + cupertinoIcon: CupertinoIcons.refresh, + onPressed: _controller.load, + ); Widget _materialShell(BuildContext context, Widget body) { return Scaffold( diff --git a/app/lib/features/pipelines/widgets/pipeline_bar_buttons.dart b/app/lib/features/pipelines/widgets/pipeline_bar_buttons.dart index ed0c2e8c..91c78725 100644 --- a/app/lib/features/pipelines/widgets/pipeline_bar_buttons.dart +++ b/app/lib/features/pipelines/widgets/pipeline_bar_buttons.dart @@ -1,11 +1,12 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import '../../../ui/adaptive/segmented_bar_overflow.dart'; import 'pipeline_controls.dart'; /// One icon action in the Pipelines bar's actions pill: a Material /// [IconButton] with its tooltip, or a Cupertino button named for -/// accessibility. +/// accessibility. A narrow bar moves it into its More menu. Widget pipelineBarIconButton( BuildContext context, { required Key key, @@ -15,7 +16,8 @@ Widget pipelineBarIconButton( required VoidCallback? onPressed, }) { final Widget button; - if (useCupertinoControls(context)) { + final cupertino = useCupertinoControls(context); + if (cupertino) { button = CupertinoButton( key: key, padding: EdgeInsets.zero, @@ -30,10 +32,17 @@ Widget pipelineBarIconButton( icon: Icon(icon), ); } - return Semantics( - button: true, - label: label, - child: ExcludeSemantics(child: button), + return SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: label, + icon: cupertino ? cupertinoIcon : icon, + onPressed: onPressed, + ), + child: Semantics( + button: true, + label: label, + child: ExcludeSemantics(child: button), + ), ); } diff --git a/app/lib/features/pipelines/widgets/pipeline_list_panel.dart b/app/lib/features/pipelines/widgets/pipeline_list_panel.dart index b4cac27a..e3744358 100644 --- a/app/lib/features/pipelines/widgets/pipeline_list_panel.dart +++ b/app/lib/features/pipelines/widgets/pipeline_list_panel.dart @@ -5,6 +5,7 @@ import 'package:flutter/material.dart'; import '../../../shell/shell_list_shortcuts.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; +import '../../../ui/adaptive/segmented_bar_overflow.dart'; import '../../../ui/status/run_status_icon.dart'; import '../pipeline_format.dart'; import '../pipeline_models.dart'; @@ -322,7 +323,13 @@ List pipelineListActions( final label = filters.count == 0 ? 'Filter pipelines' : '${filters.count} active ${filters.count == 1 ? 'filter' : 'filters'}'; - final filter = useCupertinoControls(context) + final cupertino = useCupertinoControls(context); + final filterIcon = cupertino + ? (filters.count > 0 + ? CupertinoIcons.line_horizontal_3_decrease_circle_fill + : CupertinoIcons.line_horizontal_3_decrease_circle) + : (filters.count > 0 ? Icons.filter_alt : Icons.filter_alt_outlined); + final filter = cupertino ? Semantics( button: true, label: label, @@ -332,11 +339,7 @@ List pipelineListActions( padding: EdgeInsets.zero, minimumSize: const Size.square(kMinInteractiveDimensionCupertino), onPressed: openFilters, - child: Icon( - filters.count > 0 - ? CupertinoIcons.line_horizontal_3_decrease_circle_fill - : CupertinoIcons.line_horizontal_3_decrease_circle, - ), + child: Icon(filterIcon), ), ) : IconButton( @@ -344,17 +347,38 @@ List pipelineListActions( tooltip: label, isSelected: filters.count > 0, onPressed: openFilters, - icon: Icon( - filters.count > 0 ? Icons.filter_alt : Icons.filter_alt_outlined, - ), + icon: Icon(filterIcon), ); + // A narrow bar moves either into its More menu; sorting is then a + // submenu (Material) or a second action sheet (Cupertino). return [ - filter, - PipelineSortButton( - key: const Key('pipeline-sort'), - sort: controller.sort, - onSelected: controller.setSort, - icon: true, + SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: label, + icon: filterIcon, + onPressed: openFilters, + ), + child: filter, + ), + SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: 'Sort pipelines: ${controller.sort.label}', + icon: cupertino ? CupertinoIcons.arrow_up_arrow_down : Icons.sort, + choices: [ + for (final option in PipelineSort.values) + SegmentedMenuChoice( + label: option.label, + selected: option == controller.sort, + onSelected: () => controller.setSort(option), + ), + ], + ), + child: PipelineSortButton( + key: const Key('pipeline-sort'), + sort: controller.sort, + onSelected: controller.setSort, + icon: true, + ), ), ]; } diff --git a/app/lib/features/runs/ui/run_list_row.dart b/app/lib/features/runs/ui/run_list_row.dart index b905802f..36f1d065 100644 --- a/app/lib/features/runs/ui/run_list_row.dart +++ b/app/lib/features/runs/ui/run_list_row.dart @@ -113,8 +113,8 @@ class RunListRow extends StatelessWidget { : RelativeTimeLabel(instant: createdAt, now: now), subtitle: Text(subtitle), compactSubtitle: Text(compactSubtitle), - // A narrow list pane beside the open run leaves the row no room for a - // second control; the open run's detail beside it carries the same menu. + // Kept in a narrow list pane too, so every run's actions stay one tap + // away from the list. menu: !selecting && gateway != null && diff --git a/app/lib/features/runs/ui/runs_filters.dart b/app/lib/features/runs/ui/runs_filters.dart index 8245a399..6b6db935 100644 --- a/app/lib/features/runs/ui/runs_filters.dart +++ b/app/lib/features/runs/ui/runs_filters.dart @@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; +import '../../../ui/adaptive/segmented_bar_overflow.dart'; import '../../../ui/layout/app_sheet_header.dart'; import '../../pipelines/pipeline_format.dart' show runStateLabel; import 'runs_controller.dart'; @@ -160,8 +161,9 @@ class RunsBarActions { VoidCallback onPressed, { bool active = false, }) { + final Widget button; if (cupertino) { - return Semantics( + button = Semantics( button: true, label: label, excludeSemantics: true, @@ -175,13 +177,23 @@ class RunsBarActions { child: Icon(ios), ), ); + } else { + button = IconButton( + key: key, + tooltip: label, + isSelected: active, + onPressed: onPressed, + icon: Icon(material), + ); } - return IconButton( - key: key, - tooltip: label, - isSelected: active, - onPressed: onPressed, - icon: Icon(material), + // A narrow bar moves it into its More menu. + return SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: label, + icon: cupertino ? ios : material, + onPressed: onPressed, + ), + child: button, ); } diff --git a/app/lib/features/runs/ui/runs_selection_bar.dart b/app/lib/features/runs/ui/runs_selection_bar.dart index ecce474b..38aa7c8c 100644 --- a/app/lib/features/runs/ui/runs_selection_bar.dart +++ b/app/lib/features/runs/ui/runs_selection_bar.dart @@ -3,6 +3,7 @@ import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../../../ui/adaptive/host.dart'; +import '../../../ui/adaptive/segmented_bar_overflow.dart'; import '../../../ui/layout/section_heading.dart'; import '../actions/delete_runs_action.dart'; import '../domain/run_deletion.dart'; @@ -24,29 +25,39 @@ ValueKey runsDaySelectKey(String day) => /// The Runs bar's `Select` action, entering multi-select. Widget runsSelectAction(BuildContext context, RunsController controller) { void enter() => controller.selection.enter(); - if (usesCupertinoHost(context)) { - return Semantics( - button: true, + final cupertino = usesCupertinoHost(context); + // A narrow bar moves it into its More menu. + return SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( label: 'Select', - excludeSemantics: true, - onTap: enter, - child: CupertinoButton( - key: runsSelectKey, - padding: EdgeInsets.zero, - minimumSize: const Size.square(kMinInteractiveDimensionCupertino), - onPressed: enter, - child: const Icon(CupertinoIcons.checkmark_circle), - ), - ); - } - return IconButton( - key: runsSelectKey, - tooltip: 'Select', - onPressed: enter, - icon: const Icon(Icons.checklist), + icon: cupertino ? CupertinoIcons.checkmark_circle : Icons.checklist, + onPressed: enter, + ), + child: cupertino + ? _selectButtonCupertino(enter) + : IconButton( + key: runsSelectKey, + tooltip: 'Select', + onPressed: enter, + icon: const Icon(Icons.checklist), + ), ); } +Widget _selectButtonCupertino(VoidCallback enter) => Semantics( + button: true, + label: 'Select', + excludeSemantics: true, + onTap: enter, + child: CupertinoButton( + key: runsSelectKey, + padding: EdgeInsets.zero, + minimumSize: const Size.square(kMinInteractiveDimensionCupertino), + onPressed: enter, + child: const Icon(CupertinoIcons.checkmark_circle), + ), +); + /// What the bar shows in select mode: `N selected` over the selection's size, /// then Delete and Done. ({String title, String? subtitle, List actions}) runsSelectionBar( diff --git a/app/lib/features/schedules/ui/pipeline/schedule_pipeline_row.dart b/app/lib/features/schedules/ui/pipeline/schedule_pipeline_row.dart index 6dff8706..1b9b52f7 100644 --- a/app/lib/features/schedules/ui/pipeline/schedule_pipeline_row.dart +++ b/app/lib/features/schedules/ui/pipeline/schedule_pipeline_row.dart @@ -52,7 +52,6 @@ class SchedulePipelineRow extends StatelessWidget { '${scheduleRuleSummary(schedule)}', ), // A narrow Summary still needs a way to pause or delete. - keepMenuWhenCompact: true, menu: AppOverflowMenu<_ScheduleAction>( key: scheduleRowMenuKey(id), cupertino: cupertino, diff --git a/app/lib/ui/adaptive/segmented_action_row.dart b/app/lib/ui/adaptive/segmented_action_row.dart new file mode 100644 index 00000000..411fbb53 --- /dev/null +++ b/app/lib/ui/adaptive/segmented_action_row.dart @@ -0,0 +1,289 @@ +import 'dart:math' as math; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:flutter/widgets.dart'; + +/// A bar's actions in one row, each at its natural width, with a trailing +/// [more] button that shows only when they do not all fit. +/// +/// When the actions are wider than [overflowWidth] (the room the bar leaves +/// them beside a readable title), the [overflowable] ones leave the row from +/// its trailing end until the rest and [more] fit in it, so +/// every action left keeps its own (touch-target) width instead of being +/// squeezed. [onHidden] hears which actions left (by index) after a layout +/// that changes them, so the bar can list them in its More menu. Only when +/// the actions that stay are wider than the row may be at all (a long model +/// name, which cannot move) does the first of them shrink, as a [Flexible] +/// would, so the row never overflows. +class SegmentedActionRow extends MultiChildRenderObjectWidget { + SegmentedActionRow({ + super.key, + required List actions, + required Widget more, + required this.overflowable, + required this.overflowWidth, + required this.onHidden, + }) : assert(actions.length == overflowable.length), + super(children: [...actions, more]); + + /// Whether each action may move into the More menu. + final List overflowable; + + /// Past this width, [overflowable] actions move into the More menu. + final double overflowWidth; + final ValueChanged> onHidden; + + @override + RenderSegmentedActionRow createRenderObject(BuildContext context) => + RenderSegmentedActionRow( + overflowable: overflowable, + overflowWidth: overflowWidth, + onHidden: onHidden, + textDirection: Directionality.of(context), + ); + + @override + void updateRenderObject( + BuildContext context, + RenderSegmentedActionRow renderObject, + ) { + renderObject + ..overflowable = overflowable + ..overflowWidth = overflowWidth + ..onHidden = onHidden + ..textDirection = Directionality.of(context); + } +} + +class _ActionParentData extends ContainerBoxParentData { + bool shown = true; +} + +/// The render object of [SegmentedActionRow]. +class RenderSegmentedActionRow extends RenderBox + with + ContainerRenderObjectMixin, + RenderBoxContainerDefaultsMixin { + RenderSegmentedActionRow({ + required this._overflowable, + required this._overflowWidth, + required this.onHidden, + required this._textDirection, + }); + + List _overflowable; + set overflowable(List value) { + if (listEquals(value, _overflowable)) return; + _overflowable = value; + markNeedsLayout(); + } + + double _overflowWidth; + set overflowWidth(double value) { + if (value == _overflowWidth) return; + _overflowWidth = value; + markNeedsLayout(); + } + + ValueChanged> onHidden; + + TextDirection _textDirection; + set textDirection(TextDirection value) { + if (value == _textDirection) return; + _textDirection = value; + markNeedsLayout(); + } + + /// What the last layout reported to [onHidden]; nothing is hidden at + /// first, so a bar whose actions fit reports nothing. + List _reported = const []; + + @override + void setupParentData(RenderBox child) { + if (child.parentData is! _ActionParentData) { + child.parentData = _ActionParentData(); + } + } + + List get _children { + final list = []; + var child = firstChild; + while (child != null) { + list.add(child); + child = childAfter(child); + } + return list; + } + + /// Which actions show, whether More shows, and the most the first shown + /// action may take (infinite when everything fits at its natural width). + ({List shown, bool more, double firstMax}) _plan( + List widths, + double moreWidth, + double maxWidth, + ) { + final count = widths.length; + final shown = List.filled(count, true); + var total = widths.fold(0.0, (sum, width) => sum + width); + var more = false; + final soft = math.min(_overflowWidth, maxWidth); + if (total > soft && _overflowable.contains(true)) { + more = true; + for (var index = count - 1; index >= 0; index--) { + if (total + moreWidth <= soft) break; + if (!_overflowable[index]) continue; + shown[index] = false; + total -= widths[index]; + } + } + final room = maxWidth - (more ? moreWidth : 0); + final first = shown.indexOf(true); + if (total <= room || first < 0) { + return (shown: shown, more: more, firstMax: double.infinity); + } + return ( + shown: shown, + more: more, + firstMax: math.max(0, room - (total - widths[first])), + ); + } + + Size _layout(BoxConstraints constraints, {required bool dry}) { + final children = _children; + final actions = children.sublist(0, children.length - 1); + final moreChild = children.last; + final loose = BoxConstraints(maxHeight: constraints.maxHeight); + Size size(RenderBox child, BoxConstraints box) => dry + ? child.getDryLayout(box) + : (child..layout(box, parentUsesSize: true)).size; + final sizes = [for (final child in actions) size(child, loose)]; + final moreSize = size(moreChild, loose); + final plan = _plan( + [for (final s in sizes) s.width], + moreSize.width, + constraints.maxWidth, + ); + final first = plan.shown.indexOf(true); + if (first >= 0 && plan.firstMax.isFinite) { + sizes[first] = size( + actions[first], + loose.copyWith(maxWidth: plan.firstMax), + ); + } + final placed = [ + for (final (index, s) in sizes.indexed) + if (plan.shown[index]) (actions[index], s), + if (plan.more) (moreChild, moreSize), + ]; + final height = placed.fold( + 0.0, + (most, entry) => math.max(most, entry.$2.height), + ); + final width = placed.fold(0.0, (sum, entry) => sum + entry.$2.width); + final result = constraints.constrain(Size(width, height)); + if (dry) return result; + for (final child in children) { + (child.parentData! as _ActionParentData).shown = false; + } + var x = 0.0; + for (final (child, s) in placed) { + final data = child.parentData! as _ActionParentData; + data.shown = true; + final dx = _textDirection == TextDirection.ltr + ? x + : result.width - x - s.width; + data.offset = Offset(dx, (result.height - s.height) / 2); + x += s.width; + } + final hidden = [ + for (final (index, shown) in plan.shown.indexed) + if (!shown) index, + ]; + if (!listEquals(hidden, _reported)) { + _reported = hidden; + final report = onHidden; + // The bar rebuilds its More menu from this, which it may not do in + // the middle of a layout. + SchedulerBinding.instance.addPostFrameCallback((_) => report(hidden)); + } + return result; + } + + @override + void performLayout() { + size = _layout(constraints, dry: false); + } + + @override + Size computeDryLayout(covariant BoxConstraints constraints) => + _layout(constraints, dry: true); + + Iterable get _actionChildren => + _children.sublist(0, childCount - 1); + + @override + double computeMinIntrinsicWidth(double height) { + final children = _children; + // At its narrowest: the actions that cannot move, and More. + var width = children.last.getMinIntrinsicWidth(height); + for (final (index, child) in _actionChildren.indexed) { + if (!_overflowable[index]) width += child.getMinIntrinsicWidth(height); + } + return width; + } + + @override + double computeMaxIntrinsicWidth(double height) => _actionChildren.fold( + 0.0, + (sum, child) => sum + child.getMaxIntrinsicWidth(height), + ); + + @override + double computeMinIntrinsicHeight(double width) => _children.fold( + 0.0, + (most, child) => math.max(most, child.getMinIntrinsicHeight(width)), + ); + + @override + double computeMaxIntrinsicHeight(double width) => _children.fold( + 0.0, + (most, child) => math.max(most, child.getMaxIntrinsicHeight(width)), + ); + + bool _shown(RenderBox child) => + (child.parentData! as _ActionParentData).shown; + + @override + void paint(PaintingContext context, Offset offset) { + for (final child in _children) { + if (!_shown(child)) continue; + final data = child.parentData! as _ActionParentData; + context.paintChild(child, offset + data.offset); + } + } + + @override + bool hitTestChildren(BoxHitTestResult result, {required Offset position}) { + for (final child in _children.reversed) { + if (!_shown(child)) continue; + final data = child.parentData! as _ActionParentData; + final hit = result.addWithPaintOffset( + offset: data.offset, + position: position, + hitTest: (result, transformed) => + child.hitTest(result, position: transformed), + ); + if (hit) return true; + } + return false; + } + + @override + void visitChildrenForSemantics(RenderObjectVisitor visitor) { + for (final child in _children) { + if (_shown(child)) visitor(child); + } + } +} diff --git a/app/lib/ui/adaptive/segmented_bar.dart b/app/lib/ui/adaptive/segmented_bar.dart index 34fc4bd7..0ac2948f 100644 --- a/app/lib/ui/adaptive/segmented_bar.dart +++ b/app/lib/ui/adaptive/segmented_bar.dart @@ -1,13 +1,23 @@ import 'dart:math' as math; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'pop_entrance.dart'; +import 'segmented_action_row.dart'; import 'segmented_bar_entrance.dart'; +import 'segmented_bar_overflow.dart'; import 'segmented_bar_motion.dart'; import 'segmented_bar_pieces.dart'; +export 'segmented_bar_overflow.dart' + show + SegmentedMenuChoice, + SegmentedOverflowAction, + SegmentedOverflowEntry, + SegmentedOverflowable, + segmentedMoreKey; export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill; /// The app's top bar: separate frosted-glass pieces floating over the page's @@ -17,9 +27,11 @@ export 'segmented_bar_pieces.dart' show SegmentedBackButton, SegmentedPill; /// Back, implied as the stock bars do when [automaticallyImplyLeading]), a /// pill with the [title] and an optional one-line [subtitle] that runs /// [onTitlePressed] when given, and a pill holding the [actions] at the -/// trailing edge. The actions take their natural width first and the title -/// shrinks; only when the title is down to [minTitleShare] of the bar does -/// the first action ellipsise. +/// trailing edge. The title keeps its own width (up to [maxTitleShare] of the +/// bar, and never under [minTitleShare]); the actions take their natural +/// width beside it, and those that do not fit move into one More menu +/// (when they are [SegmentedOverflowable]) rather than shrink below their +/// touch targets. Only actions that cannot move ellipsise the first of them. /// /// A page whose bar is mainly an input (Search) passes [titleWidget] instead: /// it takes the title pill's place as one pill that fills the space beside @@ -123,9 +135,14 @@ class SegmentedBar extends StatefulWidget static const double _topPadding = 8; - /// The share of the bar the title keeps before the actions shrink. + /// The share of the bar the title always keeps beside the actions. static const double minTitleShare = 0.25; + /// The most of the bar the title's own width claims before the actions: + /// a short title (a destination's name) shows whole, a long one leaves + /// the actions room. + static const double maxTitleShare = 0.5; + /// Bar text grows with the reader's setting up to this factor, like the /// stock app bar caps its title, so the bar never takes over the page. static const double maxTextScale = 2; @@ -262,42 +279,70 @@ class _SegmentedBarState extends State { ); } + /// Which actions (by identity) a narrow bar moved into its More menu. + Set _overflowed = const {}; + + void _hidden(List ids, List indexes) { + if (!mounted) return; + final next = { + for (final index in indexes) + if (index < ids.length) ids[index], + }; + if (setEquals(next, _overflowed)) return; + setState(() => _overflowed = next); + } + /// The actions, each keyed by its identity ([segmentedActionIds]) so one - /// arriving or leaving leaves the others be, each popping in on its own. - /// Only the first shrinks (ellipsises); the rest keep their natural width - /// as inflexible (flex 0) children, up to an even share of [maxWidth] (the - /// pill's widest): a long control (a permission mode's name beside the - /// model) leaves room for the first, and icons in a narrow pane squeeze - /// together rather than overflow the pill. - List _actionIslands( + /// arriving or leaving leaves the others be, each popping in on its own, + /// at its natural width. Those that do not fit leave for the trailing More + /// button ([SegmentedActionRow]), so none is squeezed below its touch + /// target; one moved there is out of the focus order too. + Widget _actionRow( List actions, List ids, List entrances, { required bool cupertino, - required double maxWidth, - }) => [ - for (final (index, action) in actions.indexed) - Flexible( - key: ValueKey(ids[index]), - flex: index == 0 ? 1 : 0, - child: ConstrainedBox( - constraints: BoxConstraints( - maxWidth: index == 0 ? double.infinity : maxWidth / actions.length, - ), - child: PopEntrance( - play: entrances[index].play, - delay: entrances[index].delay, - glass: false, - child: SegmentedPress( - cupertino: cupertino, - tint: widget.foregroundColor, - enabled: actions.length > 1, - child: action, - ), - ), + required double overflowWidth, + }) { + final entries = [ + for (final (index, action) in actions.indexed) + if (action is SegmentedOverflowable && _overflowed.contains(ids[index])) + (action as SegmentedOverflowable).overflowEntry, + ]; + return SegmentedActionRow( + overflowable: [ + for (final action in actions) action is SegmentedOverflowable, + ], + overflowWidth: overflowWidth, + onHidden: (indexes) => _hidden(ids, indexes), + more: ExcludeFocus( + excluding: entries.isEmpty, + child: SegmentedPress( + cupertino: cupertino, + tint: widget.foregroundColor, + child: SegmentedMoreButton(cupertino: cupertino, entries: entries), ), ), - ]; + actions: [ + for (final (index, action) in actions.indexed) + ExcludeFocus( + key: ValueKey(ids[index]), + excluding: _overflowed.contains(ids[index]), + child: PopEntrance( + play: entrances[index].play, + delay: entrances[index].delay, + glass: false, + child: SegmentedPress( + cupertino: cupertino, + tint: widget.foregroundColor, + enabled: actions.length > 1, + child: action, + ), + ), + ), + ], + ); + } @override Widget build(BuildContext context) { @@ -349,15 +394,39 @@ class _SegmentedBarState extends State { final Widget bar = LayoutBuilder( builder: (context, constraints) { // The actions keep to the trailing edge at their natural width, so - // a model name shows whole while the title has room; only past the - // title's minimum share does the first action ellipsise. + // a model name shows whole while the title has room. The title keeps + // at least [SegmentedBar.minTitleShare] of the bar, and its own + // width (a destination's name) up to [SegmentedBar.maxTitleShare]; + // actions that do not fit beside it move into the More menu. + final width = constraints.maxWidth; + final titleMinimum = titleWidget != null || !showTitle + ? width * SegmentedBar.minTitleShare + : math.max( + width * SegmentedBar.minTitleShare, + math.min( + segmentedTitleWidth( + context, + cupertino: cupertino, + title: title, + subtitle: subtitle, + subtitleIcon: subtitleIcon, + foreground: foregroundColor, + ), + width * SegmentedBar.maxTitleShare, + ), + ); + final besideTitle = + width - + (leading == null ? 0 : segmentedPieceHeight(cupertino) + 8) - + 8; + // Actions that cannot move (a model picker) may take the room down + // to the title's minimum share; the rest leave for the More menu + // before the title goes under its own width. final actionsMaxWidth = math.max( 0.0, - constraints.maxWidth - - (leading == null ? 0 : segmentedPieceHeight(cupertino) + 8) - - 8 - - constraints.maxWidth * SegmentedBar.minTitleShare, + besideTitle - width * SegmentedBar.minTitleShare, ); + final overflowWidth = math.max(0.0, besideTitle - titleMinimum); return Row( children: [ if (leading != null) ...[ @@ -411,15 +480,12 @@ class _SegmentedBarState extends State { child: SegmentedPill( cupertino: cupertino, fill: pillColor, - child: Row( - mainAxisSize: MainAxisSize.min, - children: _actionIslands( - actions, - actionIds, - entrances.actions, - cupertino: cupertino, - maxWidth: actionsMaxWidth, - ), + child: _actionRow( + actions, + actionIds, + entrances.actions, + cupertino: cupertino, + overflowWidth: overflowWidth, ), ), ), diff --git a/app/lib/ui/adaptive/segmented_bar_entrance.dart b/app/lib/ui/adaptive/segmented_bar_entrance.dart index e414c4d3..27ce6e60 100644 --- a/app/lib/ui/adaptive/segmented_bar_entrance.dart +++ b/app/lib/ui/adaptive/segmented_bar_entrance.dart @@ -1,24 +1,34 @@ import 'package:flutter/widgets.dart'; import '../motion/motion.dart'; +import 'segmented_bar_overflow.dart'; /// The identity of each of a bar's [actions]: its own key, or its type and /// place among actions of that type, within [group] (whoever supplied the /// actions). An action keeps its identity, and so its entrance, while the -/// bar rebuilds; a new [group] makes every action new. +/// bar rebuilds; a new [group] makes every action new. A +/// [SegmentedOverflowAction] is known by the action it wraps. List segmentedActionIds(List actions, Object? group) { final seen = {}; return [ for (final action in actions) - ( - 'segmented.action', - group, - action.key ?? - ( - action.runtimeType, - seen.update(action.runtimeType, (n) => n + 1, ifAbsent: () => 0), - ), - ), + if ((action is SegmentedOverflowAction && action.key == null + ? action.child + : action) + case final identity) + ( + 'segmented.action', + group, + identity.key ?? + ( + identity.runtimeType, + seen.update( + identity.runtimeType, + (n) => n + 1, + ifAbsent: () => 0, + ), + ), + ), ]; } diff --git a/app/lib/ui/adaptive/segmented_bar_overflow.dart b/app/lib/ui/adaptive/segmented_bar_overflow.dart new file mode 100644 index 00000000..c444a8fb --- /dev/null +++ b/app/lib/ui/adaptive/segmented_bar_overflow.dart @@ -0,0 +1,175 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +/// The key of a [SegmentedBar]'s More button, shown when its actions do not +/// all fit. +const segmentedMoreKey = ValueKey('segmented.more'); + +/// One choice under a [SegmentedOverflowEntry] (a sort order): a submenu on +/// Material, a second action sheet on Cupertino. +class SegmentedMenuChoice { + const SegmentedMenuChoice({ + required this.label, + required this.onSelected, + this.selected = false, + }); + + final String label; + final VoidCallback onSelected; + final bool selected; +} + +/// How a bar action reads in the bar's More menu once a narrow bar moves it +/// there: its label and icon, and what choosing it does. +class SegmentedOverflowEntry { + const SegmentedOverflowEntry({ + required this.label, + required this.icon, + this.onPressed, + this.choices = const [], + }); + + final String label; + final IconData icon; + + /// What choosing the entry does; null (with no [choices]) disables it. + final VoidCallback? onPressed; + + /// Choices offered under the entry instead of [onPressed]. + final List choices; +} + +/// A bar action the bar may move into its More menu. An action that is not +/// [SegmentedOverflowable] always stays in the bar. +abstract interface class SegmentedOverflowable { + SegmentedOverflowEntry get overflowEntry; +} + +/// Marks any bar action as [SegmentedOverflowable]: it draws [child] in the +/// bar and [overflowEntry] in the More menu. +class SegmentedOverflowAction extends StatelessWidget + implements SegmentedOverflowable { + const SegmentedOverflowAction({ + super.key, + required this.overflowEntry, + required this.child, + }); + + @override + final SegmentedOverflowEntry overflowEntry; + final Widget child; + + @override + Widget build(BuildContext context) => child; +} + +/// The bar's More button: the host's overflow icon at the platform's touch +/// target, opening a Material menu or a Cupertino action sheet of [entries]. +class SegmentedMoreButton extends StatelessWidget { + const SegmentedMoreButton({ + super.key, + required this.cupertino, + required this.entries, + }); + + final bool cupertino; + final List entries; + + static const String label = 'More actions'; + + @override + Widget build(BuildContext context) { + if (cupertino) { + return Semantics( + button: true, + label: label, + excludeSemantics: true, + child: CupertinoButton( + key: segmentedMoreKey, + padding: EdgeInsets.zero, + minimumSize: const Size.square(kMinInteractiveDimensionCupertino), + onPressed: () => _showSheet(context), + child: const Icon(CupertinoIcons.ellipsis), + ), + ); + } + return MenuAnchor( + menuChildren: [ + for (final entry in entries) + if (entry.choices.isEmpty) + MenuItemButton( + leadingIcon: Icon(entry.icon), + onPressed: entry.onPressed, + child: Text(entry.label), + ) + else + SubmenuButton( + leadingIcon: Icon(entry.icon), + menuChildren: [ + for (final choice in entry.choices) + MenuItemButton( + leadingIcon: Icon( + Icons.check, + color: choice.selected ? null : Colors.transparent, + ), + onPressed: choice.onSelected, + child: Semantics( + selected: choice.selected, + child: Text(choice.label), + ), + ), + ], + child: Text(entry.label), + ), + ], + builder: (context, menu, _) => IconButton( + key: segmentedMoreKey, + tooltip: label, + onPressed: () => menu.isOpen ? menu.close() : menu.open(), + icon: const Icon(Icons.more_vert), + ), + ); + } + + Future _showSheet(BuildContext context) async { + final picked = await _sheet(context, [ + for (final entry in entries) + if (entry.onPressed != null || entry.choices.isNotEmpty) + (entry.label, false, entry), + ]); + if (picked == null || !context.mounted) return; + if (picked.choices.isEmpty) { + picked.onPressed?.call(); + return; + } + final choice = await _sheet(context, [ + for (final choice in picked.choices) + (choice.label, choice.selected, choice), + ], title: picked.label); + choice?.onSelected(); + } + + Future _sheet( + BuildContext context, + List<(String, bool, T)> items, { + String? title, + }) => showCupertinoModalPopup( + context: context, + builder: (sheet) => CupertinoActionSheet( + title: title == null ? null : Text(title), + actions: [ + for (final (label, selected, value) in items) + CupertinoActionSheetAction( + // The current choice is the sheet's default (bold) action. + isDefaultAction: selected, + onPressed: () => Navigator.of(sheet).pop(value), + child: Semantics(selected: selected, child: Text(label)), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.of(sheet).pop(), + child: const Text('Cancel'), + ), + ), + ); +} diff --git a/app/lib/ui/adaptive/segmented_bar_pieces.dart b/app/lib/ui/adaptive/segmented_bar_pieces.dart index 77dd7ba6..6f597b17 100644 --- a/app/lib/ui/adaptive/segmented_bar_pieces.dart +++ b/app/lib/ui/adaptive/segmented_bar_pieces.dart @@ -83,6 +83,81 @@ Widget? impliedSegmentedLeading(BuildContext context, bool cupertino) { return dialog ? const CloseButton() : const BackButton(); } +/// The title pill's title and subtitle styles, from the host's type scale. +(TextStyle, TextStyle) segmentedTitleStyles( + BuildContext context, { + required bool cupertino, + Color? foreground, +}) { + if (cupertino) { + final text = CupertinoTheme.of(context).textTheme; + return ( + text.textStyle.copyWith(fontWeight: FontWeight.w600, color: foreground), + text.textStyle.copyWith( + fontSize: 13, + color: + foreground ?? CupertinoColors.secondaryLabel.resolveFrom(context), + ), + ); + } + final theme = Theme.of(context); + return ( + (theme.textTheme.titleSmall ?? const TextStyle()).copyWith( + color: foreground ?? theme.colorScheme.onSurface, + fontWeight: FontWeight.w600, + ), + (theme.textTheme.labelSmall ?? const TextStyle()).copyWith( + color: foreground ?? theme.colorScheme.onSurfaceVariant, + ), + ); +} + +/// The title pill's horizontal padding on each side. +const double segmentedTitlePadding = 16; + +/// The width the title pill needs to show [title] and [subtitle] whole (at +/// the text scale at [context]). +double segmentedTitleWidth( + BuildContext context, { + required bool cupertino, + required String title, + String? subtitle, + IconData? subtitleIcon, + Color? foreground, +}) { + final (titleStyle, subtitleStyle) = segmentedTitleStyles( + context, + cupertino: cupertino, + foreground: foreground, + ); + final scaler = MediaQuery.textScalerOf(context); + final direction = Directionality.of(context); + double measure(String text, TextStyle style) { + if (text.isEmpty) return 0; + final painter = TextPainter( + text: TextSpan(text: text, style: style), + textDirection: direction, + textScaler: scaler, + maxLines: 1, + )..layout(); + final width = painter.width; + painter.dispose(); + return width; + } + + final sub = subtitle ?? ''; + final subWidth = sub.isEmpty + ? 0.0 + : measure(sub, subtitleStyle) + + (subtitleIcon == null + ? 0 + : scaler.scale(subtitleStyle.fontSize ?? 12) + + measure(' ', subtitleStyle)); + final text = measure(title, titleStyle); + return (text > subWidth ? text : subWidth).ceilToDouble() + + segmentedTitlePadding * 2; +} + /// The title, with an optional one-line subtitle, in one pill; pressing it /// runs [onPressed] when given. class SegmentedTitlePill extends StatelessWidget { @@ -117,31 +192,17 @@ class SegmentedTitlePill extends StatelessWidget { @override Widget build(BuildContext context) { - final TextStyle titleStyle; - final TextStyle subtitleStyle; - if (cupertino) { - final text = CupertinoTheme.of(context).textTheme; - titleStyle = text.textStyle.copyWith( - fontWeight: FontWeight.w600, - color: foreground, - ); - subtitleStyle = text.textStyle.copyWith( - fontSize: 13, - color: - foreground ?? CupertinoColors.secondaryLabel.resolveFrom(context), - ); - } else { - final theme = Theme.of(context); - titleStyle = (theme.textTheme.titleSmall ?? const TextStyle()).copyWith( - color: foreground ?? theme.colorScheme.onSurface, - fontWeight: FontWeight.w600, - ); - subtitleStyle = (theme.textTheme.labelSmall ?? const TextStyle()) - .copyWith(color: foreground ?? theme.colorScheme.onSurfaceVariant); - } + final (titleStyle, subtitleStyle) = segmentedTitleStyles( + context, + cupertino: cupertino, + foreground: foreground, + ); final subtitle = this.subtitle ?? ''; final content = Padding( - padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4), + padding: const EdgeInsets.symmetric( + horizontal: segmentedTitlePadding, + vertical: 4, + ), child: Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.center, diff --git a/app/lib/ui/layout/app_list_row.dart b/app/lib/ui/layout/app_list_row.dart index 1579d11d..879ff312 100644 --- a/app/lib/ui/layout/app_list_row.dart +++ b/app/lib/ui/layout/app_list_row.dart @@ -5,13 +5,20 @@ import '../adaptive/host.dart'; import 'app_list_avatar.dart'; /// Below this row width (a list pane squeezed beside its detail) a row is -/// compact: no avatar, no time and (by default) no menu, so the title keeps -/// the width and a tap on the row never lands on a second control. +/// compact: it keeps its avatar (a run's status) and its menu, so no row +/// loses its state or its actions, and moves the time under the subtitle, +/// leaving the title the line's width. const double kAppListRowCompactWidth = 280; +/// Below this row width (narrower than any list pane, as in a row squeezed +/// by its host) the avatar and the menu would leave the text no room, so the +/// row drops them rather than overflow. +const double kAppListRowMinimalWidth = 160; + /// The rows' horizontal inset and the gap after the avatar. const double kAppListRowInset = 16; const double _avatarGap = 12; +const double _compactAvatarGap = 8; /// Keys on a row's highlight surface and inset divider, for tests. const appListRowSurfaceKey = ValueKey('app-list-row-surface'); @@ -29,8 +36,9 @@ bool _darkSurface(BuildContext context, bool cupertino) => cupertino /// the [time] at its trailing edge, then one muted [subtitle] line (with an /// optional [subtitleLeading] dot/prefix and [subtitleTrailing] mark), then /// the optional [menu] (an `AppOverflowMenu`). A row in a multi-select list -/// puts its checkbox in [selectionControl], which leads the row even when it -/// is compact. An inset hairline [divider] +/// puts its checkbox in [selectionControl], which leads the row. A compact +/// row (narrower than [compactWidth]) keeps all of these and shows the time +/// on a line of its own under the subtitle. An inset hairline [divider] /// under the text column separates rows. Nothing has a fixed height: the row /// grows with text scaling and every line ellipsises. /// @@ -57,7 +65,6 @@ class AppListRow extends StatelessWidget { this.selected = false, this.emphasized = false, this.divider = true, - this.keepMenuWhenCompact = false, this.compactWidth = kAppListRowCompactWidth, this.cupertino, this.accent, @@ -84,7 +91,7 @@ class AppListRow extends StatelessWidget { /// A touch long-press on the row (for example, entering a select mode). final VoidCallback? onLongPress; - /// A checkbox leading the row in a multi-select list; kept when compact. + /// A checkbox leading the row in a multi-select list. final Widget? selectionControl; /// Placed on the tap target ([InkWell] / [GestureDetector]). @@ -92,10 +99,6 @@ class AppListRow extends StatelessWidget { final bool selected; final bool emphasized; final bool divider; - - /// Keeps the menu in a compact row when the list has no other way to reach - /// the row's actions. - final bool keepMenuWhenCompact; final double compactWidth; /// Overrides the host detection (some features pick a style per platform). @@ -123,9 +126,11 @@ class AppListRow extends StatelessWidget { child: LayoutBuilder( builder: (context, constraints) { final compact = constraints.maxWidth < compactWidth; - final showLeading = leading != null && !compact; - final showMenu = menu != null && (!compact || keepMenuWhenCompact); + final minimal = constraints.maxWidth < kAppListRowMinimalWidth; + final showLeading = leading != null && !minimal; + final showMenu = menu != null && !minimal; final start = compact ? 12.0 : kAppListRowInset; + final avatarGap = compact ? _compactAvatarGap : _avatarGap; final content = Padding( padding: EdgeInsetsDirectional.only( start: start, @@ -142,7 +147,7 @@ class AppListRow extends StatelessWidget { ), if (showLeading) Padding( - padding: const EdgeInsetsDirectional.only(end: _avatarGap), + padding: EdgeInsetsDirectional.only(end: avatarGap), child: leading, ), Expanded(child: _text(styles, compact)), @@ -198,7 +203,7 @@ class AppListRow extends StatelessWidget { // not known here, so a selectable row's divider starts at the inset. final textStart = selectionControl != null ? start - : start + (showLeading ? kAppListAvatarSize + _avatarGap : 0); + : start + (showLeading ? kAppListAvatarSize + avatarGap : 0); final line = Padding( padding: EdgeInsetsDirectional.only(start: textStart), child: SizedBox( @@ -227,8 +232,27 @@ class AppListRow extends StatelessWidget { ); } + /// The time, one ellipsised line in the time style, or null without one. + Widget? _time(_RowStyles styles) { + if (timeWidget == null && (time == null || time!.isEmpty)) return null; + return timeWidget == null + ? Text( + time!, + style: styles.time, + maxLines: 1, + overflow: TextOverflow.ellipsis, + ) + : DefaultTextStyle( + style: styles.time, + maxLines: 1, + overflow: TextOverflow.ellipsis, + child: timeWidget!, + ); + } + Widget _text(_RowStyles styles, bool compact) { final line = compact ? (compactSubtitle ?? subtitle) : subtitle; + final time = _time(styles); final titleLine = LayoutBuilder( // The time keeps at most 40% of the line, so very large text in a // narrow row ellipsises instead of overflowing. @@ -242,32 +266,27 @@ class AppListRow extends StatelessWidget { child: title, ), ), - if (!compact && - (timeWidget != null || (time != null && time!.isNotEmpty))) + if (!compact && time != null) ConstrainedBox( constraints: BoxConstraints(maxWidth: constraints.maxWidth * 0.4), child: Padding( padding: const EdgeInsetsDirectional.only(start: 8), - child: timeWidget == null - ? Text( - time!, - style: styles.time, - maxLines: 1, - overflow: TextOverflow.ellipsis, - ) - : DefaultTextStyle( - style: styles.time, - maxLines: 1, - overflow: TextOverflow.ellipsis, - child: timeWidget!, - ), + child: time, ), ), ], ), ); + // A compact row has no room beside its title: the time takes a line of + // its own under the subtitle. + final timeLine = compact ? time : null; if (line == null && subtitleLeading == null && subtitleTrailing == null) { - return titleLine; + if (timeLine == null) return titleLine; + return Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [titleLine, const SizedBox(height: 2), timeLine], + ); } return Column( mainAxisSize: MainAxisSize.min, @@ -297,6 +316,7 @@ class AppListRow extends StatelessWidget { ), ], ), + ?timeLine, ], ); } diff --git a/app/notes/app-list-row.md b/app/notes/app-list-row.md index 2f7e0196..75a1b92f 100644 --- a/app/notes/app-list-row.md +++ b/app/notes/app-list-row.md @@ -26,9 +26,11 @@ component, on both hosts. Cupertino uses `separator` at 1 physical pixel; Material uses `outlineVariant` at 1. Lists turn it off for the last row of a group. - Compact mode: when the row is narrower than `kAppListRowCompactWidth` - (280), it drops the avatar, the time and the menu, and shows - `compactSubtitle` if one is given. `keepMenuWhenCompact` keeps the menu - (chat, pipeline schedules). + (280), it keeps the avatar (a run's status) and the menu, moves the time + to its own line under the subtitle and shows `compactSubtitle` if one is + given. Only below `kAppListRowMinimalWidth` (160, narrower than any list + pane) does it drop the avatar and menu so the text never overflows. + (`keepMenuWhenCompact` is gone: every row keeps its menu.) - No fixed heights. Padding is 16 horizontal and 10 vertical. At 2x text in a 320px row everything ellipsises without overflowing. - Test keys: `appListRowSurfaceKey` and `appListRowDividerKey`. diff --git a/app/test/application/settings_page_test.dart b/app/test/application/settings_page_test.dart index 6737fd19..e3a112c8 100644 --- a/app/test/application/settings_page_test.dart +++ b/app/test/application/settings_page_test.dart @@ -7,6 +7,7 @@ import 'package:pipelines_app/features/settings/settings_screen.dart'; import 'package:pipelines_app/ui/adaptive/floating_destination_bar.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import '../bar_action.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; import 'pipelines_app_test.dart' show webServer; @@ -77,7 +78,11 @@ void main() { ); expect(find.byKey(const ValueKey('shell.back')), findsNothing); - await tester.tap(find.byKey(const Key('pipelines-open-settings'))); + await tapBarAction( + tester, + find.byKey(const Key('pipelines-open-settings')), + 'Settings', + ); await tester.pump(); await tester.pump(); expect(provider.value.uri.path, '/settings'); diff --git a/app/test/application/settings_registration_test.dart b/app/test/application/settings_registration_test.dart index 08d70fdd..b0a1e487 100644 --- a/app/test/application/settings_registration_test.dart +++ b/app/test/application/settings_registration_test.dart @@ -7,6 +7,7 @@ import 'package:pipelines_app/application/pipelines_app.dart'; import 'package:pipelines_app/core/transport/pipelines_client.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import '../bar_action.dart'; import '../core/transport/pipelines_client_test.dart' show FakeChannel; import '../features/pipelines/fixtures.dart'; import 'pipelines_app_test.dart' show webServer, requestOf; @@ -51,7 +52,11 @@ void main() { ); await replyPipelines(tester, channel, PipelineFixture()); - await tester.tap(find.byKey(const Key('pipelines-open-settings'))); + await tapBarAction( + tester, + find.byKey(const Key('pipelines-open-settings')), + 'Settings', + ); await tester.pump(); await tester.pump(); expect(provider.value.uri.path, '/settings'); diff --git a/app/test/bar_action.dart b/app/test/bar_action.dart new file mode 100644 index 00000000..8546e0eb --- /dev/null +++ b/app/test/bar_action.dart @@ -0,0 +1,18 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/ui/adaptive/segmented_bar.dart'; + +/// Taps a top-bar action: in the bar while it fits, or through the bar's +/// More menu (by its [label]) when a narrow bar moved it there. +Future tapBarAction( + WidgetTester tester, + Finder action, + String label, +) async { + if (action.hitTestable().evaluate().isNotEmpty) { + await tester.tap(action); + return; + } + await tester.tap(find.byKey(segmentedMoreKey).hitTestable()); + await tester.pumpAndSettle(); + await tester.tap(find.text(label).last); +} diff --git a/app/test/features/runs/ui/run_list_row_test.dart b/app/test/features/runs/ui/run_list_row_test.dart index 47037374..6a7346e8 100644 --- a/app/test/features/runs/ui/run_list_row_test.dart +++ b/app/test/features/runs/ui/run_list_row_test.dart @@ -4,6 +4,8 @@ import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/runs/domain/run_models.dart'; import 'package:pipelines_app/features/runs/ui/run_more_menu.dart'; import 'package:pipelines_app/features/runs/ui/runs_list.dart'; +import 'package:pipelines_app/ui/status/run_status_icon.dart'; +import 'package:pipelines_app/ui/text/relative_time_label.dart'; final _finished = { 'id': 42, @@ -108,29 +110,39 @@ void main() { expect(find.byKey(runMoreMenuKey), findsNothing); }); - testWidgets('compact rows keep a title and one metadata line', ( - tester, - ) async { - tester.view.physicalSize = const Size(240, 400); - tester.view.devicePixelRatio = 1; - addTearDown(tester.view.reset); - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: RunListRow( - run: _finished, - now: DateTime.utc(2026, 10, 22), - gateway: _FakeGateway(), - onRunChanged: (_) {}, + for (final status in ['succeeded', 'failed']) { + testWidgets('a compact $status row keeps its status, menu and time', ( + tester, + ) async { + tester.view.physicalSize = const Size(240, 400); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: RunListRow( + run: {..._finished, 'status': status}, + now: DateTime.utc(2026, 10, 22), + gateway: _FakeGateway(), + onRunChanged: (_) {}, + ), ), ), - ), - ); + ); - expect(find.text('Ship the release notes'), findsOneWidget); - expect(find.text('release'), findsOneWidget); - expect(find.text('2m 5s'), findsNothing); - expect(find.byKey(const ValueKey('run-list-more-menu-42')), findsNothing); - expect(tester.takeException(), isNull); - }); + expect(find.text('Ship the release notes'), findsOneWidget); + expect(find.text('release'), findsOneWidget); + expect(find.text('2m 5s'), findsNothing); + // The status tells a failed run from a succeeded one at any width. + final icon = find.byType(RunStatusIcon); + expect(icon, findsOneWidget); + expect(tester.widget(icon).state, status); + final menu = find.byKey(const ValueKey('run-list-more-menu-42')); + expect(menu, findsOneWidget); + expect(tester.getRect(menu).right, lessThanOrEqualTo(240)); + expect(tester.getSize(menu).width, greaterThanOrEqualTo(44)); + expect(find.byType(RelativeTimeLabel), findsOneWidget); + expect(tester.takeException(), isNull); + }); + } } diff --git a/app/test/ui/adaptive/segmented_bar_overflow_test.dart b/app/test/ui/adaptive/segmented_bar_overflow_test.dart new file mode 100644 index 00000000..7fc41cc8 --- /dev/null +++ b/app/test/ui/adaptive/segmented_bar_overflow_test.dart @@ -0,0 +1,247 @@ +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/segmented_bar.dart'; + +void _size(WidgetTester tester, double width) { + tester.view.physicalSize = Size(width, 700); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); +} + +const _names = ['Needs you', 'Usage', 'Filter', 'Sort', 'Select']; + +Key _key(String name) => ValueKey('action.$name'); + +/// An icon action like the list bars' own, at the host's touch target. +Widget _icon(bool cupertino, String name, VoidCallback onPressed) => cupertino + ? Semantics( + button: true, + label: name, + excludeSemantics: true, + child: CupertinoButton( + key: _key(name), + padding: EdgeInsets.zero, + minimumSize: const Size.square(kMinInteractiveDimensionCupertino), + onPressed: onPressed, + child: const Icon(CupertinoIcons.circle), + ), + ) + : IconButton( + key: _key(name), + tooltip: name, + onPressed: onPressed, + icon: const Icon(Icons.circle_outlined), + ); + +/// The Runs bar's shape: a bell that always stays, then four that may move. +List _actions(bool cupertino, List pressed) => [ + for (final name in _names) + if (name == 'Needs you') + _icon(cupertino, name, () => pressed.add(name)) + else + SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: name, + icon: Icons.circle_outlined, + onPressed: () => pressed.add(name), + ), + child: _icon(cupertino, name, () => pressed.add(name)), + ), +]; + +Widget _app(bool cupertino, SegmentedBar bar, {Brightness? brightness}) => + cupertino + ? CupertinoApp( + theme: CupertinoThemeData(brightness: brightness), + home: CupertinoPageScaffold( + navigationBar: bar, + child: const SizedBox.expand(), + ), + ) + : MaterialApp( + theme: ThemeData(brightness: brightness ?? Brightness.light), + home: Scaffold( + extendBodyBehindAppBar: true, + appBar: bar, + body: const SizedBox.expand(), + ), + ); + +/// Pumps the bar and the frame its More menu settles in. +Future _pump(WidgetTester tester, Widget app) async { + await tester.pumpWidget(app); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); +} + +bool _whole(WidgetTester tester, String text) { + final paragraph = tester.renderObject(find.text(text)); + final natural = (TextPainter( + text: TextSpan(text: text, style: paragraph.text.style), + textDirection: TextDirection.ltr, + textScaler: paragraph.textScaler, + )..layout()).width; + return !paragraph.didExceedMaxLines && paragraph.size.width >= natural - 0.5; +} + +void main() { + for (final cupertino in [false, true]) { + final host = cupertino ? 'Cupertino' : 'Material'; + for (final width in [240.0, 270.0, 320.0]) { + for (final brightness in Brightness.values) { + testWidgets('$host ${brightness.name} ${width.toInt()}px: the title ' + 'shows whole, actions keep their targets, the rest are in More', ( + tester, + ) async { + _size(tester, width); + final pressed = []; + await _pump( + tester, + _app( + cupertino, + brightness: brightness, + SegmentedBar( + cupertino: cupertino, + title: 'Runs', + actions: _actions(cupertino, pressed), + ), + ), + ); + expect(tester.takeException(), isNull); + expect(_whole(tester, 'Runs'), isTrue); + final title = tester.getRect(find.text('Runs')); + final shown = [ + for (final name in _names) + if (find.byKey(_key(name)).hitTestable().evaluate().isNotEmpty) + name, + ]; + // The bell cannot move; at these widths the others do not all fit. + expect(shown, contains('Needs you')); + expect(shown.length, lessThan(_names.length)); + final more = find.byKey(segmentedMoreKey).hitTestable(); + expect(more, findsOneWidget); + final rects = [ + for (final name in shown) tester.getRect(find.byKey(_key(name))), + tester.getRect(more), + ]; + final target = cupertino ? 44.0 : 48.0; + for (final (index, rect) in rects.indexed) { + expect(rect.width, greaterThanOrEqualTo(target)); + expect(rect.height, greaterThanOrEqualTo(target)); + // Clear of the title and inside the bar's edge padding. + expect(rect.left, greaterThan(title.right)); + expect(rect.right, lessThanOrEqualTo(width - 12)); + if (index > 0) { + expect(rect.left, greaterThanOrEqualTo(rects[index - 1].right)); + } + } + // Each moved action is in the More menu and runs from there. + await tester.tap(more); + await tester.pumpAndSettle(); + final moved = [ + for (final name in _names) + if (!shown.contains(name)) name, + ]; + for (final name in moved) { + expect(find.text(name), findsOneWidget, reason: name); + } + await tester.tap(find.text(moved.last)); + await tester.pumpAndSettle(); + expect(pressed, [moved.last]); + }); + } + } + + testWidgets('$host: a wide bar shows every action and no More', ( + tester, + ) async { + _size(tester, 800); + await _pump( + tester, + _app( + cupertino, + SegmentedBar( + cupertino: cupertino, + title: 'Runs', + actions: _actions(cupertino, []), + ), + ), + ); + for (final name in _names) { + expect(find.byKey(_key(name)).hitTestable(), findsOneWidget); + } + expect(find.byKey(segmentedMoreKey).hitTestable(), findsNothing); + // A hidden More is out of the semantics tree too. + expect(find.bySemanticsLabel('More actions'), findsNothing); + }); + + testWidgets('$host: a longer title keeps a readable share at 240px', ( + tester, + ) async { + _size(tester, 240); + await _pump( + tester, + _app( + cupertino, + SegmentedBar( + cupertino: cupertino, + title: 'Pipelines', + actions: _actions(cupertino, []), + ), + ), + ); + expect(tester.takeException(), isNull); + // The test font's glyphs are squares, wider than a real font's, so + // the name may ellipsise here, but never to nothing. + final paragraph = tester.renderObject( + find.text('Pipelines'), + ); + expect(paragraph.size.width, greaterThan(3 * 14)); + expect(find.byKey(segmentedMoreKey).hitTestable(), findsOneWidget); + }); + + testWidgets('$host: a choice entry offers its choices from More', ( + tester, + ) async { + _size(tester, 240); + String? picked; + await _pump( + tester, + _app( + cupertino, + SegmentedBar( + cupertino: cupertino, + title: 'Runs', + actions: [ + for (final name in ['A', 'B', 'C']) + SegmentedOverflowAction( + overflowEntry: SegmentedOverflowEntry( + label: 'Sort $name', + icon: Icons.sort, + choices: [ + for (final order in ['Newest', 'Oldest']) + SegmentedMenuChoice( + label: order, + selected: order == 'Newest', + onSelected: () => picked = '$name $order', + ), + ], + ), + child: _icon(cupertino, name, () {}), + ), + ], + ), + ), + ); + await tester.tap(find.byKey(segmentedMoreKey)); + await tester.pumpAndSettle(); + await tester.tap(find.text('Sort C')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Oldest')); + await tester.pumpAndSettle(); + expect(picked, 'C Oldest'); + }); + } +} diff --git a/app/test/ui/layout/app_list_row_test.dart b/app/test/ui/layout/app_list_row_test.dart index d10b3dd9..2d6b2347 100644 --- a/app/test/ui/layout/app_list_row_test.dart +++ b/app/test/ui/layout/app_list_row_test.dart @@ -182,37 +182,40 @@ void main() { expect(_paint(tester, '9d ago').color, isNot(accent)); }); - testWidgets('$host: a narrow pane drops the avatar, time and menu', ( - tester, - ) async { - _size(tester, 240); - await tester.pumpWidget(_host(cupertino, _row())); - expect(find.byKey(const Key('avatar')), findsNothing); - expect(find.text('9d ago'), findsNothing); - expect(find.byKey(const Key('menu')), findsNothing); - expect(find.text('Running'), findsOneWidget); - expect(find.text('pipeline · 2 stages'), findsNothing); - expect(tester.takeException(), isNull); - }); - - testWidgets('$host: keepMenuWhenCompact keeps the menu', (tester) async { - _size(tester, 240); - await tester.pumpWidget( - _host( - cupertino, - AppListRow( - keepMenuWhenCompact: true, - title: const Text('Chat'), - menu: AppOverflowMenu( - key: const Key('menu'), - actions: const [AppMenuAction(value: 1, label: 'Star')], - onSelected: (_, _) {}, - ), - ), - ), - ); - expect(find.byKey(const Key('menu')), findsOneWidget); - }); + for (final width in [240.0, 270.0]) { + for (final brightness in Brightness.values) { + testWidgets('$host ${brightness.name}: a ${width.toInt()}px pane keeps ' + 'the status avatar and the menu, and the time under the subtitle', ( + tester, + ) async { + _size(tester, width); + await tester.pumpWidget( + _host(cupertino, brightness: brightness, _row(title: _longTitle)), + ); + final row = tester.getRect(find.byKey(const Key('row'))); + final avatar = tester.getRect(find.byKey(const Key('avatar'))); + final menu = tester.getRect(find.byKey(const Key('menu'))); + // Both inside the row, at the leading and trailing edges. + expect(avatar.left, greaterThanOrEqualTo(row.left)); + expect(menu.right, lessThanOrEqualTo(row.right)); + expect(menu.width, greaterThanOrEqualTo(44)); + expect(menu.height, greaterThanOrEqualTo(44)); + expect(find.text('Running'), findsOneWidget); + expect(find.text('pipeline · 2 stages'), findsNothing); + // The time sits on its own line, under the subtitle. + final time = tester.getRect(find.text('9d ago')); + final subtitle = tester.getRect(find.text('Running')); + expect(time.top, greaterThanOrEqualTo(subtitle.bottom)); + expect(time.left, greaterThan(avatar.right)); + expect(time.right, lessThan(menu.left)); + // The title ellipsises between them. + final title = tester.getRect(find.text(_longTitle)); + expect(title.left, greaterThan(avatar.right)); + expect(title.right, lessThanOrEqualTo(menu.left)); + expect(tester.takeException(), isNull); + }); + } + } for (final brightness in Brightness.values) { testWidgets('$host ${brightness.name}: 320px at 2x text ellipsises '