From aadc92d5ae307fdd0f449d095b383bfa94462983 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Fri, 18 Oct 2024 02:54:49 +0300 Subject: [PATCH 1/3] fix(runtime-wry): run `cursor_position` getter on main thread closes #10340 --- .changes/curosr-position-gtk.md | 6 ++++++ crates/tauri-runtime-wry/src/lib.rs | 32 ++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 .changes/curosr-position-gtk.md diff --git a/.changes/curosr-position-gtk.md b/.changes/curosr-position-gtk.md new file mode 100644 index 000000000000..4bc87b6bb9c6 --- /dev/null +++ b/.changes/curosr-position-gtk.md @@ -0,0 +1,6 @@ +--- +"tauri": "patch:bug" +"tauri-runtime-wry": "patch:bug" +--- + +Fix `App/AppHandle/Window/Webview/WebviewWindow::cursor_position` getter method failing on Linux with `GDK may only be used from the main thread`. diff --git a/crates/tauri-runtime-wry/src/lib.rs b/crates/tauri-runtime-wry/src/lib.rs index 0f05dd8cc8f2..4dd17d494910 100644 --- a/crates/tauri-runtime-wry/src/lib.rs +++ b/crates/tauri-runtime-wry/src/lib.rs @@ -181,6 +181,13 @@ macro_rules! window_getter { }}; } +macro_rules! event_loop_window_getter { + ($self: ident, $message: expr) => {{ + let (tx, rx) = channel(); + getter!($self, rx, Message::EventLoopWindowTarget($message(tx))) + }}; +} + macro_rules! webview_getter { ($self: ident, $message: expr) => {{ let (tx, rx) = channel(); @@ -1268,6 +1275,10 @@ pub enum WebviewMessage { IsDevToolsOpen(Sender), } +pub enum EventLoopWindowTargetMessage { + CursorPosition(Sender>>), +} + pub type CreateWindowClosure = Box>) -> Result + Send>; @@ -1282,6 +1293,7 @@ pub enum Message { Application(ApplicationMessage), Window(WindowId, WindowMessage), Webview(WindowId, WebviewId, WebviewMessage), + EventLoopWindowTarget(EventLoopWindowTargetMessage), CreateWebview(WindowId, CreateWebviewClosure), CreateWindow(WindowId, CreateWindowClosure), CreateRawWindow( @@ -2325,11 +2337,7 @@ impl RuntimeHandle for WryHandle { } fn cursor_position(&self) -> Result> { - self - .context - .main_thread - .window_target - .cursor_position() + event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)? .map(PhysicalPositionWrapper) .map(Into::into) .map_err(|_| Error::FailedToGetCursorPosition) @@ -2616,11 +2624,7 @@ impl Runtime for Wry { } fn cursor_position(&self) -> Result> { - self - .context - .main_thread - .window_target - .cursor_position() + event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)? .map(PhysicalPositionWrapper) .map(Into::into) .map_err(|_| Error::FailedToGetCursorPosition) @@ -3452,6 +3456,14 @@ fn handle_user_message( } Message::UserEvent(_) => (), + Message::EventLoopWindowTarget(message) => match message { + EventLoopWindowTargetMessage::CursorPosition(sender) => { + let pos = event_loop + .cursor_position() + .map_err(|_| Error::FailedToSendMessage); + sender.send(pos).unwrap(); + } + }, } } From 70ee1a239efa7434a681380129bfc6b7e5636a01 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Fri, 18 Oct 2024 03:35:59 +0300 Subject: [PATCH 2/3] clippy --- crates/tauri-cli/src/mobile/init.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/crates/tauri-cli/src/mobile/init.rs b/crates/tauri-cli/src/mobile/init.rs index 4c2a29e34f6a..032c898686e6 100644 --- a/crates/tauri-cli/src/mobile/init.rs +++ b/crates/tauri-cli/src/mobile/init.rs @@ -198,17 +198,9 @@ fn get_str<'a>(helper: &'a Helper) -> &'a str { fn get_str_array(helper: &Helper, formatter: impl Fn(&str) -> String) -> Option> { helper.param(0).and_then(|v| { - v.value().as_array().and_then(|arr| { - arr - .iter() - .map(|val| { - val.as_str().map( - #[allow(clippy::redundant_closure)] - |s| formatter(s), - ) - }) - .collect() - }) + v.value() + .as_array() + .and_then(|arr| arr.iter().map(|val| val.as_str().map(formatter)).collect()) }) } From 9a7d9bcf276b13301d5686bbabd00ca9215ce8bc Mon Sep 17 00:00:00 2001 From: amrbashir Date: Fri, 18 Oct 2024 03:51:07 +0300 Subject: [PATCH 3/3] clippy --- crates/tauri-cli/src/mobile/init.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/tauri-cli/src/mobile/init.rs b/crates/tauri-cli/src/mobile/init.rs index 032c898686e6..c24ddbe84408 100644 --- a/crates/tauri-cli/src/mobile/init.rs +++ b/crates/tauri-cli/src/mobile/init.rs @@ -200,7 +200,7 @@ fn get_str_array(helper: &Helper, formatter: impl Fn(&str) -> String) -> Option< helper.param(0).and_then(|v| { v.value() .as_array() - .and_then(|arr| arr.iter().map(|val| val.as_str().map(formatter)).collect()) + .and_then(|arr| arr.iter().map(|val| val.as_str().map(&formatter)).collect()) }) }