Struct wry::WebViewAttributes

source ·
pub struct WebViewAttributes {
Show 27 fields 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>, pub custom_protocols: Vec<(String, Box<dyn Fn(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>>, 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>,
}

Fields§

§user_agent: Option<String>

Whether the WebView should have a custom user-agent.

§visible: bool

Whether the WebView window should be visible.

§transparent: bool

Whether the WebView should be transparent.

§Platform-specific:

Windows 7: Not supported.

§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 by 255
§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>

Initialize javascript code 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.

§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: Vec<(String, Box<dyn Fn(Request<Vec<u8>>, RequestAsyncResponder)>)>

A list of custom loading protocols with pairs of scheme uri string and a handling closure.

The closure takes a Request and returns a Response.

§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 be wry://path/to/page/).
  • Windows and Android: http://<scheme_name>.<path> by default (so it will be http://wry.path/to/page). To use httpsinstead ofhttp, use [WebViewBuilderExtWindows::with_https_scheme] and [WebViewBuilderExtAndroid::with_https_scheme`].

§Reading assets on mobile

  • Android: Android has assets and resource 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 like wry://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>>
Available on crate feature 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.

§navigation_handler: Option<Box<dyn Fn(String) -> bool>>

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>>

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’s WebView 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.

§back_forward_navigation_gestures: bool

Indicates whether horizontal swipe gestures trigger backward and forward page navigation.

§Platform-specific:

  • 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:

  • Android: Unsupported yet.
§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.

Trait Implementations§

source§

impl Default for WebViewAttributes

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.