Skip to content
Draft
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
1 change: 1 addition & 0 deletions kmp/src/androidUnitTest/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kt_android_library(
"**/ProjectListPhoneLayoutTest.kt",
"**/ProjectListDeleteTest.kt",
"**/SketchEmbedUiTest.kt",
"**/ElementStatsHostTest.kt",
"**/SourcePagePickerTest.kt",
"**/DrawingAttachUiTest.kt",
"**/RegistryEditorScreenTest.kt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ import org.junit.runners.Suite
// AndroidPixelSpikeTest (Spike 0.1.3e) needs the commonTest DeterminismSpikeSupport and InputSpikeAndroidTest composes @Composable lambdas (no Compose plugin or commonTest in this target); both run under Gradle.
// EscOrderIntegrationTest and BracketPlateUxScenarioTest (sketch v2, Tasks 6.2.4h and 6.2.4f) mount the real host (composeRule, no Compose plugin in this target) over the commonTest rig; Gradle-only.
// KeyboardAccessHostTest (sketch v2, Task 3.6.3c and the host's accessibility mounts) mounts the real host over the commonTest rig (composeRule, no Compose plugin in this target); Gradle-only.
// ElementStatsHostTest (the element size section of the real host's Style tab) mounts the host over the commonTest rig (composeRule, no Compose plugin in this target); Gradle-only.
// Task 6.2.1c: WiringV2AndroidEntryPointTest (the root, the class seams of the plan-seam-classes block and the saver's durability) stays Gradle-only for the reasons named above; it is named here so TestRegistrationAuditTest can tell an exclusion from an unregistered class.
// SyncStatusSheetStatesTest, SyncStatusBadgeTest and SyncStatusTextScaleTest (Task 1.7b) are excluded for the same reason (1).
// MeasurementValueEditorTest, MeasurementValueEditorErrorStatesTest, ValueEditorTextScaleTest, ValueEditorSheetTest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2026 Tyler Stapler
// SPDX-License-Identifier: Elastic-2.0

package dev.stapler.stelekit.ui.sketch.v2

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import dev.stapler.stelekit.sketch.v2.V2EditorRig
import dev.stapler.stelekit.sketch.v2.commands.AddElement
import dev.stapler.stelekit.sketch.v2.scene.ElementId
import dev.stapler.stelekit.sketch.v2.scene.RectElement
import dev.stapler.stelekit.sketch.v2.text.FixedAdvanceTextMetricsProvider
import dev.stapler.stelekit.sketch.v2.tools.filledRectEl
import dev.stapler.stelekit.ui.sketch.ModernPreviewEnvironment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import kotlin.test.assertEquals
import kotlin.test.assertIs

/** The element size section of the Style tab in the real host: shown for one selected element, and typing a value moves it. */
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [29], qualifiers = "w1280dp-h900dp-mdpi")
class ElementStatsHostTest {
@get:Rule
val rule = createComposeRule()

private fun mount(): SketchEditorSession {
val rig = V2EditorRig()
runBlocking { rig.seed() }
val metrics = FixedAdvanceTextMetricsProvider()
val session = assertIs<EditorOpening.Editor>(runBlocking { V2EditorOpener(rig.deps, Dispatchers.Default).open(rig.project.uuid, metrics) }).session
rule.runOnUiThread { session.controller.submit(AddElement(filledRectEl("r1", 0, 0, 100, 50))) }
rule.setContent { SketchEditorV2Host(session, ModernPreviewEnvironment(metrics, PainterResources()), onBack = {}) }
rule.waitForIdle()
return session
}

@Test
fun sizeSection_should_AppearForOneSelectedElement_AndMoveItWhenAValueIsTyped() {
val session = mount()
rule.runOnUiThread { session.controller.tools.select(listOf(ElementId("r1"))) }
rule.waitForIdle()

rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "x").performTextClearance()
rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "x").performTextInput("40")
rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "x").performImeAction()
rule.waitForIdle()

assertEquals(40.0, (session.controller.doc.scene[ElementId("r1")] as RectElement).x.value)
}

@Test
fun sizeSection_should_ShowLockedInsteadOfFields_When_TheElementIsLocked() {
val session = mount()
rule.runOnUiThread {
session.controller.submit(dev.stapler.stelekit.sketch.v2.commands.LockElements(listOf(ElementId("r1")), true))
session.controller.tools.select(listOf(ElementId("r1")))
}
rule.waitForIdle()

rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "locked").assertExists()
rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "x").assertDoesNotExist()
}

