Expand description
A webview window managed by Tauri.
This type also implements Manager
which allows you to manage other windows attached to
the same application.
Implementations
sourceimpl Window<Wry>
impl Window<Wry>
APIs specific to the wry runtime.
sourcepub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>(
&self,
f: F
) -> Result<()>
Available on eature="wry"
only.
pub fn with_webview<F: FnOnce(PlatformWebview) + Send + 'static>(
&self,
f: F
) -> Result<()>
eature="wry"
only.Executes the closure accessing the platform’s webview handle.
The closure is executed in the main thread.
Examples
#[cfg(target_os = "macos")]
#[macro_use]
extern crate objc;
use tauri::Manager;
fn main() {
tauri::Builder::default()
.setup(|app| {
let main_window = app.get_window("main").unwrap();
main_window.with_webview(|webview| {
#[cfg(target_os = "linux")]
{
// see https://docs.rs/webkit2gtk/latest/webkit2gtk/struct.WebView.html
// and https://docs.rs/webkit2gtk/latest/webkit2gtk/trait.WebViewExt.html
use webkit2gtk::traits::WebViewExt;
webview.inner().set_zoom_level(4.);
}
#[cfg(windows)]
unsafe {
// see https://docs.rs/webview2-com/latest/webview2_com/Microsoft/Web/WebView2/Win32/struct.ICoreWebView2Controller.html
webview.controller().SetZoomFactor(4.).unwrap();
}
#[cfg(target_os = "macos")]
unsafe {
let () = msg_send![webview.inner(), setPageZoom: 4.];
let () = msg_send![webview.controller(), removeAllUserScripts];
let bg_color: cocoa::base::id = msg_send![class!(NSColor), colorWithDeviceRed:0.5 green:0.2 blue:0.4 alpha:1.];
let () = msg_send![webview.ns_window(), setBackgroundColor: bg_color];
}
});
Ok(())
});
}
sourceimpl<R: Runtime> Window<R>
impl<R: Runtime> Window<R>
Base window functions.
sourcepub fn builder<'a, M: Manager<R>, L: Into<String>>(
manager: &'a M,
label: L,
url: WindowUrl
) -> WindowBuilder<'a, R>
pub fn builder<'a, M: Manager<R>, L: Into<String>>(
manager: &'a M,
label: L,
url: WindowUrl
) -> WindowBuilder<'a, R>
Initializes a webview window builder with the given window label and URL to load on the webview.
Data URLs are only supported with the window-data-url
feature flag.
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 on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F)
pub fn on_window_event<F: Fn(&WindowEvent) + Send + 'static>(&self, f: F)
Registers a window event listener.
Registers a menu event listener.
sourceimpl<R: Runtime> Window<R>
impl<R: Runtime> Window<R>
Window getters.
Gets a handle to the window menu.
sourcepub fn scale_factor(&self) -> Result<f64>
pub fn scale_factor(&self) -> Result<f64>
Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.
sourcepub fn inner_position(&self) -> Result<PhysicalPosition<i32>>
pub fn inner_position(&self) -> Result<PhysicalPosition<i32>>
Returns the position of the top-left hand corner of the window’s client area relative to the top-left hand corner of the desktop.
sourcepub fn outer_position(&self) -> Result<PhysicalPosition<i32>>
pub fn outer_position(&self) -> Result<PhysicalPosition<i32>>
Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.
sourcepub fn inner_size(&self) -> Result<PhysicalSize<u32>>
pub fn inner_size(&self) -> Result<PhysicalSize<u32>>
Returns the physical size of the window’s client area.
The client area is the content of the window, excluding the title bar and borders.
sourcepub fn outer_size(&self) -> Result<PhysicalSize<u32>>
pub fn outer_size(&self) -> Result<PhysicalSize<u32>>
Returns the physical size of the entire window.
These dimensions include the title bar and borders. If you don’t want that (and you usually don’t), use inner_size instead.
sourcepub fn is_fullscreen(&self) -> Result<bool>
pub fn is_fullscreen(&self) -> Result<bool>
Gets the window’s current fullscreen state.
sourcepub fn is_maximized(&self) -> Result<bool>
pub fn is_maximized(&self) -> Result<bool>
Gets the window’s current maximized state.
sourcepub fn is_decorated(&self) -> Result<bool>
pub fn is_decorated(&self) -> Result<bool>
Gets the window’s current decoration state.
sourcepub fn is_resizable(&self) -> Result<bool>
pub fn is_resizable(&self) -> Result<bool>
Gets the window’s current resizable state.
sourcepub fn is_visible(&self) -> Result<bool>
pub fn is_visible(&self) -> Result<bool>
Gets the window’s current vibility state.
sourcepub fn current_monitor(&self) -> Result<Option<Monitor>>
pub fn current_monitor(&self) -> Result<Option<Monitor>>
Returns the monitor on which the window currently resides.
Returns None if current monitor can’t be detected.
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 gtk_window(&self) -> Result<ApplicationWindow>
pub fn gtk_window(&self) -> Result<ApplicationWindow>
Returns the ApplicatonWindow
from gtk crate that is used by this window.
Note that this can only be used on the main thread.
sourcepub fn theme(&self) -> Result<Theme>
pub fn theme(&self) -> Result<Theme>
Returns the current window theme.
Platform-specific
- macOS / Linux: Not implemented, always return
Theme::Light
.
sourceimpl<R: Runtime> Window<R>
impl<R: Runtime> Window<R>
Window setters and actions.
sourcepub fn request_user_attention(
&self,
request_type: Option<UserAttentionType>
) -> Result<()>
pub fn request_user_attention(
&self,
request_type: Option<UserAttentionType>
) -> Result<()>
Requests user attention to the window, this has no effect if the application
is already focused. How requesting for user attention manifests is platform dependent,
see UserAttentionType
for details.
Providing None
will unset the request for user attention. Unsetting the request for
user attention might not be done automatically by the WM when the window receives input.
Platform-specific
- macOS:
None
has no effect. - Linux: Urgency levels have the same effect.
sourcepub fn print(&self) -> Result<()>
pub fn print(&self) -> Result<()>
Opens the dialog to prints the contents of the webview.
Currently only supported on macOS on wry
.
window.print()
works on all platforms.
sourcepub fn set_resizable(&self, resizable: bool) -> Result<()>
pub fn set_resizable(&self, resizable: bool) -> Result<()>
Determines if this window should be resizable.
sourcepub fn unmaximize(&self) -> Result<()>
pub fn unmaximize(&self) -> Result<()>
Un-maximizes this window.
sourcepub fn unminimize(&self) -> Result<()>
pub fn unminimize(&self) -> Result<()>
Un-minimizes this window.
sourcepub fn close(&self) -> Result<()>
pub fn close(&self) -> Result<()>
Closes this window.
Panics
- Panics if the event loop is not running yet, usually when called on the
setup
closure. - Panics when called on the main thread, usually on the
run
closure.
You can spawn a task to use the API using crate::async_runtime::spawn
or std::thread::spawn
to prevent the panic.
sourcepub fn set_decorations(&self, decorations: bool) -> Result<()>
pub fn set_decorations(&self, decorations: bool) -> Result<()>
Determines if this window should be decorated.
sourcepub fn set_always_on_top(&self, always_on_top: bool) -> Result<()>
pub fn set_always_on_top(&self, always_on_top: bool) -> Result<()>
Determines if this window should always be on top of other windows.
sourcepub fn set_min_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()>
pub fn set_min_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()>
Sets this window’s minimum size.
sourcepub fn set_max_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()>
pub fn set_max_size<S: Into<Size>>(&self, size: Option<S>) -> Result<()>
Sets this window’s maximum size.
sourcepub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>
pub fn set_position<Pos: Into<Position>>(&self, position: Pos) -> Result<()>
Sets this window’s position.
sourcepub fn set_fullscreen(&self, fullscreen: bool) -> Result<()>
pub fn set_fullscreen(&self, fullscreen: bool) -> Result<()>
Determines if this window should be fullscreen.
sourcepub fn set_skip_taskbar(&self, skip: bool) -> Result<()>
pub fn set_skip_taskbar(&self, skip: bool) -> Result<()>
Whether to show the window icon in the task bar or not.
sourcepub fn set_cursor_grab(&self, grab: bool) -> Result<()>
pub fn set_cursor_grab(&self, grab: bool) -> Result<()>
Grabs the cursor, preventing it from leaving the window.
There’s no guarantee that the cursor will be hidden. You should hide it by yourself if you want so.
Platform-specific
- Linux: Unsupported.
- macOS: This locks the cursor in a fixed location, which looks visually awkward.
sourcepub fn set_cursor_visible(&self, visible: bool) -> Result<()>
pub fn set_cursor_visible(&self, visible: bool) -> Result<()>
Modifies the cursor’s visibility.
If false
, this will hide the cursor. If true
, this will show the cursor.
Platform-specific
- Windows: The cursor is only hidden within the confines of the window.
- macOS: The cursor is hidden as long as the window has input focus, even if the cursor is outside of the window.
sourcepub fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>
pub fn set_cursor_icon(&self, icon: CursorIcon) -> Result<()>
Modifies the cursor icon of the window.
sourcepub fn set_cursor_position<Pos: Into<Position>>(
&self,
position: Pos
) -> Result<()>
pub fn set_cursor_position<Pos: Into<Position>>(
&self,
position: Pos
) -> Result<()>
Changes the position of the cursor in window coordinates.
sourcepub fn start_dragging(&self) -> Result<()>
pub fn start_dragging(&self) -> Result<()>
Starts dragging the window.
sourceimpl<R: Runtime> Window<R>
impl<R: Runtime> Window<R>
Webview APIs.
sourcepub fn on_message(self, payload: InvokePayload) -> Result<()>
pub fn on_message(self, payload: InvokePayload) -> Result<()>
Handles this window receiving an InvokeMessage
.
sourcepub fn open_devtools(&self)
Available on debug-assertions enabled or crate feature devtools
only.
pub fn open_devtools(&self)
devtools
only.Opens the developer tools window (Web Inspector).
The devtools is only enabled on debug builds or with the devtools
feature flag.
Platform-specific
- macOS: This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
app.get_window("main").unwrap().open_devtools();
Ok(())
});
sourcepub fn close_devtools(&self)
Available on debug-assertions enabled or crate feature devtools
only.
pub fn close_devtools(&self)
devtools
only.Closes the developer tools window (Web Inspector).
The devtools is only enabled on debug builds or with the devtools
feature flag.
Platform-specific
- macOS: This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
- Windows: Unsupported.
Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
{
let window = app.get_window("main").unwrap();
window.open_devtools();
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(10));
window.close_devtools();
});
}
Ok(())
});
sourcepub fn is_devtools_open(&self) -> bool
Available on debug-assertions enabled or crate feature devtools
only.
pub fn is_devtools_open(&self) -> bool
devtools
only.Checks if the developer tools window (Web Inspector) is opened.
The devtools is only enabled on debug builds or with the devtools
feature flag.
Platform-specific
- macOS: This is a private API on macOS, so you cannot use this if your application will be published on the App Store.
- Windows: Unsupported.
Examples
use tauri::Manager;
tauri::Builder::default()
.setup(|app| {
#[cfg(debug_assertions)]
{
let window = app.get_window("main").unwrap();
if !window.is_devtools_open() {
window.open_devtools();
}
}
Ok(())
});
sourceimpl<R: Runtime> Window<R>
impl<R: Runtime> Window<R>
Event system APIs.
sourcepub fn emit_and_trigger<S: Serialize + Clone>(
&self,
event: &str,
payload: S
) -> Result<()>
pub fn emit_and_trigger<S: Serialize + Clone>(
&self,
event: &str,
payload: S
) -> Result<()>
Emits an event to both the JavaScript and the Rust listeners.
sourcepub fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
pub fn emit<S: Serialize + Clone>(&self, event: &str, payload: S) -> Result<()>
Emits an event to the JavaScript listeners on the current window.
The event is only delivered to listeners that used the WebviewWindow#listen
method on the @tauri-apps/api window
module.
sourcepub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: Fn(Event) + Send + 'static,
pub fn listen<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: Fn(Event) + Send + 'static,
Listen to an event on this window.
This listener only receives events that are triggered using the
trigger
and emit_and_trigger
methods or
the appWindow.emit
function from the @tauri-apps/api window
module.
sourcepub fn unlisten(&self, handler_id: EventHandler)
pub fn unlisten(&self, handler_id: EventHandler)
Unlisten to an event on this window.
Trait Implementations
sourceimpl<'de, R: Runtime> CommandArg<'de, R> for Window<R>
impl<'de, R: Runtime> CommandArg<'de, R> for Window<R>
sourcefn 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
. This will never fail.
sourceimpl<R: Runtime> Manager<R> for Window<R>
impl<R: Runtime> Manager<R> for Window<R>
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.
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 a window with the specified label.
sourcefn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: Fn(Event) + Send + 'static,
fn listen_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: Fn(Event) + Send + 'static,
Listen to a global event.
sourcefn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: FnOnce(Event) + Send + 'static,
fn once_global<F>(&self, event: impl Into<String>, handler: F) -> EventHandler where
F: FnOnce(Event) + Send + 'static,
Listen to a global event only once.
sourcefn trigger_global(&self, event: &str, data: Option<String>)
fn trigger_global(&self, event: &str, data: Option<String>)
Trigger a global event.
sourcefn unlisten(&self, handler_id: EventHandler)
fn unlisten(&self, handler_id: EventHandler)
Remove an event listener.
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 manage<T>(&self, state: T) -> bool where
T: Send + Sync + 'static,
fn manage<T>(&self, state: T) -> bool where
T: Send + Sync + 'static,
Add state
to the state managed by the application. Read more
sourcefn state<T>(&self) -> State<'_, T> where
T: Send + Sync + 'static,
fn state<T>(&self) -> State<'_, T> where
T: Send + Sync + 'static,
Retrieves the managed state for the type T
. Read more
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
. Read more
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.
impl<R: Runtime> Eq for Window<R>
Auto Trait Implementations
impl<R = Wry<EventLoopMessage>> !RefUnwindSafe for Window<R>
impl<R> Send for Window<R>
impl<R> Sync for Window<R>
impl<R> Unpin for Window<R> where
<R as Runtime<EventLoopMessage>>::ClipboardManager: Unpin,
<R as Runtime<EventLoopMessage>>::Dispatcher: Unpin,
<R as Runtime<EventLoopMessage>>::GlobalShortcutManager: Unpin,
<R as Runtime<EventLoopMessage>>::Handle: Unpin,
<R as Runtime<EventLoopMessage>>::TrayHandler: Unpin,
impl<R = Wry<EventLoopMessage>> !UnwindSafe for Window<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
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.