From 6336179a9eb0c4fdb3d9ffd76ad73e7d5fb832fa Mon Sep 17 00:00:00 2001 From: Hu Tao <73904+hut36@users.noreply.github.com> Date: Wed, 16 Oct 2024 10:27:53 +0800 Subject: [PATCH 1/2] fix(macos): make set_outer_position a sync operation --- src/platform_impl/macos/util/async.rs | 13 +++++++++++++ src/platform_impl/macos/window.rs | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/platform_impl/macos/util/async.rs b/src/platform_impl/macos/util/async.rs index bfaae9875..34dca9af2 100644 --- a/src/platform_impl/macos/util/async.rs +++ b/src/platform_impl/macos/util/async.rs @@ -99,6 +99,19 @@ pub unsafe fn set_frame_top_left_point_async(ns_window: id, point: NSPoint) { }); } +// `setFrameTopLeftPoint:` isn't thread-safe, but fortunately has the courtesy +// to log errors. +pub unsafe fn set_frame_top_left_point_sync(ns_window: id, point: NSPoint) { + if is_main_thread() { + ns_window.setFrameTopLeftPoint_(point); + } else { + let ns_window = MainThreadSafe(ns_window); + Queue::main().exec_sync(move || { + ns_window.setFrameTopLeftPoint_(point); + }) + } +} + // `setFrameTopLeftPoint:` isn't thread-safe, and fails silently. pub unsafe fn set_level_async(ns_window: id, level: ffi::NSWindowLevel) { let ns_window = MainThreadSafe(ns_window); diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 62c14b665..81ce7624b 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -689,7 +689,7 @@ impl UnownedWindow { let scale_factor = self.scale_factor(); let position = position.to_logical(scale_factor); unsafe { - util::set_frame_top_left_point_async(*self.ns_window, util::window_position(position)); + util::set_frame_top_left_point_sync(*self.ns_window, util::window_position(position)); } } From 4be2236600c0c2cf1cbf5b6acbf8c23b125189dd Mon Sep 17 00:00:00 2001 From: Hu Tao <73904+hut36@users.noreply.github.com> Date: Wed, 16 Oct 2024 15:10:51 +0800 Subject: [PATCH 2/2] fix(macos): make set_inner_size a sync operation --- src/platform_impl/macos/util/async.rs | 11 +++++++++++ src/platform_impl/macos/window.rs | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/platform_impl/macos/util/async.rs b/src/platform_impl/macos/util/async.rs index 34dca9af2..016cfeca7 100644 --- a/src/platform_impl/macos/util/async.rs +++ b/src/platform_impl/macos/util/async.rs @@ -90,6 +90,17 @@ pub unsafe fn set_content_size_async(ns_window: id, size: LogicalSize) { }); } +pub unsafe fn set_content_size_sync(ns_window: id, size: LogicalSize) { + if is_main_thread() { + ns_window.setContentSize_(NSSize::new(size.width as CGFloat, size.height as CGFloat)); + } else { + let ns_window = MainThreadSafe(ns_window); + Queue::main().exec_sync(move || { + ns_window.setContentSize_(NSSize::new(size.width as CGFloat, size.height as CGFloat)); + }); + } +} + // `setFrameTopLeftPoint:` isn't thread-safe, but fortunately has the courtesy // to log errors. pub unsafe fn set_frame_top_left_point_async(ns_window: id, point: NSPoint) { diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 81ce7624b..fcdbf83d7 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -715,7 +715,7 @@ impl UnownedWindow { pub fn set_inner_size(&self, size: Size) { unsafe { let scale_factor = self.scale_factor(); - util::set_content_size_async(*self.ns_window, size.to_logical(scale_factor)); + util::set_content_size_sync(*self.ns_window, size.to_logical(scale_factor)); } }