Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use self::monitor::MonitorExt;
use http::Request;
#[cfg(target_os = "macos")]
use objc2::ClassType;
use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
use raw_window_handle::{DisplayHandle, HasWindowHandle};

#[cfg(windows)]
use tauri_runtime::webview::ScrollBarStyle;
Expand Down Expand Up @@ -237,8 +237,13 @@ pub(crate) fn send_user_message<T: UserEvent>(
message: Message<T>,
) -> Result<()> {
if current_thread().id() == context.main_thread_id {
let event_loop = context
.main_thread
.window_target
.upgrade()
.ok_or(Error::EventLoopClosed)?;
handle_user_message(
&context.main_thread.window_target,
&event_loop,
message,
UserMessageContext {
window_id_map: context.window_id_map.clone(),
Expand Down Expand Up @@ -439,7 +444,7 @@ unsafe impl Sync for WindowsStore {}

#[derive(Debug, Clone)]
pub struct DispatcherMainThreadContext<T: UserEvent> {
pub window_target: EventLoopWindowTarget<Message<T>>,
pub window_target: Weak<EventLoopWindowTarget<Message<T>>>,
pub web_context: WebContextStore,
// changing this to an Rc will cause frequent app crashes.
pub windows: Arc<WindowsStore>,
Expand Down Expand Up @@ -2658,6 +2663,7 @@ pub trait Plugin<T: UserEvent> {
pub struct Wry<T: UserEvent> {
context: Context<T>,
event_loop: EventLoop<Message<T>>,
_window_target: Arc<EventLoopWindowTarget<Message<T>>>,
}

impl<T: UserEvent> fmt::Debug for Wry<T> {
Expand Down Expand Up @@ -2784,14 +2790,16 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
fn display_handle(
&self,
) -> std::result::Result<DisplayHandle<'_>, raw_window_handle::HandleError> {
self.context.main_thread.window_target.display_handle()
todo!();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know how can we deal with this life time issue though...

// self.context.main_thread.window_target.display_handle()
}

fn primary_monitor(&self) -> Option<Monitor> {
self
.context
.main_thread
.window_target
.upgrade()?
.primary_monitor()
.map(|m| MonitorHandleWrapper(m).into())
}
Expand All @@ -2801,18 +2809,20 @@ impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
.context
.main_thread
.window_target
.upgrade()?
.monitor_from_point(x, y)
.map(|m| MonitorHandleWrapper(m).into())
}

fn available_monitors(&self) -> Vec<Monitor> {
self
.context
.main_thread
.window_target
.available_monitors()
.map(|m| MonitorHandleWrapper(m).into())
.collect()
if let Some(window_target) = self.context.main_thread.window_target.upgrade() {
window_target
.available_monitors()
.map(|m| MonitorHandleWrapper(m).into())
.collect()
} else {
Vec::new()
}
}

fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
Expand Down Expand Up @@ -2926,12 +2936,14 @@ impl<T: UserEvent> Wry<T> {
let windows = Arc::new(WindowsStore(RefCell::new(BTreeMap::default())));
let window_id_map = WindowIdStore::default();

let window_target = Arc::new(event_loop.deref().clone());

let context = Context {
window_id_map,
main_thread_id,
proxy: event_loop.create_proxy(),
main_thread: DispatcherMainThreadContext {
window_target: event_loop.deref().clone(),
window_target: Arc::downgrade(&window_target),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need Arc? Since keep in main thread isn't cross thread. Or something like run_on_main ?

https://github.com/tauri-apps/tao/blob/1080241d20d6bd0044d1cdb5633a25a3560f31cb/src/platform_impl/macos/util/async.rs#L43-L49

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing here is this context can be cloned and moved to other threads, and the inner rc from tao can't be cloned/dropped from multiple threads

web_context,
windows,
#[cfg(feature = "tracing")]
Expand All @@ -2948,6 +2960,7 @@ impl<T: UserEvent> Wry<T> {
Ok(Self {
context,
event_loop,
_window_target: window_target,
})
}
}
Expand Down Expand Up @@ -3115,37 +3128,29 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {

fn primary_monitor(&self) -> Option<Monitor> {
self
.context
.main_thread
.window_target
.event_loop
.primary_monitor()
.map(|m| MonitorHandleWrapper(m).into())
}

fn monitor_from_point(&self, x: f64, y: f64) -> Option<Monitor> {
self
.context
.main_thread
.window_target
.event_loop
.monitor_from_point(x, y)
.map(|m| MonitorHandleWrapper(m).into())
}

fn available_monitors(&self) -> Vec<Monitor> {
self
.context
.main_thread
.window_target
.event_loop
.available_monitors()
.map(|m| MonitorHandleWrapper(m).into())
.collect()
}

fn cursor_position(&self) -> Result<PhysicalPosition<f64>> {
self
.context
.main_thread
.window_target
.event_loop
.cursor_position()
.map(PhysicalPositionWrapper)
.map(Into::into)
Expand Down
Loading