WebView2 with JUCE 7.0.3 and CMake

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

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" -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
add_custom_command(
            TARGET ${PROJECT_NAME}_static
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy ${WebView2_DIR}/${WebView2_LIB} ${DATA_SHARED_DIR}/${WebView2_LIB}
            COMMENT "Copy WebView2 shared lib"
        )
endif()

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!

3 Likes