Skip to content

Commit

Permalink
Run Window::set_ime_position on main thread on macOS
Browse files Browse the repository at this point in the history
Fixes #2756.
  • Loading branch information
amatho authored and kchibisov committed May 4, 2023
1 parent d37dac8 commit 0881a28
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions src/platform_impl/macos/util/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -201,3 +201,11 @@ pub(crate) fn close_sync(window: &NSWindow) {
});
});
}

pub(crate) fn set_ime_position_sync(window: &WinitWindow, logical_spot: LogicalPosition<f64>) {
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);
});
}
3 changes: 1 addition & 2 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0881a28

Please sign in to comment.