Struct wry::WebView

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

source

pub fn new(window: &impl HasWindowHandle) -> 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 using gtk::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.
source

pub fn new_as_child(parent: &impl HasWindowHandle) -> 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 the parent 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 using gtk::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 with gtk::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.
source

pub fn url(&self) -> Result<String>

Get the current url of the webview

source

pub fn evaluate_script(&self, js: &str) -> Result<()>

Evaluate and run javascript code.

source

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

pub fn print(&self) -> Result<()>

Launch print modal for the webview content.

source

pub fn open_devtools(&self)

Open the web inspector which is usually called dev tool.

§Platform-specific
  • Android / iOS: Not supported.
source

pub fn close_devtools(&self)

Close the web inspector which is usually called dev tool.

§Platform-specific
  • Windows / Android / iOS: Not supported.
source

pub fn is_devtools_open(&self) -> bool

Gets the devtool window’s current visibility state.

§Platform-specific
  • Windows / Android / iOS: Not supported.
source

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

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

pub fn load_url(&self, url: &str) -> Result<()>

Navigate to the specified url

source

pub fn load_url_with_headers(&self, url: &str, headers: HeaderMap) -> Result<()>

Navigate to the specified url using the specified headers

source

pub fn clear_all_browsing_data(&self) -> Result<()>

Clear all browsing data

source

pub fn bounds(&self) -> Result<Rect>

source

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.

source

pub fn set_visible(&self, visible: bool) -> Result<()>

Shows or hides the webview.

source

pub fn focus(&self) -> Result<()>

Try moving focus to the webview.

Trait Implementations§

source§

impl WebViewExtUnix for WebView

source§

fn new_gtk<W>(widget: &W) -> Result<Self>
where W: IsA<Container>,

Create the webview inside a GTK container widget, such as GTK window. Read more
source§

fn webview(&self) -> WebView

Returns Webkit2gtk Webview handle
source§

fn reparent<W>(&self, widget: &W) -> Result<()>
where W: IsA<Container>,

Attaches this webview to the given Widget and removes it from the current one.

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.