Struct tauri::window::WindowBuilder
source · Expand description
A builder for a webview window managed by Tauri.
Implementations
sourceimpl<'a, R: Runtime> WindowBuilder<'a, R>
impl<'a, R: Runtime> WindowBuilder<'a, R>
sourcepub fn new<M: Manager<R>, L: Into<String>>(
manager: &'a M,
label: L,
url: WindowUrl
) -> Self
pub fn new<M: Manager<R>, L: Into<String>>(
manager: &'a M,
label: L,
url: WindowUrl
) -> Self
Initializes a webview window builder with the given window label and URL to load on the webview.
Examples
- Create a window in the setup hook:
tauri::Builder::default()
.setup(|app| {
let window = tauri::WindowBuilder::new(app, "label", tauri::WindowUrl::App("index.html".into()))
.build()?;
Ok(())
});
- Create a window in a separate thread:
tauri::Builder::default()
.setup(|app| {
let handle = app.handle();
std::thread::spawn(move || {
let window = tauri::WindowBuilder::new(&handle, "label", tauri::WindowUrl::App("index.html".into()))
.build()
.unwrap();
});
Ok(())
});
- Create a window in a command:
#[tauri::command]
async fn create_window(app: tauri::AppHandle) {
let window = tauri::WindowBuilder::new(&app, "label", tauri::WindowUrl::External("https://tauri.app/".parse().unwrap()))
.build()
.unwrap();
}
Known issues
On Windows, this function deadlocks when used in a synchronous command, see the Webview2 issue.
You should use async
commands when creating windows.
sourcepub fn on_web_resource_request<F: Fn(&HttpRequest, &mut HttpResponse) + Send + Sync + 'static>(
self,
f: F
) -> Self
pub fn on_web_resource_request<F: Fn(&HttpRequest, &mut HttpResponse) + Send + Sync + 'static>(
self,
f: F
) -> Self
Defines a closure to be executed when the webview makes an HTTP request for a web resource, allowing you to modify the response.
Currently only implemented for the tauri
URI protocol.
NOTE: Currently this is not executed when using external URLs such as a development server, but it might be implemented in the future. Always check the request URL.
Examples
use tauri::{
utils::config::{Csp, CspDirectiveSources, WindowUrl},
http::header::HeaderValue,
window::WindowBuilder,
};
use std::collections::HashMap;
tauri::Builder::default()
.setup(|app| {
WindowBuilder::new(app, "core", WindowUrl::App("index.html".into()))
.on_web_resource_request(|request, response| {
if request.uri().starts_with("tauri://") {
// if we have a CSP header, Tauri is loading an HTML file
// for this example, let's dynamically change the CSP
if let Some(csp) = response.headers_mut().get_mut("Content-Security-Policy") {
// use the tauri helper to parse the CSP policy to a map
let mut csp_map: HashMap<String, CspDirectiveSources> = Csp::Policy(csp.to_str().unwrap().to_string()).into();
csp_map.entry("script-src".to_string()).or_insert_with(Default::default).push("'unsafe-inline'");
// use the tauri helper to get a CSP string from the map
let csp_string = Csp::from(csp_map).to_string();
*csp = HeaderValue::from_str(&csp_string).unwrap();
}
}
})
.build()?;
Ok(())
});
Sets the menu for the window.
sourcepub fn inner_size(self, width: f64, height: f64) -> Self
pub fn inner_size(self, width: f64, height: f64) -> Self
Window size.
sourcepub fn min_inner_size(self, min_width: f64, min_height: f64) -> Self
pub fn min_inner_size(self, min_width: f64, min_height: f64) -> Self
Window min inner size.
sourcepub fn max_inner_size(self, max_width: f64, max_height: f64) -> Self
pub fn max_inner_size(self, max_width: f64, max_height: f64) -> Self
Window max inner size.
sourcepub fn title<S: Into<String>>(self, title: S) -> Self
pub fn title<S: Into<String>>(self, title: S) -> Self
The title of the window in the title bar.
sourcepub fn fullscreen(self, fullscreen: bool) -> Self
pub fn fullscreen(self, fullscreen: bool) -> Self
Whether to start the window in fullscreen or not.
sourcepub fn maximized(self, maximized: bool) -> Self
pub fn maximized(self, maximized: bool) -> Self
Whether the window should be maximized upon creation.
sourcepub fn visible(self, visible: bool) -> Self
pub fn visible(self, visible: bool) -> Self
Whether the window should be immediately visible upon creation.
sourcepub fn theme(self, theme: Option<Theme>) -> Self
pub fn theme(self, theme: Option<Theme>) -> Self
Forces a theme or uses the system settings if None was provided.
Platform-specific
- macOS: Only supported on macOS 10.14+.
- Linux: Not implemented, the value is ignored.
sourcepub fn transparent(self, transparent: bool) -> Self
Available on non-macOS or crate feature macos-private-api
only.
pub fn transparent(self, transparent: bool) -> Self
macos-private-api
only.Whether the the window should be transparent. If this is true, writing colors
with alpha values different than 1.0
will produce a transparent window.
sourcepub fn decorations(self, decorations: bool) -> Self
pub fn decorations(self, decorations: bool) -> Self
Whether the window should have borders and bars.
sourcepub fn always_on_top(self, always_on_top: bool) -> Self
pub fn always_on_top(self, always_on_top: bool) -> Self
Whether the window should always be on top of other windows.
sourcepub fn skip_taskbar(self, skip: bool) -> Self
pub fn skip_taskbar(self, skip: bool) -> Self
Sets whether or not the window icon should be added to the taskbar.
sourcepub fn parent_window(self, parent: HWND) -> Self
pub fn parent_window(self, parent: HWND) -> Self
Sets a parent to the window to be created.
A child window has the WS_CHILD style and is confined to the client area of its parent window.
For more information, see https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows
sourcepub fn owner_window(self, owner: HWND) -> Self
pub fn owner_window(self, owner: HWND) -> Self
Set an owner to the window to be created.
From MSDN:
- An owned window is always above its owner in the z-order.
- The system automatically destroys an owned window when its owner is destroyed.
- An owned window is hidden when its owner is minimized.
For more information, see https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows
sourcepub fn initialization_script(self, script: &str) -> Self
pub fn initialization_script(self, script: &str) -> Self
Sets the init script.
sourcepub fn data_directory(self, data_directory: PathBuf) -> Self
pub fn data_directory(self, data_directory: PathBuf) -> Self
Data directory for the webview.
sourcepub fn disable_file_drop_handler(self) -> Self
pub fn disable_file_drop_handler(self) -> Self
Disables the file drop handler. This is required to use drag and drop APIs on the front end on Windows.
sourcepub fn enable_clipboard_access(self) -> Self
pub fn enable_clipboard_access(self) -> Self
Enables clipboard access for the page rendered on Linux and Windows.
macOS doesn’t provide such method and is always enabled by default, but you still need to add menu item accelerators to use shortcuts.