Modern Apps git

← Modern-Apps.git

photos: keep the active layer selected when reordering layers

Commit
9f8607f77156a38ecc61a248975038bb1786181b
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:17 UTC (28 days ago)
Parents
05b6fca
EditDocument.moveLayer remapped the active-layer index only when the
moved layer itself was active (active == from -> target). Moving any
other layer across the active one silently re-targeted the selection:
the index stayed put while the layer at that index changed, so the
Layers panel and subsequent edits acted on the wrong layer. Repro:
[A,B,C,D] with C active, move A after D gives [B,C,D,A] but the old
code kept index 2, selecting D instead of C.

The fix shifts the active index along with the move: the moved layer
stays selected at its destination, an active layer inside the shifted
range follows its layer by +-1, and anything outside the range is
untouched. The out-of-bounds guards are unchanged.

Verified with a new moveLayerKeepsActiveLayerSelected regression test
(forward move, backward move, and untouched-active cases); targeted
:photos:testDevUnitTest run green.

Diff

photos/src/main/java/com/vayunmathur/photos/data/EditDocument.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/data/EditDocument.kt b/photos/src/main/java/com/vayunmathur/photos/data/EditDocument.ktindex f4d449a15f..ec1693a9fd 100644--- a/photos/src/main/java/com/vayunmathur/photos/data/EditDocument.kt+++ b/photos/src/main/java/com/vayunmathur/photos/data/EditDocument.kt@@ -53,8 +53,10 @@ data class EditDocument(         val newLayers = layers.toMutableList()         val item = newLayers.removeAt(from)         newLayers.add(target, item)-        val newActive = when (activeLayerIndex) {-            from -> target+        val newActive = when {+            activeLayerIndex == from -> target+            from < target && activeLayerIndex in (from + 1)..target -> activeLayerIndex - 1+            from > target && activeLayerIndex in target until from -> activeLayerIndex + 1             else -> activeLayerIndex         }         return copy(layers = newLayers, activeLayerIndex = clampIndexFor(newLayers, newActive))
photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.ktindex 2f2d568fad..a8902f5b46 100644--- a/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt+++ b/photos/src/test/java/com/vayunmathur/photos/photoslice2/PhotoSlice2LayersTest.kt@@ -85,6 +85,28 @@ class PhotoSlice2LayersTest {         assertEquals(doc, doc.moveLayer(99, 0))     } +    @Test+    fun moveLayerKeepsActiveLayerSelected() {+        var doc = EditDocument(canvasWidth = 8, canvasHeight = 8)+        doc = doc.addLayer(pixelLayer("A")).addLayer(pixelLayer("B"))+            .addLayer(pixelLayer("C")).addLayer(pixelLayer("D"))+        // Active layer below the moved range shifts down with it.+        doc = doc.setActiveLayer(2).moveLayer(0, 3)+        assertEquals(listOf("B", "C", "D", "A"), doc.layers.map { it.name })+        assertEquals("C", doc.activeLayer?.name)+        assertEquals(1, doc.activeLayerIndex)+        // Active layer above the moved range shifts up with it.+        doc = doc.setActiveLayer(1).moveLayer(3, 0)+        assertEquals(listOf("A", "B", "C", "D"), doc.layers.map { it.name })+        assertEquals("C", doc.activeLayer?.name)+        assertEquals(2, doc.activeLayerIndex)+        // Active layer outside the moved range stays put.+        doc = doc.setActiveLayer(3).moveLayer(0, 1)+        assertEquals(listOf("B", "A", "C", "D"), doc.layers.map { it.name })+        assertEquals("D", doc.activeLayer?.name)+        assertEquals(3, doc.activeLayerIndex)+    }+     @Test     fun updateLayerAndCopyBase() {         var doc = EditDocument(canvasWidth = 8, canvasHeight = 8).addLayer(pixelLayer("A"))