From 875b17938ed03f60b404b2cce5a759ca24598786 Mon Sep 17 00:00:00 2001 From: audichuang <72485427+audichuang@users.noreply.github.com> Date: Wed, 2 Dec 2026 23:01:45 +0800 Subject: [PATCH] fix(macos): wait for windows to finish leaving full screen before Merge All moves tabs (#7480) Merge All Project Windows invoked from a group in native full screen aborted the app: tabs were moved with `addTabbedWindow` while the full-screen exit animation was still running, and AppKit threw. `merge` now waits (up to 3 s) for the windows that were in full screen, or already leaving it, to finish, then re-checks the candidates before moving tabs. Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com> --- src-tauri/src/commands/window_group.rs | 2 +- src-tauri/src/titlebar.rs | 21 +++++++++++++++ src-tauri/src/window_groups.rs | 37 +++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/commands/window_group.rs b/src-tauri/src/commands/window_group.rs index feea3a4002..d17dbc7493 100644 --- a/src-tauri/src/commands/window_group.rs +++ b/src-tauri/src/commands/window_group.rs @@ -14,7 +14,7 @@ pub fn window_group_current(window: WebviewWindow) -> IdeResult IdeResult<()> { - groups::merge(window.app_handle(), window.label())?; + groups::merge(window.app_handle(), window.label()).await?; groups::persist(window.app_handle()).await } diff --git a/src-tauri/src/titlebar.rs b/src-tauri/src/titlebar.rs index cb4b04f1ad..03a36e5071 100644 --- a/src-tauri/src/titlebar.rs +++ b/src-tauri/src/titlebar.rs @@ -491,6 +491,27 @@ pub fn set_height(window: &tauri::WebviewWindow, height: f64) -> tauri::Result<( }) } +/// For each of `windows`: whether it is in native full screen or on its way in or out, and whether +/// it is leaving (from `willExit` to `didExit`). +pub fn fullscreen_states(windows: &[tauri::WebviewWindow]) -> tauri::Result> { + let Some(first) = windows.first() else { return Ok(Vec::new()) }; + let labels: Vec = windows.iter().map(|w| w.label().to_owned()).collect(); + let check = move || { + WINDOWS.with_borrow(|tracked| { + let state = |l: &String| tracked.get(l).map(|t| (t.state.entered.get(), t.state.leaving.get())); + labels.iter().map(|l| state(l).unwrap_or_default()).collect() + }) + }; + if MainThreadMarker::new().is_some() { + return Ok(check()); + } + let (tx, rx) = std::sync::mpsc::sync_channel(1); + first.run_on_main_thread(move || { + let _ = tx.send(check()); + })?; + rx.recv().map_err(|_| tauri::Error::FailedToReceiveMessage) +} + /// Stops tracking a destroyed window, which removes its notification observers. pub fn forget(window: &tauri::Window) { let label = window.label().to_owned(); diff --git a/src-tauri/src/window_groups.rs b/src-tauri/src/window_groups.rs index e204769b32..9571ecae7e 100644 --- a/src-tauri/src/window_groups.rs +++ b/src-tauri/src/window_groups.rs @@ -169,19 +169,54 @@ pub fn join(app: &AppHandle, anchor: &str, new_window: &str) -> IdeResult<()> { select(app, anchor, new_window) } -pub fn merge(app: &AppHandle, caller: &str) -> IdeResult<()> { +pub async fn merge(app: &AppHandle, caller: &str) -> IdeResult<()> { let all_windows = windows::project_window_labels(app); let members = ide_workspace::window_groups::merge_candidates(&all_windows, caller, |label| is_project_window(app, label)); if members.len() < 2 || !members.iter().any(|label| label == caller) { return Err(IdeError::invalid("Open at least two project windows to create a window group")); } + // Windows in full screen, or already leaving it (another Merge All, the user): an `entered` + // left over from a failed entry is neither, so it cannot hold Merge All up. + #[cfg(target_os = "macos")] + let leaving: Vec<_> = { + let windows: Vec<_> = members.iter().filter_map(|label| app.get_webview_window(label)).collect(); + let states = crate::titlebar::fullscreen_states(&windows).map_err(|e| IdeError::internal(e.to_string()))?; + windows + .into_iter() + .zip(states) + .filter(|(w, (_, leaving))| *leaving || w.is_fullscreen().unwrap_or(false)) + .map(|(w, _)| w) + .collect() + }; // Native fullscreen puts each window in a separate Space; leave it before grouping. for label in &members { if let Some(window) = app.get_webview_window(label) { window.set_fullscreen(false).map_err(|e| IdeError::internal(e.to_string()))?; } } + // Leaving full screen animates after `set_fullscreen` returns, and moving tabs into a window + // still leaving it raises an Objective-C exception that aborts the app. + #[cfg(target_os = "macos")] + if !leaving.is_empty() { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(3); + while crate::titlebar::fullscreen_states(&leaving) + .map_err(|e| IdeError::internal(e.to_string()))? + .iter() + .any(|(entered, _)| *entered) + { + if std::time::Instant::now() >= deadline { + return Err(IdeError::invalid("Project windows are still leaving full screen")); + } + tokio::time::sleep(std::time::Duration::from_millis(50)).await; + } + // Project windows may have opened or closed while waiting. + let now = windows::project_window_labels(app); + let now = ide_workspace::window_groups::merge_candidates(&now, caller, |label| is_project_window(app, label)); + if now.iter().collect::>() != members.iter().collect::>() { + return Err(IdeError::invalid("Project windows changed while leaving full screen")); + } + } // If a member is currently grouped with a window outside `members` (e.g. a non-project // window), separate it from that native/frame group before merging. let state = app.state::();