webview

repository·master·Indexed 12 days ago

https://github.com/webview/webview

A tiny, cross-platform C/C++ library for building modern GUIs using HTML5, CSS, and JavaScript. It provides a common abstraction layer for platform web engines such as WebKit and WebView2, supporting two-way JavaScript bindings and thread-safe dispatching.

Tokens
3.3K
Snippets
8
Records
20
Agent score
46%

What's inside webview

  1. Platform Support for webview

    master
    webview is a cross-platform library that provides an HTML5 UI abstraction layer. It supports two-way JavaScript bindings, allowing you to call JavaScript from C/C++ and call C/C++ from JavaScript.
  2. Manage Application Lifecycle for existing windows (v0.11.0+)

    master

    In webview v0.11.0 and later, if you pass an existing window to the library, the library will not perform platform-specific initialization. You must manage the lifecycle yourself.

    Specifically, the following tasks are now only performed automatically if the library creates the window for you:

    PlatformInitialization Task
    LinuxCalling gtk_init_check()
    WindowsCOM initialization and DPI awareness enablement
  3. Handle thread safety in webview

    master

    Library functions generally do not have thread safety guarantees. To interact with the webview from other threads safely, use the following methods to schedule code execution on the main/GUI thread:

    • C: Use webview_dispatch()
    • C++: Use webview::dispatch()

    Additionally, webview_return() (C) and webview::resolve() (C++) are safe to call from other threads as they use dispatch internally.

    Note: The main/GUI thread must be the thread that calls webview_run() (C) or webview::run() (C++).

  4. Non-CMake Usage (Direct Compiler Invocation)

    master

    If you are not using CMake, you can invoke compilers directly. Place the amalgamated webview.h (or individual files) in libs/webview and the MS WebView2 header in libs for Windows.

    macOS

    c++ main.cc -O2 --std=c++11 -Ilibs -framework WebKit -ldl -o example

    Linux

    c++ main.cc -O2 --std=c++11 -Ilibs $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.1) -ldl -o example

    Windows

    c++ main.cc -O2 --std=c++14 -static -mwindows -Ilibs -ladvapi32 -lole32 -lshell32 -lshlwapi -luser32 -lversion -o example
  5. Cross-compile webview for Windows x64 on Linux

    master

    You can cross-compile using the toolchain files in the cmake/toolchains directory. To target Windows x64 on Linux with POSIX threads, use the following command:

    cmake -G "Ninja Multi-Config" -B build -S . -D CMAKE_TOOLCHAIN_FILE=cmake/toolchains/x86_64-w64-mingw32.cmake -D WEBVIEW_TOOLCHAIN_EXECUTABLE_SUFFIX=-posix
    cmake --build build --config CONFIG
    cmake -G "Ninja Multi-Config" -B build -S . -D CMAKE_TOOLCHAIN_FILE=cmake/toolchains/x86_64-w64-mingw32.cmake -D WEBVIEW_TOOLCHAIN_EXECUTABLE_SUFFIX=-posix
    cmake --build build --config Release
  6. Generate language bindings using SWIG

    master

    You can generate language bindings for the webview library using the provided SWIG interface file webview.i. This allows you to use the library in languages like Python, C#, Java, or Ruby.

    To generate bindings, use the swig command with the -c++ flag and specify the target language and output directory. The following examples use Unix-style command lines.

    # Create directories for the bindings
    mkdir -p build/bindings/{python,csharp,java,ruby}
    
    # Generate Python bindings
    swig -c++ -python -outdir build/bindings/python -o build/bindings/python/python_wrap.cpp webview.i
    
    # Generate C# bindings
    swig -c++ -csharp -outdir build/bindings/csharp -o build/bindings/csharp/csharp_wrap.cpp webview.i
    
    # Generate Java bindings
    swig -c++ -java -outdir build/bindings/java -o build/bindings/java/java_wrap.cpp webview.i
    
    # Generate Ruby bindings
    swig -c++ -ruby -outdir build/bindings/ruby -o build/bindings/ruby/ruby_wrap.cpp webview.i
  7. Build webview from source

    master

    To build the project using CMake and Ninja:

    1. Configure the build directory:
      cmake -G "Ninja Multi-Config" -B build -S .
    2. Build the project (replace CONFIG with Debug, Release, or Profile):
      cmake --build build --config CONFIG

    To run tests:

    ctest --test-dir build --build-config CONFIG
    cmake -G "Ninja Multi-Config" -B build -S .
    cmake --build build --config Release
  8. Build the Amalgamated Library

    master

    You can create an amalgamated library using the amalgamate.py script. This is useful for distributing the library as a single header.

    python3 scripts/amalgamate/amalgamate.py --base core --search include --output webview_amalgamation.h src
  9. Install dependencies on Windows

    master

    For Windows development:

    • Your compiler must support C++14.
    • It is recommended to use Visual Studio 2022 or later.
    • Developers and end-users must have the WebView2 runtime installed (for Windows versions prior to Windows 11).
    • You can obtain WebView2 from NuGet.
    • Required Windows libraries: advapi32, ole32, shell32, shlwapi, user32, version.
  10. Install dependencies on Linux and BSD

    master

    Linux and BSD require GTK and WebKitGTK libraries. You must use pkg-config to retrieve the correct compiler and linker flags.

    Debian Packages

    • WebKitGTK 6.0, GTK 4:
      • Development: apt install libgtk-4-dev libwebkitgtk-6.0-dev
      • Production: apt install libgtk-4-1 libwebkitgtk-6.0-4
    • WebKitGTK 4.1, GTK 3, libsoup 3:
      • Development: apt install libgtk-3-dev libwebkit2gtk-4.1-dev
      • Production: apt install libgtk-3-0 libwebkit2gtk-4.1-0
    • WebKitGTK 4.0, GTK 3, libsoup 2:
      • Development: apt install libgtk-3-dev libwebkit2gtk-4.0-dev
      • Production: apt install libgtk-3-0 libwebkit2gtk-4.0-37

    Fedora Packages

    • WebKitGTK 6.0, GTK 4:
      • Development: dnf install gtk4-devel webkitgtk6.0-devel
      • Production: dnf install gtk4 webkitgtk6.0
    • WebKitGTK 4.1, GTK 3, libsoup 3:
      • Development: dnf install gtk3-devel webkit2gtk4.1-devel
      • Production: dnf install gtk3 webkit2gtk4.1
    • WebKitGTK 4.0, GTK 3, libsoup 2:
      • Development: dnf install gtk3-devel webkit2gtk4.0-devel
      • Production: dnf install gtk3 webkit2gtk4.0

    FreeBSD Packages

    • GTK 4: pkg install webkit2-gtk4
    • GTK 3: pkg install webkit2-gtk3

    Linking Requirements

    • Linux: Use pkg-config with --cflags and --libs for one of these sets: gtk4 webkitgtk-6.0, gtk+-3.0 webkit2gtk-4.1, or gtk+-3.0 webkit2gtk-4.0. Also link dl.
    • macOS: Link frameworks WebKit and library dl.
    • BSD: Execution may require adding the wxallowed option to your fstab to bypass W^X memory protection.
  11. Migrate CMake targets from v0.11.0 to v0.12.0

    master

    When upgrading from webview v0.11.0 to v0.12.0, several CMake target names have changed to align with exported/installed target names. Update your CMakeLists.txt to use the following replacements:

    Old targetReplacement
    webview::headerswebview::core
    webview::sharedwebview::core_shared
    webview::staticwebview::core_static
    # Example replacement
    target_link_libraries(my_app PRIVATE webview::core)