JUCE8 Windows WebView2

Is there any simple way to get started with Windows WebView2 and JUCE8?

I’m not sure about Projucer as I’m trying under CMake.

I’ve tried getting some of the CMakeFiles scripts from the JUCE8 repo to try and find/install things. but it’s not an “out of the box” experience. (Windows 11 here btw)

This is meant to be mostly seamless, but we discovered about a week ago that the instructions embedded inside our CMake find script to install the WebView2 package may not be working anymore.

Ideally, you should install the WebView2 Nuget package and our FindWebView2.cmake script should find it when the NEEDS_WEBVIEW2 option is specified when creating the JUCE target.

Something could have changed on the package repository’s side. I’ll see if we can streamline it again.

Install Nuget (I don’t recommend using PowerShell)

Install the package (I like using CMake’s FetchContent directory):

execute_process(
    COMMAND nuget install Microsoft.Web.WebView2 -OutputDirectory "${CMAKE_BINARY_DIR}/_deps/Nuget"
    WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
)

Point JUCE at it:

set(JUCE_WEBVIEW2_PACKAGE_LOCATION
    "${CMAKE_BINARY_DIR}/_deps/Nuget"
)

Set NEEDS_WEBVIEW2 to TRUE:

juce_add_plugin(
    ${PROJECT_NAME}
    ...
    NEEDS_WEBVIEW2
    TRUE
)

Statically link it:

target_compile_definitions(
    ${PROJECT_NAME} PUBLIC
    ...
    JUCE_WEB_BROWSER=1
    JUCE_USE_WIN_WEBVIEW2_WITH_STATIC_LINKING=1>
)

The instructions didn’t specify NEEDS_WEBVIEW2 which confused me for a bit.

3 Likes

Just in case you’d still like to use PowerShell, it appears the JUCE provided instructions should be using the following commands

Register-PackageSource -provider NuGet -name nugetRepository -location https://www.nuget.org/api/v2
Install-Package Microsoft.Web.WebView2 -Scope CurrentUser -RequiredVersion 1.0.1901.177 -Source nugetRepository

It seems that with some Windows updates, explicitly specifying the -Source option may be necessary now. We’ll update these in the JUCE repository.

You can omit the -RequiredVersion option to always fetch the latest WebView2 package, which is probably a good thing, with the caveat that there could be breaking changes, which we haven’t tested and adjusted to yet.


That said, I really like how @mthierman is using nuget.exe with CMake’s execute_process. This seems to provide better control of the dependency on a per-project basis, so I would probably use this approach for my own projects.

That was the major missing bit.

Thanks @mthierman and @attila !

I’ve already had something similar to your suggestion but never noticed the JUCE_WEBVIEW2_PACKAGE_LOCATION.

So now I have:

if(MSVC)
    find_program(NUGET_EXE NAMES nuget)
    if(NOT NUGET_EXE)
        message("NUGET.EXE not found.")
        message(FATAL_ERROR "Please install this executable, and run CMake again.")
    endif()
    execute_process(COMMAND ${NUGET_EXE} install "Microsoft.Web.WebView2" -OutputDirectory ${CMAKE_BINARY_DIR}/packages)
    set(WebView2_DIR ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2)
    set(JUCE_WEBVIEW2_PACKAGE_LOCATION
        "${CMAKE_BINARY_DIR}/packages"
    )

Which was suggested originally here -

In addition to turning the NEEDS_WEBVIEW2

Edit - removed the explicit version as it was from the original post and might be too old.

For specifying a specific version with nuget, you can:

nuget install Microsoft.Web.WebView2 -Version 1.0.2535.41

Or use a packages.config file:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Microsoft.Web.WebView2" version="1.0.2535.41" targetFramework="native" />
</packages>

and use nuget restore

execute_process(
    COMMAND nuget restore -PackagesDirectory "${CMAKE_BINARY_DIR}/_deps/Nuget"
    WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
)
1 Like