From 7e11d0729b9b0dcdbba04e3a693fb7ffe6dbd362 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Fri, 13 Nov 2026 18:33:53 +0800 Subject: [PATCH] fix(macos): keep full screen when Escape goes unhandled (#3134) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Escape the page leaves unhandled reaches the window as cancelOperation:, and NSWindow leaves full screen on it, so pressing Escape once more after closing a popup dropped the IDE out of full screen. The IDE's windows now answer cancelOperation: with nothing; View › Appearance › Toggle Full Screen (⌃⌘F) leaves full screen. A page-level preventDefault was not used because several windows close on an otherwise unhandled Escape. Fixes #3104 --- src-tauri/src/macos.rs | 23 +++++++++++++++++++++++ src-tauri/src/windows.rs | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src-tauri/src/macos.rs b/src-tauri/src/macos.rs index 4053be31f..710f4b558 100644 --- a/src-tauri/src/macos.rs +++ b/src-tauri/src/macos.rs @@ -180,6 +180,29 @@ unsafe extern "C-unwind" fn should_terminate(_this: *mut AnyObject, _selector: S static APP: std::sync::OnceLock = std::sync::OnceLock::new(); +/// Escape the page leaves unhandled reaches the window as `cancelOperation:`, on which NSWindow +/// leaves full screen, so pressing Escape once more after closing a popup left full screen. The +/// IDE's windows answer it with nothing; View › Appearance › Toggle Full Screen (⌃⌘F) leaves it. +pub fn keep_full_screen_on_escape() { + static ONCE: std::sync::Once = std::sync::Once::new(); + ONCE.call_once(|| unsafe { + let Some(class) = AnyClass::get(c"TaoWindow") else { + tracing::warn!("no TaoWindow class: Escape leaves full screen"); + return; + }; + let imp: Imp = std::mem::transmute(ignore_cancel_operation as CancelOperation); + let class = class as *const AnyClass as *mut AnyClass; + let added = objc2::ffi::class_addMethod(class, objc2::sel!(cancelOperation:), imp, c"v@:@".as_ptr()); + if !added.as_bool() { + tracing::warn!("could not handle cancelOperation:"); + } + }); +} + +type CancelOperation = unsafe extern "C-unwind" fn(*mut AnyObject, Sel, *mut AnyObject); + +unsafe extern "C-unwind" fn ignore_cancel_operation(_this: *mut AnyObject, _selector: Sel, _sender: *mut AnyObject) {} + /// Whether AppKit is handling a key press right now. A menu item performed at this point /// was chosen by its key equivalent: a click, arrow keys plus Return and accessibility /// presses all run with a non-key current event. diff --git a/src-tauri/src/windows.rs b/src-tauri/src/windows.rs index 326045c91..e89d9a796 100644 --- a/src-tauri/src/windows.rs +++ b/src-tauri/src/windows.rs @@ -660,6 +660,8 @@ fn build_project_window(app: &AppHandle, mut config: WindowConfig) -> IdeResult< decorate_csd(&window); #[cfg(target_os = "macos")] crate::page_swipe::install(&window); + #[cfg(target_os = "macos")] + crate::macos::keep_full_screen_on_escape(); apply_zoom(app, &window); #[cfg(target_os = "macos")] align_titlebar(app, &window, app.try_state::().is_some_and(|w| w.opens_project(window.label())));