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>
impl AppHandle<Wry>
APIs specific to the wry runtime.
sourcepub fn create_tao_window<F: FnOnce() -> (String, WryWindowBuilder) + Send + 'static>(
&self,
f: F
) -> Result<Weak<Window>>
pub fn create_tao_window<F: FnOnce() -> (String, WryWindowBuilder) + 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: WindowId,
message: WindowMessage
) -> Result<()>
pub fn send_tao_window_event( &self, window_id: WindowId, 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. This is the same as std::process::exit
, but it performs cleanup on this application.
sourcepub fn restart(&self)
pub fn restart(&self)
Restarts the app. This is the same as crate::process::restart
, but it performs cleanup on this application.
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(&self) -> Option<TrayIcon<R>>
Available on desktop
and crate feature tray-icon
only.
pub fn tray(&self) -> Option<TrayIcon<R>>
desktop
and crate feature tray-icon
only.Gets the first tray icon registerd, usually the one configured in tauri config file.
sourcepub fn remove_tray(&self) -> Option<TrayIcon<R>>
Available on desktop
and crate feature tray-icon
only.
pub fn remove_tray(&self) -> Option<TrayIcon<R>>
desktop
and crate feature tray-icon
only.Removes the first tray icon registerd, usually the one configured in tauri config file, from tauri’s internal state and returns it.
Note that dropping the returned icon, will cause the tray icon to disappear.
sourcepub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>where
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
Available on desktop
and crate feature tray-icon
only.
pub fn tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>where I: ?Sized, TrayIconId: PartialEq<&'a I>,
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>>where
I: ?Sized,
TrayIconId: PartialEq<&'a I>,
Available on desktop
and crate feature tray-icon
only.
pub fn remove_tray_by_id<'a, I>(&self, id: &'a I) -> Option<TrayIcon<R>>where I: ?Sized, TrayIconId: PartialEq<&'a I>,
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, will cause the tray icon to disappear.
sourcepub fn config(&self) -> Arc<Config>
pub fn config(&self) -> Arc<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 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 default_window_icon(&self) -> Option<&Icon>
pub fn default_window_icon(&self) -> Option<&Icon>
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.
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> HasRawDisplayHandle for AppHandle<R>
impl<R: Runtime> HasRawDisplayHandle for AppHandle<R>
fn raw_display_handle(&self) -> RawDisplayHandle
source§impl<R: Runtime> Manager<R> for AppHandle<R>
impl<R: Runtime> Manager<R> for AppHandle<R>
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 emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
fn emit_all<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
source§fn 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<()>
source§fn 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,
Window::trigger
or Window::emit_and_trigger
) or with Self::trigger_global
. Read moresource§fn 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,
source§fn trigger_global(&self, event: &str, data: Option<String>)
fn trigger_global(&self, event: &str, data: Option<String>)
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
. Read moresource§fn unlisten(&self, handler_id: EventHandler)
fn unlisten(&self, handler_id: EventHandler)
source§fn get_window(&self, label: &str) -> Option<Window<R>>
fn get_window(&self, label: &str) -> Option<Window<R>>
source§fn get_focused_window(&self) -> Option<Window<R>>
fn get_focused_window(&self) -> Option<Window<R>>
None
if there is not any focused window.source§fn manage<T>(&self, state: T) -> boolwhere
T: Send + Sync + 'static,
fn manage<T>(&self, state: T) -> boolwhere T: Send + Sync + 'static,
state
to the state managed by the application. Read moresource§fn state<T>(&self) -> State<'_, T>where
T: Send + Sync + 'static,
fn state<T>(&self) -> State<'_, T>where T: Send + Sync + 'static,
T
. Read moresource§fn 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,
T
. Read more