From 0881a2830218337f59bfed1806eb6c085c19f7f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Amandus=20S=C3=B8ve=20Thorsrud?= Date: Sat, 15 Apr 2023 01:58:36 +0200 Subject: [PATCH] Run `Window::set_ime_position` on main thread on macOS Fixes #2756. --- CHANGELOG.md | 1 + src/platform_impl/macos/util/async.rs | 12 ++++++++++-- src/platform_impl/macos/window.rs | 3 +-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9e6bb3067..a6950dbd73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased - On macOS, fixed potential panic when getting refresh rate. +- On macOS, fix crash when calling `Window::set_ime_position` from another thread. # 0.28.3 diff --git a/src/platform_impl/macos/util/async.rs b/src/platform_impl/macos/util/async.rs index 6edbd8db6f..3cc9a4b46e 100644 --- a/src/platform_impl/macos/util/async.rs +++ b/src/platform_impl/macos/util/async.rs @@ -2,10 +2,10 @@ use std::ops::Deref; use dispatch::Queue; use objc2::foundation::{is_main_thread, CGFloat, NSPoint, NSSize, NSString}; -use objc2::rc::autoreleasepool; +use objc2::rc::{autoreleasepool, Id}; use crate::{ - dpi::LogicalSize, + dpi::{LogicalPosition, LogicalSize}, platform_impl::platform::{ appkit::{NSScreen, NSWindow, NSWindowLevel, NSWindowStyleMask}, ffi, @@ -201,3 +201,11 @@ pub(crate) fn close_sync(window: &NSWindow) { }); }); } + +pub(crate) fn set_ime_position_sync(window: &WinitWindow, logical_spot: LogicalPosition) { + let window = MainThreadSafe(window); + run_on_main(move || { + // TODO(madsmtm): Remove the need for this + unsafe { Id::from_shared(window.view()) }.set_ime_position(logical_spot); + }); +} diff --git a/src/platform_impl/macos/window.rs b/src/platform_impl/macos/window.rs index 6d3e79af96..69921ba78f 100644 --- a/src/platform_impl/macos/window.rs +++ b/src/platform_impl/macos/window.rs @@ -1165,8 +1165,7 @@ impl WinitWindow { pub fn set_ime_position(&self, spot: Position) { let scale_factor = self.scale_factor(); let logical_spot = spot.to_logical(scale_factor); - // TODO(madsmtm): Remove the need for this - unsafe { Id::from_shared(self.view()) }.set_ime_position(logical_spot); + util::set_ime_position_sync(self, logical_spot); } #[inline]