From 6a00a948656339e32439e5935cac0511a081cbdd Mon Sep 17 00:00:00 2001 From: InnocentusLime Date: Sat, 3 May 2025 21:17:32 +0300 Subject: [PATCH] fix and expand the support of the window_minimized_event --- js/gl.js | 4 +++- src/event.rs | 4 ++-- src/native/macos.rs | 23 +++++++++++++++++++++++ src/native/windows.rs | 7 +++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/js/gl.js b/js/gl.js index a66c4f242..df0116ac9 100644 --- a/js/gl.js +++ b/js/gl.js @@ -1365,7 +1365,9 @@ var importObject = { let lastFocus = document.hasFocus(); var checkFocus = function () { - let hasFocus = document.hasFocus(); + // The element doesn't loose focus when the user switches tabs. + // However, the document becomes invisible + let hasFocus = document.hasFocus() && document.visibilityState == "visible"; if (lastFocus != hasFocus) { wasm_exports.focus(hasFocus); lastFocus = hasFocus; diff --git a/src/event.rs b/src/event.rs index bd14dbc44..dcea3b7fa 100644 --- a/src/event.rs +++ b/src/event.rs @@ -206,9 +206,9 @@ pub trait EventHandler { fn raw_mouse_motion(&mut self, _dx: f32, _dy: f32) {} /// Window has been minimized - /// Right now is only implemented on Android, X11 and wasm, + /// Right now is only implemented on Android, Windows, OSX, X11 and wasm, /// On Andoid window_minimized_event is called on a Pause ndk callback - /// On X11 and wasm it will be called on focus change events. + /// On X11, OSX, Windows and wasm it will be called on focus change events. fn window_minimized_event(&mut self) {} /// Window has been restored diff --git a/src/native/macos.rs b/src/native/macos.rs index 8bdb3d187..22e5abc61 100644 --- a/src/native/macos.rs +++ b/src/native/macos.rs @@ -324,6 +324,9 @@ pub fn define_cocoa_window_delegate() -> *const Class { // Startup: the gl_context has not yet been created. return; } + if let Some(event_handler) = payload.context() { + event_handler.window_minimized_event(); + } unsafe { msg_send_![payload.gl_context, update]; } @@ -355,6 +358,18 @@ pub fn define_cocoa_window_delegate() -> *const Class { } } } + extern "C" fn window_did_become_key(this: &Object, _: Sel, _: ObjcId) { + let payload = get_window_payload(this); + if let Some(event_handler) = payload.context() { + event_handler.window_restored_event(); + } + } + extern "C" fn window_did_resign_key(this: &Object, _: Sel, _: ObjcId) { + let payload = get_window_payload(this); + if let Some(event_handler) = payload.context() { + event_handler.window_minimized_event(); + } + } let superclass = class!(NSObject); let mut decl = ClassDecl::new("RenderWindowDelegate", superclass).unwrap(); @@ -390,6 +405,14 @@ pub fn define_cocoa_window_delegate() -> *const Class { sel!(windowDidChangeOcclusionState:), window_did_change_occlusion_state as extern "C" fn(&Object, Sel, ObjcId), ); + decl.add_method( + sel!(windowDidBecomeKey:), + window_did_become_key as extern "C" fn(&Object, Sel, ObjcId), + ); + decl.add_method( + sel!(windowDidResignKey:), + window_did_resign_key as extern "C" fn(&Object, Sel, ObjcId), + ); } // Store internal state as user data decl.add_ivar::<*mut c_void>("display_ptr"); diff --git a/src/native/windows.rs b/src/native/windows.rs index de328aad4..fa6fc80c7 100644 --- a/src/native/windows.rs +++ b/src/native/windows.rs @@ -535,6 +535,13 @@ unsafe extern "system" fn win32_wndproc( } } } + WM_ACTIVATE => { + if LOWORD(wparam as _) == WA_ACTIVE || LOWORD(wparam as _) == WA_CLICKACTIVE { + event_handler.window_restored_event(); + } else { + event_handler.window_minimized_event(); + } + } _ => {} }