diff --git a/crates/tauri-runtime-cef/src/lib.rs b/crates/tauri-runtime-cef/src/lib.rs index a50cca76f..ad4cdc957 100644 --- a/crates/tauri-runtime-cef/src/lib.rs +++ b/crates/tauri-runtime-cef/src/lib.rs @@ -45,7 +45,7 @@ mod cef_impl; #[macro_export] macro_rules! getter { ($self: ident, $rx: expr, $message: expr) => {{ - $self.context.post_message($message)?; + $crate::send_user_message(&$self.context, $message)?; $rx .recv() .map_err(|_| tauri_runtime::Error::FailedToReceiveMessage) @@ -372,6 +372,25 @@ impl RuntimeContext { } } +// Mirrors tauri-runtime-wry's send_user_message behavior: if we're already on the main +// thread, handle the message immediately; otherwise, post it to the main thread. +pub(crate) fn send_user_message( + context: &RuntimeContext, + message: Message, +) -> Result<()> { + if thread::current().id() == context.main_thread_id { + cef_impl::handle_message(&context.cef_context, message); + } else { + context + .main_thread_task_runner + .post_task(Some(&mut cef_impl::SendMessageTask::new( + context.cef_context.clone(), + Arc::new(RefCell::new(message)), + ))); + } + Ok(()) +} + impl fmt::Debug for RuntimeContext { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("RuntimeContext").finish()