f2643a0ff4
Native Material/Cupertino prompt seeded from the run's stored title. Only
Save sends set_run_title {run_id, title} with the text untrimmed; typing and
Cancel send nothing, refusals stay verbatim with the draft kept, and only the
accepted run_detail is delivered once.
184 lines
4.9 KiB
Dart
184 lines
4.9 KiB
Dart
import 'package:flutter/cupertino.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,
|
|
cupertino: cupertino,
|
|
onSubmit: onSubmit,
|
|
);
|
|
if (cupertino) {
|
|
return showCupertinoDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => prompt,
|
|
);
|
|
}
|
|
return showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => prompt,
|
|
);
|
|
}
|
|
|
|
class _TitlePrompt extends StatefulWidget {
|
|
const _TitlePrompt({
|
|
required this.runId,
|
|
required this.initialTitle,
|
|
required this.cupertino,
|
|
required this.onSubmit,
|
|
});
|
|
|
|
final num runId;
|
|
final String initialTitle;
|
|
final bool cupertino;
|
|
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) {
|
|
final dialog = widget.cupertino ? _cupertino(context) : _material(context);
|
|
return PopScope(canPop: !_sending, child: dialog);
|
|
}
|
|
|
|
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 _cupertino(BuildContext context) {
|
|
return CupertinoAlertDialog(
|
|
title: const Text(runTitleDialogTitle),
|
|
content: _body(
|
|
field: CupertinoTextField(
|
|
key: runTitleFieldKey,
|
|
controller: _title,
|
|
enabled: !_sending,
|
|
autofocus: true,
|
|
placeholder: runTitleFieldLabel,
|
|
textInputAction: TextInputAction.done,
|
|
onSubmitted: (_) => _save(),
|
|
),
|
|
errorStyle: TextStyle(
|
|
color: CupertinoColors.systemRed.resolveFrom(context),
|
|
),
|
|
),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
key: runTitleCancelKey,
|
|
onPressed: _sending ? null : _cancel,
|
|
child: const Text(runTitleCancelLabel),
|
|
),
|
|
CupertinoDialogAction(
|
|
key: runTitleSaveKey,
|
|
isDefaultAction: true,
|
|
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),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|