Struct tauri::Builder[][src]

pub struct Builder<R: Runtime> { /* fields omitted */ }
Expand description

Builds a Tauri application.

Implementations

Creates a new App builder.

Defines the JS message handler callback.

Defines the setup hook.

Defines the page load hook.

Adds a plugin to the runtime.

Add state to the state managed by the application.

This method can be called any number of times as long as each call refers to a different T.

Managed state can be retrieved by any request handler via the State request guard. In particular, if a value of type T is managed by Tauri, adding State<T> to the list of arguments in a request handler instructs Tauri to retrieve the managed value.

Panics

Panics if state of type T is already being managed.

Mutability

Since the managed state is global and must be Send + Sync, mutations can only happen through interior mutability:

use std::{collections::HashMap, sync::Mutex};
use tauri::State;
// here we use Mutex to achieve interior mutability
struct Storage(Mutex<HashMap<u64, String>>);
struct Connection;
struct DbConnection(Mutex<Option<Connection>>);

#[tauri::command]
fn connect(connection: State<DbConnection>) {
  // initialize the connection, mutating the state with interior mutability
  *connection.0.lock().unwrap() = Some(Connection {});
}

#[tauri::command]
fn storage_insert(key: u64, value: String, storage: State<Storage>) {
  // mutate the storage behind the Mutex
  storage.0.lock().unwrap().insert(key, value);
}

fn main() {
  Builder::default()
    .manage(Storage(Default::default()))
    .manage(DbConnection(Default::default()))
    .invoke_handler(tauri::generate_handler![connect, storage_insert])
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Example

use tauri::State;

struct MyInt(isize);
struct MyString(String);

#[tauri::command]
fn int_command(state: State<MyInt>) -> String {
    format!("The stateful int is: {}", state.0)
}

#[tauri::command]
fn string_command<'r>(state: State<'r, MyString>) {
    println!("state: {}", state.inner().0);
}

fn main() {
    tauri::Builder::default()
        .manage(MyInt(10))
        .manage(MyString("Hello, managed state!".to_string()))
        .invoke_handler(tauri::generate_handler![int_command, string_command])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Creates a new webview window.

This is supported on crate feature system-tray only.

Adds the icon configured on tauri.conf.json to the system tray with the specified menu items.

This is supported on crate feature menu only.

Sets the menu to use on all windows.

This is supported on crate feature menu only.

Registers a menu event handler for all windows.

Registers a window event handler for all windows.

This is supported on crate feature system-tray only.

Registers a system tray event handler.

Registers a URI scheme protocol available to all webviews. Leverages setURLSchemeHandler on macOS, AddWebResourceRequestedFilter on Windows and webkit-web-context-register-uri-scheme on Linux.

Arguments

  • uri_scheme The URI scheme to register, such as example.
  • protocol the protocol associated with the given URI scheme. It’s a function that takes an URL such as example://localhost/asset.css.

Builds the application.

Runs the configured Tauri application.

Trait Implementations

Make Wry the default Runtime for Builder

Returns the “default value” for a type. Read more

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

Performs the conversion.

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

Performs the conversion.

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.