Skip to content

Commit ae296f3

Browse files
authored
refactor(tauri-runtime-wry): register runtime plugin after run() (#6478)
1 parent 30e9467 commit ae296f3

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

.changes/dynamic-wry-plugin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime-wry": patch
3+
"tauri": patch
4+
---
5+
6+
Allow a wry plugin to be registered at runtime.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ pub struct Context<T: UserEvent> {
199199
main_thread_id: ThreadId,
200200
pub proxy: WryEventLoopProxy<Message<T>>,
201201
main_thread: DispatcherMainThreadContext<T>,
202+
plugins: Arc<Mutex<Vec<Box<dyn Plugin<T> + Send>>>>,
202203
}
203204

204205
impl<T: UserEvent> Context<T> {
@@ -1738,8 +1739,6 @@ pub trait Plugin<T: UserEvent> {
17381739
pub struct Wry<T: UserEvent> {
17391740
context: Context<T>,
17401741

1741-
plugins: Vec<Box<dyn Plugin<T>>>,
1742-
17431742
#[cfg(all(desktop, feature = "global-shortcut"))]
17441743
global_shortcut_manager_handle: GlobalShortcutManagerHandle<T>,
17451744

@@ -1830,6 +1829,18 @@ impl<T: UserEvent> WryHandle<T> {
18301829
.map_err(|_| Error::FailedToSendMessage)?;
18311830
Ok(())
18321831
}
1832+
1833+
pub fn plugin<P: PluginBuilder<T> + 'static>(&mut self, plugin: P)
1834+
where
1835+
<P as PluginBuilder<T>>::Plugin: Send,
1836+
{
1837+
self
1838+
.context
1839+
.plugins
1840+
.lock()
1841+
.unwrap()
1842+
.push(Box::new(plugin.build(self.context.clone())));
1843+
}
18331844
}
18341845

18351846
impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
@@ -1944,6 +1955,7 @@ impl<T: UserEvent> Wry<T> {
19441955
#[cfg(all(desktop, feature = "system-tray"))]
19451956
system_tray_manager,
19461957
},
1958+
plugins: Default::default(),
19471959
};
19481960

19491961
#[cfg(all(desktop, feature = "global-shortcut"))]
@@ -1962,8 +1974,6 @@ impl<T: UserEvent> Wry<T> {
19621974
Ok(Self {
19631975
context,
19641976

1965-
plugins: Default::default(),
1966-
19671977
#[cfg(all(desktop, feature = "global-shortcut"))]
19681978
global_shortcut_manager_handle,
19691979

@@ -1973,12 +1983,6 @@ impl<T: UserEvent> Wry<T> {
19731983
event_loop,
19741984
})
19751985
}
1976-
1977-
pub fn plugin<P: PluginBuilder<T> + 'static>(&mut self, plugin: P) {
1978-
self
1979-
.plugins
1980-
.push(Box::new(plugin.build(self.context.clone())));
1981-
}
19821986
}
19831987

19841988
impl<T: UserEvent> Runtime<T> for Wry<T> {
@@ -2142,7 +2146,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
21422146
let windows = self.context.main_thread.windows.clone();
21432147
let webview_id_map = self.context.webview_id_map.clone();
21442148
let web_context = &self.context.main_thread.web_context;
2145-
let plugins = &mut self.plugins;
2149+
let plugins = self.context.plugins.clone();
21462150
#[cfg(all(desktop, feature = "system-tray"))]
21472151
let system_tray_manager = self.context.main_thread.system_tray_manager.clone();
21482152

@@ -2165,7 +2169,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
21652169
*control_flow = ControlFlow::Exit;
21662170
}
21672171

2168-
for p in plugins.iter_mut() {
2172+
for p in plugins.lock().unwrap().iter_mut() {
21692173
let prevent_default = p.on_event(
21702174
&event,
21712175
event_loop,
@@ -2219,7 +2223,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22192223
let windows = self.context.main_thread.windows.clone();
22202224
let webview_id_map = self.context.webview_id_map.clone();
22212225
let web_context = self.context.main_thread.web_context;
2222-
let mut plugins = self.plugins;
2226+
let plugins = self.context.plugins.clone();
22232227

22242228
#[cfg(all(desktop, feature = "system-tray"))]
22252229
let system_tray_manager = self.context.main_thread.system_tray_manager;
@@ -2235,7 +2239,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
22352239
let proxy = self.event_loop.create_proxy();
22362240

22372241
self.event_loop.run(move |event, event_loop, control_flow| {
2238-
for p in &mut plugins {
2242+
for p in plugins.lock().unwrap().iter_mut() {
22392243
let prevent_default = p.on_event(
22402244
&event,
22412245
event_loop,

core/tauri/src/app.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,13 @@ impl App<crate::Wry> {
629629
/// # Stability
630630
///
631631
/// This API is unstable.
632-
pub fn wry_plugin<P: tauri_runtime_wry::PluginBuilder<EventLoopMessage> + 'static>(
632+
pub fn wry_plugin<P: tauri_runtime_wry::PluginBuilder<EventLoopMessage> + Send + 'static>(
633633
&mut self,
634634
plugin: P,
635-
) {
636-
self.runtime.as_mut().unwrap().plugin(plugin);
635+
) where
636+
<P as tauri_runtime_wry::PluginBuilder<EventLoopMessage>>::Plugin: Send,
637+
{
638+
self.handle.runtime_handle.plugin(plugin);
637639
}
638640
}
639641

0 commit comments

Comments
 (0)