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