Skip to content

Commit

Permalink
fix(runtime-wry): run cursor_position getter on main thread (#11401)
Browse files Browse the repository at this point in the history
* fix(runtime-wry): run `cursor_position` getter on main thread

closes #10340

* clippy

* clippy

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Oct 22, 2024
1 parent 6dea12a commit 8c6d1e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changes/curosr-position-gtk.md
Original file line number Diff line number Diff line change
@@ -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`.
14 changes: 3 additions & 11 deletions crates/tauri-cli/src/mobile/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>> {
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)]
&formatter,
)
})
.collect()
})
v.value()
.as_array()
.and_then(|arr| arr.iter().map(|val| val.as_str().map(&formatter)).collect())
})
}

Expand Down
32 changes: 22 additions & 10 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1268,6 +1275,10 @@ pub enum WebviewMessage {
IsDevToolsOpen(Sender<bool>),
}

pub enum EventLoopWindowTargetMessage {
CursorPosition(Sender<Result<PhysicalPosition<f64>>>),
}

pub type CreateWindowClosure<T> =
Box<dyn FnOnce(&EventLoopWindowTarget<Message<T>>) -> Result<WindowWrapper> + Send>;

Expand All @@ -1282,6 +1293,7 @@ pub enum Message<T: 'static> {
Application(ApplicationMessage),
Window(WindowId, WindowMessage),
Webview(WindowId, WebviewId, WebviewMessage),
EventLoopWindowTarget(EventLoopWindowTargetMessage),
CreateWebview(WindowId, CreateWebviewClosure),
CreateWindow(WindowId, CreateWindowClosure<T>),
CreateRawWindow(
Expand Down Expand Up @@ -2325,11 +2337,7 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
}

fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
self
.context
.main_thread
.window_target
.cursor_position()
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
.map(PhysicalPositionWrapper)
.map(Into::into)
.map_err(|_| Error::FailedToGetCursorPosition)
Expand Down Expand Up @@ -2616,11 +2624,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
}

fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
self
.context
.main_thread
.window_target
.cursor_position()
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
.map(PhysicalPositionWrapper)
.map(Into::into)
.map_err(|_| Error::FailedToGetCursorPosition)
Expand Down Expand Up @@ -3452,6 +3456,14 @@ fn handle_user_message<T: UserEvent>(
}

Message::UserEvent(_) => (),
Message::EventLoopWindowTarget(message) => match message {
EventLoopWindowTargetMessage::CursorPosition(sender) => {
let pos = event_loop
.cursor_position()
.map_err(|_| Error::FailedToSendMessage);
sender.send(pos).unwrap();
}
},
}
}

Expand Down

0 comments on commit 8c6d1e8

Please sign in to comment.