Rust bindings for the WebView2 COM APIs

Overview

webview2-rs

Rust bindings for the WebView2 COM APIs

Crates in this repo

The root of this repo defines a virtual workspace in Cargo.toml which includes three crates:

Windows Metadata

The Windows crate requires a Windows Metadata (winmd) file describing the API. The one used in this repo was generated with the webview2-win32md project.

Getting started

Run all the tests from the root of the repo:

> cargo test

Or run the sample app from anywhere in the repo:

> cargo run --example sample

See the README.md in webview2-com for more details about using that crate in your own project.

Updating the WebView2 SDK

The webview2-com-sys build.rs script automatically downloads the NuGet package for the SDK and updates the static libs (WebView2LoaderStatic.lib) for each architecture under **/.windows/, if you build on a Windows machine. You can tell the build script to use a different version by updating WEBVIEW2_VERSION in build.rs:

    const WEBVIEW2_VERSION: &str = "1.0.961.33";

It will also regenerate browser_version.rs and callback_interfaces.rs if they change in a new version. These two files are used in webview2-com, and in particular, the tests in callback.rs verify that all of the interfaces listed in callback_interfaces.rs are implemented. If a new version of the SDK declared additional callback interfaces, you will need to add those interfaces to callback.rs using the #[completed_callback] (for ICoreWebView2...CompletedHandler interfaces) and #[event_callback] (for ICoreWebView2...EventHandler interfaces) macros.

It does not regenerate the winmd file automatically because that would depend on having the dotnet CLI installed. New versions of the SDK should be backwards compatible, but you may want to regenerate the Microsoft.Web.WebView2.Win32.winmd file using webview2-win32md if you need functionality which was added in a new version.

The Windows crate looks in different locations for the .windows directory depending on whether you are building as part of the Cargo virtual workspace in the repo (shared workspace root) or publishing one of the sub-crates (manifest/crate directory). If you do regenerate the winmd file, you should update it in all 3 locations where it is referenced as part of the build:

  • /.windows/winmd/Microsoft.Web.WebView2.Win32.winmd
  • /crates/bindings/.windows/winmd/Microsoft.Web.WebView2.Win32.winmd
  • /crates/webview2-com/.windows/winmd/Microsoft.Web.WebView2.Win32.winmd
