Files
pipelines/app/lib/features/runs/ui/run_more_menu.dart
T

140 lines
4.6 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../schedules/ui/run/schedule_run_action.dart';
import '../actions/retry_action.dart';
import '../actions/retry_eligibility.dart';
import '../actions/retry_wording.dart';
import '../actions/title_action.dart';
import '../actions/title_wording.dart';
const runMoreMenuKey = ValueKey<String>('run-more-menu');
/// Whether the run has anything for its more menu.
bool showRunMoreMenu(JsonMap run) =>
runTitleId(run) != null || canScheduleRun(run) || _canRetry(run);
bool _canRetry(JsonMap run) => retryRunId(run) != null && canRetryRun(run);
enum _RunMoreItem { retry, rename, schedule }
/// Each item keeps the key its standalone button had.
Key _keyOf(_RunMoreItem item) => switch (item) {
_RunMoreItem.retry => retryActionKey,
_RunMoreItem.rename => runTitleActionKey,
_RunMoreItem.schedule => scheduleRunActionKey,
};
/// The run detail's more menu: Retry (failed runs only), Rename and Schedule
/// this run, kept out of the header.
/// This widget stays mounted while a picked flow runs, so the flow's result
/// still reaches the current run.
class RunMoreMenu extends StatefulWidget {
const RunMoreMenu({
super.key,
required this.run,
required this.gateway,
required this.onRunChanged,
required this.cupertino,
this.onRunOpened,
});
final JsonMap run;
final ApiGateway gateway;
final ValueChanged<JsonMap> onRunChanged;
final bool cupertino;
/// Opens the new run an accepted retry names.
final ValueChanged<num>? onRunOpened;
@override
State<RunMoreMenu> createState() => _RunMoreMenuState();
}
class _RunMoreMenuState extends State<RunMoreMenu> {
JsonMap get run => widget.run;
List<(_RunMoreItem, String)> get _items => [
// Retry is offered only for a failed run that is no longer executing.
if (_canRetry(run)) (_RunMoreItem.retry, retryActionLabel),
if (runTitleId(run) != null) (_RunMoreItem.rename, runTitleActionLabel),
if (canScheduleRun(run)) (_RunMoreItem.schedule, scheduleRunActionLabel),
];
void _pick(BuildContext context, _RunMoreItem item) {
switch (item) {
case _RunMoreItem.retry:
openRetryRunPrompt(
context,
run: run,
gateway: widget.gateway,
onRunChanged: widget.onRunChanged,
onRunOpened: widget.onRunOpened,
owns: (owned) => mounted && retryRunId(widget.run) == owned,
);
case _RunMoreItem.rename:
openRunTitleEditor(
context,
run: run,
gateway: widget.gateway,
onRunChanged: widget.onRunChanged,
// The latest run, so a result for a run no longer shown is dropped.
owns: (owned) => mounted && runTitleId(widget.run) == owned,
);
case _RunMoreItem.schedule:
openScheduleRunEditor(context, run: run, gateway: widget.gateway);
}
}
@override
Widget build(BuildContext context) {
final items = _items;
if (items.isEmpty) return const SizedBox.shrink();
if (!widget.cupertino) {
return PopupMenuButton<_RunMoreItem>(
key: runMoreMenuKey,
icon: const Icon(Icons.more_vert),
tooltip: 'More actions',
onSelected: (item) => _pick(context, item),
itemBuilder: (_) => [
for (final (item, label) in items)
PopupMenuItem(key: _keyOf(item), value: item, child: Text(label)),
],
);
}
return Semantics(
button: true,
label: 'More actions',
excludeSemantics: true,
child: CupertinoButton(
key: runMoreMenuKey,
padding: EdgeInsets.zero,
minimumSize: const Size.square(kMinInteractiveDimensionCupertino),
onPressed: () async {
final picked = await showCupertinoModalPopup<_RunMoreItem>(
context: context,
builder: (sheet) => CupertinoActionSheet(
actions: [
for (final (item, label) in items)
CupertinoActionSheetAction(
key: _keyOf(item),
onPressed: () => Navigator.of(sheet).pop(item),
child: Text(label),
),
],
cancelButton: CupertinoActionSheetAction(
isDefaultAction: true,
onPressed: () => Navigator.of(sheet).pop(),
child: const Text('Cancel'),
),
),
);
if (picked != null && context.mounted) _pick(context, picked);
},
child: const Icon(CupertinoIcons.ellipsis_circle),
),
);
}
}