Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.withFrameNanos
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
Expand Down Expand Up @@ -50,8 +51,14 @@ internal class HudPreviewState(val runtime: PolyComposeRuntime) {
internal set
var naturalHeight by mutableStateOf(0f)
internal set
var measured by mutableStateOf(false)
internal set

val ready: Boolean get() = naturalWidth > 0f && naturalHeight > 0f

fun dispose() {
runtime.dispose()
}
}

private object HudPreviewCache {
Expand All @@ -70,7 +77,7 @@ private object HudPreviewCache {
if (cache.isEmpty()) return
for (state in cache.values) {
try {
state.runtime.dispose()
state.dispose()
} catch (failure: Throwable) {
if (failure.isFatalPreviewFailure()) throw failure
LOGGER.warn("Failed to dispose HUD preview", failure)
Expand Down Expand Up @@ -105,6 +112,8 @@ internal fun rememberHudPreview(hud: Hud): HudPreviewState {
state.naturalWidth = state.runtime.root.width
state.naturalHeight = state.runtime.root.height
}
// Zero-size previews still count as measured so they don't block shortcut navigation
state.measured = true
}
return state
}
Expand All @@ -115,17 +124,29 @@ internal fun hudPreviewScale(naturalW: Float, naturalH: Float, availableW: Float
}

@Composable
internal fun HudPreviewCanvas(state: HudPreviewState, scale: Float, modifier: Modifier = Modifier) {
internal fun HudPreviewCanvas(
state: HudPreviewState,
scale: Float,
alwaysRedraw: Boolean,
modifier: Modifier = Modifier
) {
var animationRevision by remember { mutableIntStateOf(0) }
LaunchedEffect(alwaysRedraw) {
if (!alwaysRedraw) return@LaunchedEffect
while (true) {
withFrameNanos { animationRevision++ }
}
}

Canvas(modifier) {
HudManager.previewRevision.intValue
if (alwaysRedraw) animationRevision
drawIntoCanvas { canvas ->
val skia = canvas.skiaCanvas
skia.save()
skia.clipRect(org.jetbrains.skia.Rect.makeWH(size.width, size.height))
skia.scale(scale, scale)
Snapshot.withoutReadObservation {
state.runtime.root.render(RenderContext(skia))
}
state.runtime.root.render(RenderContext(skia))
skia.restore()
}
}
Expand Down Expand Up @@ -166,7 +187,7 @@ internal fun HudPreview(hud: Hud, modifier: Modifier = Modifier) {
val scale = hudPreviewScale(state.naturalWidth, state.naturalHeight, availableW, availableH)
val w = (state.naturalWidth * scale / density).dp
val h = (state.naturalHeight * scale / density).dp
HudPreviewCanvas(state, scale, Modifier.size(w, h))
HudPreviewCanvas(state, scale, hud.alwaysRedraw, Modifier.size(w, h))
}
})
}
Loading
Loading