From b4e13706bd1b084e8c28877a71c817dacff77056 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Wed, 23 Jul 2025 22:44:05 +0800 Subject: [PATCH] refact: active terminal on conn the same remote (#12392) Signed-off-by: fufesou --- flutter/lib/desktop/pages/terminal_tab_page.dart | 12 ++++++++++++ flutter/lib/utils/multi_window_manager.dart | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/flutter/lib/desktop/pages/terminal_tab_page.dart b/flutter/lib/desktop/pages/terminal_tab_page.dart index 0a681f587..754b309ae 100644 --- a/flutter/lib/desktop/pages/terminal_tab_page.dart +++ b/flutter/lib/desktop/pages/terminal_tab_page.dart @@ -177,6 +177,18 @@ class _TerminalTabPageState extends State { tabController.clear(); } else if (call.method == kWindowActionRebuild) { reloadCurrentWindow(); + } else if (call.method == kWindowEventActiveSession) { + if (tabController.state.value.tabs.isEmpty) { + return false; + } + final currentTab = tabController.state.value.selectedTabInfo; + assert(call.arguments is String, + "Expected String arguments for kWindowEventActiveSession, got ${call.arguments.runtimeType}"); + if (currentTab.key.startsWith(call.arguments)) { + windowOnTop(windowId()); + return true; + } + return false; } }); Future.delayed(Duration.zero, () { diff --git a/flutter/lib/utils/multi_window_manager.dart b/flutter/lib/utils/multi_window_manager.dart index 95044eb74..3bbb292f4 100644 --- a/flutter/lib/utils/multi_window_manager.dart +++ b/flutter/lib/utils/multi_window_manager.dart @@ -354,6 +354,16 @@ class RustDeskMultiWindowManager { bool? forceRelay, String? connToken, }) async { + // Iterate through terminal windows in reverse order to prioritize + // the most recently added or used windows, as they are more likely + // to have an active session. + for (final windowId in _terminalWindows.reversed) { + if (await DesktopMultiWindow.invokeMethod( + windowId, kWindowEventActiveSession, remoteId)) { + return MultiWindowCallResult(windowId, null); + } + } + // Terminal windows should always create new windows, not reuse // This avoids the MissingPluginException when trying to invoke // new_terminal on an inactive window @@ -366,7 +376,7 @@ class RustDeskMultiWindowManager { "connToken": connToken, }; final msg = jsonEncode(params); - + // Always create a new window for terminal final windowId = await newSessionWindow( WindowType.Terminal, remoteId, msg, _terminalWindows, false);