From 46cc363413e0e076612496e683dbb21130cda75b Mon Sep 17 00:00:00 2001 From: lollipopkit Date: Sun, 20 Aug 2023 19:07:20 +0800 Subject: [PATCH] #94 new: option of moving out server func btns --- .dart_tool/flutter_gen/gen_l10n/l10n.dart | 12 ++ .dart_tool/flutter_gen/gen_l10n/l10n_de.dart | 6 + .dart_tool/flutter_gen/gen_l10n/l10n_en.dart | 6 + .dart_tool/flutter_gen/gen_l10n/l10n_id.dart | 6 + .dart_tool/flutter_gen/gen_l10n/l10n_zh.dart | 12 ++ lib/data/res/ui.dart | 2 +- lib/data/store/setting.dart | 6 + lib/l10n/app_de.arb | 2 + lib/l10n/app_en.arb | 2 + lib/l10n/app_id.arb | 2 + lib/l10n/app_zh.arb | 2 + lib/l10n/app_zh_tw.arb | 2 + lib/view/page/full_screen.dart | 2 +- lib/view/page/server/detail.dart | 10 +- lib/view/page/server/tab.dart | 149 ++--------------- lib/view/page/setting.dart | 9 ++ lib/view/widget/server_func_btns.dart | 158 +++++++++++++++++++ 17 files changed, 245 insertions(+), 143 deletions(-) create mode 100644 lib/view/widget/server_func_btns.dart diff --git a/.dart_tool/flutter_gen/gen_l10n/l10n.dart b/.dart_tool/flutter_gen/gen_l10n/l10n.dart index c43b8061..469296d0 100644 --- a/.dart_tool/flutter_gen/gen_l10n/l10n.dart +++ b/.dart_tool/flutter_gen/gen_l10n/l10n.dart @@ -776,6 +776,18 @@ abstract class S { /// **'Mission'** String get mission; + /// No description provided for @moveOutServerFuncBtns. + /// + /// In en, this message translates to: + /// **'Server function button location'** + String get moveOutServerFuncBtns; + + /// No description provided for @moveOutServerFuncBtnsHelp. + /// + /// In en, this message translates to: + /// **'On: can be displayed below each card on the Server Tab page.\nOff: can be displayed at the top of the Server Details page.'** + String get moveOutServerFuncBtnsHelp; + /// No description provided for @ms. /// /// In en, this message translates to: diff --git a/.dart_tool/flutter_gen/gen_l10n/l10n_de.dart b/.dart_tool/flutter_gen/gen_l10n/l10n_de.dart index 6f064e68..1f08e442 100644 --- a/.dart_tool/flutter_gen/gen_l10n/l10n_de.dart +++ b/.dart_tool/flutter_gen/gen_l10n/l10n_de.dart @@ -365,6 +365,12 @@ class SDe extends S { @override String get mission => 'Mission'; + @override + String get moveOutServerFuncBtns => 'Position der Server-Funktionsschaltfläche'; + + @override + String get moveOutServerFuncBtnsHelp => 'Ein: kann unter jeder Karte auf der Registerkarte \"Server\" angezeigt werden.\nAus: kann oben auf der Seite \"Serverdetails\" angezeigt werden.'; + @override String get ms => 'ms'; diff --git a/.dart_tool/flutter_gen/gen_l10n/l10n_en.dart b/.dart_tool/flutter_gen/gen_l10n/l10n_en.dart index 9df931a1..4ba149b2 100644 --- a/.dart_tool/flutter_gen/gen_l10n/l10n_en.dart +++ b/.dart_tool/flutter_gen/gen_l10n/l10n_en.dart @@ -365,6 +365,12 @@ class SEn extends S { @override String get mission => 'Mission'; + @override + String get moveOutServerFuncBtns => 'Server function button location'; + + @override + String get moveOutServerFuncBtnsHelp => 'On: can be displayed below each card on the Server Tab page.\nOff: can be displayed at the top of the Server Details page.'; + @override String get ms => 'ms'; diff --git a/.dart_tool/flutter_gen/gen_l10n/l10n_id.dart b/.dart_tool/flutter_gen/gen_l10n/l10n_id.dart index ac779009..44f73673 100644 --- a/.dart_tool/flutter_gen/gen_l10n/l10n_id.dart +++ b/.dart_tool/flutter_gen/gen_l10n/l10n_id.dart @@ -365,6 +365,12 @@ class SId extends S { @override String get mission => 'Misi'; + @override + String get moveOutServerFuncBtns => 'Lokasi tombol fungsi server'; + + @override + String get moveOutServerFuncBtnsHelp => 'Aktif: dapat ditampilkan di bawah setiap kartu pada halaman Tab Server.\nNonaktif: dapat ditampilkan di bagian atas halaman Rincian Server.'; + @override String get ms => 'MS'; diff --git a/.dart_tool/flutter_gen/gen_l10n/l10n_zh.dart b/.dart_tool/flutter_gen/gen_l10n/l10n_zh.dart index ff294a27..344ae857 100644 --- a/.dart_tool/flutter_gen/gen_l10n/l10n_zh.dart +++ b/.dart_tool/flutter_gen/gen_l10n/l10n_zh.dart @@ -365,6 +365,12 @@ class SZh extends S { @override String get mission => '任务'; + @override + String get moveOutServerFuncBtns => '服务器功能按钮位置'; + + @override + String get moveOutServerFuncBtnsHelp => '开启:可以在服务器 Tab 页的每个卡片下方显示。\n关闭:在服务器详情页顶部显示。'; + @override String get ms => '毫秒'; @@ -1067,6 +1073,12 @@ class SZhTw extends SZh { @override String get mission => '任務'; + @override + String get moveOutServerFuncBtns => '服務器功能按鈕位置'; + + @override + String get moveOutServerFuncBtnsHelp => '開啟:可以在服務器 Tab 頁的每個卡片下方顯示。\n關閉:在服務器詳情頁頂部顯示。'; + @override String get ms => '毫秒'; diff --git a/lib/data/res/ui.dart b/lib/data/res/ui.dart index 86ba8910..0411478a 100644 --- a/lib/data/res/ui.dart +++ b/lib/data/res/ui.dart @@ -4,7 +4,7 @@ import 'package:flutter/material.dart'; const textSize9Grey = TextStyle(color: Colors.grey, fontSize: 9); const textSize11 = TextStyle(fontSize: 11); -const textSize12Grey = TextStyle(color: Colors.grey, fontSize: 11); +const textSize11Grey = TextStyle(color: Colors.grey, fontSize: 11); const textSize13 = TextStyle(fontSize: 13); const textSize13Bold = TextStyle(fontSize: 13, fontWeight: FontWeight.bold); const textSize13Grey = TextStyle(color: Colors.grey, fontSize: 13); diff --git a/lib/data/store/setting.dart b/lib/data/store/setting.dart index ebdf24a2..38e0c90e 100644 --- a/lib/data/store/setting.dart +++ b/lib/data/store/setting.dart @@ -102,4 +102,10 @@ class SettingStore extends PersistentStore { StoreProperty get autoCheckAppUpdate => property('autoCheckAppUpdate', defaultValue: true); + + /// Display server tab function buttons on the bottom of each server card if [true] + /// + /// Otherwise, display them on the top of server detail page + StoreProperty get moveOutServerTabFuncBtns => + property('moveOutServerTabFuncBtns', defaultValue: true); } diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index da322639..097edcbe 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -113,6 +113,8 @@ "maxRetryCountEqual0": "Unbegrenzte Verbindungsversuche zum Server", "min": "min", "mission": "Mission", + "moveOutServerFuncBtns": "Position der Server-Funktionsschaltfläche", + "moveOutServerFuncBtnsHelp": "Ein: kann unter jeder Karte auf der Registerkarte \"Server\" angezeigt werden.\nAus: kann oben auf der Seite \"Serverdetails\" angezeigt werden.", "ms": "ms", "name": "Name", "needRestart": "App muss neugestartet werden", diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 17743b24..e3d697da 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -113,6 +113,8 @@ "maxRetryCountEqual0": "Will retry again and again.", "min": "min", "mission": "Mission", + "moveOutServerFuncBtns": "Server function button location", + "moveOutServerFuncBtnsHelp": "On: can be displayed below each card on the Server Tab page.\nOff: can be displayed at the top of the Server Details page.", "ms": "ms", "name": "Name", "needRestart": "Need to restart app", diff --git a/lib/l10n/app_id.arb b/lib/l10n/app_id.arb index 62556396..df1a80fe 100644 --- a/lib/l10n/app_id.arb +++ b/lib/l10n/app_id.arb @@ -113,6 +113,8 @@ "maxRetryCountEqual0": "Akan mencoba lagi lagi dan lagi.", "min": "Min", "mission": "Misi", + "moveOutServerFuncBtns": "Lokasi tombol fungsi server", + "moveOutServerFuncBtnsHelp": "Aktif: dapat ditampilkan di bawah setiap kartu pada halaman Tab Server.\nNonaktif: dapat ditampilkan di bagian atas halaman Rincian Server.", "ms": "MS", "name": "Nama", "needRestart": "Perlu memulai ulang aplikasi", diff --git a/lib/l10n/app_zh.arb b/lib/l10n/app_zh.arb index a19c6210..65dbf281 100644 --- a/lib/l10n/app_zh.arb +++ b/lib/l10n/app_zh.arb @@ -113,6 +113,8 @@ "maxRetryCountEqual0": "会无限重试", "min": "最小", "mission": "任务", + "moveOutServerFuncBtns": "服务器功能按钮位置", + "moveOutServerFuncBtnsHelp": "开启:可以在服务器 Tab 页的每个卡片下方显示。\n关闭:在服务器详情页顶部显示。", "ms": "毫秒", "name": "名称", "needRestart": "需要重启 App", diff --git a/lib/l10n/app_zh_tw.arb b/lib/l10n/app_zh_tw.arb index 2a18c08e..8a964210 100644 --- a/lib/l10n/app_zh_tw.arb +++ b/lib/l10n/app_zh_tw.arb @@ -113,6 +113,8 @@ "maxRetryCountEqual0": "會無限重試", "min": "最小", "mission": "任務", + "moveOutServerFuncBtns": "服務器功能按鈕位置", + "moveOutServerFuncBtnsHelp": "開啟:可以在服務器 Tab 頁的每個卡片下方顯示。\n關閉:在服務器詳情頁頂部顯示。", "ms": "毫秒", "name": "名稱", "needRestart": "需要重啓 App", diff --git a/lib/view/page/full_screen.dart b/lib/view/page/full_screen.dart index b928ad20..1d434816 100644 --- a/lib/view/page/full_screen.dart +++ b/lib/view/page/full_screen.dart @@ -250,7 +250,7 @@ class _FullScreenPageState extends State with AfterLayoutMixin { ); return Text( topRightStr, - style: textSize12Grey, + style: textSize11Grey, textScaleFactor: 1.0, ); } diff --git a/lib/view/page/server/detail.dart b/lib/view/page/server/detail.dart index 3fe7a4ba..00bbfd12 100644 --- a/lib/view/page/server/detail.dart +++ b/lib/view/page/server/detail.dart @@ -5,6 +5,7 @@ import 'package:toolbox/core/extension/navigator.dart'; import 'package:toolbox/core/extension/order.dart'; import 'package:toolbox/data/model/server/cpu.dart'; import 'package:toolbox/data/model/server/server_private_info.dart'; +import 'package:toolbox/view/widget/server_func_btns.dart'; import '../../../core/extension/numx.dart'; import '../../../core/route.dart'; @@ -95,7 +96,11 @@ class _ServerDetailPageState extends State ), body: ReorderableListView.builder( padding: EdgeInsets.only( - left: 13, right: 13, top: 13, bottom: _media.padding.bottom), + left: 13, + right: 13, + top: 13, + bottom: _media.padding.bottom, + ), onReorder: (int oldIndex, int newIndex) { setState(() { _cardsOrder.move( @@ -106,6 +111,9 @@ class _ServerDetailPageState extends State }); }, footer: height13, + header: _setting.moveOutServerTabFuncBtns.fetch()! + ? null + : RoundRectCard(ServerFuncBtns(spi: widget.spi, s: _s)), itemCount: _cardsOrder.length, buildDefaultDragHandles: false, itemBuilder: (context, index) => ReorderableDelayedDragStartListener( diff --git a/lib/view/page/server/tab.dart b/lib/view/page/server/tab.dart index 05e37773..6bffd12e 100644 --- a/lib/view/page/server/tab.dart +++ b/lib/view/page/server/tab.dart @@ -1,5 +1,3 @@ -import 'dart:io' show Directory, File, Platform, Process; - import 'package:after_layout/after_layout.dart'; import 'package:circle_chart/circle_chart.dart'; import 'package:flutter/material.dart'; @@ -9,16 +7,12 @@ import 'package:provider/provider.dart'; import 'package:toolbox/core/extension/media_queryx.dart'; import 'package:toolbox/core/extension/order.dart'; import 'package:toolbox/data/model/app/net_view.dart'; -import 'package:toolbox/data/model/server/snippet.dart'; -import 'package:toolbox/data/provider/snippet.dart'; -import 'package:toolbox/view/page/process.dart'; -import 'package:toolbox/view/widget/tag/picker.dart'; +import 'package:toolbox/view/widget/server_func_btns.dart'; import 'package:toolbox/view/widget/tag/switcher.dart'; import '../../../core/route.dart'; import '../../../core/utils/misc.dart' hide pathJoin; 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'; @@ -26,12 +20,10 @@ import '../../../data/model/server/server_private_info.dart'; import '../../../data/model/server/server_status.dart'; import '../../../data/provider/server.dart'; import '../../../data/res/color.dart'; -import '../../../data/model/app/menu.dart'; import '../../../data/res/ui.dart'; import '../../../data/store/setting.dart'; import '../../../locator.dart'; import '../../widget/round_rect_card.dart'; -import '../ssh/term.dart'; import 'edit.dart'; class ServerPage extends StatefulWidget { @@ -235,10 +227,11 @@ class _ServerPageState extends State ), ), height13, - SizedBox( - height: 29, - child: _buildMoreBtn(spi), - ), + if (_settingStore.moveOutServerTabFuncBtns.fetch()!) + SizedBox( + height: 27, + child: ServerFuncBtns(spi: spi, s: _s), + ), ]; } @@ -297,14 +290,14 @@ class _ServerPageState extends State onTap: () => _showFailReason(ss), child: Text( _s.viewErr, - style: textSize12Grey, + style: textSize11Grey, textScaleFactor: 1.0, ), ); } return Text( topRightStr, - style: textSize12Grey, + style: textSize11Grey, textScaleFactor: 1.0, ); } @@ -325,84 +318,6 @@ class _ServerPageState extends State ); } - void _onTapMoreBtns(ServerTabMenuType value, ServerPrivateInfo spi) async { - switch (value) { - case ServerTabMenuType.pkg: - AppRoute.pkg(spi: spi).checkClientAndGo( - context: context, - s: _s, - id: spi.id, - ); - break; - case ServerTabMenuType.sftp: - AppRoute.sftp(spi: spi).checkClientAndGo( - context: context, - s: _s, - id: spi.id, - ); - break; - case ServerTabMenuType.snippet: - final provider = locator(); - final snippets = await showDialog>( - context: context, - builder: (_) => TagPicker( - items: provider.snippets, - containsTag: (t, tag) => t.tags?.contains(tag) ?? false, - tags: provider.tags.toSet(), - name: (t) => t.name, - ), - ); - if (snippets == null) { - return; - } - final result = await _serverProvider.runSnippets(spi.id, snippets); - if (result != null && result.isNotEmpty) { - showRoundDialog( - context: context, - title: Text(_s.result), - child: Text(result), - actions: [ - TextButton( - onPressed: () => copy2Clipboard(result), - child: Text(_s.copy), - ) - ], - ); - } - break; - case ServerTabMenuType.docker: - AppRoute.docker(spi: spi).checkClientAndGo( - context: context, - s: _s, - id: spi.id, - ); - break; - case ServerTabMenuType.process: - AppRoute(ProcessPage(spi: spi), 'process page').checkClientAndGo( - context: context, - s: _s, - id: spi.id, - ); - break; - case ServerTabMenuType.terminal: - gotoSSH(spi); - break; - } - } - - Widget _buildMoreBtn(ServerPrivateInfo spi) { - return Row( - mainAxisAlignment: MainAxisAlignment.spaceAround, - children: ServerTabMenuType.values - .map((e) => IconButton( - onPressed: () => _onTapMoreBtns(e, spi), - padding: EdgeInsets.zero, - icon: Icon(e.icon, size: 15), - )) - .toList(), - ); - } - Widget _buildNet(ServerStatus ss) { return ValueListenableBuilder( valueListenable: _settingStore.netViewType.listenable(), @@ -488,7 +403,7 @@ class _ServerPageState extends State child: Text( '${percent.toStringAsFixed(1)}%', textAlign: TextAlign.center, - style: const TextStyle(fontSize: 11), + style: textSize11Grey, textScaleFactor: 1.0, ), ), @@ -497,52 +412,6 @@ class _ServerPageState extends State ); } - Future gotoSSH(ServerPrivateInfo spi) async { - // as a `Mobile first` app -> handle mobile first - // - // run built-in ssh on macOS due to incompatibility - if (!isDesktop || isMacOS) { - AppRoute(SSHPage(spi: spi), 'ssh page').go(context); - return; - } - final extraArgs = []; - if (spi.port != 22) { - extraArgs.addAll(['-p', '${spi.port}']); - } - - final path = () { - final tempKeyFileName = 'srvbox_pk_${spi.pubKeyId}'; - return pathJoin(Directory.systemTemp.path, tempKeyFileName); - }(); - final file = File(path); - final shouldGenKey = spi.pubKeyId != null; - if (shouldGenKey) { - if (await file.exists()) { - await file.delete(); - } - await file.writeAsString(getPrivateKey(spi.pubKeyId!)); - extraArgs.addAll(["-i", path]); - } - - List sshCommand = ["ssh", "${spi.user}@${spi.ip}"] + extraArgs; - final system = Platform.operatingSystem; - switch (system) { - case "windows": - await Process.start("cmd", ["/c", "start"] + sshCommand); - break; - case "linux": - await Process.start("x-terminal-emulator", ["-e"] + sshCommand); - break; - default: - showSnackBar(context, Text('Mismatch system: $system')); - } - // For security reason, delete the private key file after use - if (shouldGenKey) { - if (!await file.exists()) return; - await Future.delayed(const Duration(seconds: 2), file.delete); - } - } - @override bool get wantKeepAlive => true; diff --git a/lib/view/page/setting.dart b/lib/view/page/setting.dart index 8e9ce968..5010d678 100644 --- a/lib/view/page/setting.dart +++ b/lib/view/page/setting.dart @@ -169,6 +169,7 @@ class _SettingPageState extends State { Widget _buildServer() { return Column( children: [ + _buildMoveOutServerFuncBtns(), _buildNetViewType(), _buildUpdateInterval(), _buildMaxRetry(), @@ -969,4 +970,12 @@ class _SettingPageState extends State { }, ); } + + Widget _buildMoveOutServerFuncBtns() { + return ListTile( + title: Text(_s.moveOutServerFuncBtns), + subtitle: Text(_s.moveOutServerFuncBtnsHelp, style: textSize13Grey), + trailing: buildSwitch(context, _setting.moveOutServerTabFuncBtns), + ); + } } diff --git a/lib/view/widget/server_func_btns.dart b/lib/view/widget/server_func_btns.dart new file mode 100644 index 00000000..037f5632 --- /dev/null +++ b/lib/view/widget/server_func_btns.dart @@ -0,0 +1,158 @@ +import 'dart:io'; + +import 'package:flutter/material.dart'; +import 'package:flutter_gen/gen_l10n/l10n.dart'; +import 'package:toolbox/data/provider/server.dart'; + +import '../../core/route.dart'; +import '../../core/utils/misc.dart' hide pathJoin; +import '../../core/utils/platform.dart'; +import '../../core/utils/server.dart'; +import '../../core/utils/ui.dart'; +import '../../data/model/app/menu.dart'; +import '../../data/model/server/server_private_info.dart'; +import '../../data/model/server/snippet.dart'; +import '../../data/provider/snippet.dart'; +import '../../locator.dart'; +import '../page/process.dart'; +import 'tag/picker.dart'; + +class ServerFuncBtns extends StatelessWidget { + const ServerFuncBtns({super.key, required this.spi, required this.s}); + + final ServerPrivateInfo spi; + final S s; + + void _onTapMoreBtns( + ServerTabMenuType value, + ServerPrivateInfo spi, + BuildContext context, + ) async { + switch (value) { + case ServerTabMenuType.pkg: + AppRoute.pkg(spi: spi).checkClientAndGo( + context: context, + s: s, + id: spi.id, + ); + break; + case ServerTabMenuType.sftp: + AppRoute.sftp(spi: spi).checkClientAndGo( + context: context, + s: s, + id: spi.id, + ); + break; + case ServerTabMenuType.snippet: + final provider = locator(); + final snippets = await showDialog>( + context: context, + builder: (_) => TagPicker( + items: provider.snippets, + containsTag: (t, tag) => t.tags?.contains(tag) ?? false, + tags: provider.tags.toSet(), + name: (t) => t.name, + ), + ); + if (snippets == null) { + return; + } + final result = + await locator().runSnippets(spi.id, snippets); + if (result != null && result.isNotEmpty) { + showRoundDialog( + context: context, + title: Text(s.result), + child: Text(result), + actions: [ + TextButton( + onPressed: () => copy2Clipboard(result), + child: Text(s.copy), + ) + ], + ); + } + break; + case ServerTabMenuType.docker: + AppRoute.docker(spi: spi).checkClientAndGo( + context: context, + s: s, + id: spi.id, + ); + break; + case ServerTabMenuType.process: + AppRoute(ProcessPage(spi: spi), 'process page').checkClientAndGo( + context: context, + s: s, + id: spi.id, + ); + break; + case ServerTabMenuType.terminal: + _gotoSSH(spi, context); + break; + } + } + + @override + Widget build(BuildContext context) { + return Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, + children: ServerTabMenuType.values + .map((e) => IconButton( + onPressed: () => _onTapMoreBtns(e, spi, context), + padding: EdgeInsets.zero, + icon: Icon(e.icon, size: 15), + )) + .toList(), + ); + } + + Future _gotoSSH( + ServerPrivateInfo spi, + BuildContext context, + ) async { + // as a `Mobile first` app -> handle mobile first + // + // run built-in ssh on macOS due to incompatibility + if (!isDesktop || isMacOS) { + AppRoute.ssh(spi: spi).go(context); + return; + } + final extraArgs = []; + if (spi.port != 22) { + extraArgs.addAll(['-p', '${spi.port}']); + } + + final path = () { + final tempKeyFileName = 'srvbox_pk_${spi.pubKeyId}'; + return pathJoin(Directory.systemTemp.path, tempKeyFileName); + }(); + final file = File(path); + final shouldGenKey = spi.pubKeyId != null; + if (shouldGenKey) { + if (await file.exists()) { + await file.delete(); + } + await file.writeAsString(getPrivateKey(spi.pubKeyId!)); + extraArgs.addAll(["-i", path]); + } + + List sshCommand = ["ssh", "${spi.user}@${spi.ip}"] + extraArgs; + final system = Platform.operatingSystem; + switch (system) { + case "windows": + await Process.start("cmd", ["/c", "start"] + sshCommand); + break; + case "linux": + await Process.start("x-terminal-emulator", ["-e"] + sshCommand); + break; + default: + showSnackBar(context, Text('Mismatch system: $system')); + } + // For security reason, delete the private key file after use + if (shouldGenKey) { + if (!await file.exists()) return; + await Future.delayed(const Duration(seconds: 2), file.delete); + } + } +}