/Compose-Desktop-WebView

Primary LanguageKotlinApache License 2.0Apache-2.0

License

jcef-compose

Jcef-compose is a small API targeting jvm that offers the ability to embed a CEF Browser as a Composable. The project was born because of Swing interoperability limitations; indeed, it is currently not possible to compose on top of a swing component (#1521, #2926).

Initially, the goal was to take advantage of the CEF OSR feature to render in Canvas like in this experimental project, but the performance was not up to par.
Ultimately, the project focuses on making it easy to integrate JCEF into a new or existing application using jcefmaven while providing a lightweight and robust API.

Waiting for the JetBrains team and other compose contributors to provide an answer to Swing interoperability issue(s) (they will, sure).

Requirements

  • Java 11 or later

Supported platforms

  • Windows, OSR is not supported on arm64
  • macOS
  • Linux

Installation

Coming soon

How to use

Synchronous Cef initialization example:

fun main() {
    // https://github.com/JetBrains/compose-multiplatform/issues/2939
    // The issue above will affect the window resizing and closing
    Cef.initSync()

    singleWindowApplication(
        title = "CEF Compose Browser",
        exitProcessOnExit = false
    ) {
        CefBrowserCompose(
            url = remember { "https://github.com/Manriif/jcef-compose" },
            window = window,
            initContent = { CefInitProgressContent(it) },
            errorContent = { CefInitErrorContent(it) }
        )
    }

    Cef.dispose()
    exitProcess(0)
}

Asynchronous Cef initialization example:

fun main() = disposableSingleWindowApplication(
    title = "CEF Awt Browser",
    onExitProcess = Cef::dispose
) {
    val applicationDisposer = ApplicationDisposer.current

    // https://github.com/JetBrains/compose-multiplatform/issues/2939
    // Initializing asynchronously here fix the issue above without blocking the main thread
    Cef.initAsync(onRestartRequired = applicationDisposer::restart)

    CefBrowserAwt(
        url = remember { "https://github.com/Manriif/jcef-compose" },
        initContent = { CefInitProgressContent(it) },
        errorContent = { CefInitErrorContent(it) }
    )
}

Ready to run examples are available here.
These examples can be run on IntelliJ through run configurations.

run / debug configurations

Or as gradle tasks in the terminal:

  • ./gradlew awt-browser, example usage of CefBrowserAwt.
  • ./gradlew compose-browser, example usage of CefBrowserCompose.
  • ./gradlew local-awt-browser, example usage of CefBrowserAwt with local HTML, CSS and JS files.
  • ./gradlew local-compose-browser, example usage of CefBrowserCompose with local HTML, CSS and JS files.

Limitations

Jcef limitations apply to this project, see:

Contributing

Feel free to contribute!!