From c65b4030c39bd6b8b937b66cacfbeda116c94bba Mon Sep 17 00:00:00 2001 From: Mikko Lehtonen Date: Wed, 21 Apr 2021 00:10:58 +0300 Subject: [PATCH] macos: Handle resizes synchronously Allows for a lot better resize behaviour on macos, when allowed to redraw the frame within the resize event callback. --- CHANGELOG.md | 1 + src/platform_impl/macos/app_state.rs | 4 ++++ src/platform_impl/macos/window_delegate.rs | 12 +++++++++--- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a14bc047d4..719632cc8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ - On Windows, added `WindowExtWindows::set_enable` to allow creating modal popup windows. - On macOS, emit `RedrawRequested` events immediately while the window is being resized. - Implement `Default`, `Hash`, and `Eq` for `LogicalPosition`, `PhysicalPosition`, `LogicalSize`, and `PhysicalSize`. +- On macOS, emit `Resized` events immediately from the callback. # 0.24.0 (2020-12-09) diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index 59cf0f2aac..2906c83a67 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -331,6 +331,10 @@ impl AppState { HANDLER.handle_nonuser_event(EventWrapper::StaticEvent(Event::RedrawRequested(window_id))); } + pub fn handle_event(wrapper: EventWrapper) { + HANDLER.handle_nonuser_event(wrapper); + } + pub fn queue_event(wrapper: EventWrapper) { if !unsafe { msg_send![class!(NSThread), isMainThread] } { panic!("Event queued from different thread: {:#?}", wrapper); diff --git a/src/platform_impl/macos/window_delegate.rs b/src/platform_impl/macos/window_delegate.rs index c226a66d7a..f844562927 100644 --- a/src/platform_impl/macos/window_delegate.rs +++ b/src/platform_impl/macos/window_delegate.rs @@ -97,12 +97,18 @@ impl WindowDelegateState { AppState::queue_event(wrapper); } - pub fn emit_resize_event(&mut self) { + pub fn handle_resize_event(&mut self) { let rect = unsafe { NSView::frame(*self.ns_view) }; let scale_factor = self.get_scale_factor(); let logical_size = LogicalSize::new(rect.size.width as f64, rect.size.height as f64); let size = logical_size.to_physical(scale_factor); - self.emit_event(WindowEvent::Resized(size)); + + let event = Event::WindowEvent { + window_id: WindowId(get_window_id(*self.ns_window)), + event: WindowEvent::Resized(size), + }; + + AppState::handle_event(EventWrapper::StaticEvent(event)); } fn emit_move_event(&mut self) { @@ -287,7 +293,7 @@ extern "C" fn window_will_close(this: &Object, _: Sel, _: id) { extern "C" fn window_did_resize(this: &Object, _: Sel, _: id) { trace!("Triggered `windowDidResize:`"); with_state(this, |state| { - state.emit_resize_event(); + state.handle_resize_event(); state.emit_move_event(); }); trace!("Completed `windowDidResize:`");