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);