From 407612918489bfa642e2d653edbb54272b3f00f5 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 6 Apr 2024 14:01:30 +0200 Subject: feat(glfw): add setting window close callback --- glfw/src/window.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'glfw/src/window.rs') diff --git a/glfw/src/window.rs b/glfw/src/window.rs index 079b2fb..40eb300 100644 --- a/glfw/src/window.rs +++ b/glfw/src/window.rs @@ -117,6 +117,20 @@ impl Window }) } + /// Sets the callback which is called when the user attempts to close the window. + pub fn set_close_callback(&self, callback: impl Fn() + 'static) + { + CLOSE_CALLBACK.with_borrow_mut(|cb| { + *cb = Some(Box::new(callback)); + }); + + // SAFETY: The initialize function (called when the window is created) makes sure + // the current thread is the main thread + unsafe { + crate::ffi::glfwSetWindowCloseCallback(self.handle, Some(close_callback)); + } + } + pub fn set_framebuffer_size_callback(&self, callback: impl Fn(Size) + 'static) { FRAMEBUFFER_SIZE_CB.with_borrow_mut(|framebuffer_size_cb| { @@ -661,18 +675,38 @@ pub struct CursorPosition pub y: f64, } +type CloseCallback = Box; type FramebufferSizeCb = Box; type KeyCallback = Box; type CursorPositionCallback = Box; type MouseButtonCallback = Box; thread_local! { +static CLOSE_CALLBACK: RefCell> = RefCell::new(None); static FRAMEBUFFER_SIZE_CB: RefCell> = RefCell::new(None); static KEY_CALLBACK: RefCell> = RefCell::new(None); static CURSOR_POS_CALLBACK: RefCell> = RefCell::new(None); static MOUSE_BUTTON_CALLBACK: RefCell> = RefCell::new(None); } +extern "C" fn close_callback(_window: *mut crate::ffi::GLFWwindow) +{ + // Unwinds are catched because unwinding from Rust code into foreign code is UB. + let res = catch_unwind(|| { + CLOSE_CALLBACK + .try_with(|close_cb| { + if let Some(cb) = close_cb.borrow().as_deref() { + cb(); + } + }) + .ok(); + }); + + if res.is_err() { + println!("ERROR: Panic in window close callback"); + } +} + extern "C" fn framebuffer_size_callback( _window: *mut crate::ffi::GLFWwindow, c_width: c_int, -- cgit v1.2.3-18-g5258