pub trait Plugin<R: Runtime>: Send {
fn name(&self) -> &'static str;
fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()> { ... }
fn initialization_script(&self) -> Option<String> { ... }
fn created(&mut self, window: Window<R>) { ... }
fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload) { ... }
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) { ... }
fn extend_api(&mut self, invoke: Invoke<R>) { ... }
}
Expand description
The plugin interface.
Required Methods§
Provided Methods§
sourcefn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()>
fn initialize(&mut self, app: &AppHandle<R>, config: JsonValue) -> Result<()>
Initializes the plugin.
Examples found in repository?
526 527 528 529 530 531 532 533 534 535 536 537 538 539
pub(crate) fn initialize(
&mut self,
app: &AppHandle<R>,
config: &PluginConfig,
) -> crate::Result<()> {
self.store.values_mut().try_for_each(|plugin| {
plugin
.initialize(
app,
config.0.get(plugin.name()).cloned().unwrap_or_default(),
)
.map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))
})
}
More examples
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
pub fn plugin<P: Plugin<R> + 'static>(&self, mut plugin: P) -> crate::Result<()> {
plugin
.initialize(
self,
self
.config()
.plugins
.0
.get(plugin.name())
.cloned()
.unwrap_or_default(),
)
.map_err(|e| crate::Error::PluginInitialization(plugin.name().to_string(), e.to_string()))?;
self
.manager()
.inner
.plugins
.lock()
.unwrap()
.register(plugin);
Ok(())
}
sourcefn initialization_script(&self) -> Option<String>
fn initialization_script(&self) -> Option<String>
Add the provided JavaScript to a list of scripts that should be run after the global object has been created, but before the HTML document has been parsed and before any other script included by the HTML document is run.
Since it runs on all top-level document and child frame page navigations,
it’s recommended to check the window.location
to guard your script from running on unexpected origins.
The script is wrapped into its own context with (function () { /* your script here */ })();
,
so global variables must be assigned to window
instead of implicity declared.
sourcefn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload)
fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload)
Callback invoked when the webview performs a navigation to a page.
sourcefn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent)
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent)
Callback invoked when the event loop receives a new event.
sourcefn extend_api(&mut self, invoke: Invoke<R>)
fn extend_api(&mut self, invoke: Invoke<R>)
Extend commands to crate::Builder::invoke_handler
.
Examples found in repository?
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
pub(crate) fn extend_api(&mut self, mut invoke: Invoke<R>) {
let command = invoke.message.command.replace("plugin:", "");
let mut tokens = command.split('|');
// safe to unwrap: split always has a least one item
let target = tokens.next().unwrap();
if let Some(plugin) = self.store.get_mut(target) {
invoke.message.command = tokens
.next()
.map(|c| c.to_string())
.unwrap_or_else(String::new);
plugin.extend_api(invoke);
} else {
invoke
.resolver
.reject(format!("plugin {} not found", target));
}
}