Skip to content

Commit f946bcf

Browse files
committed
Use wrapper around sandbox visual states
Hoists the UI state up to App.kt to allow for manual reset each time a different frame is selected
1 parent 02bfd21 commit f946bcf

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/App.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.squareup.workflow1.traceviewer
22

33
import androidx.compose.foundation.layout.Box
44
import androidx.compose.runtime.Composable
5+
import androidx.compose.runtime.LaunchedEffect
56
import androidx.compose.runtime.getValue
67
import androidx.compose.runtime.mutableIntStateOf
78
import androidx.compose.runtime.mutableStateOf
89
import androidx.compose.runtime.remember
910
import androidx.compose.runtime.setValue
1011
import androidx.compose.ui.Alignment
1112
import androidx.compose.ui.Modifier
13+
import androidx.compose.ui.geometry.Offset
1214
import com.squareup.workflow1.traceviewer.model.Node
1315
import com.squareup.workflow1.traceviewer.ui.FrameSelectTab
1416
import com.squareup.workflow1.traceviewer.ui.RenderDiagram
@@ -28,13 +30,20 @@ public fun App(
2830
var selectedNode by remember { mutableStateOf<Node?>(null) }
2931
var workflowFrames by remember { mutableStateOf<List<Node>>(emptyList()) }
3032
var frameIndex by remember { mutableIntStateOf(0) }
33+
val sandboxState = remember { SandboxState() }
34+
35+
LaunchedEffect(frameIndex) {
36+
sandboxState.reset()
37+
}
3138

3239
Box(
3340
modifier = modifier
3441
) {
3542
// Main content
3643
if (selectedTraceFile != null) {
37-
SandboxBackground {
44+
SandboxBackground(
45+
sandboxState = sandboxState,
46+
) {
3847
RenderDiagram(
3948
traceFile = selectedTraceFile!!,
4049
frameInd = frameIndex,
@@ -64,3 +73,13 @@ public fun App(
6473
)
6574
}
6675
}
76+
77+
class SandboxState {
78+
var offset by mutableStateOf(Offset.Zero)
79+
var scale by mutableStateOf(1f)
80+
81+
fun reset() {
82+
offset = Offset.Zero
83+
scale = 1f
84+
}
85+
}

workflow-trace-viewer/src/jvmMain/kotlin/com/squareup/workflow1/traceviewer/util/SandboxBackground.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import androidx.compose.ui.geometry.Offset
1717
import androidx.compose.ui.graphics.graphicsLayer
1818
import androidx.compose.ui.input.pointer.PointerEventType
1919
import androidx.compose.ui.input.pointer.pointerInput
20+
import com.squareup.workflow1.traceviewer.SandboxState
2021

2122
/**
2223
* This is the backdrop for the whole app. Since there can be hundreds of modules at a time, there
@@ -27,19 +28,17 @@ import androidx.compose.ui.input.pointer.pointerInput
2728
*/
2829
@Composable
2930
public fun SandboxBackground(
31+
sandboxState: SandboxState,
3032
modifier: Modifier = Modifier,
3133
content: @Composable () -> Unit,
3234
) {
33-
var scale by remember { mutableFloatStateOf(1f) }
34-
var offset by remember { mutableStateOf(Offset.Zero) }
35-
3635
Box(
3736
modifier
3837
.fillMaxSize()
3938
.pointerInput(Unit) {
4039
// Panning capabilities: watches for drag gestures and applies the translation
4140
detectDragGestures { _, translation ->
42-
offset += translation
41+
sandboxState.offset += translation
4342
}
4443
}
4544
.pointerInput(Unit) {
@@ -49,8 +48,8 @@ public fun SandboxBackground(
4948
val event = awaitPointerEvent()
5049
if (event.type == PointerEventType.Scroll) {
5150
val scrollDelta = event.changes.first().scrollDelta.y
52-
scale *= if (scrollDelta < 0) 1.1f else 0.9f
53-
scale = scale.coerceIn(0.1f, 10f)
51+
sandboxState.scale = (sandboxState.scale * if (scrollDelta < 0) 1.1f else 0.9f)
52+
.coerceIn(0.1f, 10f)
5453
event.changes.forEach { it.consume() }
5554
}
5655
}
@@ -60,10 +59,10 @@ public fun SandboxBackground(
6059
modifier = Modifier
6160
.wrapContentSize(unbounded = true, align = Alignment.Center)
6261
.graphicsLayer {
63-
translationX = offset.x
64-
translationY = offset.y
65-
scaleX = scale
66-
scaleY = scale
62+
translationX = sandboxState.offset.x
63+
translationY = sandboxState.offset.y
64+
scaleX = sandboxState.scale
65+
scaleY = sandboxState.scale
6766
}
6867
) {
6968
content()

0 commit comments

Comments
 (0)