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§
- Accelerator
KeyPressed Args - A browser-level key press delivered before the page sees it.
- Color
- A 32-bit RGBA color, used for the browser’s default background.
- Content
Loading Args - Details about content that is starting to load, delivered to a
WebView::on_content_loadinghandler. - Controller
- Hosts a WebView2 browser inside a parent window, controlling its bounds, visibility, and lifetime.
- Controller
Options - Configures a
Controllerat 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 toEnvironment::create_controller_with_options. - Cookie
- A browser cookie. Read cookies with
CookieManager::get_cookiesand write them withCookieManager::add_or_update_cookie. - Cookie
Manager - 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
defermethod (for examplePermissionRequestedArgs::defer) and callcompleteonce the decision has been made. Dropping the deferral completes it automatically. - DevTools
Protocol Event Received Args - A Chrome DevTools Protocol event, delivered to a
WebView::on_dev_tools_protocol_eventhandler. The event’s parameters are available as a JSON object string. - Download
Operation - An in-progress or finished download.
- Download
Starting Args - Details about a download that is about to start.
- Environment
- The WebView2 environment. Owns the user data folder and browser process and
creates
Controllerinstances that host the browser in a window. - Environment
Options - 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 toEnvironment::with_options. - Event
Registration - An event subscription that unsubscribes when dropped.
- Move
Focus Requested Args - A request to move focus out of the browser.
- Navigation
Completed Args - Details about a completed navigation, delivered to a
WebView::on_navigation_completedhandler. - Navigation
Request - A request to navigate with a custom HTTP method, headers, or body, passed to
WebView::navigate_with_request. Defaults to aGETwith no extra headers or body. - Navigation
Starting Args - Details about a navigation that is about to start.
- NewWindow
Requested Args - Details about a page requesting to open a new window.
- Permission
Requested Args - Details about a permission a page is requesting (for example camera or
geolocation access), delivered to a
WebView::on_permission_requestedhandler. Decide the outcome withset_state. - Process
Failed Args - Details about a failed or unresponsive browser process, delivered to a
WebView::on_process_failedhandler. - Profile
- A WebView2 browser profile - the on-disk identity (name, path, private mode)
shared by every
WebViewcreated with it, plus its color scheme, download folder, and browsing-data controls. Obtain it withWebView::profile. - Script
Id - 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 withWebView::settings. - WebMessage
Received Args - A message posted from the hosted page’s JavaScript, delivered to a
WebView::on_web_message_receivedhandler. - WebResource
Request - A resource request intercepted by
WebView::on_web_resource_requested, exposing the requested URI, HTTP method, and headers. - WebResource
Response - The response returned from a
WebView::on_web_resource_requestedhandler to fulfill a request from memory. Defaults to200 OKwith no headers; add acontent_typeso the page interprets the body correctly. - WebView
- A WebView2 browser. Navigate to URLs and run JavaScript against the hosted page.
Enums§
- Download
Interrupt Reason - Why a
DownloadOperationwas interrupted. - Download
State - The state of a
DownloadOperation. - Host
Resource Access Kind - How a folder mapped with
WebView::set_virtual_host_name_to_folder_mappingmay be accessed. - KeyEvent
Kind - The kind of key event reported by
AcceleratorKeyPressedArgs::key_event_kind. - Memory
Usage Target Level - The level WebView2 should target for the browser’s memory usage, set with
WebView::set_memory_usage_target_level. - Move
Focus Reason - The reason focus is moving, reported by
MoveFocusRequestedArgs::reasonand passed toController::move_focus. - Permission
Kind - The kind of capability a page is requesting in a
PermissionRequestedArgs. - Permission
State - How a
PermissionRequestedArgsshould be resolved. - Preferred
Color Scheme - The color scheme a
Profilereports to pages through theprefers-color-schememedia query. - Process
Failed Kind - The kind of process that failed, reported by
ProcessFailedArgs::kind. - Same
Site - The
SameSitepolicy of aCookie, controlling whether it is sent with cross-site requests. - Scroll
BarStyle - The scrollbar appearance WebView2 uses for pages, set with
EnvironmentOptions::scrollbar_style.
Functions§
- webview
- Hosts a WebView2 in a
windows-reactorUI tree. - webview_
result - Hosts a WebView2 and reports initialization success or failure on the UI thread.