diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index 03166d272..35da2eab3 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -1,5 +1,6 @@ use std::cell::RefCell; use std::rc::Rc; +use std::rc::Weak; use crate::create_new_window; use crate::eval::EvalResult; @@ -30,8 +31,8 @@ use wry::webview::WebView; pub type ProxyType = EventLoopProxy; /// Get an imperative handle to the current window -pub fn use_window(cx: &ScopeState) -> &DesktopContext { - cx.use_hook(|| cx.consume_context::()) +pub fn use_window(cx: &ScopeState) -> &Rc { + cx.use_hook(|| cx.consume_context::>()) .as_ref() .unwrap() } @@ -111,8 +112,8 @@ impl DesktopContext { /// You can use this to control other windows from the current window. /// /// Be careful to not create a cycle of windows, or you might leak memory. - pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> DesktopContext { - let (window, desktop_context) = create_new_window( + pub fn new_window(&self, dom: VirtualDom, cfg: Config) -> Weak { + let window = create_new_window( cfg, &self.event_loop, &self.proxy, @@ -122,6 +123,12 @@ impl DesktopContext { self.shortcut_manager.clone(), ); + let desktop_context = window + .dom + .base_scope() + .consume_context::>() + .unwrap(); + let id = window.webview.window().id(); self.proxy @@ -134,7 +141,7 @@ impl DesktopContext { self.pending_windows.borrow_mut().push(window); - desktop_context + Rc::downgrade(&desktop_context) } /// trigger the drag-window event @@ -420,7 +427,7 @@ pub fn use_wry_event_handler( let id = desktop.create_wry_event_handler(handler); WryEventHandler { - handlers: desktop.event_handlers, + handlers: desktop.event_handlers.clone(), id, } }) diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index 0a9a8558c..2a6f5aa6b 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -144,7 +144,7 @@ pub fn launch_with_props(root: Component

, props: P, cfg: Config) let shortcut_manager = ShortcutRegistry::new(&event_loop); - let (web_view, _) = create_new_window( + let web_view = create_new_window( cfg, &event_loop, &proxy, @@ -282,30 +282,28 @@ fn create_new_window( queue: &WebviewQueue, event_handlers: &WindowEventHandlers, shortcut_manager: ShortcutRegistry, -) -> (WebviewHandler, DesktopContext) { +) -> WebviewHandler { let webview = webview::build(&mut cfg, event_loop, proxy.clone()); - let desktop_context = DesktopContext::new( + let desktop_context = Rc::from(DesktopContext::new( webview.clone(), proxy.clone(), event_loop.clone(), queue.clone(), event_handlers.clone(), shortcut_manager, - ); + )); dom.base_scope().provide_context(desktop_context.clone()); let id = webview.window().id(); // We want to poll the virtualdom and the event loop at the same time, so the waker will be connected to both - ( - WebviewHandler { - webview, - dom, - waker: waker::tao_waker(proxy, id), - }, - desktop_context, - ) + + WebviewHandler { + webview, + dom, + waker: waker::tao_waker(proxy, id), + } } struct WebviewHandler { diff --git a/packages/desktop/src/shortcut.rs b/packages/desktop/src/shortcut.rs index 5232bdb8b..e70d13235 100644 --- a/packages/desktop/src/shortcut.rs +++ b/packages/desktop/src/shortcut.rs @@ -146,7 +146,7 @@ pub struct ShortcutId { /// A global shortcut. This will be automatically removed when it is dropped. pub struct ShortcutHandle { - desktop: DesktopContext, + desktop: Rc, /// The id of the shortcut pub shortcut_id: ShortcutId, }