@Test
fun sizeSection_should_BeAbsent_When_NothingIsSelected() {
mount()

rule.onNodeWithTag(StylePanelTags.STATS_PREFIX + "x").assertDoesNotExist()
}

private fun androidx.compose.ui.test.SemanticsNodeInteraction.assertDoesNotExist() = assertDoesNotExist()
}
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ import org.junit.runners.Suite
dev.stapler.stelekit.sketch.v2.ExcalidrawFillTest::class,
dev.stapler.stelekit.sketch.v2.ScenePainterPixelTest::class,
dev.stapler.stelekit.sketch.v2.ToolStripModelTest::class,
dev.stapler.stelekit.sketch.v2.ElementStatsGeometryTest::class,
dev.stapler.stelekit.sketch.v2.FreehandGoldenTest::class,
dev.stapler.stelekit.sketch.v2.tools.ToolCancelTest::class,
dev.stapler.stelekit.sketch.v2.tools.ToolInventoryCompletenessTest::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ data class ElementStats(val x: Double, val y: Double, val width: Double, val hei
companion object {
private const val HALF_TURN_DEGREES = 180.0

/** Null for an element with no extent (an empty line). The angle is 0 for a kind that cannot rotate. */
/**
* Null for an element with no extent (an empty line). A box or ellipse reports its own unrotated box, the one
* [ResizeElement] writes, so a rotated element's width and height are not its rotated bounds. The angle is 0 for a kind that cannot rotate.
*/
fun of(element: SceneElement): ElementStats? {
val b = SceneBounds.of(element) ?: return null
val angle = when (element) {
is BoxElement -> element.angle
is EllipseElement -> element.angle
else -> 0.0
val own = when (element) {
is BoxElement -> listOf(element.x.value, element.y.value, element.width.value, element.height.value, element.angle)
is EllipseElement -> listOf(element.cx.value - element.rx.value, element.cy.value - element.ry.value, 2 * element.rx.value, 2 * element.ry.value, element.angle)
else -> null
}
return ElementStats(b.minX, b.minY, b.width, b.height, angle * HALF_TURN_DEGREES / PI)
if (own != null) return ElementStats(own[0], own[1], own[2], own[3], own[4] * HALF_TURN_DEGREES / PI)
val b = SceneBounds.of(element) ?: return null
return ElementStats(b.minX, b.minY, b.width, b.height, 0.0)
}

fun radians(degrees: Double): Double = degrees * PI / HALF_TURN_DEGREES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dev.stapler.stelekit.sketch.v2.commands.Command
import dev.stapler.stelekit.sketch.v2.scene.SceneElement
import dev.stapler.stelekit.ui.sketch.DimensionField
import dev.stapler.stelekit.ui.sketch.lengthTextOf
import dev.stapler.stelekit.ui.sketch.v2.strings.SketchStrings
import dev.stapler.stelekit.units.Inches


Expand All @@ -47,9 +48,14 @@ fun ElementStatsPanel(
val stats = ElementStats.of(element) ?: return
Column(modifier.padding(8.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text("Stats", style = MaterialTheme.typography.titleSmall)
// Commands do not enforce the lock (ElementCommands), so the panel must: a locked element is shown, not edited.
if (element.base.locked) {
Text(SketchStrings.NonDrag.LOCKED, Modifier.testTag(StylePanelTags.STATS_PREFIX + "locked"), color = MaterialTheme.colorScheme.onSurfaceVariant)
return@Column
}
Row(horizontalArrangement = Arrangement.spacedBy(4.dp)) {
StatNumber("x", StatField.X, stats.x, element, onCommand)
StatNumber("y", StatField.Y, stats.y, element, onCommand)
StatNumber("x", StatField.X, stats.x, element, onCommand, caption = if (technical) "x (1/16\")" else "x")
StatNumber("y", StatField.Y, stats.y, element, onCommand, caption = if (technical) "y (1/16\")" else "y")
}
if (technical) {
DimensionField(
Expand Down Expand Up @@ -80,7 +86,7 @@ private fun sizeCommand(element: SceneElement, stats: ElementStats, widthUnits:
}

@Composable
private fun StatNumber(label: String, field: StatField, value: Double, element: SceneElement, onCommand: (Command) -> Unit) {
private fun StatNumber(label: String, field: StatField, value: Double, element: SceneElement, onCommand: (Command) -> Unit, caption: String = label) {
val shown = formatStat(value)
var text by remember(element.id, element.base.version, shown) { mutableStateOf(shown) }
var error by remember(element.id, element.base.version, shown) { mutableStateOf<String?>(null) }
Expand All @@ -93,7 +99,7 @@ private fun StatNumber(label: String, field: StatField, value: Double, element:
OutlinedTextField(
value = text,
onValueChange = { text = it; error = null },
label = { Text(label) },
label = { Text(caption) },
singleLine = true,
isError = error != null,
supportingText = { error?.let { Text(it, color = MaterialTheme.colorScheme.error, modifier = Modifier.testTag(StylePanelTags.STATS_PREFIX + label + ".error")) } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ private fun StyleTab(controller: SketchEditorController, extras: EditorExtras) {
SheetKindRow(controller, onChange = extras.dimension::cancel)
SheetStyleRow(controller)
StylePanelContent(controller)
// Position, size and angle of the one selected element; several elements have no single size to show.
controller.selectedElements().singleOrNull()?.let { element ->
ElementStatsPanel(
element = element,
technical = controller.sheetKind == dev.stapler.stelekit.sketch.v2.constraints.SheetKind.Technical,
onCommand = { controller.submit(it) },
modifier = Modifier.fillMaxWidth(),
)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions kmp/src/commonTest/kotlin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ kt_jvm_library(
"dev/stapler/stelekit/sketch/v2/ScenePainterPixelTest.kt",
"dev/stapler/stelekit/sketch/v2/ScenePainterFixture.kt",
"dev/stapler/stelekit/sketch/v2/ToolStripModelTest.kt",
"dev/stapler/stelekit/sketch/v2/ElementStatsGeometryTest.kt",
"dev/stapler/stelekit/sketch/v2/FreehandGolden.kt",
"dev/stapler/stelekit/sketch/v2/FreehandGoldenTest.kt",
"dev/stapler/stelekit/sketch/v2/DrawListJson.kt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2026 Tyler Stapler
// SPDX-License-Identifier: Elastic-2.0

package dev.stapler.stelekit.sketch.v2

import dev.stapler.stelekit.sketch.v2.W6Kit.circle
import dev.stapler.stelekit.sketch.v2.W6Kit.rect
import dev.stapler.stelekit.sketch.v2.W6Kit.run
import dev.stapler.stelekit.sketch.v2.W6Kit.step
import dev.stapler.stelekit.sketch.v2.commands.AddElements
import dev.stapler.stelekit.sketch.v2.commands.SketchDocumentV2
import dev.stapler.stelekit.sketch.v2.scene.EllipseElement
import dev.stapler.stelekit.sketch.v2.scene.ElementId
import dev.stapler.stelekit.sketch.v2.scene.RectElement
import dev.stapler.stelekit.ui.sketch.v2.ElementStats
import dev.stapler.stelekit.ui.sketch.v2.ElementStatsEdits
import dev.stapler.stelekit.ui.sketch.v2.StatField
import kotlin.math.PI
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs

/** The size numbers are the element's own box, so editing a rotated element's height cannot widen or move it. */
class ElementStatsGeometryTest {
private fun rotatedDoc(): SketchDocumentV2 {
val tilted = (rect("r1", 10, 20) as RectElement).copy(angle = PI / 4)
return run(SketchDocumentV2(), AddElements(listOf(tilted)))
}

@Test
fun aRotatedRectangle_should_ReportItsOwnBoxNotItsRotatedBounds() {
val r = rotatedDoc().scene[ElementId("r1")] as RectElement
val stats = checkNotNull(ElementStats.of(r))

assertEquals(r.width.value, stats.width)
assertEquals(r.height.value, stats.height)
assertEquals(r.x.value, stats.x)
assertEquals(45.0, stats.angleDegrees, 1e-9)
}

@Test
fun editingTheHeightOfARotatedRectangle_should_LeaveItsWidthAndCornerAlone() {
val d = rotatedDoc()
val before = d.scene[ElementId("r1")] as RectElement

val edit = assertIs<ElementStatsEdits.CommitResult.Edit>(ElementStatsEdits.commit(before, StatField.Height, "60"))
val after = step(d, edit.command).scene[ElementId("r1")] as RectElement

assertEquals(60.0, after.height.value)
assertEquals(before.width.value, after.width.value)
assertEquals(before.x.value, after.x.value)
assertEquals(before.y.value, after.y.value)
}

@Test
fun anEllipse_should_ReportItsDiameters() {
val d = run(SketchDocumentV2(), AddElements(listOf(circle("c", 50, 50))))
val e = d.scene[ElementId("c")] as EllipseElement
val stats = checkNotNull(ElementStats.of(e))

assertEquals(2 * e.rx.value, stats.width)
assertEquals(e.cx.value - e.rx.value, stats.x)
}
}
Loading