From ce050e250d9e2d3c1dec467209fa7844e522020f Mon Sep 17 00:00:00 2001 From: csf Date: Tue, 16 Aug 2022 21:27:21 +0800 Subject: [PATCH] desktop close connection tab (remote page) --- flutter/lib/common.dart | 7 ++++--- flutter/lib/desktop/pages/remote_page.dart | 2 +- .../lib/desktop/widgets/tabbar_widget.dart | 20 +++++++++++++++++++ .../lib/mobile/pages/file_manager_page.dart | 2 +- flutter/lib/mobile/pages/remote_page.dart | 2 +- flutter/lib/mobile/pages/scan_page.dart | 2 +- flutter/lib/mobile/widgets/dialog.dart | 6 +++--- flutter/lib/models/model.dart | 4 ++-- 8 files changed, 33 insertions(+), 12 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 0070d5294..43b925904 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:desktop_multi_window/desktop_multi_window.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart'; import 'package:get/instance_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:window_manager/window_manager.dart'; @@ -66,11 +67,11 @@ final ButtonStyle flatButtonStyle = TextButton.styleFrom( ), ); -backToHomePage() { +closeConnection({String? id}) { if (isAndroid || isIOS) { Navigator.popUntil(globalKey.currentContext!, ModalRoute.withName("/")); } else { - // TODO desktop + closeTab(id); } } @@ -306,7 +307,7 @@ void msgBox( 0, wrap(translate('OK'), () { dialogManager.dismissAll(); - backToHomePage(); + closeConnection(); })); } if (hasCancel == null) { diff --git a/flutter/lib/desktop/pages/remote_page.dart b/flutter/lib/desktop/pages/remote_page.dart index aa839fd01..8aba86d0f 100644 --- a/flutter/lib/desktop/pages/remote_page.dart +++ b/flutter/lib/desktop/pages/remote_page.dart @@ -60,7 +60,7 @@ class _RemotePageState extends State WidgetsBinding.instance.addPostFrameCallback((_) { SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); _ffi.dialogManager - .showLoading(translate('Connecting...'), onCancel: backToHomePage); + .showLoading(translate('Connecting...'), onCancel: closeConnection); }); if (!Platform.isLinux) { Wakelock.enable(); diff --git a/flutter/lib/desktop/widgets/tabbar_widget.dart b/flutter/lib/desktop/widgets/tabbar_widget.dart index 3398ab33d..f8da7b429 100644 --- a/flutter/lib/desktop/widgets/tabbar_widget.dart +++ b/flutter/lib/desktop/widgets/tabbar_widget.dart @@ -10,6 +10,25 @@ const double _kTabBarHeight = kDesktopRemoteTabBarHeight; const double _kIconSize = 18; const double _kDividerIndent = 10; const double _kAddIconSize = _kTabBarHeight - 15; +final tabBarKey = GlobalKey(); + +void closeTab(String? id) { + final tabBar = tabBarKey.currentWidget as TabBar?; + if (tabBar == null) return; + final tabs = tabBar.tabs as List<_Tab>; + if (id == null) { + final current = tabBar.controller?.index; + if (current == null) return; + tabs[current].onClose(); + } else { + for (final tab in tabs) { + if (tab.label == id) { + tab.onClose(); + break; + } + } + } +} class TabInfo { late final String label; @@ -59,6 +78,7 @@ class DesktopTabBar extends StatelessWidget { ), Flexible( child: Obx(() => TabBar( + key: tabBarKey, indicatorColor: _theme.indicatorColor, labelPadding: const EdgeInsets.symmetric( vertical: 0, horizontal: 0), diff --git a/flutter/lib/mobile/pages/file_manager_page.dart b/flutter/lib/mobile/pages/file_manager_page.dart index c361e7b7c..87169b987 100644 --- a/flutter/lib/mobile/pages/file_manager_page.dart +++ b/flutter/lib/mobile/pages/file_manager_page.dart @@ -29,7 +29,7 @@ class _FileManagerPageState extends State { gFFI.connect(widget.id, isFileTransfer: true); WidgetsBinding.instance.addPostFrameCallback((_) { gFFI.dialogManager - .showLoading(translate('Connecting...'), onCancel: backToHomePage); + .showLoading(translate('Connecting...'), onCancel: closeConnection); }); gFFI.ffiModel.updateEventListener(widget.id); Wakelock.enable(); diff --git a/flutter/lib/mobile/pages/remote_page.dart b/flutter/lib/mobile/pages/remote_page.dart index 8a4df6a9d..ceb3df0ff 100644 --- a/flutter/lib/mobile/pages/remote_page.dart +++ b/flutter/lib/mobile/pages/remote_page.dart @@ -51,7 +51,7 @@ class _RemotePageState extends State { WidgetsBinding.instance.addPostFrameCallback((_) { SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []); gFFI.dialogManager - .showLoading(translate('Connecting...'), onCancel: backToHomePage); + .showLoading(translate('Connecting...'), onCancel: closeConnection); _interval = Timer.periodic(Duration(milliseconds: 30), (timer) => interval()); }); diff --git a/flutter/lib/mobile/pages/scan_page.dart b/flutter/lib/mobile/pages/scan_page.dart index 9f6c36ca8..2487c0f58 100644 --- a/flutter/lib/mobile/pages/scan_page.dart +++ b/flutter/lib/mobile/pages/scan_page.dart @@ -132,7 +132,7 @@ class _ScanPageState extends State { } void showServerSettingFromQr(String data) async { - backToHomePage(); + closeConnection(); await controller?.pauseCamera(); if (!data.startsWith('config=')) { showToast('Invalid QR code'); diff --git a/flutter/lib/mobile/widgets/dialog.dart b/flutter/lib/mobile/widgets/dialog.dart index 82aed42a1..d648cd497 100644 --- a/flutter/lib/mobile/widgets/dialog.dart +++ b/flutter/lib/mobile/widgets/dialog.dart @@ -184,7 +184,7 @@ void enterPasswordDialog(String id, OverlayDialogManager dialogManager) async { style: flatButtonStyle, onPressed: () { close(); - backToHomePage(); + closeConnection(); }, child: Text(translate('Cancel')), ), @@ -196,7 +196,7 @@ void enterPasswordDialog(String id, OverlayDialogManager dialogManager) async { gFFI.login(id, text, remember); close(); dialogManager.showLoading(translate('Logging in...'), - onCancel: backToHomePage); + onCancel: closeConnection); }, child: Text(translate('OK')), ), @@ -214,7 +214,7 @@ void wrongPasswordDialog(String id, OverlayDialogManager dialogManager) { style: flatButtonStyle, onPressed: () { close(); - backToHomePage(); + closeConnection(); }, child: Text(translate('Cancel')), ), diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index f3e47ff7f..dda22a779 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -287,7 +287,7 @@ class FfiModel with ChangeNotifier { bind.sessionReconnect(id: id); clearPermissions(); dialogManager.showLoading(translate('Connecting...'), - onCancel: backToHomePage); + onCancel: closeConnection); }); _reconnects *= 2; } else { @@ -335,7 +335,7 @@ class FfiModel with ChangeNotifier { if (displays.length > 0) { parent.target?.dialogManager.showLoading( translate('Connected, waiting for image...'), - onCancel: backToHomePage); + onCancel: closeConnection); _waitForImage = true; _reconnects = 1; }