Crate wry

source ·
Expand description

Wry is a Cross-platform WebView rendering library.

The webview requires a running event loop and a window type that implements HasWindowHandle, or a gtk container widget if you need to support X11 and Wayland. You can use a windowing library like tao or winit.

§Examples

This example leverages the HasWindowHandle and supports Windows, macOS, iOS, Android and Linux (X11 Only). See the following example using winit.

let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new().build(&event_loop).unwrap();

let webview = WebViewBuilder::new(&window)
  .with_url("https://tauri.app")
  .build()
  .unwrap();

If you also want to support Wayland too, then we recommend you use WebViewBuilderExtUnix::new_gtk on Linux. See the following example using tao.

let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();

#[cfg(not(target_os = "linux"))]
let builder = WebViewBuilder::new(&window);
#[cfg(target_os = "linux")]
let builder = WebViewBuilder::new_gtk(window.gtk_window());

let webview = builder
  .with_url("https://tauri.app")
  .build()
  .unwrap();

§Child webviews

You can use WebView::new_as_child or WebViewBuilder::new_as_child to create the webview as a child inside another window. This is supported on macOS, Windows and Linux (X11 Only).

let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new().build(&event_loop).unwrap();

let webview = WebViewBuilder::new_as_child(&window)
  .with_url("https://tauri.app")
  .with_bounds(Rect {
    x: 100,
    y: 100,
    width: 200,
    height: 200,
  })
  .build()
  .unwrap();

If you want to support X11 and Wayland at the same time, we recommend using WebViewExtUnix::new_gtk or WebViewBuilderExtUnix::new_gtk with gtk::Fixed.

let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();

#[cfg(not(target_os = "linux"))]
let builder = WebViewBuilder::new_as_child(&window);

#[cfg(target_os = "linux")]
let gtk_fixed = {
  let vbox = window.default_vbox().unwrap(); // tao adds a gtk::Box by default
  let fixed = gtk::Fixed::new();
  vbox.pack_start(&fixed, true, true, 0);
  fixed
};

#[cfg(target_os = "linux")]
let builder = WebViewBuilder::new_gtk(&gtk_fixed);

let webview = builder
  .with_url("https://tauri.app")
  .with_bounds(Rect {
    x: 100,
    y: 100,
    width: 200,
    height: 200,
  })
  .build()
  .unwrap();

§Platform Considerations

Note that on Linux, we use webkit2gtk webviews so if the windowing library doesn’t support gtk (as in winit) you’ll need to call gtk::init before creating the webview and then call gtk::main_iteration_do alongside your windowing library event loop.

#[cfg(target_os = "linux")]
gtk::init().unwrap(); // <----- IMPORTANT
let event_loop = EventLoop::new().unwrap();

let window = Window::new(&event_loop).unwrap();
let webview = WebView::new(&window);

event_loop.run(|_e, _evl|{
  // process winit events

  // then advance gtk event loop  <----- IMPORTANT
  #[cfg(target_os = "linux")]
  while gtk::events_pending() {
    gtk::main_iteration_do(false);
  }
}).unwrap();

§Android

In order for wry to be able to create webviews on Android, there is a few requirements that your application needs to uphold:

  1. You need to set a few environment variables that will be used to generate the necessary kotlin files that you need to include in your Android application for wry to function properly.
    • WRY_ANDROID_PACKAGE: which is the reversed domain name of your android project and the app name in snake_case, for example, com.wry.example.wry_app
    • WRY_ANDROID_LIBRARY: for example, if your cargo project has a lib name wry_app, it will generate libwry_app.so so you se this env var to wry_app
    • WRY_ANDROID_KOTLIN_FILES_OUT_DIR: for example, path/to/app/src/main/kotlin/com/wry/example
  2. Your main Android Activity needs to inherit AppCompatActivity, preferably it should use the generated WryActivity or inherit it.
  3. Your Rust app needs to call wry::android_setup function to setup the necessary logic to be able to create webviews later on.
  4. Your Rust app needs to call wry::android_binding! macro to setup the JNI functions that will be called by WryActivity and various other places.

It is recommended to use tao crate as it provides maximum compatibility with wry

#[cfg(target_os = "android")]
{
  tao::android_binding!(
      com_example,
      wry_app,
      WryActivity,
      wry::android_setup, // pass the wry::android_setup function to tao which will invoke when the event loop is created
      _start_app
  );
  wry::android_binding!(com_example, ttt);
}

If this feels overwhelming, you can just use the preconfigured template from cargo-mobile2.

For more inforamtion, checkout MOBILE.md.

§Feature flags

Wry uses a set of feature flags to toggle several advanced features.

  • os-webview (default): Enables the default WebView framework on the platform. This must be enabled for the crate to work. This feature was added in preparation of other ports like cef and servo.
  • protocol (default): Enables WebViewBuilder::with_custom_protocol to define custom URL scheme for handling tasks like loading assets.
  • drag-drop (default): Enables WebViewBuilder::with_drag_drop_handler to control the behaviour when there are files interacting with the window.
  • devtools: Enables devtools on release builds. Devtools are always enabled in debug builds. On macOS, enabling devtools, requires calling private apis so you should not enable this flag in release build if your app needs to publish to App Store.
  • transparent: Transparent background on macOS requires calling private functions. Avoid this in release build if your app needs to publish to App Store.
  • fullscreen: Fullscreen video and other media on macOS requires calling private functions. Avoid this in release build if your app needs to publish to App Store. libraries and prevent from building documentation on doc.rs fails.
  • linux-body: Enables body support of custom protocol request on Linux. Requires webkit2gtk v2.40 or above.
  • tracing: enables tracing for evaluate_script, ipc_handler and `custom_protocols.

Re-exports§

Structs§

Enums§

Traits§

Functions§

Type Aliases§

  • Type alias for a color in the RGBA format.
  • Convenient type alias of Result type for wry.