Skip to content

Commit

Permalink
refactor(tauri-runtime-wry): register runtime plugin after run() (#6478)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Mar 17, 2023
1 parent 30e9467 commit ae296f3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changes/dynamic-wry-plugin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-runtime-wry": patch
"tauri": patch
---

Allow a wry plugin to be registered at runtime.
32 changes: 18 additions & 14 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub struct Context<T: UserEvent> {
main_thread_id: ThreadId,
pub proxy: WryEventLoopProxy<Message<T>>,
main_thread: DispatcherMainThreadContext<T>,
plugins: Arc<Mutex<Vec<Box<dyn Plugin<T> + Send>>>>,
}

impl<T: UserEvent> Context<T> {
Expand Down Expand Up @@ -1738,8 +1739,6 @@ pub trait Plugin<T: UserEvent> {
pub struct Wry<T: UserEvent> {
context: Context<T>,

plugins: Vec<Box<dyn Plugin<T>>>,

#[cfg(all(desktop, feature = "global-shortcut"))]
global_shortcut_manager_handle: GlobalShortcutManagerHandle<T>,

Expand Down Expand Up @@ -1830,6 +1829,18 @@ impl<T: UserEvent> WryHandle<T> {
.map_err(|_| Error::FailedToSendMessage)?;
Ok(())
}

pub fn plugin<P: PluginBuilder<T> + 'static>(&mut self, plugin: P)
where
<P as PluginBuilder<T>>::Plugin: Send,
{
self
.context
.plugins
.lock()
.unwrap()
.push(Box::new(plugin.build(self.context.clone())));
}
}

impl<T: UserEvent> RuntimeHandle<T> for WryHandle<T> {
Expand Down Expand Up @@ -1944,6 +1955,7 @@ impl<T: UserEvent> Wry<T> {
#[cfg(all(desktop, feature = "system-tray"))]
system_tray_manager,
},
plugins: Default::default(),
};

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

plugins: Default::default(),

#[cfg(all(desktop, feature = "global-shortcut"))]
global_shortcut_manager_handle,

Expand All @@ -1973,12 +1983,6 @@ impl<T: UserEvent> Wry<T> {
event_loop,
})
}

pub fn plugin<P: PluginBuilder<T> + 'static>(&mut self, plugin: P) {
self
.plugins
.push(Box::new(plugin.build(self.context.clone())));
}
}

impl<T: UserEvent> Runtime<T> for Wry<T> {
Expand Down Expand Up @@ -2142,7 +2146,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
let windows = self.context.main_thread.windows.clone();
let webview_id_map = self.context.webview_id_map.clone();
let web_context = &self.context.main_thread.web_context;
let plugins = &mut self.plugins;
let plugins = self.context.plugins.clone();
#[cfg(all(desktop, feature = "system-tray"))]
let system_tray_manager = self.context.main_thread.system_tray_manager.clone();

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

for p in plugins.iter_mut() {
for p in plugins.lock().unwrap().iter_mut() {
let prevent_default = p.on_event(
&event,
event_loop,
Expand Down Expand Up @@ -2219,7 +2223,7 @@ impl<T: UserEvent> Runtime<T> for Wry<T> {
let windows = self.context.main_thread.windows.clone();
let webview_id_map = self.context.webview_id_map.clone();
let web_context = self.context.main_thread.web_context;
let mut plugins = self.plugins;
let plugins = self.context.plugins.clone();

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

self.event_loop.run(move |event, event_loop, control_flow| {
for p in &mut plugins {
for p in plugins.lock().unwrap().iter_mut() {
let prevent_default = p.on_event(
&event,
event_loop,
Expand Down
8 changes: 5 additions & 3 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,13 @@ impl App<crate::Wry> {
/// # Stability
///
/// This API is unstable.
pub fn wry_plugin<P: tauri_runtime_wry::PluginBuilder<EventLoopMessage> + 'static>(
pub fn wry_plugin<P: tauri_runtime_wry::PluginBuilder<EventLoopMessage> + Send + 'static>(
&mut self,
plugin: P,
) {
self.runtime.as_mut().unwrap().plugin(plugin);
) where
<P as tauri_runtime_wry::PluginBuilder<EventLoopMessage>>::Plugin: Send,
{
self.handle.runtime_handle.plugin(plugin);
}
}

Expand Down

0 comments on commit ae296f3

Please sign in to comment.