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.
impl AppHandle<Wry>
APIs specific to the wry runtime.
Sourcepub fn create_tao_window<F: FnOnce() -> (String, TaoWindowBuilder) + Send + 'static>(
&self,
f: F,
) -> Result<Weak<Window>>
pub fn create_tao_window<F: FnOnce() -> (String, TaoWindowBuilder) + 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.
Sourcepub fn send_tao_window_event(
&self,
window_id: TaoWindowId,
message: WindowMessage,
) -> Result<()>
pub fn send_tao_window_event( &self, window_id: TaoWindowId, message: WindowMessage, ) -> Result<()>
Sends a window message to the event loop.
Source§impl<R: Runtime> AppHandle<R>
impl<R: Runtime> AppHandle<R>
Sourcepub fn run_on_main_thread<F: FnOnce() + Send + 'static>(
&self,
f: F,
) -> Result<()>
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>( &self, f: F, ) -> Result<()>
Runs the given closure on the main thread.
Sourcepub fn plugin<P: Plugin<R> + 'static>(&self, plugin: P) -> Result<()>
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().clone();
std::thread::spawn(move || {
handle.plugin(init_plugin());
});
Ok(())
});
Sourcepub fn remove_plugin(&self, plugin: &'static str) -> bool
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().clone();
std::thread::spawn(move || {
handle.remove_plugin(plugin_name);
});
Ok(())
});
Sourcepub fn exit(&self, exit_code: i32)
pub fn exit(&self, exit_code: i32)
Exits the app by triggering RunEvent::ExitRequested
and RunEvent::Exit
.
Sourcepub fn restart(&self) -> !
pub fn restart(&self) -> !
Restarts the app by triggering RunEvent::ExitRequested
with code RESTART_EXIT_CODE
and RunEvent::Exit
.
When this function is called on the main thread, we cannot guarantee the delivery of those events, so we skip them and directly restart the process.
If you want to trigger them reliably, use Self::request_restart
instead
Sourcepub fn request_restart(&self)
pub fn request_restart(&self)
Restarts the app by triggering RunEvent::ExitRequested
with code RESTART_EXIT_CODE
and RunEvent::Exit
.
Source§impl<R: Runtime> AppHandle<R>
impl<R: Runtime> AppHandle<R>
Registers a global menu event listener.
Sourcepub fn on_tray_icon_event<F: Fn(&AppHandle<R>, TrayIconEvent) + Send + Sync + 'static>(
&self,
handler: F,
)
Available on desktop
and crate feature tray-icon
only.
pub fn on_tray_icon_event<F: Fn(&AppHandle<R>, TrayIconEvent) + Send + Sync + 'static>( &self, handler: F, )
desktop
and crate feature tray-icon
only.Registers a global tray icon menu event listener.
Sourcepub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
Available on desktop
and crate feature tray-icon
only.
pub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
desktop
and crate feature tray-icon
only.Gets a tray icon using the provided id.
Sourcepub fn remove_tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
Available on desktop
and crate feature tray-icon
only.
pub fn remove_tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>
desktop
and crate feature tray-icon
only.Removes a tray icon using the provided id from tauri’s internal state and returns it.
Note that dropping the returned icon, may cause the tray icon to disappear if it wasn’t cloned somewhere else or referenced by JS.
Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Gets the app’s configuration, defined on the tauri.conf.json
file.
Sourcepub fn package_info(&self) -> &PackageInfo
pub fn package_info(&self) -> &PackageInfo
Gets the app’s package information.
Sourcepub fn asset_resolver(&self) -> AssetResolver<R>
pub fn asset_resolver(&self) -> AssetResolver<R>
The application’s asset resolver.
Sourcepub fn primary_monitor(&self) -> Result<Option<Monitor>>
pub fn primary_monitor(&self) -> Result<Option<Monitor>>
Returns the primary monitor of the system.
Returns None if it can’t identify any monitor as a primary one.
Sourcepub fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>
pub fn monitor_from_point(&self, x: f64, y: f64) -> Result<Option<Monitor>>
Returns the monitor that contains the given point.
Sourcepub fn available_monitors(&self) -> Result<Vec<Monitor>>
pub fn available_monitors(&self) -> Result<Vec<Monitor>>
Returns the list of all the monitors available on the system.
Sourcepub fn cursor_position(&self) -> Result<PhysicalPosition<f64>>
pub fn cursor_position(&self) -> Result<PhysicalPosition<f64>>
Get the cursor position relative to the top-left hand corner of the desktop.
Note that the top-left hand corner of the desktop is not necessarily the same as the screen. If the user uses a desktop with multiple monitors, the top-left hand corner of the desktop is the top-left hand corner of the main monitor on Windows and macOS or the top-left of the leftmost monitor on X11.
The coordinates can be negative if the top-left hand corner of the window is outside of the visible screen region.
Sourcepub fn default_window_icon(&self) -> Option<&Image<'_>>
pub fn default_window_icon(&self) -> Option<&Image<'_>>
Returns the default window icon.
Returns the app-wide menu.
Sets the app-wide menu and returns the previous one.
If a window was not created with an explicit menu or had one set explicitly, this menu will be assigned to it.
Remove the app-wide menu and returns it.
If a window was not created with an explicit menu or had one set explicitly, this will remove the menu from it.
Hides the app-wide menu from windows that have it.
If a window was not created with an explicit menu or had one set explicitly, this will hide the menu from it.
Shows the app-wide menu for windows that have it.
If a window was not created with an explicit menu or had one set explicitly, this will show the menu for it.
Sourcepub fn cleanup_before_exit(&self)
pub fn cleanup_before_exit(&self)
Runs necessary cleanup tasks before exiting the process. You should always exit the tauri app immediately after this function returns and not use any tauri-related APIs.
Sourcepub fn invoke_key(&self) -> &str
pub fn invoke_key(&self) -> &str
Gets the invoke key that must be referenced when using crate::webview::InvokeRequest
.
§Security
DO NOT expose this key to third party scripts as might grant access to the backend from external URLs and iframes.
Trait Implementations§
Source§impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R>
impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R>
Source§fn from_command(command: CommandItem<'de, R>) -> Result<Self, InvokeError>
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: Runtime> Emitter<R> for AppHandle<R>
impl<R: Runtime> Emitter<R> for AppHandle<R>
Source§fn emit_str(&self, event: &str, payload: String) -> Result<()>
fn emit_str(&self, event: &str, payload: String) -> Result<()>
Emitter::emit
but the payload is json serialized.Source§fn emit_str_to<I>(&self, target: I, event: &str, payload: String) -> Result<()>where
I: Into<EventTarget>,
fn emit_str_to<I>(&self, target: I, event: &str, payload: String) -> Result<()>where
I: Into<EventTarget>,
Emitter::emit_to
but the payload is json serialized.Source§fn emit_str_filter<F>(
&self,
event: &str,
payload: String,
filter: F,
) -> Result<()>
fn emit_str_filter<F>( &self, event: &str, payload: String, filter: F, ) -> Result<()>
Emitter::emit_filter
but the payload is json serialized.Source§impl<R: Runtime> FunctionArg for AppHandle<R>
impl<R: Runtime> FunctionArg for AppHandle<R>
Source§impl<R: Runtime> HasDisplayHandle for AppHandle<R>
impl<R: Runtime> HasDisplayHandle for AppHandle<R>
Source§fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
Source§impl<R: Runtime> Listener<R> for AppHandle<R>
impl<R: Runtime> Listener<R> for AppHandle<R>
Source§fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventId
Listen to an event on this app.
§Examples
use tauri::Listener;
tauri::Builder::default()
.setup(|app| {
app.listen("component-loaded", move |event| {
println!("window just loaded a component");
});
Ok(())
});
Source§fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
fn once<F>(&self, event: impl Into<String>, handler: F) -> EventId
Listen to an event on this app only once.
See Self::listen
for more information.
Source§fn unlisten(&self, id: EventId)
fn unlisten(&self, id: EventId)
Unlisten to an event on this app.
§Examples
use tauri::Listener;
tauri::Builder::default()
.setup(|app| {
let handler = app.listen("component-loaded", move |event| {
println!("app just loaded a component");
});
// stop listening to the event when you do not need it anymore
app.unlisten(handler);
Ok(())
});
Source§impl<R: Runtime> Manager<R> for AppHandle<R>
impl<R: Runtime> Manager<R> for AppHandle<R>
Source§fn resources_table(&self) -> MutexGuard<'_, ResourceTable>
fn resources_table(&self) -> MutexGuard<'_, ResourceTable>
Source§fn app_handle(&self) -> &AppHandle<R>
fn app_handle(&self) -> &AppHandle<R>
Source§fn package_info(&self) -> &PackageInfo
fn package_info(&self) -> &PackageInfo
PackageInfo
the manager was created with.Source§fn get_window(&self, label: &str) -> Option<Window<R>>
fn get_window(&self, label: &str) -> Option<Window<R>>
unstable
only.Source§fn get_focused_window(&self) -> Option<Window<R>>
fn get_focused_window(&self) -> Option<Window<R>>
unstable
only.None
if there is not any focused window.Source§fn windows(&self) -> HashMap<String, Window<R>>
fn windows(&self) -> HashMap<String, Window<R>>
unstable
only.Source§fn get_webview(&self, label: &str) -> Option<Webview<R>>
fn get_webview(&self, label: &str) -> Option<Webview<R>>
unstable
only.Source§fn webviews(&self) -> HashMap<String, Webview<R>>
fn webviews(&self) -> HashMap<String, Webview<R>>
unstable
only.Source§fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
fn get_webview_window(&self, label: &str) -> Option<WebviewWindow<R>>
Source§fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
fn webview_windows(&self) -> HashMap<String, WebviewWindow<R>>
Source§fn manage<T>(&self, state: T) -> bool
fn manage<T>(&self, state: T) -> bool
state
to the state managed by the application. Read moreSource§fn unmanage<T>(&self) -> Option<T>
fn unmanage<T>(&self) -> Option<T>
Source§fn try_state<T>(&self) -> Option<State<'_, T>>
fn try_state<T>(&self) -> Option<State<'_, T>>
T
. Read moreSource§fn asset_protocol_scope(&self) -> Scope
fn asset_protocol_scope(&self) -> Scope
Source§fn path(&self) -> &PathResolver<R>
fn path(&self) -> &PathResolver<R>
Source§fn add_capability(&self, capability: impl RuntimeCapability) -> Result<()>
fn add_capability(&self, capability: impl RuntimeCapability) -> Result<()>
Auto Trait Implementations§
impl<R> Freeze for AppHandle<R>
impl<R = Wry<EventLoopMessage>> !RefUnwindSafe for AppHandle<R>
impl<R> Send for AppHandle<R>
impl<R> Sync for AppHandle<R>
impl<R> Unpin for AppHandle<R>
impl<R = Wry<EventLoopMessage>> !UnwindSafe for AppHandle<R>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
Source§fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
HasDisplayHandle
instead