tauri_plugin_cef_webview/
lib.rs1mod commands;
5mod registry;
6mod scripts;
7
8pub use registry::{CefWebview, Registry};
9
10use tauri::{
11 plugin::{Builder as PluginBuilder, TauriPlugin},
12 Manager, Runtime,
13};
14
15const HOST_RUNTIME: &str = include_str!("element.js.inc");
19
20#[derive(Clone, Debug, Default)]
22pub struct Config {
23 pub js: Vec<String>,
25 pub css: Vec<String>,
27}
28
29impl Config {
30 pub fn js(mut self, script: impl Into<String>) -> Self {
32 self.js.push(script.into());
33 self
34 }
35
36 pub fn css(mut self, css: impl Into<String>) -> Self {
38 self.css.push(css.into());
39 self
40 }
41}
42
43pub fn init<R: Runtime>() -> TauriPlugin<R> {
45 init_with(Config::default())
46}
47
48pub fn init_with<R: Runtime>(config: Config) -> TauriPlugin<R> {
50 PluginBuilder::new("cef-webview")
51 .initialization_script(HOST_RUNTIME)
52 .invoke_handler(tauri::generate_handler![
53 commands::create,
54 commands::set_rect,
55 commands::set_visible,
56 commands::destroy,
57 commands::eval,
58 commands::insert_css,
59 commands::remove_inserted_css,
60 commands::navigate,
61 commands::reload,
62 commands::open_devtools,
63 commands::call,
64 ])
65 .setup(move |app, _api| {
66 app.manage(Registry::<R>::new(config));
67 Ok(())
68 })
69 .build()
70}
71
72pub trait CefWebviewExt<R: Runtime> {
74 fn cef_webviews(&self) -> tauri::State<'_, Registry<R>>;
76}
77
78impl<R: Runtime, M: Manager<R>> CefWebviewExt<R> for M {
79 fn cef_webviews(&self) -> tauri::State<'_, Registry<R>> {
80 self.state::<Registry<R>>()
81 }
82}