From 96e7dd750960a39098368782fe420812a2a78c12 Mon Sep 17 00:00:00 2001 From: Inspire Queue Development Date: Fri, 27 Nov 2026 22:24:43 -0800 Subject: [PATCH] LS-92 Support search callbacks and local server file opening Co-authored-by: multica-agent --- serverbox/lib/view/page/storage/local.dart | 80 +-------- .../view/page/storage/local_file_editor.dart | 74 ++++++++ .../lib/view/page/storage/server_file.dart | 5 + .../lib/src/view/widget/inline_search.dart | 75 +++++++-- .../test/widget/inline_search_bar_test.dart | 114 +++++++++++++ .../widget/local_server_file_open_test.dart | 158 ++++++++++++++++++ 6 files changed, 416 insertions(+), 90 deletions(-) create mode 100644 serverbox/lib/view/page/storage/local_file_editor.dart create mode 100644 serverbox/test/widget/inline_search_bar_test.dart create mode 100644 serverbox/test/widget/local_server_file_open_test.dart diff --git a/serverbox/lib/view/page/storage/local.dart b/serverbox/lib/view/page/storage/local.dart index 811c4f3..1b40f0b 100644 --- a/serverbox/lib/view/page/storage/local.dart +++ b/serverbox/lib/view/page/storage/local.dart @@ -3,15 +3,13 @@ import 'dart:io'; import 'package:fl_lib/fl_lib.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:material_ui/material_ui.dart'; -import 'package:server_box/core/extension/context/locale.dart'; import 'package:server_box/core/utils/local_file_backend.dart'; import 'package:server_box/core/utils/local_files.dart'; import 'package:server_box/data/model/file/file_backend.dart'; import 'package:server_box/data/model/file/file_ref.dart'; import 'package:server_box/data/provider/server/all.dart'; -import 'package:server_box/data/res/misc.dart'; -import 'package:server_box/data/res/store.dart'; import 'package:server_box/view/page/storage/file_browser.dart'; +import 'package:server_box/view/page/storage/local_file_editor.dart'; import 'package:server_box/view/page/storage/show_transfers.dart'; final class LocalFilePageArgs { @@ -91,9 +89,13 @@ class _LocalFilePageState extends ConsumerState { onPathChanged: widget.args?.onPathChanged, extraActions: _actions, createActions: _createActions, - entryActions: _entryActions, + entryActions: (handle, entry, path) => + localFileEntryActions(context, handle, entry, path), labelOf: _labelOf, - onOpenFile: _isPickFile ? null : _openEditor, + onOpenFile: _isPickFile + ? null + : (handle, entry, path) => + openLocalFileEditor(context, handle, entry, path), ), ), ); @@ -127,27 +129,6 @@ class _LocalFilePageState extends ConsumerState { ), ]; - List _entryActions( - FileBrowserHandle handle, - FileEntry entry, - String fullPath, - ) => [ - if (!entry.isDir) ...[ - if (isMobile) - ContextMenuAction( - icon: Icons.edit, - text: libL10n.edit, - onTap: () => _openEditor(handle, entry, fullPath), - ), - ContextMenuAction( - icon: Icons.open_in_new, - text: libL10n.open, - onTap: () => - Pfs.sharePaths(paths: [LocalFileBackend.nativePath(fullPath)]), - ), - ], - ]; - /// A directory directly under [Paths.file] named after a server id is that /// server's downloads, and its id is not what anyone calls it. String? _labelOf(FileEntry entry, String fullPath) { @@ -181,51 +162,4 @@ class _LocalFilePageState extends ConsumerState { } await handle.refresh(); } - - Future _openEditor( - FileBrowserHandle handle, - FileEntry entry, - String fullPath, - ) async { - final size = entry.size ?? (await _backend.stat(fullPath))?.size ?? 0; - if (size > Miscs.editorMaxSize) { - if (!mounted) return; - await context.showRoundDialog( - title: libL10n.attention, - child: Text( - l10n.fileTooLarge( - entry.name, - size.bytes2Str, - Miscs.editorMaxSize.bytes2Str, - ), - ), - ); - return; - } - if (!mounted) return; - - await EditorPage.route.go( - context, - args: EditorPageArgs( - path: LocalFileBackend.nativePath(fullPath), - onSave: (_) { - Toast.show(libL10n.saved); - handle.refresh(); - }, - closeAfterSave: Stores.setting.closeAfterSave.fetch(), - softWrap: Stores.setting.editorSoftWrap.fetch(), - enableHighlight: Stores.setting.editorHighlight.fetch(), - lightTheme: HighlightTheme.fromThemeMapKey( - Stores.setting.editorTheme.fetch(), - ), - darkTheme: HighlightTheme.fromThemeMapKey( - Stores.setting.editorDarkTheme.fetch(), - ), - fontFamily: () { - final font = Stores.setting.editorFontFamily.fetch(); - return font.isEmpty ? null : font; - }(), - ), - ); - } } diff --git a/serverbox/lib/view/page/storage/local_file_editor.dart b/serverbox/lib/view/page/storage/local_file_editor.dart new file mode 100644 index 0000000..1401fb0 --- /dev/null +++ b/serverbox/lib/view/page/storage/local_file_editor.dart @@ -0,0 +1,74 @@ +import 'package:fl_lib/fl_lib.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:server_box/core/extension/context/locale.dart'; +import 'package:server_box/core/utils/local_file_backend.dart'; +import 'package:server_box/data/model/file/file_backend.dart'; +import 'package:server_box/data/res/misc.dart'; +import 'package:server_box/data/res/store.dart'; +import 'package:server_box/view/page/storage/file_browser.dart'; + +List localFileEntryActions( + BuildContext context, + FileBrowserHandle handle, + FileEntry entry, + String fullPath, +) => [ + if (!entry.isDir) ...[ + if (isMobile) + ContextMenuAction( + icon: Icons.edit, + text: libL10n.edit, + onTap: () => openLocalFileEditor(context, handle, entry, fullPath), + ), + ContextMenuAction( + icon: Icons.open_in_new, + text: libL10n.open, + onTap: () => + Pfs.sharePaths(paths: [LocalFileBackend.nativePath(fullPath)]), + ), + ], +]; + +Future openLocalFileEditor( + BuildContext context, + FileBrowserHandle handle, + FileEntry entry, + String fullPath, +) async { + final size = entry.size ?? (await handle.backend.stat(fullPath))?.size ?? 0; + if (!context.mounted) return; + if (size > Miscs.editorMaxSize) { + await context.showRoundDialog( + title: libL10n.attention, + child: Text( + l10n.fileTooLarge( + entry.name, + size.bytes2Str, + Miscs.editorMaxSize.bytes2Str, + ), + ), + ); + return; + } + + final settings = Stores.setting; + final font = settings.editorFontFamily.fetch(); + await EditorPage.route.go( + context, + args: EditorPageArgs( + path: LocalFileBackend.nativePath(fullPath), + onSave: (_) { + Toast.show(libL10n.saved); + handle.refresh(); + }, + closeAfterSave: settings.closeAfterSave.fetch(), + softWrap: settings.editorSoftWrap.fetch(), + enableHighlight: settings.editorHighlight.fetch(), + lightTheme: HighlightTheme.fromThemeMapKey(settings.editorTheme.fetch()), + darkTheme: HighlightTheme.fromThemeMapKey( + settings.editorDarkTheme.fetch(), + ), + fontFamily: font.isEmpty ? null : font, + ), + ); +} diff --git a/serverbox/lib/view/page/storage/server_file.dart b/serverbox/lib/view/page/storage/server_file.dart index 77b9fb8..bbd5538 100644 --- a/serverbox/lib/view/page/storage/server_file.dart +++ b/serverbox/lib/view/page/storage/server_file.dart @@ -12,6 +12,7 @@ import 'package:server_box/data/model/server/system.dart'; import 'package:server_box/data/provider/server/single.dart'; import 'package:server_box/data/res/store.dart'; import 'package:server_box/view/page/storage/file_browser.dart'; +import 'package:server_box/view/page/storage/local_file_editor.dart'; import 'package:server_box/view/page/storage/sftp.dart'; import 'package:server_box/view/page/storage/show_transfers.dart'; @@ -130,6 +131,10 @@ class _LocalServerFilePage extends StatelessWidget { onTap: () => showTransfers(context), ), ], + entryActions: (handle, entry, path) => + localFileEntryActions(context, handle, entry, path), + onOpenFile: (handle, entry, path) => + openLocalFileEditor(context, handle, entry, path), // This device's files are this device's, whichever page lists them: // a transfer from here needs no server to reach them. refOf: LocalFileRef.new, diff --git a/serverbox/packages/fl_lib/lib/src/view/widget/inline_search.dart b/serverbox/packages/fl_lib/lib/src/view/widget/inline_search.dart index 14b4457..4c56927 100644 --- a/serverbox/packages/fl_lib/lib/src/view/widget/inline_search.dart +++ b/serverbox/packages/fl_lib/lib/src/view/widget/inline_search.dart @@ -18,6 +18,10 @@ class InlineSearchField extends StatelessWidget { required this.onChanged, required this.onClose, this.hint, + this.onSubmitted, + this.onKeyEvent, + this.focusNode, + this.trailing, }); final TextEditingController controller; @@ -29,6 +33,15 @@ class InlineSearchField extends StatelessWidget { final String? hint; + /// Handles submission while keeping focus available to the caller. + /// Without a handler, submission dismisses the keyboard. + final ValueChanged? onSubmitted; + + /// Return [KeyEventResult.ignored] to allow normal text-field shortcuts. + final FocusOnKeyEventCallback? onKeyEvent; + final FocusNode? focusNode; + final Widget? trailing; + @override Widget build(BuildContext context) { // The bar is the field's box; a theme's form-input border would be a @@ -38,27 +51,34 @@ class InlineSearchField extends StatelessWidget { children: [ const SizedBox(width: 13), Expanded( - child: TextField( - controller: controller, - autofocus: true, - textInputAction: TextInputAction.search, - onChanged: onChanged, - // Leaves the field but not the search: what was typed is still - // narrowing the list, and dismissing the keyboard to look at it is - // the reason to submit. - onSubmitted: (_) => FocusScope.of(context).unfocus(), - style: style.textColor == null - ? null - : TextStyle(color: style.textColor), - decoration: bareInputDecoration( - isDense: true, - hintText: hint ?? libL10n.search, - hintStyle: style.hintColor == null + child: Focus( + canRequestFocus: false, + onKeyEvent: onKeyEvent, + child: TextField( + controller: controller, + focusNode: focusNode, + autofocus: true, + textInputAction: TextInputAction.search, + onChanged: onChanged, + onEditingComplete: onSubmitted == null ? null - : TextStyle(color: style.hintColor), + : controller.clearComposing, + onSubmitted: + onSubmitted ?? (_) => FocusScope.of(context).unfocus(), + style: style.textColor == null + ? null + : TextStyle(color: style.textColor), + decoration: bareInputDecoration( + isDense: true, + hintText: hint ?? libL10n.search, + hintStyle: style.hintColor == null + ? null + : TextStyle(color: style.hintColor), + ), ), ), ), + ?trailing, Btn.icon( text: libL10n.close, icon: Icon(Icons.close, size: 18, color: style.iconColor), @@ -91,6 +111,15 @@ class InlineSearchController extends ValueNotifier { /// empty, so a caller can treat "searching for nothing" as "not searching". String get needle => value?.trim().toLowerCase() ?? ''; + /// Sets the search text and places the caret at the end. + void setQuery(String query) { + text.value = TextEditingValue( + text: query, + selection: TextSelection.collapsed(offset: query.length), + ); + value = query; + } + void start() { text.clear(); value = ''; @@ -119,6 +148,10 @@ class InlineSearchBar extends StatelessWidget { required this.controller, required this.child, this.hint, + this.onSubmitted, + this.onKeyEvent, + this.focusNode, + this.trailing, }); final InlineSearchController controller; @@ -127,6 +160,10 @@ class InlineSearchBar extends StatelessWidget { final Widget child; final String? hint; + final ValueChanged? onSubmitted; + final FocusOnKeyEventCallback? onKeyEvent; + final FocusNode? focusNode; + final Widget? trailing; @override Widget build(BuildContext context) { @@ -141,6 +178,10 @@ class InlineSearchBar extends StatelessWidget { onChanged: (value) => controller.value = value, onClose: controller.end, hint: hint, + onSubmitted: onSubmitted, + onKeyEvent: onKeyEvent, + focusNode: focusNode, + trailing: trailing, ) : KeyedSubtree( key: const ValueKey('inline-search-rest'), diff --git a/serverbox/test/widget/inline_search_bar_test.dart b/serverbox/test/widget/inline_search_bar_test.dart new file mode 100644 index 0000000..3a97364 --- /dev/null +++ b/serverbox/test/widget/inline_search_bar_test.dart @@ -0,0 +1,114 @@ +import 'package:fl_lib/fl_lib.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; + +void main() { + late InlineSearchController controller; + + setUp(() => controller = InlineSearchController()); + tearDown(() => controller.dispose()); + + Future pumpBar( + WidgetTester tester, { + ValueChanged? onSubmitted, + FocusOnKeyEventCallback? onKeyEvent, + FocusNode? focusNode, + Widget? trailing, + }) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: InlineSearchBar( + controller: controller, + onSubmitted: onSubmitted, + onKeyEvent: onKeyEvent, + focusNode: focusNode, + trailing: trailing, + child: const Text('Files'), + ), + ), + ), + ); + controller.start(); + await tester.pumpAndSettle(); + } + + testWidgets('default submission dismisses focus and retains the query', ( + tester, + ) async { + await pumpBar(tester); + final field = find.byType(TextField); + final editable = tester.widget(find.byType(EditableText)); + expect(editable.focusNode.hasFocus, isTrue); + + await tester.enterText(field, 'report.txt'); + await tester.testTextInput.receiveAction(TextInputAction.search); + await tester.pumpAndSettle(); + + expect(editable.focusNode.hasFocus, isFalse); + expect(controller.needle, 'report.txt'); + expect(field, findsOneWidget); + }); + + testWidgets('submission and navigation use the caller’s handlers', ( + tester, + ) async { + final focus = FocusNode(); + addTearDown(focus.dispose); + final submitted = []; + final navigation = []; + await pumpBar( + tester, + focusNode: focus, + onSubmitted: submitted.add, + onKeyEvent: (_, event) { + if (event is KeyDownEvent && + event.logicalKey == LogicalKeyboardKey.arrowDown) { + navigation.add(event.logicalKey); + return KeyEventResult.handled; + } + return KeyEventResult.ignored; + }, + ); + expect(tester.widget(find.byType(TextField)).focusNode, focus); + expect(focus.hasFocus, isTrue); + + await tester.enterText(find.byType(TextField), 'example'); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown); + await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft); + expect(controller.text.selection, const TextSelection.collapsed(offset: 6)); + await tester.testTextInput.receiveAction(TextInputAction.search); + await tester.pumpAndSettle(); + + expect(navigation, [LogicalKeyboardKey.arrowDown]); + expect(submitted, ['example']); + expect(focus.hasFocus, isTrue); + }); + + testWidgets('a trailing action pre-fills the query and places the caret', ( + tester, + ) async { + const query = ' /Example/Report.txt '; + await pumpBar( + tester, + trailing: IconButton( + tooltip: 'Paste path', + onPressed: () => controller.setQuery(query), + icon: const Icon(Icons.paste), + ), + ); + final action = find.byTooltip('Paste path'); + final close = find.byTooltip(libL10n.close); + expect(tester.getCenter(action).dx, lessThan(tester.getCenter(close).dx)); + + await tester.tap(action); + await tester.pumpAndSettle(); + + final text = tester.widget(find.byType(TextField)).controller!; + expect(text.text, query); + expect(text.selection, TextSelection.collapsed(offset: query.length)); + expect(controller.value, query); + expect(controller.needle, '/example/report.txt'); + }); +} diff --git a/serverbox/test/widget/local_server_file_open_test.dart b/serverbox/test/widget/local_server_file_open_test.dart new file mode 100644 index 0000000..8129a5b --- /dev/null +++ b/serverbox/test/widget/local_server_file_open_test.dart @@ -0,0 +1,158 @@ +import 'dart:io'; + +import 'package:fl_lib/fl_lib.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:server_box/core/extension/context/locale.dart'; +import 'package:server_box/data/model/server/server_private_info.dart'; +import 'package:server_box/data/provider/server/single.dart'; +import 'package:server_box/data/res/misc.dart'; +import 'package:server_box/data/res/status.dart'; +import 'package:server_box/data/res/store.dart'; +import 'package:server_box/data/store/history.dart'; +import 'package:server_box/data/store/setting.dart'; +import 'package:server_box/generated/l10n/l10n.dart'; +import 'package:server_box/view/page/storage/file_browser.dart'; +import 'package:server_box/view/page/storage/server_file.dart'; +import 'package:server_box/view/page/storage/sftp.dart'; + +import '../helpers/test_db.dart'; + +const _spi = Spi(name: 'Test device', id: 'test-local', local: true); +const _largeName = 'oversized.txt'; + +class _LocalServerNotifier extends ServerNotifier { + @override + ServerState build(String id) => + ServerState(spi: _spi, status: InitStatus.status); +} + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + late Directory directory; + late File report; + + setUp(() async { + directory = await Directory.systemTemp.createTemp('local-server-file-'); + report = await File( + '${directory.path}/report.txt', + ).writeAsString('Example report'); + final oversized = await File( + '${directory.path}/$_largeName', + ).open(mode: FileMode.write); + await oversized.truncate(Miscs.editorMaxSize + 1); + await oversized.close(); + await Directory('${directory.path}/folder').create(); + await openTestDb(); + getIt.registerSingleton(SettingStore('setting_test')); + getIt.registerSingleton(HistoryStore('history_test')); + }); + + tearDown(() async { + await getIt.reset(); + await closeTestDb(); + await directory.delete(recursive: true); + }); + + Future pumpBrowser(WidgetTester tester, {bool isSelect = false}) async { + await tester.pumpWidget( + ProviderScope( + overrides: [ + serverProvider(_spi.id).overrideWith(_LocalServerNotifier.new), + ], + child: MaterialApp( + localizationsDelegates: appLocalizationsDelegates, + supportedLocales: AppLocalizations.supportedLocales, + home: ServerFilePage( + args: SftpPageArgs( + spi: _spi, + initPath: directory.path.replaceAll(r'\', '/'), + isSelect: isSelect, + ), + ), + ), + ), + ); + // Wait for both real filesystem I/O and the widgets driven by fake time. + final row = find.text(_largeName); + final wait = Stopwatch()..start(); + const interval = Duration(milliseconds: 20); + while (row.evaluate().isEmpty && + wait.elapsed < const Duration(seconds: 5)) { + await tester.runAsync(() => Future.delayed(interval)); + await tester.pump(interval); + } + expect(row, findsOneWidget); + await tester.pumpAndSettle(); + } + + String sizeWarning() => l10n.fileTooLarge( + _largeName, + (Miscs.editorMaxSize + 1).bytes2Str, + Miscs.editorMaxSize.bytes2Str, + ); + + testWidgets('a local file opens the editor and has file menu actions', ( + tester, + ) async { + await pumpBrowser(tester); + final browser = tester.widget( + find.byType(FileBrowserPage), + ); + expect(browser.args.onOpenFile, isNotNull); + expect(browser.args.entryActions, isNotNull); + + final row = find.text('report.txt'); + await tester.longPress(row); + await tester.pumpAndSettle(); + expect(find.text(libL10n.open), findsOneWidget); + expect(find.text(libL10n.edit), isMobile ? findsOneWidget : findsNothing); + Navigator.of(tester.element(find.text(libL10n.open))).pop(); + await tester.pumpAndSettle(); + + await tester.longPress(find.text('folder')); + await tester.pumpAndSettle(); + expect(find.text(libL10n.open), findsNothing); + expect(find.text(libL10n.edit), findsNothing); + Navigator.of(tester.element(find.byType(FileBrowserPage))).pop(); + await tester.pumpAndSettle(); + + await tester.tap(row); + await tester.pump(); + await tester.pump(Durations.long4); + expect( + tester.widget(find.byType(EditorPage)).args?.path, + report.path, + ); + }); + + testWidgets('opening an oversized local file shows its size and limit', ( + tester, + ) async { + await pumpBrowser(tester); + + await tester.tap(find.text(_largeName)); + await tester.pumpAndSettle(); + + expect(find.text(sizeWarning()), findsOneWidget); + expect(find.byType(EditorPage), findsNothing); + }); + + testWidgets('directory selection leaves file taps and menus inert', ( + tester, + ) async { + await pumpBrowser(tester, isSelect: true); + final row = find.text(_largeName); + + await tester.tap(row); + await tester.longPress(row); + await tester.pumpAndSettle(); + + expect(find.text(sizeWarning()), findsNothing); + expect(find.text(libL10n.open), findsNothing); + expect(find.byType(EditorPage), findsNothing); + expect(find.byTooltip(libL10n.ok), findsOneWidget); + }); +}