瀏覽代碼

refactor: return Weak<Window> on create_tao_window

Lucas Nogueira 3 年之前
父節點
當前提交
c1494b3532
共有 3 個文件被更改,包括 12 次插入6 次删除
  1. 6 0
      .changes/refactor-create-tao-window.md
  2. 4 4
      core/tauri-runtime-wry/src/lib.rs
  3. 2 2
      core/tauri/src/app.rs

+ 6 - 0
.changes/refactor-create-tao-window.md

@@ -0,0 +1,6 @@
+---
+"tauri-runtime-wry": patch
+"tauri": patch
+---
+
+Refactor `create_tao_window` API to return `Weak<Window>` instead of `Arc<Window>`.

+ 4 - 4
core/tauri-runtime-wry/src/lib.rs

@@ -93,7 +93,7 @@ use std::{
   sync::{
     atomic::{AtomicBool, Ordering},
     mpsc::{channel, Sender},
-    Arc, Mutex, MutexGuard,
+    Arc, Mutex, MutexGuard, Weak,
   },
   thread::{current as current_thread, ThreadId},
 };
@@ -976,7 +976,7 @@ pub enum Message {
   ),
   CreateWindow(
     Box<dyn FnOnce() -> (String, WryWindowBuilder) + Send>,
-    Sender<Result<Arc<Window>>>,
+    Sender<Result<Weak<Window>>>,
   ),
   GlobalShortcut(GlobalShortcutMessage),
   Clipboard(ClipboardMessage),
@@ -1485,7 +1485,7 @@ impl WryHandle {
   pub fn create_tao_window<F: FnOnce() -> (String, WryWindowBuilder) + Send + 'static>(
     &self,
     f: F,
-  ) -> Result<Arc<Window>> {
+  ) -> Result<Weak<Window>> {
     let (tx, rx) = channel();
     self
       .dispatcher_context
@@ -2181,7 +2181,7 @@ fn handle_event_loop(
               menu_items: Default::default(),
             },
           );
-          sender.send(Ok(w)).unwrap();
+          sender.send(Ok(Arc::downgrade(&w))).unwrap();
         } else {
           sender.send(Err(Error::CreateWindow)).unwrap();
         }

+ 2 - 2
core/tauri/src/app.rs

@@ -28,7 +28,7 @@ use tauri_utils::PackageInfo;
 use std::{
   collections::HashMap,
   path::PathBuf,
-  sync::{mpsc::Sender, Arc},
+  sync::{mpsc::Sender, Arc, Weak},
 };
 
 use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
@@ -187,7 +187,7 @@ impl AppHandle<crate::Wry> {
   >(
     &self,
     f: F,
-  ) -> crate::Result<Arc<tauri_runtime_wry::Window>> {
+  ) -> crate::Result<Weak<tauri_runtime_wry::Window>> {
     self.runtime_handle.create_tao_window(f).map_err(Into::into)
   }