Comments
  • Is there a way to reflect CreateCoreWebView2EnvironmentOptions to webview2?

    Is there a way to reflect CreateCoreWebView2EnvironmentOptions to webview2?

    Thank you for creating great crates. I have some questions.

    I want to customize CreateCoreWebView2EnvironmentOptions. And I implemented feature to create environment like the following. This is customizing language through CreateCoreWebView2EnvironmentOptions.

    CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
      Box::new(move |environmentcreatedhandler| unsafe {
        let options = {
          let options: ICoreWebView2EnvironmentOptions =
            CoreWebView2EnvironmentOptions::default().into();
    
          // Setting user's system language
          let lcid = Globalization::GetUserDefaultUILanguage();
          let mut lang = [0; MAX_LOCALE_NAME as usize];
          Globalization::LCIDToLocaleName(
            lcid as u32,
            &mut lang,
            Globalization::LOCALE_ALLOW_NEUTRAL_NAMES,
          );
          options
            .SetLanguage(PCWSTR(lang.as_ptr()))
            .map_err(webview2_com::Error::WindowsError)?;
          options
        };
    
        // remove "mini menu" - See https://github.com/tauri-apps/wry/issues/535
        let _ = options.SetAdditionalBrowserArguments("--disable-features=msWebOOUI,msPdfOOUI");
        CreateCoreWebView2EnvironmentWithOptions(
          PCWSTR::default(),
          PCWSTR::default(),
          options,
          environmentcreatedhandler,
        )
        .map_err(webview2_com::Error::WindowsError)
      }),
      Box::new(move |error_code, environment| {
        error_code?;
        tx.send(environment.ok_or_else(|| windows::core::Error::from(E_POINTER)))
          .expect("send over mpsc channel");
        Ok(())
      }),
    )?;
    

    And call environment.CreateCoreWebView2Controller then get CoreWebView2 and navigate webview2. But My custom language is not reflected.

    To resolve this problem, I think webview2 need to call EnsureCoreWebView2Async(CoreWebView2Environment, CoreWebView2ControllerOptions) or set environment to CreationProperties like written in here. Or does webview2-com crate reflect environment implecitly?

    Thank you for reading my issue.

    opened by keiya01 5
  • Compiling webview2-com-sys fails on GitLab Windows runner

    Compiling webview2-com-sys fails on GitLab Windows runner

    I have a very simple application based on wry that does little more than displaying a website in a window right now. A mandatory part of my "walking skeleton" is a fully functional build pipeline and this is where I have been stuck for 2 days now.

    Here's what I do to build the Windows version of the application:

    1. I use a shared Windows runner provided by GitLab, which comes with this default configuration: https://gitlab.com/gitlab-org/ci-cd/shared-runners/images/gcp/windows-containers/blob/main/cookbooks/preinstalled-software/README.md
    2. In the CI script, I use chocolatey to install llvm and rust
    3. I run cargo build --release

    This all sounds pretty vanilla and works fine until this happens:

    Compiling webview2-com-sys v0.13.0
    error: failed to run custom build command for `webview2-com-sys v0.13.0`
    Caused by:
      process didn't exit successfully: `C:\GitLab-Runner\builds\[redacted]\target\release\build\webview2-com-sys-d5a6ffdd174e101d\build-script-build` (exit code: 1)
      --- stdout
      cargo:rustc-link-search=native=C:\GitLab-Runner\builds\[redacted]\target\release\build\webview2-com-sys-3ac9dd9042e7e7c5\out\x64
      --- stderr
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\arm64\\WebView2Loader.dll" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\arm64\\WebView2Loader.dll"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\arm64\\WebView2Loader.dll.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\arm64\\WebView2Loader.dll.lib"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\arm64\\WebView2LoaderStatic.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\arm64\\WebView2LoaderStatic.lib"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x64\\WebView2Loader.dll" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x64\\WebView2Loader.dll"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x64\\WebView2Loader.dll.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x64\\WebView2Loader.dll.lib"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x64\\WebView2LoaderStatic.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x64\\WebView2LoaderStatic.lib"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x86\\WebView2Loader.dll" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x86\\WebView2Loader.dll"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x86\\WebView2Loader.dll.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x86\\WebView2Loader.dll.lib"
      Copy from "C:\\Users\\gitlab_runner\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\webview2-com-sys-0.13.0\\x86\\WebView2LoaderStatic.lib" -> "C:\\GitLab-Runner\\builds\\[redacted]\\target\\release\\build\\webview2-com-sys-3ac9dd9042e7e7c5\\out\\x86\\WebView2LoaderStatic.lib"
      Error: Io(Error { kind: NotFound, message: "program not found" })
    

    This seems to be specific to the build environment. When I run the same steps on a regular Windows 10 machine, everything works out fine.

    What am I missing? Do I have to prepare any files by hand? Have I overlooked a critical piece of configuration?

    Any help would be greatly appreciated!

    opened by lunikon 2
  • Doesn't play nicely with cross compiling (via cargo cross)

    Doesn't play nicely with cross compiling (via cargo cross)

    Hello! I was trying to compile wry with cargo cross from wsl2 to windows, and it fails on building webview2-com-sys due to this error:

    error: could not find native static libraryWebView2LoaderStatic, perhaps an -L flag is missing?
    

    When I looked at the build.rs script for that package, it seems to use the cfg attributes to test for windows or not windows. Unfortunately it doesn't play well with cross compiling (as explained here: https://kazlauskas.me/entries/writing-proper-buildrs-scripts).

    Any thoughts?

    opened by kdar 2
  • fix(webview2-com-sys): add source on nuget CLI usage, halt on error

    fix(webview2-com-sys): add source on nuget CLI usage, halt on error

    On Windows, sometimes the Nuget configuration is not set so the CLI fails without a source to load a packag. We're seeing this issue oftenly at Tauri, and even had to start our FAQ with a way to troubleshoot this issue. I believe this happened to almost 10 users.

    This PR changes the build script to exit if the nuget installation fails, showing the CLI output to help users find the problem.

    opened by lucasfernog 1
  • Add support for cross-compilation and  *-pc-windows-gnu targets

    Add support for cross-compilation and *-pc-windows-gnu targets

    Cross-compilation needs to use mono to run the nuget.exe CLI tool and download the WebView2 SDK to the output directory. Now that there's support for that, the binaries no longer need to be committed to the repo, the build script can do it every time, as long as mono is installed. If mono is not installed, the build script will show an error (with panic!) indicating that it needs mono to run nuget.exe.

    The *-pc-windows-msvc targets support linking against the WebView2LoaderStatic.lib static library, but the *-pc-windows-gnu targets used in most cross-compilation scenarios need to link to the WebView2Loader.dll DLL with the WebView2Loader.dll.lib import library. To get this working, I had to patch the generated bindings and replace the #[link(...)] attributes with different versions wrapped in #[cfg_attr(...)] for target = "msvc" and not(target = "msvc").

    Now that every host system is expected to run nuget.exe, there's no more reason to copy the binaries to the .windows/ directory. Adding the path from the NuGet package installation under target/ to cargo:rustc-link-search in the build script automatically adds it to the DLL search path when you run a test or example through cargo. This should also apply to consumers of webview2-com.

    opened by wravery 0
Owner
Bill Avery
Bill Avery
Provides two APIs for easily cancelling futures, with the option to fallback to a timeout cancellation

