Skip to content

Commit c17532f

Browse files
authored
refactor(core): change Plugin initialize signature, move register t… (#2347)
* refactor(core): change Plugin `initialize` signature, move register to AppHandle * clippy
1 parent d0142e8 commit c17532f

File tree

7 files changed

+44
-20
lines changed

7 files changed

+44
-20
lines changed

Diff for: .changes/dynamic-plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"tauri": patch
33
---
44

5-
Allow registering a plugin on structs that implements the `Manager` trait (`App`, `AppHandle`, `Window`) using the trait's `plugin` method.
5+
Allow registering a plugin through an `AppHandle` instance using the `plugin` method.

Diff for: .changes/plugin-initialize-app-handle.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
**Breaking change**: The `Plugin` trait `initialize` method now takes an `AppHandle` reference instead of `App`.

Diff for: core/tauri/src/app.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ impl<R: Runtime> AppHandle<R> {
199199
fn remove_system_tray(&self) -> crate::Result<()> {
200200
self.runtime_handle.remove_system_tray().map_err(Into::into)
201201
}
202+
203+
/// Adds a plugin to the runtime.
204+
pub fn plugin<P: Plugin<R> + 'static>(&self, mut plugin: P) -> crate::Result<()> {
205+
plugin
206+
.initialize(
207+
self,
208+
self
209+
.config()
210+
.plugins
211+
.0
212+
.get(plugin.name())
213+
.cloned()
214+
.unwrap_or_default(),
215+
)
216+
.map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))?;
217+
self
218+
.manager()
219+
.inner
220+
.plugins
221+
.lock()
222+
.unwrap()
223+
.register(plugin);
224+
Ok(())
225+
}
202226
}
203227

204228
impl<R: Runtime> Manager<R> for AppHandle<R> {}
@@ -832,7 +856,7 @@ impl<R: Runtime> Builder<R> {
832856
},
833857
};
834858

835-
app.manager.initialize_plugins(&app)?;
859+
app.manager.initialize_plugins(&app.handle())?;
836860

837861
let pending_labels = self
838862
.pending_windows

Diff for: core/tauri/src/lib.rs

-11
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,6 @@ pub trait Manager<R: Runtime>: sealed::ManagerBase<R> {
323323
{
324324
self.manager().inner.state.try_get()
325325
}
326-
327-
/// Adds a plugin to the runtime.
328-
fn plugin<P: plugin::Plugin<R> + 'static>(&self, plugin: P) {
329-
self
330-
.manager()
331-
.inner
332-
.plugins
333-
.lock()
334-
.unwrap()
335-
.register(plugin);
336-
}
337326
}
338327

339328
/// Prevent implementation details from leaking out of the [`Manager`] trait.

Diff for: core/tauri/src/manager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
window::{dpi::PhysicalSize, DetachedWindow, PendingWindow, WindowEvent},
2121
Icon, Runtime,
2222
},
23-
App, Context, Invoke, StateManager, Window,
23+
Context, Invoke, StateManager, Window,
2424
};
2525

2626
#[cfg(target_os = "windows")]
@@ -534,7 +534,7 @@ impl<R: Runtime> WindowManager<R> {
534534
.extend_api(invoke);
535535
}
536536

537-
pub fn initialize_plugins(&self, app: &App<R>) -> crate::Result<()> {
537+
pub fn initialize_plugins(&self, app: &AppHandle<R>) -> crate::Result<()> {
538538
self
539539
.inner
540540
.plugins

Diff for: core/tauri/src/plugin.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
//! Extend Tauri functionality.
66
7-
use crate::{api::config::PluginConfig, runtime::Runtime, App, Invoke, PageLoadPayload, Window};
7+
use crate::{
8+
api::config::PluginConfig, runtime::Runtime, AppHandle, Invoke, PageLoadPayload, Window,
9+
};
810
use serde_json::Value as JsonValue;
911
use std::collections::HashMap;
1012

@@ -20,7 +22,7 @@ pub trait Plugin<R: Runtime>: Send {
2022

2123
/// Initialize the plugin.
2224
#[allow(unused_variables)]
23-
fn initialize(&mut self, app: &App<R>, config: JsonValue) -> Result<()> {
25+
fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> {
2426
Ok(())
2527
}
2628

@@ -69,7 +71,11 @@ impl<R: Runtime> PluginStore<R> {
6971
}
7072

7173
/// Initializes all plugins in the store.
72-
pub(crate) fn initialize(&mut self, app: &App<R>, config: &PluginConfig) -> crate::Result<()> {
74+
pub(crate) fn initialize(
75+
&mut self,
76+
app: &AppHandle<R>,
77+
config: &PluginConfig,
78+
) -> crate::Result<()> {
7379
self.store.values_mut().try_for_each(|plugin| {
7480
plugin
7581
.initialize(

Diff for: docs/usage/guides/plugin.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Plugins allow you to hook into the Tauri application lifecycle and introduce new
1717
To write a plugin you just need to implement the `tauri::plugin::Plugin` trait:
1818

1919
```rust
20-
use tauri::{plugin::{Plugin, Result as PluginResult}, Runtime, PageLoadPayload, Window, Invoke, App};
20+
use tauri::{plugin::{Plugin, Result as PluginResult}, Runtime, PageLoadPayload, Window, Invoke, AppHandle};
2121

2222
struct MyAwesomePlugin<R: Runtime> {
2323
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
@@ -59,7 +59,7 @@ impl<R: Runtime> Plugin<R> for MyAwesomePlugin<R> {
5959
}
6060

6161
/// initialize plugin with the config provided on `tauri.conf.json > plugins > $yourPluginName` or the default value.
62-
fn initialize(&mut self, app: &App<R>, config: serde_json::Value) -> PluginResult<()> {
62+
fn initialize(&mut self, app: &AppHandle<R>, config: serde_json::Value) -> PluginResult<()> {
6363
Ok(())
6464
}
6565

0 commit comments

Comments
 (0)