Browse Source

feat(tauri): fetch window(s) from Manager (#1434)

chip 4 years ago
parent
commit
f4ae2d2487
2 changed files with 41 additions and 27 deletions
  1. 23 26
      tauri/src/runtime/manager.rs
  2. 18 1
      tauri/src/runtime/mod.rs

+ 23 - 26
tauri/src/runtime/manager.rs

@@ -22,14 +22,14 @@ use serde::Serialize;
 use serde_json::Value as JsonValue;
 use std::{
   borrow::Cow,
-  collections::HashSet,
+  collections::{HashMap, HashSet},
   convert::TryInto,
-  sync::{Arc, Mutex},
+  sync::{Arc, Mutex, MutexGuard},
 };
 use uuid::Uuid;
 
 pub struct InnerWindowManager<M: Params> {
-  windows: Mutex<HashSet<Window<M>>>,
+  windows: Mutex<HashMap<M::Label, Window<M>>>,
   plugins: Mutex<PluginStore<M>>,
   listeners: Listeners<M::Event, M::Label>,
 
@@ -101,6 +101,11 @@ where
     }
   }
 
+  /// Get a locked handle to the windows.
+  pub(crate) fn windows_lock(&self) -> MutexGuard<'_, HashMap<L, Window<Self>>> {
+    self.inner.windows.lock().expect("poisoned window manager")
+  }
+
   // setup content for dev-server
   #[cfg(dev)]
   fn get_url(&self) -> String {
@@ -439,11 +444,8 @@ where
     // insert the window into our manager
     {
       self
-        .inner
-        .windows
-        .lock()
-        .expect("poisoned window manager")
-        .insert(window.clone());
+        .windows_lock()
+        .insert(window.label().clone(), window.clone());
     }
 
     // let plugins know that a new window has been added to the manager
@@ -466,11 +468,8 @@ where
     filter: F,
   ) -> crate::Result<()> {
     self
-      .inner
-      .windows
-      .lock()
-      .expect("poisoned window manager")
-      .iter()
+      .windows_lock()
+      .values()
       .filter(|&w| filter(w))
       .try_for_each(|window| window.emit_internal(event.clone(), payload.clone()))
   }
@@ -482,24 +481,14 @@ where
     filter: F,
   ) -> crate::Result<()> {
     self
-      .inner
-      .windows
-      .lock()
-      .expect("poisoned window manager")
-      .iter()
+      .windows_lock()
+      .values()
       .filter(|&w| filter(w))
       .try_for_each(|window| window.emit(&event, payload.clone()))
   }
 
   fn labels(&self) -> HashSet<L> {
-    self
-      .inner
-      .windows
-      .lock()
-      .expect("poisoned window manager")
-      .iter()
-      .map(|w| w.label().clone())
-      .collect()
+    self.windows_lock().keys().cloned().collect()
   }
 
   fn config(&self) -> &Config {
@@ -569,6 +558,14 @@ where
       .expect("poisoned salt mutex")
       .remove(&uuid)
   }
+
+  fn get_window(&self, label: &L) -> Option<Window<Self>> {
+    self.windows_lock().get(label).cloned()
+  }
+
+  fn windows(&self) -> HashMap<L, Window<Self>> {
+    self.windows_lock().clone()
+  }
 }
 
 impl<E, L, A, R> Params for WindowManager<E, L, A, R>

+ 18 - 1
tauri/src/runtime/mod.rs

@@ -19,6 +19,7 @@ pub(crate) mod webview;
 pub(crate) mod window;
 
 pub use self::tag::Tag;
+use std::collections::HashMap;
 
 /// Important configurable items required by Tauri.
 pub struct Context<A: Assets> {
@@ -154,7 +155,7 @@ pub(crate) mod sealed {
     },
   };
   use serde::Serialize;
-  use std::collections::HashSet;
+  use std::collections::{HashMap, HashSet};
   use uuid::Uuid;
 
   /// private manager api
@@ -241,6 +242,12 @@ pub(crate) mod sealed {
 
     /// Verify that the passed salt is a valid salt in the manager.
     fn verify_salt(&self, salt: String) -> bool;
+
+    /// Get a single managed window.
+    fn get_window(&self, label: &M::Label) -> Option<Window<M>>;
+
+    /// Get all managed windows.
+    fn windows(&self) -> HashMap<M::Label, Window<M>>;
   }
 
   /// Represents a managed handle to the application runner.
@@ -326,6 +333,16 @@ pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
   fn unlisten(&self, handler_id: EventHandler) {
     self.manager().unlisten(handler_id)
   }
+
+  /// Fetch a single window from the manager.
+  fn get_window(&self, label: &M::Label) -> Option<Window<M>> {
+    self.manager().get_window(label)
+  }
+
+  /// Fetch all managed windows.
+  fn windows(&self) -> HashMap<M::Label, Window<M>> {
+    self.manager().windows()
+  }
 }
 
 /// Types that the manager needs to have passed in by the application.