opt.: ssh tab

This commit is contained in:
lollipopkit
2024-06-08 21:39:42 +08:00
parent 46b98df153
commit e5ef28415b
5 changed files with 70 additions and 60 deletions

View File

@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:after_layout/after_layout.dart';
import 'package:dartssh2/dartssh2.dart';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
@@ -49,7 +50,8 @@ class SSHPage extends StatefulWidget {
const _horizonPadding = 7.0;
class SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
class SSHPageState extends State<SSHPage>
with AutomaticKeepAliveClientMixin, AfterLayoutMixin {
final _keyboard = VirtKeyProvider();
late final _terminal = Terminal(inputHandler: _keyboard);
final TerminalController _terminalController = TerminalController();
@@ -73,13 +75,6 @@ class SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
super.initState();
_initStoredCfg();
_initVirtKeys();
Future.delayed(const Duration(milliseconds: 77), () async {
_showHelp();
await _initTerminal();
if (Stores.setting.sshWakeLock.fetch()) WakelockPlus.enable();
});
}
@override
@@ -153,7 +148,7 @@ class SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
textStyle: _terminalStyle,
theme: _terminalTheme,
deleteDetection: isMobile,
autofocus: true,
autofocus: false,
keyboardAppearance: _isDark ? Brightness.dark : Brightness.light,
showToolbar: isMobile,
viewOffset: Offset(
@@ -432,6 +427,8 @@ class SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
}
}
SSHPage.focusNode.requestFocus();
await session.done;
if (mounted && widget.notFromTab) {
context.pop();
@@ -518,22 +515,30 @@ class SSHPageState extends State<SSHPage> with AutomaticKeepAliveClientMixin {
_terminalStyle = TerminalStyle.fromTextStyle(textStyle);
}
void _showHelp() {
if (!Stores.setting.sshTermHelpShown.fetch()) {
context.showRoundDialog(
title: l10n.doc,
child: Text(l10n.sshTermHelp),
actions: [
TextButton(
onPressed: () {
Stores.setting.sshTermHelpShown.put(true);
context.pop();
},
child: Text(l10n.noPromptAgain),
),
],
);
}
Future<void> _showHelp() async {
if (Stores.setting.sshTermHelpShown.fetch()) return;
return await context.showRoundDialog(
title: l10n.doc,
child: Text(l10n.sshTermHelp),
actions: [
TextButton(
onPressed: () {
Stores.setting.sshTermHelpShown.put(true);
context.pop();
},
child: Text(l10n.noPromptAgain),
),
],
);
}
@override
FutureOr<void> afterFirstLayout(BuildContext context) async {
await _showHelp();
await _initTerminal();
if (Stores.setting.sshWakeLock.fetch()) WakelockPlus.enable();
}
}

View File

@@ -43,10 +43,10 @@ class _SSHTabPageState extends State<SSHTabPage>
},
),
body: _buildBody(),
floatingActionButton: ListenableBuilder(
floatingActionButton: ValBuilder(
listenable: _fabVN,
builder: (_, __) {
if (_fabVN.value != 0) return const SizedBox();
builder: (idx) {
if (idx != 0) return const SizedBox();
return FloatingActionButton(
heroTag: 'sshAddServer',
onPressed: () => AppRoutes.serverEdit().go(context),
@@ -60,11 +60,12 @@ class _SSHTabPageState extends State<SSHTabPage>
void _onTapTab(int idx) async {
await _toPage(idx);
_fabVN.value = idx;
FocusScope.of(context).unfocus();
SSHPage.focusNode.unfocus();
}
void _onTapClose(String name) async {
SSHPage.focusNode.unfocus();
final confirm = await showDialog<bool>(
context: context,
builder: (context) {
@@ -89,6 +90,8 @@ class _SSHTabPageState extends State<SSHTabPage>
_tabMap.remove(name);
_tabRN.build();
_pageCtrl.previousPage(
duration: Durations.medium1, curve: Curves.fastEaseInToSlowEaseOut);
}
Widget _buildAddPage() {
@@ -134,6 +137,7 @@ class _SSHTabPageState extends State<SSHTabPage>
final name = _tabMap.keys.elementAt(idx);
return _tabMap[name]?.page ?? UIs.placeholder;
},
onPageChanged: (value) => _fabVN.value = value,
);
},
);
@@ -141,12 +145,14 @@ class _SSHTabPageState extends State<SSHTabPage>
void _onTapInitCard(ServerPrivateInfo spi) async {
final name = () {
final reg = RegExp(r'\((\d+)\)');
final reg = RegExp('${spi.name}\\((\\d+)\\)');
final idxs = _tabMap.keys
.map((e) => reg.firstMatch(e))
.map((e) => e?.group(1))
.where((e) => e != null);
if (idxs.isEmpty) return spi.name;
if (idxs.isEmpty) {
return _tabMap.keys.contains(spi.name) ? '${spi.name}(1)' : spi.name;
}
final biggest = idxs.reduce((a, b) => a!.length > b!.length ? a : b);
final biggestInt = int.tryParse(biggest ?? '0');
if (biggestInt != null && biggestInt > 0) {
@@ -172,7 +178,6 @@ class _SSHTabPageState extends State<SSHTabPage>
await Future.delayed(Durations.short3);
final idx = _tabMap.keys.toList().indexOf(name);
await _toPage(idx);
_fabVN.value = idx;
}
Future<void> _toPage(int idx) => _pageCtrl.animateToPage(idx,