pub struct WebView { /* private fields */ }
Expand description
The fundamental type to present a WebView
.
WebViewBuilder
/ WebView
are the basic building blocks to construct WebView contents and
scripts for those who prefer to control fine grained window creation and event handling.
WebView
presents the actual WebView window and let you still able to perform actions on it.
Implementations§
Source§impl WebView
impl WebView
Sourcepub fn new(
window: &impl HasWindowHandle,
attrs: WebViewAttributes<'_>,
) -> Result<Self>
pub fn new( window: &impl HasWindowHandle, attrs: WebViewAttributes<'_>, ) -> Result<Self>
Create a WebView
from from a type that implements HasWindowHandle
.
Note that calling this directly loses
abilities to initialize scripts, add ipc handler, and many more before starting WebView. To
benefit from above features, create a WebViewBuilder
instead.
§Platform-specific:
-
Linux: Only X11 is supported, if you want to support Wayland too, use
WebViewExtUnix::new_gtk
.Although this methods only needs an X11 window handle, you use webkit2gtk, so you still need to initialize gtk by callling
gtk::init
and advance its loop alongside your event loop usinggtk::main_iteration_do
. Checkout the Platform Considerations section in the crate root documentation. -
macOS / Windows: The webview will auto-resize when the passed handle is resized.
-
Linux (X11): Unlike macOS and Windows, the webview will not auto-resize and you’ll need to call
WebView::set_bounds
manually.
§Panics:
- Panics if the provided handle was not supported or invalid.
- Panics on Linux, if
gtk::init
was not called in this thread.
Sourcepub fn new_as_child(
parent: &impl HasWindowHandle,
attrs: WebViewAttributes<'_>,
) -> Result<Self>
pub fn new_as_child( parent: &impl HasWindowHandle, attrs: WebViewAttributes<'_>, ) -> Result<Self>
Create WebViewBuilder
as a child window inside the provided HasWindowHandle
.
§Platform-specific
-
Windows: This will create the webview as a child window of the
parent
window. -
macOS: This will create the webview as a
NSView
subview of theparent
window’s content view. -
Linux: This will create the webview as a child window of the
parent
window. Only X11 is supported. This method won’t work on Wayland.Although this methods only needs an X11 window handle, you use webkit2gtk, so you still need to initialize gtk by callling
gtk::init
and advance its loop alongside your event loop usinggtk::main_iteration_do
. Checkout the Platform Considerations section in the crate root documentation.If you want to support child webviews on X11 and Wayland at the same time, we recommend using [
WebViewBuilderExtUnix::new_gtk
] withgtk::Fixed
. -
Android/iOS: Unsupported.
§Panics:
- Panics if the provided handle was not support or invalid.
- Panics on Linux, if
gtk::init
was not called in this thread.
Sourcepub fn evaluate_script(&self, js: &str) -> Result<()>
pub fn evaluate_script(&self, js: &str) -> Result<()>
Evaluate and run javascript code.
Sourcepub fn evaluate_script_with_callback(
&self,
js: &str,
callback: impl Fn(String) + Send + 'static,
) -> Result<()>
pub fn evaluate_script_with_callback( &self, js: &str, callback: impl Fn(String) + Send + 'static, ) -> Result<()>
Evaluate and run javascript code with callback function. The evaluation result will be serialized into a JSON string and passed to the callback function.
Exception is ignored because of the limitation on windows. You can catch it yourself and return as string as a workaround.
- ** Android:** Not implemented yet.
Get a list of cookies for specific url.
Sourcepub fn open_devtools(&self)
pub fn open_devtools(&self)
Open the web inspector which is usually called dev tool.
§Platform-specific
- Android / iOS: Not supported.
Sourcepub fn close_devtools(&self)
pub fn close_devtools(&self)
Close the web inspector which is usually called dev tool.
§Platform-specific
- Windows / Android / iOS: Not supported.
Sourcepub fn is_devtools_open(&self) -> bool
pub fn is_devtools_open(&self) -> bool
Gets the devtool window’s current visibility state.
§Platform-specific
- Windows / Android / iOS: Not supported.
Sourcepub fn zoom(&self, scale_factor: f64) -> Result<()>
pub fn zoom(&self, scale_factor: f64) -> Result<()>
Set the webview zoom level
§Platform-specific:
- Android: Not supported.
- macOS: available on macOS 11+ only.
- iOS: available on iOS 14+ only.
Sourcepub fn set_background_color(&self, background_color: RGBA) -> Result<()>
pub fn set_background_color(&self, background_color: RGBA) -> Result<()>
Specify the webview background color.
The color uses the RGBA format.
§Platfrom-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
Sourcepub fn load_url_with_headers(&self, url: &str, headers: HeaderMap) -> Result<()>
pub fn load_url_with_headers(&self, url: &str, headers: HeaderMap) -> Result<()>
Navigate to the specified url using the specified headers
Sourcepub fn clear_all_browsing_data(&self) -> Result<()>
pub fn clear_all_browsing_data(&self) -> Result<()>
Clear all browsing data
pub fn bounds(&self) -> Result<Rect>
Sourcepub fn set_bounds(&self, bounds: Rect) -> Result<()>
pub fn set_bounds(&self, bounds: Rect) -> Result<()>
Set the webview bounds.
This is only effective if the webview was created as a child
or created using [WebViewBuilderExtUnix::new_gtk
] with gtk::Fixed
.
Sourcepub fn set_visible(&self, visible: bool) -> Result<()>
pub fn set_visible(&self, visible: bool) -> Result<()>
Shows or hides the webview.
Sourcepub fn focus_parent(&self) -> Result<()>
pub fn focus_parent(&self) -> Result<()>
Try moving focus away from the webview back to the parent window.
§Platform-specific:
- Android: Not implemented.