-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tauri) add plugin system for rust (#494)
* feat(tauri) add extension system * chore(tauri) rename extension to plugin * chore(tauri) add plugin docs * chore(tauri) expose WebView type * chore(changes) add changefile * fix(tauri) clippy warns * fix(changes) format * fix(changes) typo
- Loading branch information
1 parent
660a2d8
commit 78afee9
Showing
5 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"tauri": minor | ||
--- | ||
|
||
Plugin system added. You can hook into the webview lifecycle (`created`, `ready`) and extend the API adding logic to the `invoke_handler` by implementing the `tauri::plugin::Plugin` trait. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use std::sync::{Arc, Mutex}; | ||
use web_view::WebView; | ||
|
||
/// The plugin interface. | ||
pub trait Plugin { | ||
/// Callback invoked when the webview is created. | ||
#[allow(unused_variables)] | ||
fn created(&self, webview: &mut WebView<'_, ()>) {} | ||
|
||
/// Callback invoked when the webview is ready. | ||
#[allow(unused_variables)] | ||
fn ready(&self, webview: &mut WebView<'_, ()>) {} | ||
|
||
/// Add invoke_handler API extension commands. | ||
#[allow(unused_variables)] | ||
fn extend_api(&self, webview: &mut WebView<'_, ()>, payload: &str) -> Result<bool, String> { | ||
Err("unknown variant".to_string()) | ||
} | ||
} | ||
|
||
thread_local!(static PLUGINS: Arc<Mutex<Vec<Box<dyn Plugin>>>> = Default::default()); | ||
|
||
/// Registers a plugin. | ||
pub fn register(ext: impl Plugin + 'static) { | ||
PLUGINS.with(|plugins| { | ||
let mut exts = plugins.lock().unwrap(); | ||
exts.push(Box::new(ext)); | ||
}); | ||
} | ||
|
||
fn run_plugin<T: FnMut(&Box<dyn Plugin>)>(mut callback: T) { | ||
PLUGINS.with(|plugins| { | ||
let exts = plugins.lock().unwrap(); | ||
for ext in exts.iter() { | ||
callback(ext); | ||
} | ||
}); | ||
} | ||
|
||
pub(crate) fn created(webview: &mut WebView<'_, ()>) { | ||
run_plugin(|ext| { | ||
ext.created(webview); | ||
}); | ||
} | ||
|
||
pub(crate) fn ready(webview: &mut WebView<'_, ()>) { | ||
run_plugin(|ext| { | ||
ext.ready(webview); | ||
}); | ||
} | ||
|
||
pub(crate) fn extend_api(webview: &mut WebView<'_, ()>, arg: &str) -> Result<bool, String> { | ||
PLUGINS.with(|plugins| { | ||
let exts = plugins.lock().unwrap(); | ||
for ext in exts.iter() { | ||
match ext.extend_api(webview, arg) { | ||
Ok(handled) => { | ||
if handled { | ||
return Ok(true); | ||
} | ||
} | ||
Err(e) => { | ||
if !e.contains("unknown variant") { | ||
return Err(e); | ||
} | ||
} | ||
} | ||
} | ||
Ok(false) | ||
}) | ||
} |