Pārlūkot izejas kodu

turn desktop context into Rc<DesktopContext>

Andrew Collins 2 gadi atpakaļ
vecāks
revīzija
0e1f015b78

+ 13 - 6
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<UserWindowEvent>;
 
 /// Get an imperative handle to the current window
-pub fn use_window(cx: &ScopeState) -> &DesktopContext {
-    cx.use_hook(|| cx.consume_context::<DesktopContext>())
+pub fn use_window(cx: &ScopeState) -> &Rc<DesktopContext> {
+    cx.use_hook(|| cx.consume_context::<Rc<DesktopContext>>())
         .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<DesktopContext> {
+        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::<Rc<DesktopContext>>()
+            .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,
         }
     })

+ 10 - 12
packages/desktop/src/lib.rs

@@ -144,7 +144,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, 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 {

+ 1 - 1
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<DesktopContext>,
     /// The id of the shortcut
     pub shortcut_id: ShortcutId,
 }