compose-rust 0.0.11

Declarative UI in Rust, rendered by an AOT-compiled Compose Multiplatform engine.
Documentation
package dev.darkpyonix.composerust.runtime

import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import dev.darkpyonix.composerust.protocol.HostEvent
import dev.darkpyonix.composerust.protocol.WindowHeightClass
import dev.darkpyonix.composerust.protocol.WindowSizeClass
import kotlin.math.floor

/**
 * The width in dp at which each size class begins.
 *
 * These are the Material 3 window size class boundaries, and the Host uses the same two
 * numbers. If the two sides ever disagree, the Compose components adapt at one width and
 * the layout the Host authored switches at another, which is visible on screen.
 */
const val MEDIUM_MIN_WIDTH_DP: Float = 600f
const val EXPANDED_MIN_WIDTH_DP: Float = 840f

/**
 * The height in dp at which each height class begins.
 *
 * From the same table as the width boundaries. A layout folds at the height Compose's own
 * components already treat as short, so the two sides fold together.
 */
const val MEDIUM_MIN_HEIGHT_DP: Float = 480f
const val EXPANDED_MIN_HEIGHT_DP: Float = 900f

/**
 * The size class of the window this content is in.
 *
 * The Host is told about this too, but only so a component can choose what to put on the
 * screen. The widgets that change shape with the window read it here instead, because they
 * are drawn on this side and asking the Host would mean a boundary call, a VirtualDom pass
 * and a rebuilt subtree to arrive at a layout this side could reach by moving the nodes it
 * already has.
 *
 * Not `staticCompositionLocalOf`: this one does change, and only the widgets that read it
 * should be invalidated when it does.
 */
val LocalWindowSizeClass = compositionLocalOf { WindowSizeClass.Compact }

/** The class a window of this width belongs to. Height does not take part. */
fun windowSizeClassOf(widthDp: Float): WindowSizeClass = when {
    widthDp >= EXPANDED_MIN_WIDTH_DP -> WindowSizeClass.Expanded
    widthDp >= MEDIUM_MIN_WIDTH_DP -> WindowSizeClass.Medium
    else -> WindowSizeClass.Compact
}

/** The class a window of this height belongs to. Width does not take part. */
fun windowHeightClassOf(heightDp: Float): WindowHeightClass = when {
    heightDp >= EXPANDED_MIN_HEIGHT_DP -> WindowHeightClass.Expanded
    heightDp >= MEDIUM_MIN_HEIGHT_DP -> WindowHeightClass.Medium
    else -> WindowHeightClass.Compact
}

/**
 * Tells the Host which size class the window is in, and only when that changes.
 *
 * Dragging a window edge produces a layout pass many times a second. Sending the size on
 * each of them would run the Host's VirtualDom just as often, for a value almost every
 * layout would report as unchanged. So the last class sent is remembered and compared, and
 * nothing crosses the boundary until it differs.
 *
 * The remembered class starts at [WindowSizeClass.Compact] because that is what a Host
 * assumes before it hears anything. A window that opens phone-sized is therefore silent:
 * telling the Host what it already believes would cost a boundary call and a VirtualDom
 * pass and change nothing. A window that opens wider reports once, on its first
 * measurement.
 *
 * The dp measurements ride along with the class rather than being reported on their own,
 * because a Host that needs pixel-by-pixel sizes is asking for work that belongs in
 * Compose's own layout.
 */
val LocalWindowDrag = staticCompositionLocalOf<() -> Unit> { {} }

data class WindowSafeArea(val top: Float = 0f, val leading: Float = 0f, val trailing: Float = 0f)

/**
 * One number that changes exactly when [WindowSizeReporter.report] would send for a new
 * size: the two classes, and with [everyDpChange] the size rounded to whole dp the same way
 * the reporter rounds it. A number rather than an object, so comparing it on every layout
 * pass allocates nothing.
 */
fun windowReportKey(widthDp: Float, heightDp: Float, everyDpChange: Boolean): Long {
    val classes = (windowSizeClassOf(widthDp).ordinal * 3 + windowHeightClassOf(heightDp).ordinal).toLong()
    if (!everyDpChange) return classes
    val width = floor(widthDp + 0.5f).toLong() and 0xFFFFFF
    val height = floor(heightDp + 0.5f).toLong() and 0xFFFFFF
    return (1L shl 62) or (classes shl 48) or (width shl 24) or height
}

