Struct wry::webview::WebViewAttributes
source · [−]pub struct WebViewAttributes {
pub user_agent: Option<String>,
pub visible: bool,
pub transparent: bool,
pub url: Option<Url>,
pub html: Option<String>,
pub initialization_scripts: Vec<String>,
pub custom_protocols: Vec<(String, Box<dyn Fn(&HttpRequest) -> Result<HttpResponse>>)>,
pub ipc_handler: Option<Box<dyn Fn(&Window, String)>>,
pub file_drop_handler: Option<Box<dyn Fn(&Window, FileDropEvent) -> bool>>,
pub clipboard: bool,
pub devtool: bool,
}
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. Not supported on Windows 7.
url: Option<Url>
Whether load the provided URL to WebView
.
html: Option<String>
Whether load the provided html string to WebView
.
This will be ignored if the url
is provided.
Warning
The loaded from html string 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:
http://localhost
- Linux:
http://localhost
- Windows:
null
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
.
custom_protocols: Vec<(String, Box<dyn Fn(&HttpRequest) -> Result<HttpResponse>>)>
Register custom file loading protocols with pairs of scheme uri string and a handling closure.
The closure takes a url string slice, and returns a two item tuple of a vector of bytes which is the content and a mimetype string of the content.
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:
<scheme_name>://<path>
(so it will bewry://examples
incustom_protocol
example) - Linux: Though it’s same as macOS, there’s a bug that Origin header in the request will be
empty. So the only way to pass the server is setting
Access-Control-Allow-Origin: *
. - Windows:
https://<scheme_name>.<path>
(so it will behttps://wry.examples
incustom_protocol
example)
ipc_handler: Option<Box<dyn Fn(&Window, String)>>
Set the IPC handler to receive the message from Javascript on webview to host Rust code.
The message sent from webview should call window.ipc.postMessage("insert_message_here");
.
Both functions return promises but notify()
resolves immediately.
file_drop_handler: Option<Box<dyn Fn(&Window, FileDropEvent) -> bool>>
Set a handler closure to process incoming FileDropEvent
of the webview.
Blocking OS Default Behavior
Return true
in the callback to block the OS’ default behavior of handling a file drop.
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.
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 you still need to add menu item accelerators to use shortcuts.
devtool: bool
Enable web insepctor which is usually called dev tool.
Note this only enables dev tool to the webview. To open it, you can call
WebView::devtool
, or right click the page and open it from the context menu.
Warning
This will call private functions on macOS. It’s still enabled if set in debug build on mac,
but requires devtool
feature flag to actually enable it in release build.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for WebViewAttributes
impl !Send for WebViewAttributes
impl !Sync for WebViewAttributes
impl Unpin for WebViewAttributes
impl !UnwindSafe for WebViewAttributes
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more