Struct tauri::AppHandle

source ·
pub struct AppHandle<R: Runtime = Wry> { /* private fields */ }
Expand description

A handle to the currently running application.

This type implements Manager which allows for manipulation of global application items.

Implementations§

APIs specific to the wry runtime.

Create a new tao window using a callback. The event loop must be running at this point.

Sends a window message to the event loop.

Runs the given closure on the main thread.

Adds a Tauri application plugin. This function can be used to register a plugin that is loaded dynamically e.g. after login. For plugins that are created when the app is started, prefer Builder::plugin.

See Builder::plugin for more information.

Examples
use tauri::{plugin::{Builder as PluginBuilder, TauriPlugin}, Runtime};

fn init_plugin<R: Runtime>() -> TauriPlugin<R> {
  PluginBuilder::new("dummy").build()
}

tauri::Builder::default()
  .setup(move |app| {
    let handle = app.handle();
    std::thread::spawn(move || {
      handle.plugin(init_plugin());
    });

    Ok(())
  });

Removes the plugin with the given name.

Examples
use tauri::{plugin::{Builder as PluginBuilder, TauriPlugin, Plugin}, Runtime};

fn init_plugin<R: Runtime>() -> TauriPlugin<R> {
  PluginBuilder::new("dummy").build()
}

let plugin = init_plugin();
// `.name()` requires the `PLugin` trait import
let plugin_name = plugin.name();
tauri::Builder::default()
  .plugin(plugin)
  .setup(move |app| {
    let handle = app.handle();
    std::thread::spawn(move || {
      handle.remove_plugin(plugin_name);
    });

    Ok(())
  });

Exits the app. This is the same as std::process::exit, but it performs cleanup on this application.

Restarts the app. This is the same as crate::api::process::restart, but it performs cleanup on this application.

Available on crate feature updater only.

Gets the updater builder to manually check if an update is available.

Examples
tauri::Builder::default()
  .setup(|app| {
    let handle = app.handle();
    tauri::async_runtime::spawn(async move {
     let response = handle.updater().check().await;
    });
    Ok(())
  });
Available on crate feature system-tray only.

Gets a handle to the first system tray.

Prefer Self::tray_handle_by_id when multiple system trays are created.

Examples
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};

tauri::Builder::default()
  .setup(|app| {
    let app_handle = app.handle();
    SystemTray::new()
      .with_menu(
        SystemTrayMenu::new()
          .add_item(CustomMenuItem::new("quit", "Quit"))
          .add_item(CustomMenuItem::new("open", "Open"))
      )
      .on_event(move |event| {
        let tray_handle = app_handle.tray_handle();
      })
      .build(app)?;
    Ok(())
  });
Available on crate feature system-tray only.

Gets a handle to a system tray by its id.

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};

tauri::Builder::default()
  .setup(|app| {
    let app_handle = app.handle();
    let tray_id = "my-tray";
    SystemTray::new()
      .with_id(tray_id)
      .with_menu(
        SystemTrayMenu::new()
          .add_item(CustomMenuItem::new("quit", "Quit"))
          .add_item(CustomMenuItem::new("open", "Open"))
      )
      .on_event(move |event| {
        let tray_handle = app_handle.tray_handle_by_id(tray_id).unwrap();
      })
      .build(app)?;
    Ok(())
  });

The path resolver for the application.

Available on crate feature global-shortcut only.

Gets a copy of the global shortcut manager instance.

Available on crate feature clipboard only.

Gets a copy of the clipboard manager instance.

Gets the app’s configuration, defined on the tauri.conf.json file.

Gets the app’s package information.

The application’s asset resolver.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Grabs the Window from the CommandItem and returns the associated AppHandle. This will never fail.

Formats the value using the given formatter. Read more
The application handle associated with this manager.
The Config the manager was created with.
Emits a event to all windows.
Emits an event to a window with the specified label.
Listen to a global event.
Listen to a global event only once.
Trigger a global event.
Remove an event listener.
Fetch a single window from the manager.
Fetch all managed windows.
Add state to the state managed by the application. Read more
Retrieves the managed state for the type T. Read more
Attempts to retrieve the managed state for the type T. Read more
Gets the managed Env.
Gets the scope for the filesystem APIs.
Gets the scope for the asset protocol.
Gets the scope for the shell execute APIs.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more