diff --git a/app/lib/features/pipelines/ui/run_pipeline_action.dart b/app/lib/features/pipelines/ui/run_pipeline_action.dart index a8111940..7a58cde5 100644 --- a/app/lib/features/pipelines/ui/run_pipeline_action.dart +++ b/app/lib/features/pipelines/ui/run_pipeline_action.dart @@ -51,6 +51,7 @@ class _RunPipelineActionState extends State { await showRunPipelineDialog( context: context, detail: widget.detail, + gateway: widget.gateway, pending: widget.controller.pendingRunFor(_pipelineId, _version), onSubmit: _submit, onPendingChanged: (intent) { diff --git a/app/lib/features/pipelines/ui/run_pipeline_dialog.dart b/app/lib/features/pipelines/ui/run_pipeline_dialog.dart index b3c724e8..5bb650d0 100644 --- a/app/lib/features/pipelines/ui/run_pipeline_dialog.dart +++ b/app/lib/features/pipelines/ui/run_pipeline_dialog.dart @@ -2,6 +2,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; +import '../../settings/settings_store.dart'; import '../../../ui/adaptive/host.dart'; import '../pipeline_models.dart'; import '../run_pipeline_intent.dart'; @@ -16,6 +17,7 @@ import 'run_pipeline_wording.dart'; Future showRunPipelineDialog({ required BuildContext context, required PipelineDetail detail, + required ApiGateway gateway, required RunPipelineIntent? pending, required Future Function(RunPipelineIntent intent) onSubmit, @@ -24,6 +26,7 @@ Future showRunPipelineDialog({ }) { final dialog = _RunPipelineDialog( detail: detail, + gateway: gateway, pending: pending, cupertino: usesCupertinoHost(context), onSubmit: onSubmit, @@ -46,6 +49,7 @@ Future showRunPipelineDialog({ class _RunPipelineDialog extends StatefulWidget { const _RunPipelineDialog({ required this.detail, + required this.gateway, required this.pending, required this.cupertino, required this.onSubmit, @@ -54,6 +58,7 @@ class _RunPipelineDialog extends StatefulWidget { }); final PipelineDetail detail; + final ApiGateway gateway; final RunPipelineIntent? pending; final bool cupertino; final Future Function(RunPipelineIntent intent) onSubmit; @@ -71,6 +76,8 @@ class _RunPipelineDialogState extends State<_RunPipelineDialog> { late RunOutputFormat _outputFormat; late RunPipelineIntent? _intent; bool _sending = false; + bool _directoriesLoading = true; + List _directories = const []; String? _error; @override @@ -88,8 +95,45 @@ class _RunPipelineDialogState extends State<_RunPipelineDialog> { _prompt = TextEditingController(text: restored?.prompt ?? ''); _workspace = TextEditingController(text: restored?.workspace ?? ''); _error = restored == null ? null : runPipelineUnknownAdmission; + _loadDirectories(restored); } + Future _loadDirectories(RunPipelineIntent? restored) async { + final savedWorkspace = const SharedPreferencesSettingsStore().load().then( + (settings) => settings.workspace, + ); + try { + final reply = await _gateway.call('chat_directories'); + final value = reply['chat_directories']; + final directories = value is List + ? value.whereType().toList() + : []; + final saved = await savedWorkspace; + if (!mounted) return; + setState(() { + _directories = directories + .where((path) => path.startsWith('/')) + .toList(); + if (_workspace.text.isEmpty) { + _workspace.text = saved.startsWith('/') + ? saved + : _directories.isEmpty + ? '' + : _directories.first; + } + _directoriesLoading = false; + }); + } catch (error) { + if (!mounted) return; + setState(() { + _directoriesLoading = false; + _error = '$error'; + }); + } + } + + ApiGateway get _gateway => widget.gateway; + @override void dispose() { _prompt.dispose(); @@ -216,6 +260,9 @@ class _RunPipelineDialogState extends State<_RunPipelineDialog> { RunPipelineFields( prompt: _prompt, workspace: _workspace, + directories: _directories, + cupertino: widget.cupertino, + onChooseDirectory: _chooseDirectory, notifyMode: _notify, outputFormat: _outputFormat, locked: _locked, @@ -223,6 +270,11 @@ class _RunPipelineDialogState extends State<_RunPipelineDialog> { onOutputFormatChanged: (format) => setState(() => _outputFormat = format), ), + if (_directoriesLoading) + Padding( + padding: const EdgeInsets.only(top: 4), + child: Text(runPipelineDirectoriesLoading), + ), if (_error case final error?) Padding( padding: const EdgeInsets.only(top: 12), @@ -234,4 +286,8 @@ class _RunPipelineDialogState extends State<_RunPipelineDialog> { ], ); } + + void _chooseDirectory(String directory) { + _workspace.text = directory; + } } diff --git a/app/lib/features/pipelines/ui/run_pipeline_fields.dart b/app/lib/features/pipelines/ui/run_pipeline_fields.dart index 95ae5d57..bd77c202 100644 --- a/app/lib/features/pipelines/ui/run_pipeline_fields.dart +++ b/app/lib/features/pipelines/ui/run_pipeline_fields.dart @@ -14,6 +14,9 @@ class RunPipelineFields extends StatelessWidget { super.key, required this.prompt, required this.workspace, + required this.directories, + required this.cupertino, + required this.onChooseDirectory, required this.notifyMode, required this.outputFormat, required this.locked, @@ -23,6 +26,9 @@ class RunPipelineFields extends StatelessWidget { final TextEditingController prompt; final TextEditingController workspace; + final List directories; + final bool cupertino; + final ValueChanged onChooseDirectory; final RunNotifyMode notifyMode; final RunOutputFormat outputFormat; final bool locked; @@ -85,18 +91,51 @@ class RunPipelineFields extends StatelessWidget { } Widget _workspaceField(BuildContext context, bool cupertino) { - if (cupertino) { - return CupertinoTextField( - key: runPipelineWorkspaceKey, - controller: workspace, - placeholder: runPipelineWorkspacePlaceholder, - placeholderStyle: _cupertinoPlaceholder(context), - style: CupertinoTheme.of(context).textTheme.textStyle, - enabled: !locked, - autocorrect: false, - padding: const EdgeInsets.all(8), - ); - } + final field = cupertino + ? _cupertinoWorkspace(context) + : _materialWorkspace(); + return Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Expanded(child: field), + if (directories.isNotEmpty) + cupertino + ? CupertinoButton( + key: runPipelineWorkspaceSuggestionsKey, + padding: EdgeInsets.zero, + onPressed: locked + ? null + : () => _chooseCupertinoDirectory(context), + child: const Icon(CupertinoIcons.folder), + ) + : PopupMenuButton( + key: runPipelineWorkspaceSuggestionsKey, + tooltip: runPipelineWorkspaceLabel, + enabled: !locked, + onSelected: onChooseDirectory, + itemBuilder: (_) => [ + for (final path in directories) + PopupMenuItem(value: path, child: Text(path)), + ], + ), + ], + ); + } + + Widget _cupertinoWorkspace(BuildContext context) { + return CupertinoTextField( + key: runPipelineWorkspaceKey, + controller: workspace, + placeholder: runPipelineWorkspacePlaceholder, + placeholderStyle: _cupertinoPlaceholder(context), + style: CupertinoTheme.of(context).textTheme.textStyle, + enabled: !locked, + autocorrect: false, + padding: const EdgeInsets.all(8), + ); + } + + Widget _materialWorkspace() { return TextField( key: runPipelineWorkspaceKey, controller: workspace, @@ -109,6 +148,27 @@ class RunPipelineFields extends StatelessWidget { ); } + Future _chooseCupertinoDirectory(BuildContext context) async { + final chosen = await showCupertinoModalPopup( + context: context, + builder: (sheetContext) => CupertinoActionSheet( + title: const Text(runPipelineWorkspaceLabel), + actions: [ + for (final path in directories) + CupertinoActionSheetAction( + onPressed: () => Navigator.of(sheetContext).pop(path), + child: Text(path), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.of(sheetContext).pop(), + child: const Text(runPipelineCancelLabel), + ), + ), + ); + if (chosen != null) onChooseDirectory(chosen); + } + Widget _notifyField(BuildContext context, bool cupertino) { if (cupertino) { return CupertinoButton( @@ -222,7 +282,9 @@ class RunPipelineFields extends StatelessWidget { TextStyle _cupertinoPlaceholder(BuildContext context) { // The host's own dynamic placeholder colour (light and dark), resolved for // the dialog's inherited Cupertino brightness. - return TextStyle(color: CupertinoColors.placeholderText.resolveFrom(context)); + return TextStyle( + color: CupertinoColors.placeholderText.resolveFrom(context), + ); } Widget _helper(BuildContext context, String text, bool cupertino) { diff --git a/app/lib/features/pipelines/ui/run_pipeline_wording.dart b/app/lib/features/pipelines/ui/run_pipeline_wording.dart index 54ad56de..531fba87 100644 --- a/app/lib/features/pipelines/ui/run_pipeline_wording.dart +++ b/app/lib/features/pipelines/ui/run_pipeline_wording.dart @@ -12,6 +12,10 @@ const runPipelineWorkspaceLabel = 'Working directory (on the server)'; const runPipelineWorkspaceHelper = 'Choose a recent directory or type an absolute path.'; const runPipelineWorkspacePlaceholder = '/absolute/path/on/the/server'; +const runPipelineDirectoriesLoading = 'Loading directories…'; +const runPipelineWorkspaceSuggestionsKey = ValueKey( + 'run-pipeline-workspace-suggestions', +); const runPipelineNotifyLabel = 'Notifications for this run'; const runPipelineOutputFormatLabel = 'Report format'; const runPipelineOutputFormatHelper = @@ -47,7 +51,5 @@ const runPipelineErrorKey = ValueKey('run-pipeline-error'); /// The server's refusal, verbatim. Only a blank error gets a sentence so the /// prompt never claims a run that was not admitted. -String describeRunPipelineError(Object error) => describeErrorReason( - error, - fallback: 'The server did not start the run.', -); +String describeRunPipelineError(Object error) => + describeErrorReason(error, fallback: 'The server did not start the run.'); diff --git a/app/test/features/pipelines/ui/run_pipeline_launch_test.dart b/app/test/features/pipelines/ui/run_pipeline_launch_test.dart index 57e8866b..c1724324 100644 --- a/app/test/features/pipelines/ui/run_pipeline_launch_test.dart +++ b/app/test/features/pipelines/ui/run_pipeline_launch_test.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/features/pipelines/screen.dart'; @@ -28,6 +29,10 @@ FakeGateway launchGateway( )!, ), 'list_schedules' => {'type': 'schedules', 'schedules': []}, + 'chat_directories' => { + 'type': 'chat_directories', + 'chat_directories': ['/work/recent', 'relative', '/work/other'], + }, 'run_pipeline' => onLaunch(fields), 'get_run' when onLookup != null => onLookup(fields), _ => errorReply('unexpected $type'), @@ -74,6 +79,45 @@ Future fillFields( } void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues({}); + testWidgets( + 'workspace directory suggestions load on open and selection is local', + (tester) async { + final fixture = PipelineFixture(); + final gateway = launchGateway(fixture, onLaunch: (_) => acceptedRun(76)); + await pumpScreen(tester, gateway, []); + await openDetail(tester); + await tester.tap(find.byKey(runPipelineActionKey)); + await tester.pumpAndSettle(); + + expect(gateway.callsOf('chat_directories'), hasLength(1)); + expect(find.text(runPipelineDirectoriesLoading), findsNothing); + expect( + tester + .widget(find.byKey(runPipelineWorkspaceKey)) + .controller + ?.text, + '/work/recent', + ); + await tester.tap(find.byKey(runPipelineWorkspaceSuggestionsKey)); + await tester.pumpAndSettle(); + expect(find.text('/work/other'), findsOneWidget); + expect(find.text('relative'), findsNothing); + await tester.tap(find.text('/work/other')); + await tester.pumpAndSettle(); + expect( + tester + .widget(find.byKey(runPipelineWorkspaceKey)) + .controller + ?.text, + '/work/other', + ); + expect(gateway.callsOf('run_pipeline'), isEmpty); + expect(gateway.callsOf('chat_directories'), hasLength(1)); + }, + ); + testWidgets('the detail Run action sends the web request for the pinned ' 'version and opens only the reply run', (tester) async { final fixture = PipelineFixture(); @@ -106,10 +150,7 @@ void main() { expect(gateway.callsOf('run_pipeline'), isEmpty); await fillFields(tester, prompt: ' Review the change. '); - await tester.enterText( - find.byKey(runPipelineWorkspaceKey), - '/work/alpha ', - ); + await tester.enterText(find.byKey(runPipelineWorkspaceKey), '/work/alpha '); await tester.tap(find.byKey(runPipelineNotifyKey)); await tester.pumpAndSettle(); await tester.tap(find.text('Silent').last); @@ -508,13 +549,7 @@ void main() { final helper = tester .widget(find.text(runPipelineWorkspaceHelper)) .style; - expect( - helper?.color, - CupertinoColors.secondaryLabel.resolveFrom(context), - ); - expect( - helper?.fontSize, - theme.textTheme.tabLabelTextStyle.fontSize, - ); + expect(helper?.color, CupertinoColors.secondaryLabel.resolveFrom(context)); + expect(helper?.fontSize, theme.textTheme.tabLabelTextStyle.fontSize); }); }