Skip to main content

Crate windows_webview

Crate windows_webview 

Source
Expand description

§windows-webview

Windows WebView wraps the WebView2 COM APIs to host Microsoft Edge (Chromium) in a window.

Start by adding the following to your Cargo.toml file:

[dependencies]
windows-webview = "0.100"
windows-window = "0.100"

WebView2 requires a window and a running message loop. Environment and controller creation complete asynchronously on the UI thread. These constructors pump the message loop until each step finishes.

use windows_webview::*;
use windows_window::{Window, run};

fn main() -> Result<()> {
    let window = Window::new("WebView2").size(1000, 700).create()?;
    let environment = Environment::new()?;
    let controller = environment.create_controller(&window)?;
    let webview = controller.webview()?;

    let (width, height) = window.client_size();
    controller.set_bounds(0, 0, width, height)?;
    webview.navigate("https://github.com/microsoft/windows-rs")?;

    run();
    Ok(())
}

Keep the Controller alive while the browser is hosted.

Structs§

AcceleratorKeyPressedArgs
A browser-level key press delivered before the page sees it.
Color
A 32-bit RGBA color, used for the browser’s default background.
ContentLoadingArgs
Details about content that is starting to load, delivered to a WebView::on_content_loading handler.
Controller
Hosts a WebView2 browser inside a parent window, controlling its bounds, visibility, and lifetime.
ControllerOptions
Configures a Controller at creation time - its profile, whether it runs in private mode, and the color painted before content loads. Build one with the fluent setters and pass it to Environment::create_controller_with_options.
Cookie
A browser cookie. Read cookies with CookieManager::get_cookies and write them with CookieManager::add_or_update_cookie.
CookieManager
Reads, writes, and deletes the browser’s cookies. Obtain it with WebView::cookie_manager.
Deferral
Keeps an event request pending so it can be resolved after the handler returns. Obtain one from an event’s defer method (for example PermissionRequestedArgs::defer) and call complete once the decision has been made. Dropping the deferral completes it automatically.
DevToolsProtocolEventReceivedArgs
A Chrome DevTools Protocol event, delivered to a WebView::on_dev_tools_protocol_event handler. The event’s parameters are available as a JSON object string.
DownloadOperation
An in-progress or finished download.
DownloadStartingArgs
Details about a download that is about to start.
Environment
The WebView2 environment. Owns the user data folder and browser process and creates Controller instances that host the browser in a window.
EnvironmentOptions
Configures the WebView2 Environment, including the user data folder, the browser executable folder, additional browser command-line arguments, and the UI language. Build one with the fluent setters and pass it to Environment::with_options.
EventRegistration
An event subscription that unsubscribes when dropped.
MoveFocusRequestedArgs
A request to move focus out of the browser.
NavigationCompletedArgs
Details about a completed navigation, delivered to a WebView::on_navigation_completed handler.
NavigationRequest
A request to navigate with a custom HTTP method, headers, or body, passed to WebView::navigate_with_request. Defaults to a GET with no extra headers or body.
NavigationStartingArgs
Details about a navigation that is about to start.
NewWindowRequestedArgs
Details about a page requesting to open a new window.
PermissionRequestedArgs
Details about a permission a page is requesting (for example camera or geolocation access), delivered to a WebView::on_permission_requested handler. Decide the outcome with set_state.
ProcessFailedArgs
Details about a failed or unresponsive browser process, delivered to a WebView::on_process_failed handler.
Profile
A WebView2 browser profile - the on-disk identity (name, path, private mode) shared by every WebView created with it, plus its color scheme, download folder, and browsing-data controls. Obtain it with WebView::profile.
ScriptId
Identifies a script registered on document creation.
Settings
The configurable settings of a WebView, such as whether JavaScript, the dev tools, or the default context menus are enabled. Obtain it with WebView::settings.
WebMessageReceivedArgs
A message posted from the hosted page’s JavaScript, delivered to a WebView::on_web_message_received handler.
WebResourceRequest
A resource request intercepted by WebView::on_web_resource_requested, exposing the requested URI, HTTP method, and headers.
WebResourceResponse
The response returned from a WebView::on_web_resource_requested handler to fulfill a request from memory. Defaults to 200 OK with no headers; add a content_type so the page interprets the body correctly.
WebView
A WebView2 browser. Navigate to URLs and run JavaScript against the hosted page.

Enums§

DownloadInterruptReason
Why a DownloadOperation was interrupted.
DownloadState
The state of a DownloadOperation.
HostResourceAccessKind
How a folder mapped with WebView::set_virtual_host_name_to_folder_mapping may be accessed.
KeyEventKind
The kind of key event reported by AcceleratorKeyPressedArgs::key_event_kind.
MemoryUsageTargetLevel
The level WebView2 should target for the browser’s memory usage, set with WebView::set_memory_usage_target_level.
MoveFocusReason
The reason focus is moving, reported by MoveFocusRequestedArgs::reason and passed to Controller::move_focus.
PermissionKind
The kind of capability a page is requesting in a PermissionRequestedArgs.
PermissionState
How a PermissionRequestedArgs should be resolved.
PreferredColorScheme
The color scheme a Profile reports to pages through the prefers-color-scheme media query.
ProcessFailedKind
The kind of process that failed, reported by ProcessFailedArgs::kind.
SameSite
The SameSite policy of a Cookie, controlling whether it is sent with cross-site requests.
ScrollBarStyle
The scrollbar appearance WebView2 uses for pages, set with EnvironmentOptions::scrollbar_style.

Functions§

webview
Hosts a WebView2 in a windows-reactor UI tree.
webview_result
Hosts a WebView2 and reports initialization success or failure on the UI thread.

Type Aliases§

Result
A specialized Result type that provides Windows error information.