diff --git a/.changes/plugin-boxed.md b/.changes/plugin-boxed.md new file mode 100644 index 000000000..7108ae40b --- /dev/null +++ b/.changes/plugin-boxed.md @@ -0,0 +1,5 @@ +--- +tauri: "minor:enhance" +--- + +Add `AppHandle::plugin_boxed` and `Builder::plugin_boxed` methods to allow adding plugins in the form of boxed trait objects. diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index eba237315..08abce267 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -484,10 +484,16 @@ impl AppHandle { /// Ok(()) /// }); /// ``` - #[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))] pub fn plugin + 'static>(&self, plugin: P) -> crate::Result<()> { - let mut plugin = Box::new(plugin) as Box>; + self.plugin_boxed(Box::new(plugin)) + } + /// Adds a Tauri application plugin. + /// + /// This method is similar to [`Self::plugin`], + /// but accepts a boxed trait object instead of a generic type. + #[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))] + pub fn plugin_boxed(&self, mut plugin: Box>) -> crate::Result<()> { let mut store = self.manager().plugins.lock().unwrap(); store.initialize(&mut plugin, self, &self.config().plugins)?; store.register(plugin); @@ -1683,8 +1689,17 @@ tauri::Builder::default() /// .plugin(plugin::init()); /// ``` #[must_use] - pub fn plugin + 'static>(mut self, plugin: P) -> Self { - self.plugins.register(Box::new(plugin)); + pub fn plugin + 'static>(self, plugin: P) -> Self { + self.plugin_boxed(Box::new(plugin)) + } + + /// Adds a Tauri application plugin. + /// + /// This method is similar to [`Self::plugin`], + /// but accepts a boxed trait object instead of a generic type. + #[must_use] + pub fn plugin_boxed(mut self, plugin: Box>) -> Self { + self.plugins.register(plugin); self }