Skip to content

Commit

Permalink
Fixed undo and redo crashing app when no paths are visible
Browse files Browse the repository at this point in the history
  • Loading branch information
robothaver committed Jun 2, 2024
1 parent 1b40005 commit 4b97e2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.robothaver.kandraw.dialogs.penSettingsDialog.composables.Title
Expand All @@ -35,20 +36,28 @@ fun EraserSettingsDialog(
Title(text = "Eraser width: ${eraserWidth.value.roundToInt()}")
Slider(
value = eraserWidth.value,
onValueChange = {eraserWidth.value = it},
onValueChange = { eraserWidth.value = it },
valueRange = 10f..200f,
)
HorizontalDivider(
modifier = Modifier.padding(vertical = 12.dp),
color = MaterialTheme.colorScheme.surfaceVariant
)
Button(onClick = { canvasController.clearCanvas() },
Button(
onClick = { canvasController.clearCanvas() },
modifier = Modifier.fillMaxWidth()
) {
Row(verticalAlignment = Alignment.CenterVertically) {
Icon(Icons.Rounded.Delete, null)
Text(text = "Clear canvas", fontSize = 18.sp, fontWeight = FontWeight.Bold)
}
}
Text(
text = "Clear canvas is experimental and it could break the app!",
fontSize = 10.sp,
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.error
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,14 @@ class CanvasController(
}
} else if (undoPaths.last().action == Actions.Erase) {
allPaths.add(undoPaths.last().index, undoPaths.last())
visiblePaths.add(undoPaths.last().index, undoPaths.last())
if (visiblePaths.isNotEmpty()) {
visiblePaths.add(undoPaths.last().index, undoPaths.last())
}
} else {
allPaths.removeLast()
visiblePaths.removeLast()
if (visiblePaths.isNotEmpty()) {
visiblePaths.removeLast()
}
}
undoPaths.removeLast()
}
Expand All @@ -174,7 +178,9 @@ class CanvasController(

} else if (redoPaths.last().action == Actions.Erase) {
allPaths.removeAt(redoPaths.last().index)
visiblePaths.removeAt(redoPaths.last().index)
if (visiblePaths.isNotEmpty() || redoPaths.last().index != visiblePaths.lastIndex) {
visiblePaths.removeAt(redoPaths.last().index)
}
} else {
allPaths.add(redoPaths.last())
visiblePaths.add(redoPaths.last())
Expand Down

0 comments on commit 4b97e2a

Please sign in to comment.