Struct tauri::window::Window

source ·
pub struct Window<R: Runtime = Wry> { /* private fields */ }
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§

APIs specific to the wry runtime.

Available on crate feature wry and desktop only.

Executes a closure, providing it with the webview handle that is specific to the current platform.

The closure is executed on 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/0.18.2/webkit2gtk/struct.WebView.html
          // and https://docs.rs/webkit2gtk/0.18.2/webkit2gtk/trait.WebViewExt.html
          use webkit2gtk::traits::WebViewExt;
          webview.inner().set_zoom_level(4.);
        }

        #[cfg(windows)]
        unsafe {
          // see https://docs.rs/webview2-com/0.19.1/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(())
  });
}

Base window functions.

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.

Runs the given closure on the main thread.

The label of this window.

Registers a window event listener.

Registers a menu event listener.

Window getters.

Gets a handle to the window menu.

Returns the scale factor that can be used to map logical pixels to physical pixels, and vice versa.

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.

Returns the position of the top-left hand corner of the window relative to the top-left hand corner of the desktop.

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.

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.

Gets the window’s current fullscreen state.

Gets the window’s current maximized state.

Gets the window’s current decoration state.

Gets the window’s current resizable state.

Gets the window’s current visibility state.

Returns the monitor on which the window currently resides.

Returns None if current monitor can’t be detected.

Returns the primary monitor of the system.

Returns None if it can’t identify any monitor as a primary one.

Returns the list of all the monitors available on the system.

Returns the ApplicationWindow from gtk crate that is used by this window.

Note that this can only be used on the main thread.

Returns the current window theme.

Platform-specific
  • macOS: Only supported on macOS 10.14+.

Window setters and actions.

Centers the window.

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.

Opens the dialog to prints the contents of the webview. Currently only supported on macOS on wry. window.print() works on all platforms.

Determines if this window should be resizable.

Set this window’s title.

Maximizes this window.

Un-maximizes this window.

Minimizes this window.

Un-minimizes this window.

Show this window.

Hide this window.

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.

Determines if this window should be decorated.

Determines if this window should always be on top of other windows.

Resizes this window.

Sets this window’s minimum size.

Sets this window’s maximum size.

Sets this window’s position.

Determines if this window should be fullscreen.

Bring the window to front and focus.

Sets this window’ icon.

Whether to hide the window icon from the taskbar or not.

Platform-specific
  • macOS: Unsupported.

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.

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.

Modifies the cursor icon of the window.

Changes the position of the cursor in window coordinates.

Ignores the window cursor events.

Starts dragging the window.

Webview APIs.

Handles this window receiving an InvokeMessage.

Evaluates JavaScript on this window.

Available on debug-assertions enabled or crate feature 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: Only supported on macOS 10.15+. 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(())
  });
Available on debug-assertions enabled or crate feature 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: Only supported on macOS 10.15+. 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(())
  });
Available on debug-assertions enabled or crate feature 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: Only supported on macOS 10.15+. 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(())
  });

Event system APIs.

Emits an event to both the JavaScript and the Rust listeners.

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.

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.

Unlisten to an event on this window.

Listen to an event on this window a single time.

Triggers an event to the Rust listeners on this window.

The event is only delivered to listeners that used the listen method.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more

Grabs the Window from the CommandItem. This will never fail.

Formats the value using the given formatter. Read more

Only use the Window’s label to represent its hash.

Feeds a slice of this type into the given Hasher. Read more
Emits an event to a window with the specified label.
Emits a event to all windows.
The application handle associated with this manager.
The Config the manager was created with.
Listen to a global event.
Listen to a global event only once.
Trigger a global event.
Remove an event listener.
Fetch a single window from the manager.
Fetch all managed windows.
Add state to the state managed by the application. Read more
Retrieves the managed state for the type T. Read more
Attempts to retrieve the managed state for the type T. Read more
Gets the managed Env.
Gets the scope for the filesystem APIs.
Gets the scope for the asset protocol.
Gets the scope for the shell execute APIs.

Only use the Window’s label to compare equality.

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more