/** What a window's size report carries besides the size, kept where its layout can read it. */
internal class WindowReportInputs {
    var safeArea: WindowSafeArea = WindowSafeArea()
    var everyDp: Boolean = false
}

class WindowSizeReporter(private val nodeId: Int = 0) {
    private var lastClass: WindowSizeClass = WindowSizeClass.Compact
    private var lastSafeArea = WindowSafeArea()
    private var lastHeightClass: WindowHeightClass = WindowHeightClass.Compact
    private var lastWidthDp = 0
    private var lastHeightDp = 0

    /**
     * Reports one measurement. Returns whether an event was sent.
     *
     * Nothing is allocated when neither class has changed, which is the common case. The
     * two classes ride in one record, so a window that gets wider and shorter in the same
     * drag costs one event rather than two.
     *
     * A window that asked for [everyDpChange] also reports when its width or height, each
     * rounded to the nearest whole dp (a half rounds up), differs from the rounded values of
     * the last event sent. The rounded integers are compared, never the raw floats, so jitter
     * under one dp is ignored. The event still carries the measured, unrounded size.
     */
    fun report(
        widthDp: Float,
        heightDp: Float,
        dispatcher: EventDispatcher,
        safeArea: WindowSafeArea = WindowSafeArea(),
        everyDpChange: Boolean = false,
    ): Boolean {
        val sizeClass = windowSizeClassOf(widthDp)
        val heightClass = windowHeightClassOf(heightDp)
        if (sizeClass == lastClass && heightClass == lastHeightClass && safeArea == lastSafeArea) {
            if (!everyDpChange) return false
            if (floor(widthDp + 0.5f).toInt() == lastWidthDp && floor(heightDp + 0.5f).toInt() == lastHeightDp) return false
        }
        if (everyDpChange) {
            lastWidthDp = floor(widthDp + 0.5f).toInt()
            lastHeightDp = floor(heightDp + 0.5f).toInt()
        }
        lastClass = sizeClass
        lastHeightClass = heightClass
        lastSafeArea = safeArea
        dispatcher.dispatch(
            HostEvent.WindowSizeChanged(
                nodeId = nodeId,
                handlerId = 0,
                widthDp = widthDp,
                heightDp = heightDp,
                sizeClass = sizeClass,
                heightClass = heightClass,
                safeAreaTop = safeArea.top,
                safeAreaLeading = safeArea.leading,
                safeAreaTrailing = safeArea.trailing,
            ),
        )
        return true
    }
}

/**
 * The `Window` node the content being composed is drawn in, or 0 for the one window
 * of a single window application. The events a window names itself by - its size class, its
 * zoom - carry this as their node id, and the theme and zoom level are looked up under it.
 */
val LocalWindowNodeId = staticCompositionLocalOf { 0 }

/** Bounds are in window pixels, matching the Linux backend's screen coordinates. */
internal object WindowDropTargets {
    private data class Target(val left: Float, val top: Float, val right: Float, val bottom: Float, val density: Float, val depth: Int)
    private val targets = LinkedHashMap<Pair<Int, Int>, Target>()

    fun update(windowId: Int, nodeId: Int, left: Float, top: Float, right: Float, bottom: Float, density: Float, depth: Int) {
        targets[windowId to nodeId] = Target(left, top, right, bottom, density, depth)
    }

    fun remove(windowId: Int, nodeId: Int) { targets.remove(windowId to nodeId) }

    fun find(windowId: Int, x: Float, y: Float): Pair<Int, Pair<Float, Float>>? {
        val target = targets.entries.toList().asReversed().filter { (key, rect) ->
            key.first == windowId && x >= rect.left && x < rect.right && y >= rect.top && y < rect.bottom
        }.maxByOrNull { it.value.depth } ?: return null
        val rect = target.value
        return target.key.second to ((x - rect.left) / rect.density to (y - rect.top) / rect.density)
    }
}