Modern Apps git

← Modern-Apps.git

photos: an empty selection no longer lets 'Delete area' erase the whole layer

Commit
2b13c0c9eb1d9db62f1bcf86ec98433df10af23f
Author
Vayun Mathur <[email protected]>
When
2026-12-01 00:52 UTC (5 days ago)
Parents
a9cf650
Selecting everything and then subtracting it (or inverting a
full selection) leaves a non-null but empty Selection, and
the panel enables selection-scoped edits for any non-null
selection. PixelEditController's blend branch required a
non-empty selection to blend back inside it, so for an empty
selection it fell through to the transform's raw output โ€”
the blank bitmap intended only as the blend source โ€”
replacing the entire layer. Applied through the editor's
Save overwrite, that blanked layer could be written over the
original photo.

No-selection (whole layer) and empty-selection (nothing)
were conflated; applyToActivePixelLayer now returns
immediately for a present-but-empty selection: no transform,
no document update, no undo entry. That covers delete area,
healing, red-eye, liquify, flood fill and shapes, which all
funnel through it. New regression test builds the
Subtract-emptied selection and asserts reference, pixels and
undo all unchanged (failed pre-fix). Gallery ordering, album
moves, trash restore and share GPS-stripping were audited and
cleared. Verified: native suite 413/413; the one JVM failure
is a pre-existing sync/favourites test unrelated to this
diff, and lint errors are pre-existing on untouched files.

Diff

photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt b/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.ktindex 5bba735f18..48d9cbbc8a 100644--- a/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt+++ b/photos/src/main/java/com/vayunmathur/photos/platform/PixelEditController.kt@@ -23,12 +23,19 @@ class PixelEditController(private val vm: PhotoEditViewModel) {     /**      * Applies [transform] to the active pixel layer's bitmap on a background thread.      * If a [selection] is present, the result is constrained to the selected area.+     * A present-but-empty selection (e.g. a full selection combined with+     * itself under Subtract, or inverted) constrains the edit to nothing, so+     * it is a no-op: falling through to the unblended [transform] result+     * would apply it to the whole layer โ€” for the Delete-area transform,+     * whose result is a blank bitmap meant only to be blended back inside+     * the selection, that erases the entire layer.      */     fun applyToActivePixelLayer(transform: (Bitmap) -> Bitmap) {         val doc = vm.document.value         val index = doc.activeLayerIndex         val layer = doc.layers.getOrNull(index) as? PixelLayer ?: return         val sel = vm.selection.value+        if (sel != null && sel.isEmpty()) return         vm.editScope.launch(Dispatchers.Default) {             val src = layer.bitmapRef.bitmap             val edited = transform(src)
photos/src/test/java/com/vayunmathur/photos/util/PhotosCovC2PixelTest.kt
diff --git a/photos/src/test/java/com/vayunmathur/photos/util/PhotosCovC2PixelTest.kt b/photos/src/test/java/com/vayunmathur/photos/util/PhotosCovC2PixelTest.ktindex 758fd88396..f2f8f824a3 100644--- a/photos/src/test/java/com/vayunmathur/photos/util/PhotosCovC2PixelTest.kt+++ b/photos/src/test/java/com/vayunmathur/photos/util/PhotosCovC2PixelTest.kt@@ -13,8 +13,10 @@ import com.vayunmathur.photos.data.PerspectiveCorners import com.vayunmathur.photos.data.PhotosRepository import com.vayunmathur.photos.data.PixelLayer import com.vayunmathur.photos.data.Selection+import com.vayunmathur.photos.data.SelectionCombine import kotlin.test.Test import kotlin.test.assertEquals+import kotlin.test.assertFalse import kotlin.test.assertTrue import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner@@ -111,6 +113,38 @@ class PhotosCovC2PixelTest {         awaitVm { activeBitmap(vm).getPixel(0, 0) == Color.GREEN }     } +    @Test fun emptySelectionDeleteTransformLeavesLayerUntouched() {+        val vm = vmWithLayer()+        // Exact UI state: a full selection combined with itself under+        // Subtract (or an inverted full selection) is non-null but empty.+        // SelectionPanel enables "Delete area" for any non-null selection,+        // and its transform returns a blank bitmap meant only to be blended+        // back inside the selection.+        val full = Selection.rectangle(16, 16, 0f, 0f, 1f, 1f)+        val emptied = full.combine(full, SelectionCombine.Subtract)+        assertTrue(emptied.isEmpty())+        vm.setSelection(emptied)+        val before = (vm.document.value.activeLayer as PixelLayer).bitmapRef++        vm.pixel.applyToActivePixelLayer {+            Bitmap.createBitmap(16, 16, Bitmap.Config.ARGB_8888)+        }+        // Give the controller's background launch ample time to land (it+        // must not): pump the main looper while wall time passes.+        val deadline = System.currentTimeMillis() + 1_500+        while (System.currentTimeMillis() < deadline) {+            org.robolectric.Shadows.shadowOf(android.os.Looper.getMainLooper())+                .idleFor(100, java.util.concurrent.TimeUnit.MILLISECONDS)+            Thread.sleep(50)+        }++        val after = (vm.document.value.activeLayer as PixelLayer).bitmapRef+        assertEquals(before.id, after.id)+        assertEquals(Color.RED, activeBitmap(vm).getPixel(0, 0))+        assertEquals(Color.BLUE, activeBitmap(vm).getPixel(15, 0))+        assertFalse(vm.canUndo.value)+    }+     @Test fun applyWithSelectionBlendsAndContentAwareFill() {         val vm = vmWithLayer()         // Left half selected: edits apply there, right half keeps the original.