ProCraft for Android: An Android native painting app written in Rust
Rust
feat(text): undo and redo of a finished text go back to editing it
Josh92b9cd194bbf
diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.ktindex 06d6eaf..b032925 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/canvas/CanvasViewModel.kt@@ -2681,7 +2681,20 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {startText(info)}- private fun startText(info: TextInfo) {+ /** Text layer [id] back in its box, selected but with the keyboard away (undo and redo of it). */+ private fun resumeText(id: ULong): Boolean {+ val s = session ?: return false+ val info = s.layerText(id) ?: return false+ s.textBegin(id, info) ?: return false+ startText(info, keyboard = false)+ return true+ }++ /** Whether the keyboard is wanted for the text being edited (a touch on the text asks for it). */+ var textKeyboard by mutableStateOf(true)++ private fun startText(info: TextInfo, keyboard: Boolean = true) {+ textKeyboard = keyboardtextInfo = infotextField = TextFieldValue(info.text, TextRange(0, info.text.length))textStyleOpen = false@@ -3853,6 +3866,8 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {fun undo() {feedback()+ // a finished text's step goes back to editing that text first, as a step of its own+ if (textInfo == null && !transforming) session?.textUndoLayer()?.let { if (resumeText(it)) return }// undoing while editing text undoes the whole editif (textInfo != null) {textInfo = null@@ -3900,9 +3915,12 @@ class CanvasViewModel(app: Application) : AndroidViewModel(app) {puckAfter.removeLastOrNull()?.let { after -> clonePuck?.let { puckBefore.addLast(it) }; clonePuck = after }}val could = canRedo+ // a text edit redone is edited again+ val text = if (textInfo == null) session?.textRedoLayer() else nullval name = session?.redo()if (could) showNotice("Redo" + name?.let { " $it" }.orEmpty())refresh()+ text?.let { resumeText(it) }}// ---- adjustments ----diff --git a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/TextUi.kt b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/TextUi.ktindex 3b1a695..d68c621 100644--- a/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/TextUi.kt+++ b/apps/procraft-android/android/app/src/main/java/io/github/artcraftandroid/procraft/ui/TextUi.kt@@ -110,6 +110,7 @@ fun BoxScope.TextEditor(vm: CanvasViewModel) {return@awaitEachGesture}// a touch on the text brings the keyboard back if it was put away+ vm.textKeyboard = truefocus.requestFocus()keyboard?.show()var last = down.position@@ -124,8 +125,8 @@ fun BoxScope.TextEditor(vm: CanvasViewModel) {})TextBox(vm, info)BasicTextField(vm.textField, vm::changeTextField, Modifier.size(1.dp).alpha(0f).focusRequester(focus))- LaunchedEffect(vm.textStyleOpen) {- if (vm.textStyleOpen) keyboard?.hide() else {+ LaunchedEffect(vm.textStyleOpen, vm.textKeyboard) {+ if (vm.textStyleOpen || !vm.textKeyboard) keyboard?.hide() else {focus.requestFocus()keyboard?.show()}diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rsindex a64896c..21154d2 100644--- a/apps/procraft-android/src/lib.rs+++ b/apps/procraft-android/src/lib.rs@@ -258,6 +258,9 @@ pub(crate) struct State {crop_redo: Option<(Canvas, Document, u64)>,/// The text layer being edited, and the document as the edit began.text_edit: Option<(procraft_engine::document::LayerId, Document)>,+ /// The steps finished text edits made (by [`Canvas::undo_step`]'s numbering) and their layers:+ /// undo and redo of one go back to editing that text.+ pub(crate) text_steps: Vec<(u64, procraft_engine::document::LayerId)>,/// The library as the user has it, and as it ships.library: Vec<brush_library::LibrarySet>,shipped: Vec<BrushSet>,@@ -683,6 +686,7 @@ impl CanvasSession {reconfigure: false,tool: ToolKind::Paint,text_edit: None,+ text_steps: Vec::new(),crop_undo: None,selection_visibility: 0.25,brush_on_screen: false,diff --git a/apps/procraft-android/src/tests.rs b/apps/procraft-android/src/tests.rsindex f125f54..f2a68c4 100644--- a/apps/procraft-android/src/tests.rs+++ b/apps/procraft-android/src/tests.rs@@ -1334,8 +1334,16 @@ fn text_is_edited_as_one_step_and_rasterized() {s.stroke_begin(PointerKind::Finger, vec![sample(5.0, 5.0, 0.0)]);s.stroke_end(vec![]);assert_eq!(pixel(&s, 5, 5), [255, 0, 0, 255]);- // one undo takes the new text layer away; redo brings it back+ // the step undo would undo next is that text's, so the app edits it again first+ assert_eq!(s.text_undo_layer(), Some(id));+ // edited again without a change, ending it makes no step: the text step is still next+ assert_eq!(s.text_begin(Some(id), s.layer_text(id).unwrap()), Some(id));+ assert_eq!(s.text_undo_layer(), None);+ s.text_end(true);+ assert_eq!(s.text_undo_layer(), Some(id));+ // one undo takes the new text layer away; redo brings it back, to edit agains.undo();+ assert_eq!(s.text_redo_layer(), Some(id));assert_eq!((s.layers().len(), pixel(&s, 5, 5)), (1, [255, 255, 255, 255]));s.redo();assert_eq!(s.layer_text(id).unwrap().text, "Hello");diff --git a/apps/procraft-android/src/text.rs b/apps/procraft-android/src/text.rsindex 0ce5ce8..0d1bcb7 100644--- a/apps/procraft-android/src/text.rs+++ b/apps/procraft-android/src/text.rs@@ -105,6 +105,9 @@ impl From<TextInfo> for LayerText {}}+/// How many finished text edits are remembered for undo and redo to edit again.+const TEXT_STEPS_KEPT: usize = 256;+#[uniffi::export]impl CanvasSession {/// Starts editing text: a new text layer above the current one with `info` (`id` none), or@@ -160,24 +163,55 @@ impl CanvasSession {}/// Ends editing text: `keep` makes the edit one undo step; otherwise the layer goes back to- /// how it was (a new one goes away). New text left empty goes away too.+ /// how it was (a new one goes away). New text left empty goes away too, and an edit that+ /// changed nothing makes no step.pub fn text_end(&self, keep: bool) {let mut guard = self.shared.lock();let st = &mut *guard;let Some((layer, before)) = st.text_edit.take() else { return };let new = before.layer(layer).is_none();let empty = st.doc.layer(layer).and_then(|l| l.text.as_ref()).is_none_or(|t| t.text.trim().is_empty());- let keep = keep && !(new && empty);+ let unchanged = !new && before.layer(layer).map(|l| &l.text) == st.doc.layer(layer).map(|l| &l.text);+ let keep = keep && !(new && empty) && !unchanged;if let Some(canvas) = st.canvas.as_mut() {canvas.release_layer(keep, keep.then(|| before.clone()));}if !keep {st.doc = before;}+ if keep {+ // the step made, so undo and redo of it can go back to editing this text+ st.flush();+ if let Some(step) = st.canvas.as_ref().and_then(procraft_gpu::Canvas::undo_step) {+ st.text_steps.push((step, layer));+ let over = st.text_steps.len().saturating_sub(TEXT_STEPS_KEPT);+ st.text_steps.drain(..over);+ }+ }st.revision += 1;self.shared.frame(st);}+ /// The text layer undo would take back to editing first: the next step undone is a finished+ /// text edit, its layer still text. None while text is being edited.+ pub fn text_undo_layer(&self) -> Option<u64> {+ let st = self.shared.lock();+ if st.text_edit.is_some() {+ return None;+ }+ let step = st.canvas.as_ref()?.undo_step()?;+ let layer = st.text_steps.iter().rev().find(|(s, _)| *s == step)?.1;+ st.doc.layer(layer).filter(|l| l.text.is_some() && !l.locked).map(|_| layer.0)+ }++ /// The text layer the next redo brings back, when that step is a finished text edit: after+ /// the redo it is edited again.+ pub fn text_redo_layer(&self) -> Option<u64> {+ let st = self.shared.lock();+ let step = st.canvas.as_ref()?.redo_step()?;+ st.text_steps.iter().rev().find(|(s, _)| *s == step).map(|(_, l)| l.0)+ }+/// The text layer being edited, if any.pub fn editing_text(&self) -> Option<u64> {self.shared.lock().text_edit.as_ref().map(|(l, _)| l.0)diff --git a/crates/gpu/src/canvas.rs b/crates/gpu/src/canvas.rsindex 6e8710b..e79c2c3 100644--- a/crates/gpu/src/canvas.rs+++ b/crates/gpu/src/canvas.rs@@ -1472,6 +1472,16 @@ impl Canvas {self.redo.last().and_then(|s| s.name.clone())}+ /// Which step undo would undo next, and redo would redo, numbered as [`Canvas::steps_made`]+ /// counted them when they were made.+ pub fn undo_step(&self) -> Option<u64> {+ self.undo.last().map(|s| s.made)+ }++ pub fn redo_step(&self) -> Option<u64> {+ self.redo.last().map(|s| s.made)+ }+/// Marks where the history is once the queued work is done, for [`Canvas::collapse_steps`].pub fn mark_steps(&mut self) {self.end_stroke();