new: ssh tab

This commit is contained in:
lollipopkit
2023-11-12 15:58:54 +08:00
parent 8693ce07a2
commit 790812901d
8 changed files with 195 additions and 101 deletions

View File

@@ -111,7 +111,16 @@ class _HomePageState extends State<HomePage>
return Scaffold(
drawer: _buildDrawer(),
appBar: _buildAppBar(),
appBar: CustomAppBar(
title: const Text(BuildData.name),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.developer_mode, size: 23),
tooltip: l10n.debug,
onPressed: () => AppRoute.debug().go(context),
),
],
),
body: PageView.builder(
controller: _pageController,
itemCount: AppTab.values.length,
@@ -129,37 +138,6 @@ class _HomePageState extends State<HomePage>
);
}
PreferredSizeWidget _buildAppBar() {
final actions = <Widget>[
IconButton(
icon: const Icon(Icons.developer_mode, size: 23),
tooltip: l10n.debug,
onPressed: () => AppRoute.debug().go(context),
),
];
if (isDesktop && _selectIndex.value == AppTab.server.index) {
actions.add(
ValueBuilder(
listenable: _selectIndex,
build: () {
if (_selectIndex.value != AppTab.server.index) {
return const SizedBox();
}
return IconButton(
icon: const Icon(Icons.refresh, size: 23),
tooltip: 'Refresh',
onPressed: () => Pros.server.refreshData(onlyFailed: true),
);
},
),
);
}
return CustomAppBar(
title: const Text(BuildData.name),
actions: actions,
);
}
Widget _buildBottomBar() {
return NavigationBar(
selectedIndex: _selectIndex.value,
@@ -185,16 +163,16 @@ class _HomePageState extends State<HomePage>
label: l10n.server,
selectedIcon: const Icon(Icons.cloud),
),
const NavigationDestination(
icon: Icon(Icons.terminal_outlined),
label: 'SSH',
selectedIcon: Icon(Icons.terminal),
),
NavigationDestination(
icon: const Icon(Icons.snippet_folder_outlined),
label: l10n.snippet,
selectedIcon: const Icon(Icons.snippet_folder),
),
const NavigationDestination(
icon: Icon(Icons.network_check_outlined),
label: 'Ping',
selectedIcon: Icon(Icons.network_check),
),
],
);
}

View File

