|
@@ -59,6 +59,7 @@ type SetupWithConfigHook<R, T> = dyn FnOnce(&AppHandle<R>, T) -> Result<()> + Se
|
|
|
type OnWebviewReady<R> = dyn FnMut(Window<R>) + Send;
|
|
|
type OnEvent<R> = dyn FnMut(&AppHandle<R>, &RunEvent) + Send;
|
|
|
type OnPageLoad<R> = dyn FnMut(Window<R>, PageLoadPayload) + Send;
|
|
|
+type OnDrop<R> = dyn FnOnce(AppHandle<R>) + Send;
|
|
|
|
|
|
/// Builds a [`TauriPlugin`].
|
|
|
///
|
|
@@ -141,6 +142,7 @@ pub struct Builder<R: Runtime, C: DeserializeOwned = ()> {
|
|
|
on_page_load: Box<OnPageLoad<R>>,
|
|
|
on_webview_ready: Box<OnWebviewReady<R>>,
|
|
|
on_event: Box<OnEvent<R>>,
|
|
|
+ on_drop: Option<Box<OnDrop<R>>>,
|
|
|
}
|
|
|
|
|
|
impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
@@ -155,6 +157,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
on_page_load: Box::new(|_, _| ()),
|
|
|
on_webview_ready: Box::new(|_| ()),
|
|
|
on_event: Box::new(|_, _| ()),
|
|
|
+ on_drop: None,
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -311,7 +314,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
#[must_use]
|
|
|
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
|
|
|
where
|
|
|
- F: FnMut(Window<R>, PageLoadPayload) + Send + Sync + 'static,
|
|
|
+ F: FnMut(Window<R>, PageLoadPayload) + Send + 'static,
|
|
|
{
|
|
|
self.on_page_load = Box::new(on_page_load);
|
|
|
self
|
|
@@ -335,7 +338,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
#[must_use]
|
|
|
pub fn on_webview_ready<F>(mut self, on_webview_ready: F) -> Self
|
|
|
where
|
|
|
- F: FnMut(Window<R>) + Send + Sync + 'static,
|
|
|
+ F: FnMut(Window<R>) + Send + 'static,
|
|
|
{
|
|
|
self.on_webview_ready = Box::new(on_webview_ready);
|
|
|
self
|
|
@@ -367,16 +370,42 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
#[must_use]
|
|
|
pub fn on_event<F>(mut self, on_event: F) -> Self
|
|
|
where
|
|
|
- F: FnMut(&AppHandle<R>, &RunEvent) + Send + Sync + 'static,
|
|
|
+ F: FnMut(&AppHandle<R>, &RunEvent) + Send + 'static,
|
|
|
{
|
|
|
self.on_event = Box::new(on_event);
|
|
|
self
|
|
|
}
|
|
|
|
|
|
+ /// Callback invoked when the plugin is dropped.
|
|
|
+ ///
|
|
|
+ /// # Examples
|
|
|
+ ///
|
|
|
+ /// ```rust
|
|
|
+ /// use tauri::{plugin::{Builder, TauriPlugin}, Runtime};
|
|
|
+ ///
|
|
|
+ /// fn init<R: Runtime>() -> TauriPlugin<R> {
|
|
|
+ /// Builder::new("example")
|
|
|
+ /// .on_drop(|app| {
|
|
|
+ /// println!("plugin has been dropped and is no longer running");
|
|
|
+ /// // you can run cleanup logic here
|
|
|
+ /// })
|
|
|
+ /// .build()
|
|
|
+ /// }
|
|
|
+ /// ```
|
|
|
+ #[must_use]
|
|
|
+ pub fn on_drop<F>(mut self, on_drop: F) -> Self
|
|
|
+ where
|
|
|
+ F: FnOnce(AppHandle<R>) + Send + 'static,
|
|
|
+ {
|
|
|
+ self.on_drop.replace(Box::new(on_drop));
|
|
|
+ self
|
|
|
+ }
|
|
|
+
|
|
|
/// Builds the [TauriPlugin].
|
|
|
pub fn build(self) -> TauriPlugin<R, C> {
|
|
|
TauriPlugin {
|
|
|
name: self.name,
|
|
|
+ app: None,
|
|
|
invoke_handler: self.invoke_handler,
|
|
|
setup: self.setup,
|
|
|
setup_with_config: self.setup_with_config,
|
|
@@ -384,6 +413,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
on_page_load: self.on_page_load,
|
|
|
on_webview_ready: self.on_webview_ready,
|
|
|
on_event: self.on_event,
|
|
|
+ on_drop: self.on_drop,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -391,6 +421,7 @@ impl<R: Runtime, C: DeserializeOwned> Builder<R, C> {
|
|
|
/// Plugin struct that is returned by the [`Builder`]. Should only be constructed through the builder.
|
|
|
pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> {
|
|
|
name: &'static str,
|
|
|
+ app: Option<AppHandle<R>>,
|
|
|
invoke_handler: Box<InvokeHandler<R>>,
|
|
|
setup: Option<Box<SetupHook<R>>>,
|
|
|
setup_with_config: Option<Box<SetupWithConfigHook<R, C>>>,
|
|
@@ -398,6 +429,15 @@ pub struct TauriPlugin<R: Runtime, C: DeserializeOwned = ()> {
|
|
|
on_page_load: Box<OnPageLoad<R>>,
|
|
|
on_webview_ready: Box<OnWebviewReady<R>>,
|
|
|
on_event: Box<OnEvent<R>>,
|
|
|
+ on_drop: Option<Box<OnDrop<R>>>,
|
|
|
+}
|
|
|
+
|
|
|
+impl<R: Runtime, C: DeserializeOwned> Drop for TauriPlugin<R, C> {
|
|
|
+ fn drop(&mut self) {
|
|
|
+ if let (Some(on_drop), Some(app)) = (self.on_drop.take(), self.app.take()) {
|
|
|
+ on_drop(app);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> {
|
|
@@ -406,6 +446,7 @@ impl<R: Runtime, C: DeserializeOwned> Plugin<R> for TauriPlugin<R, C> {
|
|
|
}
|
|
|
|
|
|
fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> {
|
|
|
+ self.app.replace(app.clone());
|
|
|
if let Some(s) = self.setup.take() {
|
|
|
(s)(app)?;
|
|
|
}
|
|
@@ -466,6 +507,11 @@ impl<R: Runtime> PluginStore<R> {
|
|
|
self.store.insert(plugin.name(), Box::new(plugin)).is_some()
|
|
|
}
|
|
|
|
|
|
+ /// Removes the plugin with the given name from the store.
|
|
|
+ pub fn unregister(&mut self, plugin: &'static str) -> bool {
|
|
|
+ self.store.remove(plugin).is_some()
|
|
|
+ }
|
|
|
+
|
|
|
/// Initializes all plugins in the store.
|
|
|
pub(crate) fn initialize(
|
|
|
&mut self,
|