feat(tauri): add plugin_boxed methods (#13837)

* feat(tauri): add `plugin_dyn` methods

* refactor: rename `plugin_dyn` to `plugin_boxed`
This commit is contained in:
Sean Wang
2025-07-18 11:48:10 +08:00
committed by GitHub
parent bda8304107
commit 7f3c989111
2 changed files with 24 additions and 4 deletions

5
.changes/plugin-boxed.md Normal file
View File

@@ -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.

View File

@@ -484,10 +484,16 @@ impl<R: Runtime> AppHandle<R> {
/// Ok(())
/// });
/// ```
#[cfg_attr(feature = "tracing", tracing::instrument(name = "app::plugin::register", skip(plugin), fields(name = plugin.name())))]
pub fn plugin<P: Plugin<R> + 'static>(&self, plugin: P) -> crate::Result<()> {
let mut plugin = Box::new(plugin) as Box<dyn Plugin<R>>;
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<dyn Plugin<R>>) -> 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<P: Plugin<R> + 'static>(mut self, plugin: P) -> Self {
self.plugins.register(Box::new(plugin));
pub fn plugin<P: Plugin<R> + '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<dyn Plugin<R>>) -> Self {
self.plugins.register(plugin);
self
}