diff --git a/lib/view/page/process.dart b/lib/view/page/process.dart index 07911a40..8d1c1998 100644 --- a/lib/view/page/process.dart +++ b/lib/view/page/process.dart @@ -43,8 +43,7 @@ class _ProcessPageState extends State { showSnackBar(context, Text(_s.noClient)); return; } - _timer = - Timer.periodic(const Duration(seconds: 3), (_) => _refresh()); + _timer = Timer.periodic(const Duration(seconds: 3), (_) => _refresh()); } Future _refresh() async { diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index d84c0f5d..6a8b635a 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -1,3 +1,5 @@ +import 'dart:io' show File, Platform, Process; + import 'package:after_layout/after_layout.dart'; import 'package:circle_chart/circle_chart.dart'; import 'package:flutter/material.dart'; @@ -15,6 +17,8 @@ import 'package:toolbox/view/widget/tag/picker.dart'; import 'package:toolbox/view/widget/tag/switcher.dart'; import '../../../core/route.dart'; +import '../../../core/utils/platform.dart'; +import '../../../core/utils/server.dart'; import '../../../core/utils/ui.dart'; import '../../../data/model/server/disk.dart'; import '../../../data/model/server/server.dart'; @@ -225,7 +229,6 @@ class _ServerPageState extends State _buildTopRightText(ss, cs), width13, _buildSSHBtn(spi), -// SizedBox(width: 5,), _buildMoreBtn(spi), ], ) @@ -271,12 +274,9 @@ class _ServerPageState extends State } Widget _buildSSHBtn(ServerPrivateInfo spi) { - return IconButton( - icon: const Icon( - Icons.terminal, - size: 21, - ), - onPressed: () => startSSH(spi, context), + return GestureDetector( + child: const Icon(Icons.terminal, size: 21), + onTap: () => gotoSSH(spi), ); } @@ -447,6 +447,38 @@ class _ServerPageState extends State ); } + void gotoSSH(ServerPrivateInfo spi) { + // as a `Mobile first` app -> handle mobile first + if (!isDesktop) { + AppRoute(SSHPage(spi: spi), 'ssh page').go(context); + return; + } + List extarArgs = []; + if (spi.pubKeyId != null) { + String path = "/tmp/.serverbox_pk_${spi.pubKeyId}"; + File(path).openWrite().write(getPrivateKey(spi.pubKeyId!)); + extarArgs += ["-i", path]; + } + List sshCommand = ["ssh", "${spi.user}@${spi.ip}"] + extarArgs; + switch (Platform.operatingSystem) { + case "windows": + Process.start("cmd", ["/c", "start"] + sshCommand); + return; + case "linux": + Process.start("x-terminal-emulator", ["-e"] + sshCommand); + return; + case "macos": + Process.start("osascript", [ + "-e", + 'tell application "Terminal" to do script "${sshCommand.join(" ")}"' + ]); + return; + default: + AppRoute(SSHPage(spi: spi), 'ssh page').go(context); + return; + } + } + @override bool get wantKeepAlive => true; diff --git a/lib/view/page/ssh/term.dart b/lib/view/page/ssh/term.dart index 947c8c7c..88d13393 100644 --- a/lib/view/page/ssh/term.dart +++ b/lib/view/page/ssh/term.dart @@ -1,4 +1,3 @@ -import 'dart:io' show Process, File; import 'dart:async'; import 'dart:convert'; @@ -26,33 +25,6 @@ import '../../../data/store/setting.dart'; import '../../../locator.dart'; import '../storage/sftp.dart'; -startSSH(ServerPrivateInfo spi, BuildContext context) { - if (isLinux || isMacOS) { - unawaited(() async { - List extarArgs = []; - if (spi.pubKeyId != null) { - String path = "/tmp/.serverbox_pk_${spi.pubKeyId}"; - File(path).openWrite().write(getPrivateKey(spi.pubKeyId!)); - extarArgs += ["-i", path]; - } - List sshCommand = ["ssh", spi.user + "@" + spi.ip] + extarArgs; - if (isLinux) { - Process.start("x-terminal-emulator", ["-e"] + sshCommand); - return; - } - if (isMacOS) { - Process.start("osascript", [ - "-e", - 'tell application "Terminal" to do script "${sshCommand.join(" ")}"' - ]); - return; - } - }()); - return; - } - AppRoute(SSHPage(spi: spi), 'ssh page').go(context); -} - class SSHPage extends StatefulWidget { final ServerPrivateInfo spi; final String? initCmd;