pub struct WebViewAttributes<'a> {Show 31 fields
pub id: Option<WebViewId<'a>>,
pub context: Option<&'a mut WebContext>,
pub user_agent: Option<String>,
pub visible: bool,
pub transparent: bool,
pub background_color: Option<RGBA>,
pub url: Option<String>,
pub headers: Option<HeaderMap>,
pub zoom_hotkeys_enabled: bool,
pub html: Option<String>,
pub initialization_scripts: Vec<(String, bool)>,
pub custom_protocols: HashMap<String, Box<dyn Fn(WebViewId<'_>, Request<Vec<u8>>, RequestAsyncResponder)>>,
pub ipc_handler: Option<Box<dyn Fn(Request<String>)>>,
pub drag_drop_handler: Option<Box<dyn Fn(DragDropEvent) -> bool>>,
pub navigation_handler: Option<Box<dyn Fn(String) -> bool>>,
pub download_started_handler: Option<Box<dyn FnMut(String, &mut PathBuf) -> bool + 'static>>,
pub download_completed_handler: Option<Rc<dyn Fn(String, Option<PathBuf>, bool) + 'static>>,
pub new_window_req_handler: Option<Box<dyn Fn(String) -> bool>>,
pub clipboard: bool,
pub devtools: bool,
pub accept_first_mouse: bool,
pub back_forward_navigation_gestures: bool,
pub document_title_changed_handler: Option<Box<dyn Fn(String)>>,
pub incognito: bool,
pub autoplay: bool,
pub on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent, String)>>,
pub proxy_config: Option<ProxyConfig>,
pub focused: bool,
pub bounds: Option<Rect>,
pub background_throttling: Option<BackgroundThrottlingPolicy>,
pub javascript_disabled: bool,
}
Fields§
§id: Option<WebViewId<'a>>
An id that will be passed when this webview makes requests in certain callbacks.
context: Option<&'a mut WebContext>
Web context to be shared with this webview.
user_agent: Option<String>
Whether the WebView should have a custom user-agent.
visible: bool
Whether the WebView window should be visible.
transparent: bool
§background_color: Option<RGBA>
Specify the webview background color. This will be ignored if transparent
is set to true
.
The color uses the RGBA format.
§Platform-specific:
- macOS / iOS: Not implemented.
- Windows:
- On Windows 7, transparency is not supported and the alpha value will be ignored.
- On Windows higher than 7: translucent colors are not supported so any alpha value other than
0
will be replaced by255
url: Option<String>
Whether load the provided URL to WebView
.
§Note
Data URLs are not supported, use html
option instead.
headers: Option<HeaderMap>
Headers used when loading the requested url
.
zoom_hotkeys_enabled: bool
Whether page zooming by hotkeys is enabled
§Platform-specific
macOS / Linux / Android / iOS: Unsupported
html: Option<String>
Whether load the provided html string to WebView
.
This will be ignored if the url
is provided.
§Warning
The Page loaded from html string will have null
origin.
§PLatform-specific:
- Windows: the string can not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size
initialization_scripts: Vec<(String, bool)>
A list of initialization javascript scripts to run when loading new pages.
When webview load a new page, this initialization code will be executed.
It is guaranteed that code is executed before window.onload
.
Second parameter represents if script should be added to main frame only or sub frames also.
true
for main frame only, false
for sub frames.
§Platform-specific
- Android: The Android WebView does not provide an API for initialization scripts, so we prepend them to each HTML head. They are only implemented on custom protocol URLs.
custom_protocols: HashMap<String, Box<dyn Fn(WebViewId<'_>, Request<Vec<u8>>, RequestAsyncResponder)>>
A list of custom loading protocols with pairs of scheme uri string and a handling closure.
The closure takes an Id (WebViewId), Request and RequestAsyncResponder as arguments and returns a Response.
§Note
If using a shared WebContext, make sure custom protocols were not already registered on that web context on Linux.
§Warning
Pages loaded from custom protocol will have different Origin on different platforms. And
servers which enforce CORS will need to add exact same Origin header in Access-Control-Allow-Origin
if you wish to send requests with native fetch
and XmlHttpRequest
APIs. Here are the
different Origin headers across platforms:
- macOS, iOS and Linux:
<scheme_name>://<path>
(so it will bewry://path/to/page/
). - Windows and Android:
http://<scheme_name>.<path>
by default (so it will behttp://wry.path/to/page). To use
httpsinstead of
http, use [
WebViewBuilderExtWindows::with_https_scheme] and [
WebViewBuilderExtAndroid::with_https_scheme`].
§Reading assets on mobile
- Android: Android has
assets
andresource
path finder to locate your files in those directories. For more information, see Loading in-app content page. - iOS: To get the path of your assets, you can call
CFBundle::resources_path
. So url likewry://assets/index.html
could get the html file in assets directory.
ipc_handler: Option<Box<dyn Fn(Request<String>)>>
The IPC handler to receive the message from Javascript on webview
using window.ipc.postMessage("insert_message_here")
to host Rust code.
drag_drop_handler: Option<Box<dyn Fn(DragDropEvent) -> bool>>
drag-drop
only.A handler closure to process incoming DragDropEvent
of the webview.
§Blocking OS Default Behavior
Return true
in the callback to block the OS’ default behavior.
Note, that if you do block this behavior, it won’t be possible to drop files on <input type="file">
forms.
Also note, that it’s not possible to manually set the value of a <input type="file">
via JavaScript for security reasons.
A navigation handler to decide if incoming url is allowed to navigate.
The closure take a String
parameter as url and returns a bool
to determine whether the navigation should happen.
true
allows to navigate and false
does not.
download_started_handler: Option<Box<dyn FnMut(String, &mut PathBuf) -> bool + 'static>>
A download started handler to manage incoming downloads.
The closure takes two parameters, the first is a String
representing the url being downloaded from and and the
second is a mutable PathBuf
reference that (possibly) represents where the file will be downloaded to. The latter
parameter can be used to set the download location by assigning a new path to it, the assigned path must be
absolute. The closure returns a bool
to allow or deny the download.
download_completed_handler: Option<Rc<dyn Fn(String, Option<PathBuf>, bool) + 'static>>
A download completion handler to manage downloads that have finished.
The closure is fired when the download completes, whether it was successful or not.
The closure takes a String
representing the URL of the original download request, an Option<PathBuf>
potentially representing the filesystem path the file was downloaded to, and a bool
indicating if the download
succeeded. A value of None
being passed instead of a PathBuf
does not necessarily indicate that the download
did not succeed, and may instead indicate some other failure, always check the third parameter if you need to
know if the download succeeded.
§Platform-specific:
- macOS: The second parameter indicating the path the file was saved to, is always empty, due to API limitations.
new_window_req_handler: Option<Box<dyn Fn(String) -> bool>>
A new window handler to decide if incoming url is allowed to open in a new window.
The closure take a String
parameter as url and return bool
to determine whether the window should open.
true
allows to open and false
does not.
clipboard: bool
Enables clipboard access for the page rendered on Linux and Windows.
macOS doesn’t provide such method and is always enabled by default. But your app will still need to add menu item accelerators to use the clipboard shortcuts.
devtools: bool
Enable web inspector which is usually called browser devtools.
Note this only enables devtools to the webview. To open it, you can call
WebView::open_devtools
, or right click the page and open it from the context menu.
§Platform-specific
- macOS: This will call private functions on macOS. It is enabled in debug builds,
but requires
devtools
feature flag to actually enable it in release builds. - Android: Open
chrome://inspect/#devices
in Chrome to get the devtools window. Wry’sWebView
devtools API isn’t supported on Android. - iOS: Open Safari > Develop > [Your Device Name] > [Your WebView] to get the devtools window.
accept_first_mouse: bool
Whether clicking an inactive window also clicks through to the webview. Default is false
.
§Platform-specific
This configuration only impacts macOS.
Indicates whether horizontal swipe gestures trigger backward and forward page navigation.
§Platform-specific:
-
Windows: Setting to
false
does nothing on WebView2 Runtime version before 92.0.902.0, see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10902-prerelease -
Android / iOS: Unsupported.
document_title_changed_handler: Option<Box<dyn Fn(String)>>
Set a handler closure to process the change of the webview’s document title.
incognito: bool
Run the WebView with incognito mode. Note that WebContext will be ingored if incognito is enabled.
§Platform-specific:
- Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions, see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039
- Android: Unsupported yet.
- macOS / iOS: Uses the nonPersistent DataStore.
autoplay: bool
Whether all media can be played without user interaction.
on_page_load_handler: Option<Box<dyn Fn(PageLoadEvent, String)>>
Set a handler closure to process page load events.
proxy_config: Option<ProxyConfig>
Set a proxy configuration for the webview. Supports HTTP CONNECT and SOCKSv5 proxies
- macOS: Requires macOS 14.0+ and the
mac-proxy
feature flag to be enabled. - Android / iOS: Not supported.
focused: bool
Whether the webview should be focused when created.
§Platform-specific:
- macOS / Android / iOS: Unsupported.
bounds: Option<Rect>
The webview bounds. Defaults to x: 0, y: 0, width: 200, height: 200
.
This is only effective if the webview was created by WebView::new_as_child
or [WebViewBuilder::new_as_child
]
or on Linux, if was created by WebViewExtUnix::new_gtk
or [WebViewBuilderExtUnix::new_gtk
] with gtk::Fixed
.
background_throttling: Option<BackgroundThrottlingPolicy>
Whether background throttling should be disabled.
By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state changes back from hidden to visible by bringing the view back to the foreground.
§Platform-specific
- Linux / Windows / Android: Unsupported. Workarounds like a pending WebLock transaction might suffice.
- iOS: Supported since version 17.0+.
- macOS: Supported since version 14.0+.
see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
javascript_disabled: bool
Whether JavaScript should be disabled.