refactor(ssh): Update Terminal page to adapt new copy and paste logic (#1054)

* refactor(ssh): Remove terminal copy button and streamline clipboard functionality

Update dependency versions and remove the copy button from the terminal page, integrating clipboard functionality into the terminal controller.

* refactor(ssh): Optimize clipboard paste functionality implementation

Convert the _OnTerminalPaste method to an asynchronous implementation and remove duplicate _paste methods.

* refactor(ssh): Rename clipboard operation function and add new functionality

Renamed _onTerminalPaste to _onClipboardAction and expanded its functionality

Now supports copying selected text to the clipboard and performing paste operations when no text is selected

* fix: Fixed an issue where clipboard operations did not wait for asynchronous completion.

* fix(ssh): Fixed terminal paste operation return logic

Prevented redundant _onTerminalPaste calls after terminal paste operations
This commit is contained in:
GT610
2026-02-27 21:56:16 +08:00
committed by GitHub
parent ad0ef52f71
commit 8b1326ff64
6 changed files with 76 additions and 78 deletions

View File

@@ -8,7 +8,6 @@ import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:icons_plus/icons_plus.dart';
import 'package:server_box/core/chan.dart';
import 'package:server_box/core/extension/context/locale.dart';
import 'package:server_box/core/utils/server.dart';
@@ -187,7 +186,6 @@ class SSHPageState extends ConsumerState<SSHPage>
? CustomAppBar(
leading: BackButton(onPressed: context.pop),
title: Text(widget.args.spi.name),
actions: [_buildCopyBtn],
centerTitle: false,
)
: null,
@@ -257,6 +255,9 @@ class SSHPageState extends ConsumerState<SSHPage>
hideScrollBar: false,
focusNode: widget.args.focusNode,
toolbarBuilder: _buildTerminalToolbar,
onCopied: _onTerminalCopied,
onSelectAll: _onTerminalSelectAll,
onPaste: _onTerminalPaste,
),
),
);
@@ -364,18 +365,39 @@ class SSHPageState extends ConsumerState<SSHPage>
);
}
Widget get _buildCopyBtn {
return IconButton(
icon: Icon(MingCute.copy_2_fill),
tooltip: libL10n.copy,
onPressed: () {
final selected = terminalSelected;
if (selected == null || selected.isEmpty) {
return;
}
Pfs.copy(selected);
},
);
void _onTerminalCopied() {
if (!mounted) return;
context.showSnackBar(libL10n.success);
_terminalController.clearSelection();
}
void _onTerminalSelectAll() {
if (!mounted) return;
_termKey.currentState?.renderTerminal.selectAll();
}
Future<void> _onTerminalPaste() async {
final value = await Clipboard.getData(Clipboard.kTextPlain);
if (!mounted) return;
final text = value?.text;
if (text == null) return;
_terminal.textInput(text);
_terminalController.clearSelection();
}
Future<void> _onClipboardAction() async {
if (_terminalController.selection != null) {
final selectedText = _termKey.currentState?.renderTerminal.selectedText;
if (selectedText != null && selectedText.isNotEmpty) {
await Clipboard.setData(ClipboardData(text: selectedText));
if (!mounted) return;
context.showSnackBar(libL10n.success);
_terminalController.clearSelection();
return;
}
return;
}
await _onTerminalPaste();
}
@override

View File

@@ -44,12 +44,7 @@ extension _VirtKey on SSHPageState {
_terminal.keyInput(TerminalKey.backspace);
break;
case VirtualKeyFunc.clipboard:
final selected = terminalSelected;
if (selected != null) {
Pfs.copy(selected);
} else {
_paste();
}
await _onClipboardAction();
break;
case VirtualKeyFunc.snippet:
final snippetState = ref.read(snippetProvider);
@@ -127,25 +122,6 @@ extension _VirtKey on SSHPageState {
}
}
void _paste() {
Clipboard.getData(Clipboard.kTextPlain).then((value) {
final text = value?.text;
if (text != null) {
_terminal.textInput(text);
} else {
context.showRoundDialog(title: libL10n.error, child: Text(libL10n.empty));
}
});
}
String? get terminalSelected {
final range = _terminalController.selection;
if (range == null) {
return null;
}
return _terminal.buffer.getText(range);
}
void _initVirtKeys() {
final virtKeys = VirtKeyX.loadFromStore();
for (int len = 0; len < virtKeys.length; len += 7) {