Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defer certain events on macOS to avoid re-entrant calls #189

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 33 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,33 @@ 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();
prokopyl marked this conversation as resolved.
Show resolved Hide resolved
let status = window_handler.on_event(&mut window, event);
self.trigger_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);
httnn marked this conversation as resolved.
Show resolved Hide resolved
} 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();
self.trigger_deferred_events(window_handler.as_mut());
window_handler.on_frame(&mut window);
}

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

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

fn trigger_deferred_events(&self, window_handler: &mut dyn WindowHandler) {
let mut window = crate::Window::new(Window { inner: &self.window_inner });
for event in self.deferred_events.borrow_mut().drain(..) {
httnn marked this conversation as resolved.
Show resolved Hide resolved
window_handler.on_event(&mut window, event);
}
}
}

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