Skip to content

Commit

Permalink
defer certain events on macOS to avoid re-entrant calls (#189)
Browse files Browse the repository at this point in the history
* defer certain events on macOS to avoid re-entrant calls

* immediately trigger deferred event if borrowing succeeds

* nicer signature

* rename method: defer_event -> trigger_deferrable_event

* clean up, add comments

* address review comments

* fix borrow logic when sending deferred events

* handle deferred events after on_frame
  • Loading branch information
httnn authored Dec 9, 2024
1 parent 21fdc89 commit 9a0b42c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,14 @@ extern "C" fn become_first_responder(this: &Object, _sel: Sel) -> BOOL {
is_key_window == YES
};
if is_key_window {
state.trigger_event(Event::Window(WindowEvent::Focused));
state.trigger_deferrable_event(Event::Window(WindowEvent::Focused));
}
YES
}

extern "C" fn resign_first_responder(this: &Object, _sel: Sel) -> BOOL {
let state = unsafe { WindowState::from_view(this) };
state.trigger_event(Event::Window(WindowEvent::Unfocused));
state.trigger_deferrable_event(Event::Window(WindowEvent::Unfocused));
YES
}

Expand Down
41 changes: 39 additions & 2 deletions src/macos/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::ffi::c_void;
use std::ptr;
use std::rc::Rc;
Expand Down Expand Up @@ -267,6 +268,7 @@ impl<'a> Window<'a> {
keyboard_state: KeyboardState::new(),
frame_timer: Cell::new(None),
window_info: Cell::new(window_info),
deferred_events: RefCell::default(),
});

let window_state_ptr = Rc::into_raw(Rc::clone(&window_state));
Expand Down Expand Up @@ -360,6 +362,9 @@ pub(super) struct WindowState {
frame_timer: Cell<Option<CFRunLoopTimer>>,
/// The last known window info for this window.
pub window_info: Cell<WindowInfo>,

/// Events that will be triggered at the end of `window_handler`'s borrow.
deferred_events: RefCell<VecDeque<Event>>,
}

impl WindowState {
Expand All @@ -378,14 +383,34 @@ impl WindowState {
state
}

/// Trigger the event immediately and return the event status.
/// Will panic if `window_handler` is already borrowed (see `trigger_deferrable_event`).
pub(super) fn trigger_event(&self, event: Event) -> EventStatus {
let mut window = crate::Window::new(Window { inner: &self.window_inner });
self.window_handler.borrow_mut().on_event(&mut window, event)
let mut window_handler = self.window_handler.borrow_mut();
let status = window_handler.on_event(&mut window, event);
self.send_deferred_events(window_handler.as_mut());
status
}

/// Trigger the event immediately if `window_handler` can be borrowed mutably,
/// otherwise add the event to a queue that will be cleared once `window_handler`'s mutable borrow ends.
/// As this method might result in the event triggering asynchronously, it can't reliably return the event status.
pub(super) fn trigger_deferrable_event(&self, event: Event) {
if let Ok(mut window_handler) = self.window_handler.try_borrow_mut() {
let mut window = crate::Window::new(Window { inner: &self.window_inner });
window_handler.on_event(&mut window, event);
self.send_deferred_events(window_handler.as_mut());
} else {
self.deferred_events.borrow_mut().push_back(event);
}
}

pub(super) fn trigger_frame(&self) {
let mut window = crate::Window::new(Window { inner: &self.window_inner });
self.window_handler.borrow_mut().on_frame(&mut window);
let mut window_handler = self.window_handler.borrow_mut();
window_handler.on_frame(&mut window);
self.send_deferred_events(window_handler.as_mut());
}

pub(super) fn keyboard_state(&self) -> &KeyboardState {
Expand Down Expand Up @@ -419,6 +444,18 @@ impl WindowState {

(*window_state_ptr).frame_timer.set(Some(timer));
}

fn send_deferred_events(&self, window_handler: &mut dyn WindowHandler) {
let mut window = crate::Window::new(Window { inner: &self.window_inner });
loop {
let next_event = self.deferred_events.borrow_mut().pop_front();
if let Some(event) = next_event {
window_handler.on_event(&mut window, event);
} else {
break;
}
}
}
}

unsafe impl<'a> HasRawWindowHandle for Window<'a> {
Expand Down

0 comments on commit 9a0b42c

Please sign in to comment.