pub trait Manager<R: Runtime>: ManagerBase<R> {
Show 18 methods
// Required method
fn resources_table(&self) -> MutexGuard<'_, ResourceTable>;
// Provided methods
fn app_handle(&self) -> &AppHandle<R> { ... }
fn config(&self) -> &Config { ... }
fn package_info(&self) -> &PackageInfo { ... }
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 get_webview(&self, label: &str) -> Option<Webview<R>> { ... }
fn webviews(&self) -> HashMap<String, Webview<R>> { ... }
fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>> { ... }
fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>> { ... }
fn manage<T>(&self, state: T) -> bool
where T: Send + Sync + 'static { ... }
fn unmanage<T>(&self) -> Option<T>
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 asset_protocol_scope(&self) -> Scope { ... }
fn path(&self) -> &PathResolver<R> { ... }
}
Expand description
Manages a running application.
Required Methods§
Sourcefn resources_table(&self) -> MutexGuard<'_, ResourceTable>
fn resources_table(&self) -> MutexGuard<'_, ResourceTable>
Get a reference to the resources table of this manager.
Provided Methods§
Sourcefn app_handle(&self) -> &AppHandle<R>
fn app_handle(&self) -> &AppHandle<R>
The application handle associated with this manager.
Sourcefn package_info(&self) -> &PackageInfo
fn package_info(&self) -> &PackageInfo
The PackageInfo
the manager was created with.
Sourcefn get_window(&self, label: &str) -> Option<Window<R>>
Available on crate feature unstable
only.
fn get_window(&self, label: &str) -> Option<Window<R>>
unstable
only.Fetch a single window from the manager.
Sourcefn get_focused_window(&self) -> Option<Window<R>>
Available on crate feature unstable
only.
fn get_focused_window(&self) -> Option<Window<R>>
unstable
only.Fetch the focused window. Returns None
if there is not any focused window.
Sourcefn windows(&self) -> HashMap<String, Window<R>>
Available on crate feature unstable
only.
fn windows(&self) -> HashMap<String, Window<R>>
unstable
only.Fetch all managed windows.
Sourcefn get_webview(&self, label: &str) -> Option<Webview<R>>
Available on crate feature unstable
only.
fn get_webview(&self, label: &str) -> Option<Webview<R>>
unstable
only.Fetch a single webview from the manager.
Sourcefn webviews(&self) -> HashMap<String, Webview<R>>
Available on crate feature unstable
only.
fn webviews(&self) -> HashMap<String, Webview<R>>
unstable
only.Fetch all managed webviews.
Sourcefn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
Fetch a single webview window from the manager.
Sourcefn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
Fetch all managed webview windows.
Sourcefn manage<T>(&self, state: T) -> bool
fn manage<T>(&self, state: T) -> bool
Add state
to the state managed by the application.
If the state for the T
type has previously been set, the state is unchanged and false is returned. Otherwise true is returned.
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.
Additionally, state
can be used to retrieve the value manually.
§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 unmanage<T>(&self) -> Option<T>
👎Deprecated since 2.3.0: This method is unsafe, since it can cause dangling references.
fn unmanage<T>(&self) -> Option<T>
Removes the state managed by the application for T. Returns the state if it was actually removed.
This method is UNSAFE and calling it will cause previously obtained references through Manager::state and State::inner to become dangling references.
It is currently deprecated and may be removed in the future.
If you really want to unmanage a state, use std::sync::Mutex and Option::take to wrap the state instead.
See tauri-apps/tauri#12721 for more information.
Sourcefn try_state<T>(&self) -> Option<State<'_, T>>
fn try_state<T>(&self) -> Option<State<'_, T>>
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) -> Scope
fn asset_protocol_scope(&self) -> Scope
Gets the scope for the asset protocol.
Sourcefn path(&self) -> &PathResolver<R>
fn path(&self) -> &PathResolver<R>
The path resolver.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.