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§

source§

impl AppHandle<Wry>

APIs specific to the wry runtime.

source

pub fn create_tao_window<F: FnOnce() -> (String, WryWindowBuilder) + Send + 'static>(
    &self,
    f: F
) -> Result<Weak<Window>>

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

source

pub fn send_tao_window_event(
    &self,
    window_id: WindowId,
    message: WindowMessage
) -> Result<()>

Sends a window message to the event loop.

source§

impl<R: Runtime> AppHandle<R>

source

pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(
    &self,
    f: F
) -> Result<()>

Runs the given closure on the main thread.

source

pub fn plugin<P: Plugin<R> + 'static>(&self, plugin: P) -> Result<()>

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(())
  });
source

pub fn remove_plugin(&self, plugin: &'static str) -> bool

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(())
  });
source

pub fn exit(&self, exit_code: i32)

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

source

pub fn restart(&self)

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

source§

impl<R: Runtime> AppHandle<R>

source

pub fn updater(&self) -> UpdateBuilder<R>

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(())
  });
source

pub fn tray_handle(&self) -> SystemTrayHandle<R>

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(())
  });
source

pub fn tray_handle_by_id(&self, id: &str) -> Option<SystemTrayHandle<R>>

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(())
  });
source

pub fn path_resolver(&self) -> PathResolver

The path resolver for the application.

source

pub fn global_shortcut_manager(&self) -> R::GlobalShortcutManager

Available on crate feature global-shortcut only.

Gets a copy of the global shortcut manager instance.

source

pub fn clipboard_manager(&self) -> R::ClipboardManager

Available on crate feature clipboard only.

Gets a copy of the clipboard manager instance.

source

pub fn config(&self) -> Arc<Config>

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

source

pub fn package_info(&self) -> &PackageInfo

Gets the app’s package information.

source

pub fn asset_resolver(&self) -> AssetResolver<R>

The application’s asset resolver.

Trait Implementations§

source§

impl<R: Runtime> Clone for AppHandle<R>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R>

source§

fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>

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

source§

impl<R: Debug + Runtime> Debug for AppHandle<R>where
    R::Handle: Debug,
    R::GlobalShortcutManager: Debug,
    R::ClipboardManager: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: Runtime> HasRawDisplayHandle for AppHandle<R>

source§

impl<R: Runtime> Manager<R> for AppHandle<R>

source§

fn app_handle(&self) -> AppHandle<R>

The application handle associated with this manager.
source§

fn config(&self) -> Arc<Config>

The Config the manager was created with.
source§

fn emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>

Emits a event to all windows.
source§

fn emit_to<S: Serialize + Clone>(
    &self,
    label: &str,
    event: &str,
    payload: S
) -> Result<()>

Emits an event to a window with the specified label.
source§

fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere
    F: Fn(Event) + Send + 'static,

Listen to a global event.
source§

fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere
    F: FnOnce(Event) + Send + 'static,

Listen to a global event only once.
source§

fn trigger_global(&self, event: &str, data: Option<String>)

Trigger a global event.
source§

fn unlisten(&self, handler_id: EventHandler)

Remove an event listener.
source§

fn get_window(&self, label: &str) -> Option<Window<R>>

Fetch a single window from the manager.
source§

fn windows(&self) -> HashMap<String, Window<R>>

Fetch all managed windows.
source§

fn manage<T>(&self, state: T) -> boolwhere
    T: Send + Sync + 'static,

Add state to the state managed by the application. Read more
source§

fn state<T>(&self) -> State<'_, T>where
    T: Send + Sync + 'static,

Retrieves the managed state for the type T. Read more
source§

fn try_state<T>(&self) -> Option<State<'_, T>>where
    T: Send + Sync + 'static,

Attempts to retrieve the managed state for the type T. Read more
source§

fn env(&self) -> Env

Gets the managed Env.
source§

fn fs_scope(&self) -> FsScope

Gets the scope for the filesystem APIs.
source§

fn asset_protocol_scope(&self) -> FsScope

Gets the scope for the asset protocol.
source§

fn shell_scope(&self) -> ShellScope

Gets the scope for the shell execute APIs.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

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

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere
    V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where
    S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> UserEvent for Twhere
    T: Debug + Clone + Send + 'static,