Modern Apps git

← Modern-Apps.git

photos: undo button

Commit
37d175b4d71e3e7db72c32ee2a7a22e790862af0
Author
Vayun Mathur <[email protected]>
When
2026-04-14 03:54 UTC (7 months ago)
Parents
0283c09

Diff

library/src/main/java/com/vayunmathur/library/ui/Icons.kt
diff --git a/library/src/main/java/com/vayunmathur/library/ui/Icons.kt b/library/src/main/java/com/vayunmathur/library/ui/Icons.ktindex b22c6968fb..4a5d787a47 100644--- a/library/src/main/java/com/vayunmathur/library/ui/Icons.kt+++ b/library/src/main/java/com/vayunmathur/library/ui/Icons.kt@@ -142,3 +142,8 @@ fun IconArchive(tint: Color = LocalContentColor.current) { fun IconChevronRight(tint: Color = LocalContentColor.current) {     Icon(painterResource(R.drawable.chevron_right_24px), "Chevron", tint = tint) }++@Composable+fun IconUndo(tint: Color = LocalContentColor.current) {+    Icon(painterResource(R.drawable.undo_24px), "Undo", tint = tint)+}
library/src/main/res/drawable/undo_24px.xml
diff --git a/library/src/main/res/drawable/undo_24px.xml b/library/src/main/res/drawable/undo_24px.xmlnew file mode 100644index 0000000000..9afea49b77--- /dev/null+++ b/library/src/main/res/drawable/undo_24px.xml@@ -0,0 +1,10 @@+<vector xmlns:android="http://schemas.android.com/apk/res/android"+    android:autoMirrored="true"+    android:height="24dp"+    android:viewportHeight="960"+    android:viewportWidth="960"+    android:width="24dp">+    <path+        android:fillColor="#FF000000"+        android:pathData="M280,680L80,480L280,280L337,337L233,440H520Q620,440 690,510T760,680V800H680V680Q680,614 633,567T520,520H233L337,623L280,680Z" />+</vector>
photos/src/main/java/com/vayunmathur/photos/ui/EditPhotoPage.kt
diff --git a/photos/src/main/java/com/vayunmathur/photos/ui/EditPhotoPage.kt b/photos/src/main/java/com/vayunmathur/photos/ui/EditPhotoPage.ktindex 7c0e5c586f..b6ba084e8f 100644--- a/photos/src/main/java/com/vayunmathur/photos/ui/EditPhotoPage.kt+++ b/photos/src/main/java/com/vayunmathur/photos/ui/EditPhotoPage.kt@@ -25,12 +25,13 @@ import androidx.compose.foundation.shape.CircleShape import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.ExperimentalMaterial3Api+import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable-import androidx.compose.runtime.getValue+import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember@@ -57,6 +58,7 @@ import androidx.core.net.toUri import androidx.compose.foundation.Image import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.graphics.asImageBitmap+import androidx.compose.runtime.getValue import com.vayunmathur.library.ui.IconCheck import com.vayunmathur.library.ui.IconClose import com.vayunmathur.library.ui.IconCrop@@ -64,6 +66,7 @@ import com.vayunmathur.library.ui.IconNavigation import com.vayunmathur.library.ui.IconRotateLeft import com.vayunmathur.library.ui.IconRotateRight import com.vayunmathur.library.ui.IconSave+import com.vayunmathur.library.ui.IconUndo import com.vayunmathur.library.util.DatabaseViewModel import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.photos.R@@ -86,8 +89,24 @@ fun EditPhotoPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel,     var isCropping by remember { mutableStateOf(false) }     var rotation by remember { mutableFloatStateOf(0f) }     var cropRect by remember { mutableStateOf(Rect(0f, 0f, 1f, 1f)) }+    var startCropRect by remember { mutableStateOf(Rect(0f, 0f, 1f, 1f)) }     var showSaveMenu by remember { mutableStateOf(false) } +    data class EditState(val rotation: Float, val cropRect: Rect)+    val history = remember { mutableStateListOf<EditState>() }++    fun pushState() {+        history.add(EditState(rotation, cropRect))+    }++    fun undo() {+        if (history.isNotEmpty()) {+            val lastState = history.removeAt(history.size - 1)+            rotation = lastState.rotation+            cropRect = lastState.cropRect+        }+    }+     var originalBitmap by remember { mutableStateOf<Bitmap?>(null) }     var transformedBitmap by remember { mutableStateOf<Bitmap?>(null) } @@ -125,7 +144,7 @@ fun EditPhotoPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel,         }     } -    LaunchedEffect(originalBitmap, rotation, isCropping) {+    LaunchedEffect(originalBitmap, rotation, isCropping, if (isCropping) Unit else cropRect) {         val original = originalBitmap ?: return@LaunchedEffect         withContext(Dispatchers.IO) {             val matrix = Matrix()@@ -160,23 +179,43 @@ fun EditPhotoPage(backStack: NavBackStack<Route>, viewModel: DatabaseViewModel,                 },                 actions = {                     if (isCropping) {-                        IconButton(onClick = { isCropping = false }) {+                        IconButton(onClick = {+                            if (cropRect != startCropRect) {+                                history.add(EditState(rotation, startCropRect))+                            }+                            isCropping = false+                        }) {                             IconCheck()                         }                         IconButton(onClick = {-                            cropRect = Rect(0f, 0f, 1f, 1f)+                            cropRect = startCropRect                             isCropping = false                         }) {                             IconClose()                         }                     } else {-                        IconButton(onClick = { isCropping = true }) {+                        IconButton(+                            onClick = { undo() },+                            enabled = history.isNotEmpty()+                        ) {+                            IconUndo()+                        }+                        IconButton(onClick = {+                            startCropRect = cropRect+                            isCropping = true+                        }) {                             IconCrop()                         }-                        IconButton(onClick = { rotation -= 90f }) {+                        IconButton(onClick = {+                            pushState()+                            rotation -= 90f+                        }) {                             IconRotateLeft()                         }-                        IconButton(onClick = { rotation += 90f }) {+                        IconButton(onClick = {+                            pushState()+                            rotation += 90f+                        }) {                             IconRotateRight()                         }                         Box {
photos/src/main/res/values/strings.xml
diff --git a/photos/src/main/res/values/strings.xml b/photos/src/main/res/values/strings.xmlindex e49c2aac70..fb941f21c1 100644--- a/photos/src/main/res/values/strings.xml+++ b/photos/src/main/res/values/strings.xml@@ -3,6 +3,7 @@     <string name="title_edit_photo">Edit Photo</string>     <string name="action_save">Save</string>     <string name="action_save_as_copy">Save as Copy</string>+    <string name="action_undo">Undo</string>     <string name="label_gallery">Gallery</string>     <string name="label_map">Map</string>     <string name="grant_image_video_permissions">Grant Image/Video Permissions</string>