pub trait Manager<R: Runtime>: ManagerBase<R> {
Show 19 methods
// Provided methods
fn app_handle(&self) -> AppHandle<R> { ... }
fn config(&self) -> Arc<Config> { ... }
fn emit_all<S: Serialize + Clone>(
&self,
event: &str,
payload: S
) -> Result<()> { ... }
fn emit_to<S: Serialize + Clone>(
&self,
label: &str,
event: &str,
payload: S
) -> Result<()> { ... }
fn listen_global<F>(
&self,
event: impl Into<String>,
handler: F
) -> EventHandler
where F: Fn(Event) + Send + 'static { ... }
fn once_global<F>(
&self,
event: impl Into<String>,
handler: F
) -> EventHandler
where F: FnOnce(Event) + Send + 'static { ... }
fn trigger_global(&self, event: &str, data: Option<String>) { ... }
fn unlisten(&self, handler_id: EventHandler) { ... }
fn get_window(&self, label: &str) -> Option<Window<R>> { ... }
fn get_focused_window(&self) -> Option<Window<R>> { ... }
fn windows(&self) -> HashMap<String, Window<R>> { ... }
fn manage<T>(&self, state: T) -> bool
where T: Send + Sync + 'static { ... }
fn state<T>(&self) -> State<'_, T>
where T: Send + Sync + 'static { ... }
fn try_state<T>(&self) -> Option<State<'_, T>>
where T: Send + Sync + 'static { ... }
fn env(&self) -> Env { ... }
fn fs_scope(&self) -> FsScope { ... }
fn ipc_scope(&self) -> IpcScope { ... }
fn asset_protocol_scope(&self) -> FsScope { ... }
fn shell_scope(&self) -> ShellScope { ... }
}
Expand description
Manages a running application.
Provided Methods§
sourcefn app_handle(&self) -> AppHandle<R>
fn app_handle(&self) -> AppHandle<R>
The application handle associated with this manager.
sourcefn emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
fn emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
Emits a event to all windows.
Only the webviews receives this event.
To trigger Rust listeners, use Self::trigger_global
, Window::trigger
or Window::emit_and_trigger
.
Examples
use tauri::Manager;
#[tauri::command]
fn synchronize(app: tauri::AppHandle) {
// emits the synchronized event to all windows
app.emit_all("synchronized", ());
}
sourcefn emit_to<S: Serialize + Clone>(
&self,
label: &str,
event: &str,
payload: S
) -> Result<()>
fn emit_to<S: Serialize + Clone>( &self, label: &str, event: &str, payload: S ) -> Result<()>
Emits an event to the window with the specified label.
Examples
use tauri::Manager;
#[tauri::command]
fn download(app: tauri::AppHandle) {
for i in 1..100 {
std::thread::sleep(std::time::Duration::from_millis(150));
// emit a download progress event to the updater window
app.emit_to("updater", "download-progress", i);
}
}
sourcefn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere
F: Fn(Event) + Send + 'static,
fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere F: Fn(Event) + Send + 'static,
Listen to a event triggered on any window (Window::trigger
or Window::emit_and_trigger
) or with Self::trigger_global
.
Examples
use tauri::Manager;
#[tauri::command]
fn synchronize(window: tauri::Window) {
// emits the synchronized event to all windows
window.emit_and_trigger("synchronized", ());
}
tauri::Builder::default()
.setup(|app| {
app.listen_global("synchronized", |event| {
println!("app is in sync");
});
Ok(())
})
.invoke_handler(tauri::generate_handler![synchronize]);
sourcefn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere
F: FnOnce(Event) + Send + 'static,
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandlerwhere F: FnOnce(Event) + Send + 'static,
Listen to a global event only once.
See Self::listen_global
for more information.
sourcefn trigger_global(&self, event: &str, data: Option<String>)
fn trigger_global(&self, event: &str, data: Option<String>)
Trigger a global event to Rust listeners.
To send the events to the webview, see Self::emit_all
and Self::emit_to
.
To trigger listeners registed on an specific window, see Window::trigger
.
To trigger all listeners, see Window::emit_and_trigger
.
A global event does not have a source or target window attached.
Examples
use tauri::Manager;
#[tauri::command]
fn download(app: tauri::AppHandle) {
for i in 1..100 {
std::thread::sleep(std::time::Duration::from_millis(150));
// emit a download progress event to all listeners registed in Rust
app.trigger_global("download-progress", Some(i.to_string()));
}
}
sourcefn unlisten(&self, handler_id: EventHandler)
fn unlisten(&self, handler_id: EventHandler)
Remove an event listener.
Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
let handle = app.handle();
let handler = app.listen_global("ready", move |event| {
println!("app is ready");
// we no longer need to listen to the event
// we also could have used `app.once_global` instead
handle.unlisten(event.id());
});
// stop listening to the event when you do not need it anymore
app.unlisten(handler);
Ok(())
});
sourcefn get_window(&self, label: &str) -> Option<Window<R>>
fn get_window(&self, label: &str) -> Option<Window<R>>
Fetch a single window from the manager.
sourcefn get_focused_window(&self) -> Option<Window<R>>
fn get_focused_window(&self) -> Option<Window<R>>
Fetch the focused window. Returns None
if there is not any focused window.
sourcefn manage<T>(&self, state: T) -> boolwhere
T: Send + Sync + 'static,
fn manage<T>(&self, state: T) -> boolwhere 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
.
If a state for T
is already managed, the function returns false and the value is ignored.
Managed state can be retrieved by any command handler via the
State
guard. In particular, if a value of type T
is managed by Tauri, adding State<T>
to the list of arguments in a
command 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 {
store: Mutex<HashMap<u64, String>>,
}
struct Connection;
struct DbConnection {
db: Mutex<Option<Connection>>,
}
#[tauri::command]
fn connect(connection: State<DbConnection>) {
// initialize the connection, mutating the state with interior mutability
*connection.db.lock().unwrap() = Some(Connection {});
}
#[tauri::command]
fn storage_insert(key: u64, value: String, storage: State<Storage>) {
// mutate the storage behind the Mutex
storage.store.lock().unwrap().insert(key, value);
}
tauri::Builder::default()
.manage(Storage { store: Default::default() })
.manage(DbConnection { db: 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::{Manager, 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()
.setup(|app| {
app.manage(MyInt(0));
app.manage(MyString("tauri".into()));
// `MyInt` is already managed, so `manage()` returns false
assert!(!app.manage(MyInt(1)));
// read the `MyInt` managed state with the turbofish syntax
let int = app.state::<MyInt>();
assert_eq!(int.0, 0);
// read the `MyString` managed state with the `State` guard
let val: State<MyString> = app.state();
assert_eq!(val.0, "tauri");
Ok(())
})
.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");
sourcefn try_state<T>(&self) -> Option<State<'_, T>>where
T: Send + Sync + 'static,
fn try_state<T>(&self) -> Option<State<'_, T>>where T: Send + Sync + 'static,
Attempts to retrieve the managed state for the type T
.
Returns Some
if the state has previously been managed. Otherwise returns None
.
sourcefn asset_protocol_scope(&self) -> FsScope
fn asset_protocol_scope(&self) -> FsScope
Gets the scope for the asset protocol.
sourcefn shell_scope(&self) -> ShellScope
fn shell_scope(&self) -> ShellScope
Gets the scope for the shell execute APIs.