@@ -12,6 +12,7 @@ import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/core/extension/context/snackbar.dart';
import 'package:toolbox/core/utils/platform/base.dart';
import 'package:toolbox/core/utils/share.dart';
import 'package:toolbox/data/model/server/server.dart';
import 'package:toolbox/data/model/server/snippet.dart';
import 'package:toolbox/data/provider/virtual_keyboard.dart';
import 'package:toolbox/data/res/provider.dart';
@@ -19,13 +20,12 @@ import 'package:toolbox/data/res/store.dart';
import 'package:xterm/core.dart';
import 'package:xterm/ui.dart' hide TerminalThemes;
import '../../core/route.dart';
import '../../core/utils/misc.dart';
import '../../core/utils/server.dart';
import '../../data/model/server/server_private_info.dart';
import '../../data/model/ssh/virtual_key.dart';
import '../../data/res/color.dart';
import '../../data/res/terminal.dart';
import '../../../core/route.dart';
import '../../../core/utils/misc.dart';
import '../../../data/model/server/server_private_info.dart';
import '../../../data/model/ssh/virtual_key.dart';
import '../../../data/res/color.dart';
import '../../../data/res/terminal.dart';
const echoPWD = 'echo \$PWD';
@@ -38,7 +38,7 @@ class SSHPage extends StatefulWidget {
_SSHPageState createState() => _SSHPageState();
}
class _SSHPageState extends State<SSHPage> {
class _SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
final _keyboard = VirtKeyProvider();
late final _terminal = Terminal(inputHandler: _keyboard);
final TerminalController _terminalController = TerminalController();
@@ -53,8 +53,9 @@ class _SSHPageState extends State<SSHPage> {
bool _isDark = false;
Timer? _virtKeyLongPressTimer;
SSHClient? _client;
SSHSession? _session;
late final Server? _server = widget.spi.server;
late final SSHClient? _client = _server?.client;
late final SSHSession? _session;
Timer? _discontinuityTimer;
@override
@@ -76,11 +77,13 @@ class _SSHPageState extends State<SSHPage> {
super.dispose();
_virtKeyLongPressTimer?.cancel();
_terminalController.dispose();
if (_client?.isClosed == false) {
try {
_client?.close();
} catch (_) {}
}
/// Use the same [SSHClient], so don't close it
// if (_client?.isClosed == false) {
// try {
// _client?.close();
// } catch (_) {}
// }
_discontinuityTimer?.cancel();
}
@@ -100,6 +103,7 @@ class _SSHPageState extends State<SSHPage> {
@override
Widget build(BuildContext context) {
super.build(context);
Widget child = Scaffold(
backgroundColor: _terminalTheme.background,
body: _buildBody(),
@@ -320,26 +324,26 @@ class _SSHPageState extends State<SSHPage> {
Future<void> _initTerminal() async {
_write('Connecting...\r\n');
_client = await genClient(
widget.spi,
onStatus: (p0) {
switch (p0) {
case GenSSHClientStatus.socket:
_write('Destination: ${widget.spi.id}');
return _write('Establishing socket...');
case GenSSHClientStatus.key:
return _write('Using private key to connect...');
case GenSSHClientStatus.pwd:
return _write('Sending password to auth...');
}
},
timeout: Stores.setting.timeoutD,
);
_write('Connected\r\n');
// _client = await genClient(
// widget.spi,
// onStatus: (p0) {
// switch (p0) {
// case GenSSHClientStatus.socket:
// _write('Destination: ${widget.spi.id}');
// return _write('Establishing socket...');
// case GenSSHClientStatus.key:
// return _write('Using private key to connect...');
// case GenSSHClientStatus.pwd:
// return _write('Sending password to auth...');
// }
// },
// timeout: Stores.setting.timeoutD,
// );
// _write('Connected\r\n');
_write('Terminal size: ${_terminal.viewWidth}x${_terminal.viewHeight}\r\n');
_write('Starting shell...\r\n');
_session = await _client!.shell(
_session = await _client?.shell(
pty: SSHPtyConfig(
width: _terminal.viewWidth,
height: _terminal.viewHeight,
@@ -353,8 +357,8 @@ class _SSHPageState extends State<SSHPage> {
return;
}
_terminal.buffer.clear();
_terminal.buffer.setCursor(0, 0);
// _terminal.buffer.clear();
// _terminal.buffer.setCursor(0, 0);
_terminal.onOutput = (data) {
_session?.write(utf8.encode(data) as Uint8List);
@@ -424,4 +428,7 @@ class _SSHPageState extends State<SSHPage> {
],
);
}
@override
bool get wantKeepAlive => true;
}

110
lib/view/page/ssh/tab.dart Normal file
View File

@@ -0,0 +1,110 @@
import 'package:flutter/material.dart';
import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/data/model/server/server.dart';
import 'package:toolbox/data/res/provider.dart';
import 'package:toolbox/view/page/ssh/page.dart';
class SSHTabPage extends StatefulWidget {
const SSHTabPage({super.key});
@override
_SSHTabPageState createState() => _SSHTabPageState();
}
class _SSHTabPageState extends State<SSHTabPage>
with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
final _tabIds = <String, SSHPage>{};
final _tabKeys = <String, GlobalKey>{};
late var _tabController = TabController(
length: _tabIds.length,
vsync: this,
);
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
appBar: TabBar(
controller: _tabController,
tabs: _tabIds.keys.map(_buildTabItem).toList(),
isScrollable: true,
),
body: _buildBody(),
floatingActionButton: _buildFAB(),
);
}
Widget _buildTabItem(String e) {
return Tab(
child: Row(
children: [
Text(e),
IconButton(
icon: const Icon(Icons.close),
padding: EdgeInsets.zero,
onPressed: () {
_tabKeys[e]?.currentState?.dispose();
_tabIds.remove(e);
_refreshTabs();
},
),
],
),
);
}
Widget _buildFAB() {
return FloatingActionButton(
onPressed: () async {
final spi = (await context.showPickDialog<Server>(
items: Pros.server.servers.toList(),
name: (e) => e.spi.name,
multi: false,
))
?.first
.spi;
if (spi == null) {
return;
}
final name = () {
if (_tabIds.containsKey(spi.name)) {
return '${spi.name}(${_tabIds.length + 1})';
}
return spi.name;
}();
final key = GlobalKey(debugLabel: 'sshTabPage_$name');
_tabIds[name] = SSHPage(
key: key,
spi: spi,
);
_tabKeys[name] = key;
_refreshTabs();
_tabController.animateTo(_tabIds.length - 1);
},
child: const Icon(Icons.add),
);
}
Widget _buildBody() {
if (_tabIds.isEmpty) {
return const Center(
child: Text('Click the fab to open a session'),
);
}
return TabBarView(
controller: _tabController,
children: _tabIds.values.toList(),
);
}
void _refreshTabs() {
_tabController = TabController(
length: _tabIds.length,
vsync: this,
);
setState(() {});
}
@override
bool get wantKeepAlive => true;
}