pub struct Builder<R: Runtime> { /* private fields */ }
Expand description
Builds a Tauri application.
Examples
tauri::Builder::default()
// on an actual app, remove the string argument
.run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
.expect("error while running tauri application");
Implementations
sourceimpl<R: Runtime> Builder<R>
impl<R: Runtime> Builder<R>
sourcepub fn any_thread(self) -> Self
pub fn any_thread(self) -> Self
Builds a new Tauri application running on any thread, bypassing the main thread requirement.
Platform-specific
- macOS: on macOS the application must be executed on the main thread, so this function is not exposed.
sourcepub fn invoke_handler<F>(self, invoke_handler: F) -> Self where
F: Fn(Invoke<R>) + Send + Sync + 'static,
pub fn invoke_handler<F>(self, invoke_handler: F) -> Self where
F: Fn(Invoke<R>) + Send + Sync + 'static,
Defines the JS message handler callback.
Examples
#[tauri::command]
fn command_1() -> String {
return "hello world".to_string();
}
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
command_1,
// etc...
]);
sourcepub fn invoke_system<F>(
self,
initialization_script: String,
responder: F
) -> Self where
F: Fn(Window<R>, InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
pub fn invoke_system<F>(
self,
initialization_script: String,
responder: F
) -> Self where
F: Fn(Window<R>, InvokeResponse, CallbackFn, CallbackFn) + Send + Sync + 'static,
Defines a custom JS message system.
The responder
is a function that will be called when a command has been executed and must send a response to the JS layer.
The initialization_script
is a script that initializes window.__TAURI_POST_MESSAGE__
.
That function must take the message: object
argument and send it to the backend.
sourcepub fn setup<F>(self, setup: F) -> Self where
F: FnOnce(&mut App<R>) -> Result<(), Box<dyn Error + Send>> + Send + 'static,
pub fn setup<F>(self, setup: F) -> Self where
F: FnOnce(&mut App<R>) -> Result<(), Box<dyn Error + Send>> + Send + 'static,
Defines the setup hook.
Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
let main_window = app.get_window("main").unwrap();
tauri::api::dialog::blocking::message(Some(&main_window), "Hello", "Welcome back!");
Ok(())
});
sourcepub fn on_page_load<F>(self, on_page_load: F) -> Self where
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
pub fn on_page_load<F>(self, on_page_load: F) -> Self where
F: Fn(Window<R>, PageLoadPayload) + Send + Sync + 'static,
Defines the page load hook.
sourcepub fn manage<T>(self, state: T) -> Self where
T: Send + Sync + 'static,
pub fn manage<T>(self, state: T) -> Self where
T: Send + Sync + 'static,
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);
}
tauri::Builder::default()
.manage(Storage(Default::default()))
.manage(DbConnection(Default::default()))
.invoke_handler(tauri::generate_handler![connect, storage_insert])
// on an actual app, remove the string argument
.run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
.expect("error while running tauri application");
Examples
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);
}
tauri::Builder::default()
.manage(MyInt(10))
.manage(MyString("Hello, managed state!".to_string()))
.invoke_handler(tauri::generate_handler![int_command, string_command])
// on an actual app, remove the string argument
.run(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
.expect("error while running tauri application");
sourcepub fn create_window<F>(
self,
label: impl Into<String>,
url: WindowUrl,
setup: F
) -> Result<Self> where
F: FnOnce(<R::Dispatcher as Dispatch>::WindowBuilder, WebviewAttributes) -> (<R::Dispatcher as Dispatch>::WindowBuilder, WebviewAttributes),
pub fn create_window<F>(
self,
label: impl Into<String>,
url: WindowUrl,
setup: F
) -> Result<Self> where
F: FnOnce(<R::Dispatcher as Dispatch>::WindowBuilder, WebviewAttributes) -> (<R::Dispatcher as Dispatch>::WindowBuilder, WebviewAttributes),
Creates a new webview window.
Examples
use tauri::WindowBuilder;
tauri::Builder::default()
.create_window("main", tauri::WindowUrl::default(), |win, webview| {
let win = win
.title("My Main Window")
.resizable(true)
.inner_size(800.0, 550.0)
.min_inner_size(400.0, 200.0);
return (win, webview);
});
sourcepub fn system_tray(self, system_tray: SystemTray) -> Self
This is supported on crate feature system-tray
only.
pub fn system_tray(self, system_tray: SystemTray) -> Self
system-tray
only.Adds the icon configured on tauri.conf.json
to the system tray with the specified menu items.
Sets the menu to use on all windows.
Examples
use tauri::{MenuEntry, Submenu, MenuItem, Menu, CustomMenuItem};
tauri::Builder::default()
.menu(Menu::with_items([
MenuEntry::Submenu(Submenu::new(
"File",
Menu::with_items([
MenuItem::CloseWindow.into(),
#[cfg(target_os = "macos")]
CustomMenuItem::new("hello", "Hello").into(),
]),
)),
]));
Registers a menu event handler for all windows.
Examples
use tauri::{Menu, MenuEntry, Submenu, CustomMenuItem, api, Manager};
tauri::Builder::default()
.menu(Menu::with_items([
MenuEntry::Submenu(Submenu::new(
"File",
Menu::with_items([
CustomMenuItem::new("New", "New").into(),
CustomMenuItem::new("Learn More", "Learn More").into(),
]),
)),
]))
.on_menu_event(|event| {
match event.menu_item_id() {
"Learn More" => {
// open in browser (requires the `shell-open-api` feature)
api::shell::open(&event.window().shell_scope(), "https://github.com/tauri-apps/tauri".to_string(), None).unwrap();
}
id => {
// do something with other events
println!("got menu event: {}", id);
}
}
});
sourcepub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
self,
handler: F
) -> Self
pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
self,
handler: F
) -> Self
Registers a window event handler for all windows.
Examples
tauri::Builder::default()
.on_window_event(|event| match event.event() {
tauri::WindowEvent::Focused(focused) => {
// hide window whenever it loses focus
if !focused {
event.window().hide().unwrap();
}
}
_ => {}
});
sourcepub fn on_system_tray_event<F: Fn(&AppHandle<R>, SystemTrayEvent) + Send + Sync + 'static>(
self,
handler: F
) -> Self
This is supported on crate feature system-tray
only.
pub fn on_system_tray_event<F: Fn(&AppHandle<R>, SystemTrayEvent) + Send + Sync + 'static>(
self,
handler: F
) -> Self
system-tray
only.Registers a system tray event handler.
Examples
use tauri::Manager;
tauri::Builder::default()
.on_system_tray_event(|app, event| match event {
// show window with id "main" when the tray is left clicked
tauri::SystemTrayEvent::LeftClick { .. } => {
let window = app.get_window("main").unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
_ => {}
});
sourcepub fn register_uri_scheme_protocol<N: Into<String>, H: Fn(&AppHandle<R>, &HttpRequest) -> Result<HttpResponse, Box<dyn Error>> + Send + Sync + 'static>(
self,
uri_scheme: N,
protocol: H
) -> Self
pub fn register_uri_scheme_protocol<N: Into<String>, H: Fn(&AppHandle<R>, &HttpRequest) -> Result<HttpResponse, Box<dyn Error>> + Send + Sync + 'static>(
self,
uri_scheme: N,
protocol: H
) -> Self
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 asexample
.protocol
the protocol associated with the given URI scheme. It’s a function that takes an URL such asexample://localhost/asset.css
.
Trait Implementations
Auto Trait Implementations
impl<R> !RefUnwindSafe for Builder<R>
impl<R> Send for Builder<R> where
<<R as Runtime>::Dispatcher as Dispatch>::WindowBuilder: Send,
impl<R> !Sync for Builder<R>
impl<R> Unpin for Builder<R> where
<<R as Runtime>::Dispatcher as Dispatch>::WindowBuilder: Unpin,
impl<R> !UnwindSafe for Builder<R>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> NoneValue for T where
T: Default,
impl<T> NoneValue for T where
T: Default,
type NoneType = T
fn null_value() -> T
fn null_value() -> T
The none-equivalent value.