From 7f3c989111e007d7eeb5da118421214848e4bfcd Mon Sep 17 00:00:00 2001 From: Sean Wang <126865849+WSH032@users.noreply.github.com> Date: Fri, 18 Jul 2025 11:48:10 +0800 Subject: [PATCH] feat(tauri): add `plugin_boxed` methods (#13837) * feat(tauri): add `plugin_dyn` methods * refactor: rename `plugin_dyn` to `plugin_boxed` --- .changes/plugin-boxed.md | 5 +++++ crates/tauri/src/app.rs | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 .changes/plugin-boxed.md 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 }