import 'package:flutter/material.dart'; import '../../../core/api_gateway.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'; import 'runs_controller.dart'; const runsSelectKey = ValueKey('runs.select'); const runsSelectDoneKey = ValueKey('runs.select.done'); const runsDeleteSelectedKey = ValueKey('runs.select.delete'); const runsSelectionCountKey = ValueKey('runs.select.count'); const runsDeleteReportKey = ValueKey('runs.delete.report'); const runsDeleteReportDismissKey = ValueKey( 'runs.delete.report.dismiss', ); /// The day heading's Select all / Deselect all toggle key. ValueKey runsDaySelectKey(String day) => ValueKey('runs.select.day.$day'); /// The Runs bar's `Select` action, entering multi-select. Widget runsSelectAction(BuildContext context, RunsController controller) { void enter() => controller.selection.enter(); // A narrow bar moves it into its More menu. return SegmentedOverflowAction( overflowEntry: SegmentedOverflowEntry( label: 'Select', icon: Icons.checklist, onPressed: enter, ), child: IconButton( key: runsSelectKey, tooltip: 'Select', onPressed: enter, icon: const Icon(Icons.checklist), ), ); } /// 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( BuildContext context, RunsController controller, ) { final selection = controller.selection; final selected = [ for (final run in controller.runs) if (selection.contains(run['id'])) run, ]; final count = selected.length; final size = count == 0 ? null : runsSizeLabel(selected); final delete = count == 0 ? null : () => confirmDeleteRuns( context, runs: selected, gateway: controller.repository.gateway, onDeleted: controller.applyDeletion, ); final deleteLabel = count == 0 ? 'Delete selected runs' : 'Delete $count selected ${count == 1 ? 'run' : 'runs'}'; final scheme = Theme.of(context).colorScheme; final actions = [ Semantics( label: deleteLabel, excludeSemantics: true, onTap: delete, button: true, enabled: delete != null, child: TextButton( key: runsDeleteSelectedKey, style: TextButton.styleFrom(foregroundColor: scheme.error), onPressed: delete, child: const Text('Delete'), ), ), TextButton( key: runsSelectDoneKey, onPressed: selection.exit, child: const Text('Done'), ), ]; return (title: '$count selected', subtitle: size, actions: actions); } /// A day's heading; in select mode it carries that day's Select all / /// Deselect all toggle, covering only the day's finished runs. class RunsDayHeading extends StatelessWidget { const RunsDayHeading({ super.key, required this.label, required this.runs, required this.controller, }); final String label; final List runs; final RunsController controller; @override Widget build(BuildContext context) { final selection = controller.selection; final trailing = []; if (selection.active) { final any = deletableRunIds(runs).isNotEmpty; final all = selection.allSelected(runs); final text = all ? 'Deselect all' : 'Select all'; final toggle = any ? () => selection.toggleAll(runs) : null; final key = runsDaySelectKey(label); trailing.add( Semantics( button: true, enabled: any, label: any ? '$text $label' : '$text $label. No finished runs to select.', excludeSemantics: true, onTap: toggle, child: TextButton(key: key, onPressed: toggle, child: Text(text)), ), ); } return Padding( padding: const EdgeInsets.only(top: 8), child: SectionHeading( title: label, minHeight: 32, header: true, trailing: trailing, ), ); } } /// The last deletion's outcome above the list, until dismissed. class RunsDeleteReport extends StatelessWidget { const RunsDeleteReport({super.key, required this.controller}); final RunsController controller; @override Widget build(BuildContext context) { final report = controller.selection.report; if (report == null) return const SizedBox.shrink(); final dismiss = controller.selection.dismissReport; return Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 4, 8, 4), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded( child: Semantics( liveRegion: true, child: Text(report, key: runsDeleteReportKey), ), ), TextButton( key: runsDeleteReportDismissKey, onPressed: dismiss, child: const Text('Dismiss'), ), ], ), ); } }