Trait tauri::plugin::Plugin

source ·
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§

The plugin name. Used as key on the plugin config object.

Provided Methods§

Initializes the plugin.

Examples found in repository?
src/plugin.rs (lines 533-536)
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
Hide additional examples
src/app.rs (lines 427-436)
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(())
  }

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.

Examples found in repository?
src/plugin.rs (line 546)
542
543
544
545
546
547
548
549
550
  pub(crate) fn initialization_script(&self) -> String {
    self
      .store
      .values()
      .filter_map(|p| p.initialization_script())
      .fold(String::new(), |acc, script| {
        format!("{}\n(function () {{ {} }})();", acc, script)
      })
  }

Callback invoked when the webview is created.

Examples found in repository?
src/plugin.rs (line 557)
553
554
555
556
557
558
  pub(crate) fn created(&mut self, window: Window<R>) {
    self
      .store
      .values_mut()
      .for_each(|plugin| plugin.created(window.clone()))
  }

Callback invoked when the webview performs a navigation to a page.

Examples found in repository?
src/plugin.rs (line 565)
561
562
563
564
565
566
  pub(crate) fn on_page_load(&mut self, window: Window<R>, payload: PageLoadPayload) {
    self
      .store
      .values_mut()
      .for_each(|plugin| plugin.on_page_load(window.clone(), payload.clone()))
  }

Callback invoked when the event loop receives a new event.

Examples found in repository?
src/plugin.rs (line 573)
569
570
571
572
573
574
  pub(crate) fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
    self
      .store
      .values_mut()
      .for_each(|plugin| plugin.on_event(app, event))
  }

Extend commands to crate::Builder::invoke_handler.

Examples found in repository?
src/plugin.rs (line 587)
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));
    }
  }

Implementors§