Struct wry::webview::WebViewBuilder
source · [−]pub struct WebViewBuilder<'a> {
pub webview: WebViewAttributes,
/* private fields */
}
Expand description
Builder type of WebView
.
WebViewBuilder
/ WebView
are the basic building blocks to constrcut WebView contents and
scripts for those who prefer to control fine grained window creation and event handling.
WebViewBuilder
privides ability to setup initialization before web engine starts.
Fields
webview: WebViewAttributes
Implementations
sourceimpl<'a> WebViewBuilder<'a>
impl<'a> WebViewBuilder<'a>
sourcepub fn with_transparent(self, transparent: bool) -> Self
pub fn with_transparent(self, transparent: bool) -> Self
Sets whether the WebView should be transparent. Not supported on Windows 7.
sourcepub fn with_visible(self, visible: bool) -> Self
pub fn with_visible(self, visible: bool) -> Self
Sets whether the WebView should be transparent.
sourcepub fn with_initialization_script(self, js: &str) -> Self
pub fn with_initialization_script(self, js: &str) -> Self
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
.
sourcepub fn with_custom_protocol<F>(self, name: String, handler: F) -> Self where
F: Fn(&HttpRequest) -> Result<HttpResponse> + 'static,
pub fn with_custom_protocol<F>(self, name: String, handler: F) -> Self where
F: Fn(&HttpRequest) -> Result<HttpResponse> + 'static,
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)
sourcepub fn with_ipc_handler<F>(self, handler: F) -> Self where
F: Fn(&Window, String) + 'static,
pub fn with_ipc_handler<F>(self, handler: F) -> Self where
F: Fn(&Window, String) + 'static,
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.
sourcepub fn with_file_drop_handler<F>(self, handler: F) -> Self where
F: Fn(&Window, FileDropEvent) -> bool + 'static,
pub fn with_file_drop_handler<F>(self, handler: F) -> Self where
F: Fn(&Window, FileDropEvent) -> bool + 'static,
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.
sourcepub fn with_url(self, url: &str) -> Result<Self>
pub fn with_url(self, url: &str) -> Result<Self>
Load the provided URL when the builder calling WebViewBuilder::build
to create the
WebView
. The provided URL must be valid.
sourcepub fn with_html(self, html: impl Into<String>) -> Result<Self>
pub fn with_html(self, html: impl Into<String>) -> Result<Self>
Load the provided HTML string when the builder calling WebViewBuilder::build
to create the
WebView
. This will be ignored if url
is already provided.
Warning
The Page 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
sourcepub fn with_web_context(self, web_context: &'a mut WebContext) -> Self
pub fn with_web_context(self, web_context: &'a mut WebContext) -> Self
Set the web context that can share with multiple WebView
s.
sourcepub fn with_user_agent(self, user_agent: &str) -> Self
pub fn with_user_agent(self, user_agent: &str) -> Self
Set a custom user-agent for the WebView.
sourcepub fn with_dev_tool(self, devtool: bool) -> Self
pub fn with_dev_tool(self, devtool: bool) -> Self
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.
Auto Trait Implementations
impl<'a> !RefUnwindSafe for WebViewBuilder<'a>
impl<'a> !Send for WebViewBuilder<'a>
impl<'a> !Sync for WebViewBuilder<'a>
impl<'a> Unpin for WebViewBuilder<'a>
impl<'a> !UnwindSafe for WebViewBuilder<'a>
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