mod commands;
mod registry;
mod scripts;
pub use registry::{CefWebview, Registry};
use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, Runtime,
};
#[derive(Clone, Debug, Default)]
pub struct Config {
pub js: Vec<String>,
pub css: Vec<String>,
}
impl Config {
pub fn js(mut self, script: impl Into<String>) -> Self {
self.js.push(script.into());
self
}
pub fn css(mut self, css: impl Into<String>) -> Self {
self.css.push(css.into());
self
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
init_with(Config::default())
}
pub fn init_with<R: Runtime>(config: Config) -> TauriPlugin<R> {
PluginBuilder::new("cef-webview")
.invoke_handler(tauri::generate_handler![
commands::create,
commands::set_rect,
commands::set_visible,
commands::destroy,
commands::eval,
commands::insert_css,
commands::remove_inserted_css,
commands::navigate,
commands::reload,
commands::open_devtools,
])
.setup(move |app, _api| {
app.manage(Registry::<R>::new(config));
Ok(())
})
.build()
}
pub trait CefWebviewExt<R: Runtime> {
fn cef_webviews(&self) -> tauri::State<'_, Registry<R>>;
}
impl<R: Runtime, M: Manager<R>> CefWebviewExt<R> for M {
fn cef_webviews(&self) -> tauri::State<'_, Registry<R>> {
self.state::<Registry<R>>()
}
}