JWM icon indicating copy to clipboard operation
JWM copied to clipboard

Jetpack Compose example

Open DevSrSouza opened this issue 4 years ago • 6 comments

I saw that Compose-jb now was an experimental example for LWJGL/GLFW using ComposeScene.

Is possible, with this new API from Compose, be able to use with JWM? If so, would be awesome to see a code sample.

DevSrSouza avatar Nov 07 '21 17:11 DevSrSouza

Should be possible, yes. Do you want to make a PR with an example?

tonsky avatar Nov 08 '21 14:11 tonsky

I don't know JWM enough to do that, I think.

DevSrSouza avatar Nov 08 '21 17:11 DevSrSouza

Great places to start is https://github.com/HumbleUI/JWM/blob/main/docs/Getting%20Started.md and https://github.com/HumbleUI/JWM/blob/main/examples/dashboard/java/Example.java. Let me know if you have any questions. I’d do it myself but I am not sure how long it might be delayed because of conflicting priorities

tonsky avatar Nov 08 '21 17:11 tonsky

cc @smallshen

DevSrSouza avatar Nov 12 '21 16:11 DevSrSouza

Had a look at that example l, am pretty sure you could integrate it easily.

You need access to just 2 things for this to work:

  • OpenGL Surface
  • Window handle

Here is what it should look like: (You would need to sort out the remaining glfw() calls because JWM manages these internally I believe)

fun main() {
    var width = 640
    var height = 480

    // Initialize JWM OpenGL
    // Then:
    var surface = // get JWM GL Surface
    val windowHandle = //get JWM GL Window Handle

    val glfwDispatcher = GlfwCoroutineDispatcher() // a custom coroutine dispatcher, in which Compose will run
    glfwSetWindowCloseCallback(windowHandle) { glfwDispatcher.stop() }

    lateinit var composeScene: ComposeScene

    fun render() {
       // You might need to change this call, not sure what the proper code is to get the canvas from the surface
        surface.canvas.clear(Color.WHITE)
        composeScene.constraints = Constraints(maxWidth = width, maxHeight = height)
        composeScene.render(surface.canvas, System.nanoTime())

        context.flush()
        glfwSwapBuffers(windowHandle)
    }

    val frameDispatcher = FrameDispatcher(glfwDispatcher) { render() }

    val density = Density(glfwGetWindowContentScale(windowHandle))
    composeScene = ComposeScene(glfwDispatcher, density, invalidate = frameDispatcher::scheduleFrame)

    glfwSetWindowSizeCallback(windowHandle) { _, windowWidth, windowHeight ->
        width = windowWidth
        height = windowHeight
        surface.close()
        surface = createSurface(width, height, context)

        glfwSwapInterval(0)
        render()
        glfwSwapInterval(1)
    }

    composeScene.subscribeToGLFWEvents(windowHandle)
    composeScene.setContent { App() }
    glfwShowWindow(windowHandle)

    glfwDispatcher.runLoop()

    composeScene.close()
    glfwDestroyWindow(windowHandle)

    exitProcess(0)
}

GavinRay97 avatar Jan 22 '22 17:01 GavinRay97

@DevSrSouza https://github.com/smallshen/JWM-Compose

smallshen avatar May 19 '22 00:05 smallshen