WebView2 with JUCE 7.0.3 and CMake

Hi everyone,

Has anyone managed to get WebView2 working on their JUCE 7.0.3 based project? If so what are the steps required to get this feature to work?

From what I can see, the WebView2Loader.dll is required. What would be the best way to import this file on to a CMake project? I’ve seen people mentioning different approaches like decompressing and including the contents of the webview2.nupkg file or calling Nuget and installing WebView2 directly from CMake.

Can the JUCE_USE_WIN_WEBVIEW2 flag be enabled in the CMakeLists.txt target_compile_definitions section or does it still need to be modified within juce_gui_extra?

Now that WindowsWebView2WebBrowserComponent is no longer supported I assume we have to refer to WebBrowserComponent for our implementation? How do we specify

WebBrowserComponent::Options::WinWebView2 dll and user data file paths if we are releasing our software?

Also, it would be nice to redirect users looking for the deprecated WindowsWebView2WebBrowserComponent documentation to the WebBrowserComponent docs. The web page currently displays a “not available” error which is confusing.

Sorry for all the questions but I couldn’t find a full guide/explanation for this.

Thanks in advance

Hey there, sorry for the late reply, just seeing your question. It’s a bit of a hassle but I made it work with the nuget approach. I donwloaded the nuget binary and put it somewher in my path. Then in my plugin cmake I added these lines

    find_program(NUGET_EXE NAMES nuget)
        message("NUGET.EXE not found.")
        message(FATAL_ERROR "Please install this executable, and run CMake again.")
    execute_process(COMMAND ${NUGET_EXE} install "Microsoft.Web.WebView2" -Version 1.0.1210.39 -ExcludeVersion -OutputDirectory ${CMAKE_BINARY_DIR}/packages)
    execute_process(COMMAND ${NUGET_EXE} install "Microsoft.Windows.ImplementationLibrary" -ExcludeVersion -OutputDirectory ${CMAKE_BINARY_DIR}/packages)

    set(WebView2_DIR ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native)
    set(WebView2_LIB WebView2Loader.dll)

    target_link_libraries(${PROJECT_NAME}_static INTERFACE Version.lib Shlwapi.lib ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native/x64/WebView2LoaderStatic.lib ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.ImplementationLibrary/build/native/Microsoft.Windows.ImplementationLibrary.targets)
    target_include_directories(juce SYSTEM PUBLIC ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native/include ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.ImplementationLibrary/include)

# To install the dll somewhere to load it in your program, do what suits your project best
            TARGET ${PROJECT_NAME}_static
            COMMAND ${CMAKE_COMMAND} -E copy ${WebView2_DIR}/${WebView2_LIB} ${DATA_SHARED_DIR}/${WebView2_LIB}
            COMMENT "Copy WebView2 shared lib"

Then, in the c++ code, where I load the WindowsWebView2WebBrowserComponent (I’m still on juce 6), I pass the path to the dll. The other problematic part is that you have to ensure the user has the WebView2 runtime installed. There is a pretty good documentation about that on Microsoft website :slight_smile:

With all that I can display webviews in my plugin. Some of the juce integration code can be improved by managing some extra settings using the ICoreWebView2Settings object (to hide the contextual menu or debug tools for example). It’s pretty well documented too.

I still have some freezing issues in certain cases that I couldn’t identify well but for the basic aspects, it works!