Skip to content

Commit 8c6d1e8

Browse files
fix(runtime-wry): run cursor_position getter on main thread (#11401)
* fix(runtime-wry): run `cursor_position` getter on main thread closes #10340 * clippy * clippy --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 6dea12a commit 8c6d1e8

File tree

3 files changed

+31
-21
lines changed

3 files changed

+31
-21
lines changed

.changes/curosr-position-gtk.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": "patch:bug"
3+
"tauri-runtime-wry": "patch:bug"
4+
---
5+
6+
Fix `App/AppHandle/Window/Webview/WebviewWindow::cursor_position` getter method failing on Linux with `GDK may only be used from the main thread`.

crates/tauri-cli/src/mobile/init.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,9 @@ fn get_str<'a>(helper: &'a Helper) -> &'a str {
198198

199199
fn get_str_array(helper: &Helper, formatter: impl Fn(&str) -> String) -> Option<Vec<String>> {
200200
helper.param(0).and_then(|v| {
201-
v.value().as_array().and_then(|arr| {
202-
arr
203-
.iter()
204-
.map(|val| {
205-
val.as_str().map(
206-
#[allow(clippy::redundant_closure)]
207-
&formatter,
208-
)
209-
})
210-
.collect()
211-
})
201+
v.value()
202+
.as_array()
203+
.and_then(|arr| arr.iter().map(|val| val.as_str().map(&formatter)).collect())
212204
})
213205
}
214206

crates/tauri-runtime-wry/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ macro_rules! window_getter {
181181
}};
182182
}
183183

184+
macro_rules! event_loop_window_getter {
185+
($self: ident, $message: expr) => {{
186+
let (tx, rx) = channel();
187+
getter!($self, rx, Message::EventLoopWindowTarget($message(tx)))
188+
}};
189+
}
190+
184191
macro_rules! webview_getter {
185192
($self: ident, $message: expr) => {{
186193
let (tx, rx) = channel();
@@ -1268,6 +1275,10 @@ pub enum WebviewMessage {
12681275
IsDevToolsOpen(Sender<bool>),
12691276
}
12701277

1278+
pub enum EventLoopWindowTargetMessage {
1279+
CursorPosition(Sender<Result<PhysicalPosition<f64>>>),
1280+
}
1281+
12711282
pub type CreateWindowClosure<T> =
12721283
Box<dyn FnOnce(&EventLoopWindowTarget<Message<T>>) -> Result<WindowWrapper> + Send>;
12731284

@@ -1282,6 +1293,7 @@ pub enum Message<T: 'static> {
12821293
Application(ApplicationMessage),
12831294
Window(WindowId, WindowMessage),
12841295
Webview(WindowId, WebviewId, WebviewMessage),
1296+
EventLoopWindowTarget(EventLoopWindowTargetMessage),
12851297
CreateWebview(WindowId, CreateWebviewClosure),
12861298
CreateWindow(WindowId, CreateWindowClosure<T>),
12871299
CreateRawWindow(
@@ -2325,11 +2337,7 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
23252337
}
23262338

23272339
fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
2328-
self
2329-
.context
2330-
.main_thread
2331-
.window_target
2332-
.cursor_position()
2340+
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
23332341
.map(PhysicalPositionWrapper)
23342342
.map(Into::into)
23352343
.map_err(|_| Error::FailedToGetCursorPosition)
@@ -2616,11 +2624,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
26162624
}
26172625

26182626
fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
2619-
self
2620-
.context
2621-
.main_thread
2622-
.window_target
2623-
.cursor_position()
2627+
event_loop_window_getter!(self, EventLoopWindowTargetMessage::CursorPosition)?
26242628
.map(PhysicalPositionWrapper)
26252629
.map(Into::into)
26262630
.map_err(|_| Error::FailedToGetCursorPosition)
@@ -3452,6 +3456,14 @@ fn handle_user_message<T: UserEvent>(
34523456
}
34533457

34543458
Message::UserEvent(_) => (),
3459+
Message::EventLoopWindowTarget(message) => match message {
3460+
EventLoopWindowTargetMessage::CursorPosition(sender) => {
3461+
let pos = event_loop
3462+
.cursor_position()
3463+
.map_err(|_| Error::FailedToSendMessage);
3464+
sender.send(pos).unwrap();
3465+
}
3466+
},
34553467
}
34563468
}
34573469

0 commit comments

Comments
 (0)