|
|
|
@@ -5,13 +5,21 @@ import androidx.compose.foundation.background
|
|
|
|
|
import androidx.compose.foundation.gestures.detectTransformGestures
|
|
|
|
|
import androidx.compose.foundation.layout.Box
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
|
import androidx.compose.material.icons.Icons
|
|
|
|
|
import androidx.compose.material.icons.filled.FitScreen
|
|
|
|
|
import androidx.compose.material3.Icon
|
|
|
|
|
import androidx.compose.material3.IconButton
|
|
|
|
|
import androidx.compose.material3.MaterialTheme
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
|
import androidx.compose.runtime.mutableFloatStateOf
|
|
|
|
|
import androidx.compose.runtime.mutableIntStateOf
|
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
|
import androidx.compose.runtime.remember
|
|
|
|
|
import androidx.compose.runtime.setValue
|
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.draw.clipToBounds
|
|
|
|
|
import androidx.compose.ui.geometry.Offset
|
|
|
|
@@ -102,19 +110,59 @@ fun WorkflowGraph(
|
|
|
|
|
success = scheme.tertiary,
|
|
|
|
|
error = scheme.error,
|
|
|
|
|
accent = listOf(scheme.primary, scheme.secondary, scheme.primaryContainer, scheme.secondaryContainer),
|
|
|
|
|
// Labels sit on a surface-coloured chip, so they must not reuse the
|
|
|
|
|
// container *fill* colours (primaryContainer/secondaryContainer), which
|
|
|
|
|
// are near-invisible as text on the surface. Draw those outcomes in
|
|
|
|
|
// their matching "on-container" colour instead; the other accents are
|
|
|
|
|
// already legible on the surface in both light and dark themes.
|
|
|
|
|
labelAccent = listOf(
|
|
|
|
|
scheme.primary,
|
|
|
|
|
scheme.secondary,
|
|
|
|
|
scheme.onPrimaryContainer,
|
|
|
|
|
scheme.onSecondaryContainer,
|
|
|
|
|
),
|
|
|
|
|
nodeFill = scheme.surfaceVariant,
|
|
|
|
|
nodeFillActive = scheme.primaryContainer,
|
|
|
|
|
nodeBorder = scheme.outline,
|
|
|
|
|
nodeText = scheme.onSurface,
|
|
|
|
|
terminalFill = scheme.secondaryContainer,
|
|
|
|
|
labelBackground = scheme.surface,
|
|
|
|
|
labelText = scheme.onSurfaceVariant,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val fitPaddingPx = with(density) { FIT_PADDING.toPx() }
|
|
|
|
|
val minVisiblePx = with(density) { MIN_VISIBLE.toPx() }
|
|
|
|
|
|
|
|
|
|
var scale by remember { mutableFloatStateOf(1f) }
|
|
|
|
|
var offset by remember { mutableStateOf(Offset.Zero) }
|
|
|
|
|
var canvasSize by remember { mutableStateOf(IntSize.Zero) }
|
|
|
|
|
var centered by remember { mutableStateOf(false) }
|
|
|
|
|
// Once the user pans or zooms, automatic re-fitting stops fighting them; the
|
|
|
|
|
// reset affordance below clears this and re-applies the fit transform.
|
|
|
|
|
var userAdjusted by remember { mutableStateOf(false) }
|
|
|
|
|
var fitRequest by remember { mutableIntStateOf(0) }
|
|
|
|
|
|
|
|
|
|
// Fit the whole graph into the viewport on first layout and whenever the
|
|
|
|
|
// viewport itself changes size, unless the user has taken over the view.
|
|
|
|
|
LaunchedEffect(layout, canvasSize, fitRequest) {
|
|
|
|
|
if (userAdjusted) return@LaunchedEffect
|
|
|
|
|
val viewportWidth = canvasSize.width.toFloat()
|
|
|
|
|
val viewportHeight = canvasSize.height.toFloat()
|
|
|
|
|
if (viewportWidth <= 0f || viewportHeight <= 0f) return@LaunchedEffect
|
|
|
|
|
val viewport = Size(viewportWidth, viewportHeight)
|
|
|
|
|
val fitted = fitScale(viewport, Size(layout.width, layout.height), fitPaddingPx)
|
|
|
|
|
val scaled = Size(layout.width * fitted, layout.height * fitted)
|
|
|
|
|
scale = fitted
|
|
|
|
|
offset = clampOffset(
|
|
|
|
|
offset = Offset(
|
|
|
|
|
x = (viewport.width - scaled.width) / 2f,
|
|
|
|
|
y = (viewport.height - scaled.height) / 2f,
|
|
|
|
|
),
|
|
|
|
|
content = scaled,
|
|
|
|
|
viewport = viewport,
|
|
|
|
|
minVisible = minVisiblePx,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Box(
|
|
|
|
|
modifier = modifier
|
|
|
|
@@ -124,20 +172,20 @@ fun WorkflowGraph(
|
|
|
|
|
.clipToBounds()
|
|
|
|
|
.background(MaterialTheme.colorScheme.surface)
|
|
|
|
|
.semantics { contentDescription = description }
|
|
|
|
|
.onSizeChanged { size ->
|
|
|
|
|
canvasSize = size
|
|
|
|
|
if (!centered && layout.width > 0f && layout.height > 0f) {
|
|
|
|
|
offset = Offset(
|
|
|
|
|
x = (size.width - layout.width * scale) / 2f,
|
|
|
|
|
y = (size.height - layout.height * scale) / 2f,
|
|
|
|
|
).coerceAtLeast(0f)
|
|
|
|
|
centered = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.pointerInput(Unit) {
|
|
|
|
|
.onSizeChanged { canvasSize = it }
|
|
|
|
|
.pointerInput(layout) {
|
|
|
|
|
detectTransformGestures { _, pan, zoom, _ ->
|
|
|
|
|
scale = (scale * zoom).coerceIn(MIN_SCALE, MAX_SCALE)
|
|
|
|
|
offset += pan
|
|
|
|
|
userAdjusted = true
|
|
|
|
|
val newScale = (scale * zoom).coerceIn(MIN_SCALE, MAX_SCALE)
|
|
|
|
|
scale = newScale
|
|
|
|
|
// Keep at least MIN_VISIBLE of the scaled content on screen so a
|
|
|
|
|
// drag can never leave an empty viewport.
|
|
|
|
|
offset = clampOffset(
|
|
|
|
|
offset = offset + pan,
|
|
|
|
|
content = Size(layout.width * newScale, layout.height * newScale),
|
|
|
|
|
viewport = Size(canvasSize.width.toFloat(), canvasSize.height.toFloat()),
|
|
|
|
|
minVisible = minVisiblePx,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
) {
|
|
|
|
@@ -147,28 +195,78 @@ fun WorkflowGraph(
|
|
|
|
|
translate(offset.x, offset.y)
|
|
|
|
|
scale(scale, scale, pivot = Offset.Zero)
|
|
|
|
|
}) {
|
|
|
|
|
drawEdges(layout, colors, runState, textMeasurer)
|
|
|
|
|
// Labels are drawn only after every curve so they always sit on
|
|
|
|
|
// top of a neighbouring edge instead of being crossed by it.
|
|
|
|
|
drawEdgePaths(layout, colors, runState)
|
|
|
|
|
drawEdgeLabels(layout, colors, runState, textMeasurer)
|
|
|
|
|
drawNodes(layout, colors, runState, selectedStage, textMeasurer)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
IconButton(
|
|
|
|
|
onClick = {
|
|
|
|
|
userAdjusted = false
|
|
|
|
|
// Re-run the fit effect even when the viewport is unchanged.
|
|
|
|
|
fitRequest += 1
|
|
|
|
|
},
|
|
|
|
|
modifier = Modifier.align(Alignment.TopEnd).padding(4.dp),
|
|
|
|
|
) {
|
|
|
|
|
Icon(Icons.Default.FitScreen, contentDescription = "Fit graph to view")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun Offset.coerceAtLeast(minimum: Float): Offset =
|
|
|
|
|
Offset(x.coerceAtLeast(minimum), y.coerceAtLeast(minimum))
|
|
|
|
|
|
|
|
|
|
private class GraphColors(
|
|
|
|
|
val success: Color,
|
|
|
|
|
val error: Color,
|
|
|
|
|
val accent: List<Color>,
|
|
|
|
|
val labelAccent: List<Color>,
|
|
|
|
|
val nodeFill: Color,
|
|
|
|
|
val nodeFillActive: Color,
|
|
|
|
|
val nodeBorder: Color,
|
|
|
|
|
val nodeText: Color,
|
|
|
|
|
val terminalFill: Color,
|
|
|
|
|
val labelBackground: Color,
|
|
|
|
|
val labelText: Color,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Uniform scale that fits [content] inside [viewport] with [padding] on every
|
|
|
|
|
* side, never upscaling above 1:1. Degenerate (zero/negative) content or
|
|
|
|
|
* viewport sizes return 1f rather than dividing by zero, so callers never see
|
|
|
|
|
* NaN or infinity.
|
|
|
|
|
*/
|
|
|
|
|
internal fun fitScale(viewport: Size, content: Size, padding: Float): Float {
|
|
|
|
|
val availableWidth = viewport.width - 2f * padding
|
|
|
|
|
val availableHeight = viewport.height - 2f * padding
|
|
|
|
|
if (content.width <= 0f || content.height <= 0f ||
|
|
|
|
|
availableWidth <= 0f || availableHeight <= 0f
|
|
|
|
|
) {
|
|
|
|
|
return 1f
|
|
|
|
|
}
|
|
|
|
|
return minOf(1f, availableWidth / content.width, availableHeight / content.height)
|
|
|
|
|
.coerceAtLeast(MIN_SCALE)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bounds a pan [offset] so at least [minVisible] pixels of the scaled [content]
|
|
|
|
|
* stay inside [viewport] on each axis, i.e. `offset.x` in
|
|
|
|
|
* `[minVisible - content.width, viewport.width - minVisible]` (same for y), so
|
|
|
|
|
* panning can never leave an empty viewport. A fitted/centred offset already
|
|
|
|
|
* inside those bounds is returned unchanged. Degenerate content or an empty
|
|
|
|
|
* bound range (a viewport smaller than twice [minVisible]) is returned
|
|
|
|
|
* unchanged so the caller never divides by zero or produces NaN.
|
|
|
|
|
*/
|
|
|
|
|
internal fun clampOffset(offset: Offset, content: Size, viewport: Size, minVisible: Float): Offset {
|
|
|
|
|
if (content.width <= 0f || content.height <= 0f) return offset
|
|
|
|
|
val minX = minVisible - content.width
|
|
|
|
|
val maxX = viewport.width - minVisible
|
|
|
|
|
val minY = minVisible - content.height
|
|
|
|
|
val maxY = viewport.height - minVisible
|
|
|
|
|
val x = if (minX <= maxX) offset.x.coerceIn(minX, maxX) else offset.x
|
|
|
|
|
val y = if (minY <= maxY) offset.y.coerceIn(minY, maxY) else offset.y
|
|
|
|
|
return Offset(x, y)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density): GraphLayout {
|
|
|
|
|
if (graph.stages.isEmpty()) return GraphLayout(emptyList(), emptyList(), 0f, 0f)
|
|
|
|
|
|
|
|
|
@@ -274,15 +372,13 @@ private fun layoutGraph(graph: Graph, density: androidx.compose.ui.unit.Density)
|
|
|
|
|
internal fun rankStages(graph: Graph): Map<String, Int> =
|
|
|
|
|
graph.stages.withIndex().associate { (index, stage) -> stage.key to index }
|
|
|
|
|
|
|
|
|
|
private fun DrawScope.drawEdges(
|
|
|
|
|
private fun DrawScope.drawEdgePaths(
|
|
|
|
|
layout: GraphLayout,
|
|
|
|
|
colors: GraphColors,
|
|
|
|
|
runState: RunGraphState?,
|
|
|
|
|
textMeasurer: TextMeasurer,
|
|
|
|
|
) {
|
|
|
|
|
for (edge in layout.edges) {
|
|
|
|
|
val route = GraphRoute(edge.edge.source, edge.edge.outcome, edge.edge.target)
|
|
|
|
|
val executed = runState == null || route in runState.takenRoutes
|
|
|
|
|
val executed = edge.isExecuted(runState)
|
|
|
|
|
val color = outcomeColor(edge.edge, colors)
|
|
|
|
|
val alpha = if (executed) 1f else 0.25f
|
|
|
|
|
val path = Path()
|
|
|
|
@@ -296,14 +392,31 @@ private fun DrawScope.drawEdges(
|
|
|
|
|
}
|
|
|
|
|
drawPath(path, color = color.copy(alpha = alpha), style = Stroke(width = 3f))
|
|
|
|
|
drawArrowHead(edge.from, edge.to, color.copy(alpha = alpha))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val label = edge.edge.label
|
|
|
|
|
val measured = textMeasurer.measure(label, TextStyle(fontSize = 11.sp, color = color.copy(alpha = alpha)))
|
|
|
|
|
/**
|
|
|
|
|
* Draws every outcome label in a second pass, after all curves, so a
|
|
|
|
|
* neighbouring edge can never be drawn across a label. Executed edges use a
|
|
|
|
|
* legible on-surface variant of their outcome colour (see [outcomeLabelColor]);
|
|
|
|
|
* dimmed edges use a muted colour on a near-opaque background instead of
|
|
|
|
|
* inheriting the curve's 0.25 alpha, which left the text unreadable.
|
|
|
|
|
*/
|
|
|
|
|
private fun DrawScope.drawEdgeLabels(
|
|
|
|
|
layout: GraphLayout,
|
|
|
|
|
colors: GraphColors,
|
|
|
|
|
runState: RunGraphState?,
|
|
|
|
|
textMeasurer: TextMeasurer,
|
|
|
|
|
) {
|
|
|
|
|
for (edge in layout.edges) {
|
|
|
|
|
val executed = edge.isExecuted(runState)
|
|
|
|
|
val textColor = if (executed) outcomeLabelColor(edge.edge, colors) else colors.labelText
|
|
|
|
|
val measured = textMeasurer.measure(edge.edge.label, TextStyle(fontSize = 11.sp, color = textColor))
|
|
|
|
|
// Every outgoing edge of a stage shares the same `from` anchor, so drawing
|
|
|
|
|
// each label at a fixed offset overprints distinct outcomes (success/error,
|
|
|
|
|
// blocked/skipped). Give each label its own row in a per-source stack,
|
|
|
|
|
// mirroring the independent ports/tracks of internal/tui/graph_layout.go,
|
|
|
|
|
// and draw a faint background so crossing edges stay legible.
|
|
|
|
|
// and draw a near-opaque background so crossing edges stay legible.
|
|
|
|
|
val spread = (edge.labelCount - 1) / 2f
|
|
|
|
|
val labelTopLeft = Offset(
|
|
|
|
|
x = edge.from.x + 8f,
|
|
|
|
@@ -311,7 +424,7 @@ private fun DrawScope.drawEdges(
|
|
|
|
|
(edge.labelSlot - spread) * (measured.size.height + 2f),
|
|
|
|
|
)
|
|
|
|
|
drawRect(
|
|
|
|
|
color = colors.labelBackground.copy(alpha = 0.82f * alpha),
|
|
|
|
|
color = colors.labelBackground.copy(alpha = if (executed) 0.92f else 0.86f),
|
|
|
|
|
topLeft = Offset(labelTopLeft.x - 3f, labelTopLeft.y - 1f),
|
|
|
|
|
size = Size(measured.size.width + 6f, measured.size.height + 2f),
|
|
|
|
|
)
|
|
|
|
@@ -402,6 +515,22 @@ private fun outcomeColor(edge: GraphEdge, colors: GraphColors): Color = when (ed
|
|
|
|
|
else -> colors.accent[abs(edge.outcome.hashCode()) % colors.accent.size]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Legible label colour for an outcome, using [GraphColors.labelAccent] so
|
|
|
|
|
* container-filled accents are rendered as text in their on-container colour.
|
|
|
|
|
* Shares the outcome→index mapping with [outcomeColor] so a label always matches
|
|
|
|
|
* its curve's colour family.
|
|
|
|
|
*/
|
|
|
|
|
private fun outcomeLabelColor(edge: GraphEdge, colors: GraphColors): Color = when (edge.outcome) {
|
|
|
|
|
"error" -> colors.error
|
|
|
|
|
"success" -> colors.success
|
|
|
|
|
else -> colors.labelAccent[abs(edge.outcome.hashCode()) % colors.labelAccent.size]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** True when [runState] is null (definition view) or recorded this edge's route. */
|
|
|
|
|
private fun EdgeLayout.isExecuted(runState: RunGraphState?): Boolean =
|
|
|
|
|
runState == null || GraphRoute(edge.source, edge.outcome, edge.target) in runState.takenRoutes
|
|
|
|
|
|
|
|
|
|
/** A screen-reader description so meaning is never conveyed by colour alone. */
|
|
|
|
|
private fun graphDescription(graph: Graph, runState: RunGraphState?): String {
|
|
|
|
|
if (graph.stages.isEmpty()) return "Empty workflow graph"
|
|
|
|
@@ -422,3 +551,7 @@ private fun graphDescription(graph: Graph, runState: RunGraphState?): String {
|
|
|
|
|
|
|
|
|
|
private const val MIN_SCALE = 0.3f
|
|
|
|
|
private const val MAX_SCALE = 3f
|
|
|
|
|
|
|
|
|
|
/** Breathing room left around a fitted graph, and the least content kept visible. */
|
|
|
|
|
private val FIT_PADDING = 16.dp
|
|
|
|
|
private val MIN_VISIBLE = 48.dp
|
|
|
|
|