Files
pipelines/app/lib/features/runs/actions/title_dialog.dart
T
alan 364fd5b538 Remove Cupertino run action branches
Keep the Material-only run action behavior while preserving public Cupertino parameters for callers that will be cleaned up separately. Narrow the related tests to the supported Material path.
2026-11-11 04:18:26 +00:00

139 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import 'action_refusal.dart';
import 'title_wording.dart';
/// Sends the typed title. Returns null once the server has accepted it, or an
/// [ActionRefusal] carrying the server's words verbatim; the prompt then stays
/// open with the typed title intact.
typedef TitleSubmit = Future<ActionRefusal?> Function(String title);
/// Opens the native rename prompt for [runId], seeded with the run's stored
/// [initialTitle].
///
/// Typing and the dismiss action send nothing; only Save sends the title. The
/// title is passed through exactly as typed, including surrounding whitespace,
/// matching `setRunTitle` in `web/src/repository.js`; the server decides whether
/// a blank title is acceptable.
Future<void> showRunTitlePrompt({
required BuildContext context,
required num runId,
required String initialTitle,
required bool cupertino,
required TitleSubmit onSubmit,
}) {
final prompt = _TitlePrompt(
runId: runId,
initialTitle: initialTitle,
onSubmit: onSubmit,
);
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (_) => prompt,
);
}
class _TitlePrompt extends StatefulWidget {
const _TitlePrompt({
required this.runId,
required this.initialTitle,
required this.onSubmit,
});
final num runId;
final String initialTitle;
final TitleSubmit onSubmit;
@override
State<_TitlePrompt> createState() => _TitlePromptState();
}
class _TitlePromptState extends State<_TitlePrompt> {
late final TextEditingController _title = TextEditingController(
text: widget.initialTitle,
);
bool _sending = false;
String? _error;
@override
void dispose() {
_title.dispose();
super.dispose();
}
void _cancel() {
if (!_sending) Navigator.of(context).pop();
}
Future<void> _save() async {
if (_sending) return;
setState(() {
_sending = true;
_error = null;
});
final refusal = await widget.onSubmit(_title.text);
if (!mounted) return;
if (refusal == null) {
Navigator.of(context).pop();
return;
}
setState(() {
_sending = false;
_error = refusal.message;
});
}
@override
Widget build(BuildContext context) {
return PopScope(canPop: !_sending, child: _material(context));
}
Widget _material(BuildContext context) {
final theme = Theme.of(context);
return AlertDialog(
scrollable: true,
title: const Text(runTitleDialogTitle),
content: _body(
field: TextField(
key: runTitleFieldKey,
controller: _title,
enabled: !_sending,
autofocus: true,
decoration: const InputDecoration(labelText: runTitleFieldLabel),
textInputAction: TextInputAction.done,
onSubmitted: (_) => _save(),
),
errorStyle: TextStyle(color: theme.colorScheme.error),
),
actions: [
TextButton(
key: runTitleCancelKey,
onPressed: _sending ? null : _cancel,
child: const Text(runTitleCancelLabel),
),
FilledButton(
key: runTitleSaveKey,
onPressed: _sending ? null : _save,
child: const Text(runTitleSaveLabel),
),
],
);
}
Widget _body({required Widget field, required TextStyle errorStyle}) {
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
field,
if (_error case final error?)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(error, key: runTitleErrorKey, style: errorStyle),
),
],
);
}
}