Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 7 additions & 73 deletions serverbox/lib/view/page/storage/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -91,9 +89,13 @@ class _LocalFilePageState extends ConsumerState<LocalFilePage> {
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),
),
),
);
Expand Down Expand Up @@ -127,27 +129,6 @@ class _LocalFilePageState extends ConsumerState<LocalFilePage> {
),
];

List<ContextMenuAction> _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) {
Expand Down Expand Up @@ -181,51 +162,4 @@ class _LocalFilePageState extends ConsumerState<LocalFilePage> {
}
await handle.refresh();
}

Future<void> _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;
}(),
),
);
}
}
74 changes: 74 additions & 0 deletions serverbox/lib/view/page/storage/local_file_editor.dart
Original file line number Diff line number Diff line change
@@ -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<ContextMenuAction> 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<void> 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,
),
);
}
5 changes: 5 additions & 0 deletions serverbox/lib/view/page/storage/server_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
Expand Down
75 changes: 58 additions & 17 deletions serverbox/packages/fl_lib/lib/src/view/widget/inline_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String>? 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
Expand All @@ -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),
Expand Down Expand Up @@ -91,6 +111,15 @@ class InlineSearchController extends ValueNotifier<String?> {
/// 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 = '';
Expand Down Expand Up @@ -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;
Expand All @@ -127,6 +160,10 @@ class InlineSearchBar extends StatelessWidget {
final Widget child;

final String? hint;
final ValueChanged<String>? onSubmitted;
final FocusOnKeyEventCallback? onKeyEvent;
final FocusNode? focusNode;
final Widget? trailing;

@override
Widget build(BuildContext context) {
Expand All @@ -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'),
Expand Down
Loading
Loading