tokio-context Provides two different methods for cancelling futures with a provided handle for cancelling all related futures, with a fallback timeout

Peter Farr 18 Dec 27, 2022
A library for working with alpaca.markets APIs, including the Broker API.

alpaca-rs, an unofficial Rust SDK for the Alpaca API (WIP) Features Easy to use - Minimal setup and no boilerplate. Cross-platform - can run on most p

null 4 Dec 2, 2023
Rust bindings to Cloudflare Worker KV Stores using wasm-bindgen and js-sys.

worker-kv Rust bindings to Cloudflare Worker KV Stores using wasm-bindgen and js-sys

Zeb Piasecki 39 Dec 4, 2022
Rust bindings for the KING OF TIME API

Rust bindings for the KING OF TIME API Example Prints if you are at work or not at work. $ cargo run --example tc -- status Record the time you start

Idein Inc. 2 Oct 11, 2021
Rust bindings for the Mattermost API

mattermost_api Rust bindings for the Mattermost API Installing Add the latest version to your Cargo.toml. Using Docs link. Developing Building Require

Matt Boulanger 1 Feb 2, 2022
High-level PortMidi bindings and wrappers for Rust

High-level PortMidi bindings and wrappers for Rust

Philippe Delrieu 69 Dec 1, 2022
Rust bindings for libjuice. Look at datachannel-rs if you need more batteries.

Rust bindings for libjuice. Look at datachannel-rs if you need more batteries.

Vyacheslav S. Troshin 3 Sep 25, 2022
GGML bindings that aim to be idiomatic Rust rather than directly corresponding to the C/C++ interface

rusty-ggml GGML bindings that aim to be idiomatic Rust rather than directly corresponding to the C/C++ interface. GG-what? See: https://github.com/gge

Kerfuffle 6 May 16, 2023
Unofficial python bindings for the rust llm library. 馃悕鉂わ笍馃

llm-rs-python: Python Bindings for Rust's llm Library Welcome to llm-rs, an unofficial Python interface for the Rust-based llm library, made possible

Lukas Kreussel 7 May 20, 2023
Rust bindings to llama.cpp, using metal on macOS

llama-rs Rust bindings to llama.cpp, for macOS, with metal support, for testing and evaluating whether it would be worthwhile to run an Llama model lo

Max Brunsfeld 7 Aug 31, 2023
High-level, optionally asynchronous Rust bindings to llama.cpp

llama_cpp-rs Safe, high-level Rust bindings to the C++ project of the same name, meant to be as user-friendly as possible. Run GGUF-based large langua

Binedge.ai 4 Nov 21, 2023
First Git on Rust is reimplementation with rust in order to learn about rust, c and git.

First Git on Rust First Git on Rust is reimplementation with rust in order to learn about rust, c and git. Reference project This project refer to the

Nobkz 1 Jan 28, 2022
A stupid macro that compiles and executes Rust and spits the output directly into your Rust code

inline-rust This is a stupid macro inspired by inline-python that compiles and executes Rust and spits the output directly into your Rust code. There

William 19 Nov 29, 2022
Learn-rust - An in-depth resource to learn Rust 馃

Learning Rust ?? Hello friend! ?? Welcome to my "Learning Rust" repo, a home for my notes as I'm learning Rust. I'm structuring everything into lesson

Lazar Nikolov 7 Jan 28, 2022
A highly modular Bitcoin Lightning library written in Rust. Its Rust-Lightning, not Rusty's Lightning!

Rust-Lightning is a Bitcoin Lightning library written in Rust. The main crate, lightning, does not handle networking, persistence, or any other I/O. Thus, it is runtime-agnostic, but users must implement basic networking logic, chain interactions, and disk storage. More information is available in the About section.

Lightning Dev Kit 850 Jan 3, 2023
Telegram bot help you to run Rust code in Telegram via Rust playground

RPG_BOT (Rust Playground Bot) Telegram bot help you to run Rust code in Telegram via Rust playground Bot interface The bot supports 3 straightforward

TheAwiteb 8 Dec 6, 2022
`Debug` in rust, but only supports valid rust syntax and outputs nicely formatted using pretty-please

dbg-pls A Debug-like trait for rust that outputs properly formatted code Showcase Take the following code: let code = r#" [ "Hello, World!

Conrad Ludgate 12 Dec 22, 2022
Playing with web dev in Rust. This is a sample Rust microservice that can be deployed on Kubernetes.

Playing with web dev in Rust. This is a sample Rust microservice that can be deployed on Kubernetes.

Andr茅 Gomes 10 Nov 17, 2022
馃悁 Building a federated alternative to reddit in rust

Lemmy A link aggregator / Reddit clone for the fediverse. Join Lemmy 路 Documentation 路 Report Bug 路 Request Feature 路 Releases 路 Code of Conduct About

LemmyNet 7.2k Jan 3, 2023