Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
blitz-dom's text editing: IME, keys and undo
shinyobjectz8c52f63c4e74
diff --git a/vendor/blitz-dom/src/events/ime.rs b/vendor/blitz-dom/src/events/ime.rsindex 2202fc76..759bf6e3 100644--- a/vendor/blitz-dom/src/events/ime.rs+++ b/vendor/blitz-dom/src/events/ime.rs@@ -22,11 +22,21 @@ pub(crate) fn handle_ime_event<F: FnMut(DomEvent)>(.downcast_element_mut().and_then(|el| el.text_input_data_mut());if let Some(input_data) = text_input_data {+ // raea: the field before a composition began, so its commit is one step from there (node/undo.rs)+ let commit = matches!(event, BlitzImeEvent::Commit(_));+ if !input_data.editor.is_composing() && input_data.history.composing.is_none() {+ input_data.history.composing = Some(input_data.snap());+ }let generated_event = input_data.apply_ime_event(&mut doc.font_ctx.lock().unwrap(),&mut doc.layout_ctx,event,);+ if commit || !input_data.editor.is_composing() {+ if let Some(before) = input_data.history.composing.take() {+ input_data.note_change(before, Some(crate::node::undo::Run::Typing));+ }+ }if let Some(generated_event) = generated_event {doc.apply_generated_text_input_event(node_id, generated_event, dispatch_event);diff --git a/vendor/blitz-dom/src/events/keyboard.rs b/vendor/blitz-dom/src/events/keyboard.rsindex 6e7dbbd0..e5cba8d8 100644--- a/vendor/blitz-dom/src/events/keyboard.rs+++ b/vendor/blitz-dom/src/events/keyboard.rs@@ -84,6 +84,17 @@ pub(crate) fn handle_key_or_input_event<F: FnMut(DomEvent)>(};if let Some(input_data) = element_data.text_input_data_mut() {+ // raea: Cmd+Z and Cmd+Shift+Z (Ctrl+Z and Ctrl+Y elsewhere) step the field's history (node/undo.rs)+ if let Some(redo) = history_step(&event) {+ let generated =+ input_data.step_history(&mut doc.font_ctx.lock().unwrap(), &mut doc.layout_ctx, redo);+ if let Some(generated) = generated {+ doc.apply_generated_text_input_event(node_id, generated, dispatch_event);+ }+ return;+ }+ let before = input_data.snap();+ let run = run_of(&event);let generated_event = match event {KeyboardOrTextInputEvent::KeyPress(blitz_key_event) => input_data.apply_keypress_event(@@ -100,6 +111,7 @@ pub(crate) fn handle_key_or_input_event<F: FnMut(DomEvent)>(&command,),};+ input_data.note_change(before, run);if let Some(generated_event) = generated_event {doc.apply_generated_text_input_event(node_id, generated_event, dispatch_event);@@ -108,6 +120,35 @@ pub(crate) fn handle_key_or_input_event<F: FnMut(DomEvent)>(}}+/// raea: Some(redo?) for the keys that step a field's history: Cmd+Z, Cmd+Shift+Z and Cmd+Y (Ctrl elsewhere), and+/// the Mac's undo: and redo: commands+fn history_step(event: &KeyboardOrTextInputEvent) -> Option<bool> {+ match event {+ KeyboardOrTextInputEvent::KeyPress(e) if e.state.is_pressed() && e.modifiers.contains(ACTION_MOD) => match &e.key {+ Key::Character(c) if c.eq_ignore_ascii_case("z") => Some(e.modifiers.contains(Modifiers::SHIFT)),+ Key::Character(c) if c.eq_ignore_ascii_case("y") => Some(true),+ _ => None,+ },+ KeyboardOrTextInputEvent::AppleStandardKeyBinding(c) if c.as_str() == "undo:" => Some(false),+ KeyboardOrTextInputEvent::AppleStandardKeyBinding(c) if c.as_str() == "redo:" => Some(true),+ _ => None,+ }+}++/// raea: what a key does to the words, for grouping steps: typing or deleting runs join; anything else stands alone+fn run_of(event: &KeyboardOrTextInputEvent) -> Option<crate::node::undo::Run> {+ use crate::node::undo::Run;+ match event {+ KeyboardOrTextInputEvent::KeyPress(e) if !e.modifiers.contains(ACTION_MOD) => match &e.key {+ Key::Character(c) if c.as_str() != "\n" => Some(Run::Typing),+ Key::Named(NamedKey::Backspace | NamedKey::Delete) => Some(Run::Deleting),+ _ => None,+ },+ KeyboardOrTextInputEvent::AppleStandardKeyBinding(c) if c.as_str().starts_with("delete") => Some(Run::Deleting),+ _ => None,+ }+}+impl BaseDocument {pub(crate) fn apply_generated_text_input_event<F: FnMut(DomEvent)>(&mut self,diff --git a/vendor/blitz-dom/src/node/mod.rs b/vendor/blitz-dom/src/node/mod.rsindex b02242f5..da33d917 100644--- a/vendor/blitz-dom/src/node/mod.rs+++ b/vendor/blitz-dom/src/node/mod.rs@@ -11,6 +11,8 @@ mod stylo_data;#[cfg(feature = "svg")]mod svg;mod text;+/// raea: a text field's history, for Cmd+Z (undo.rs)+pub(crate) mod undo;pub use attributes::{Attribute, Attributes};#[cfg(feature = "custom-widget")]diff --git a/vendor/blitz-dom/src/node/text.rs b/vendor/blitz-dom/src/node/text.rsindex c9411e27..d40b0541 100644--- a/vendor/blitz-dom/src/node/text.rs+++ b/vendor/blitz-dom/src/node/text.rs@@ -84,6 +84,8 @@ pub struct TextInputData {pub placeholder: Option<Box<parley::PlainEditor<TextBrush>>>,/// raea: a password field's text as drawn, a bullet a character (Blitz drew a password as typed)pub mask: Option<Box<parley::PlainEditor<TextBrush>>>,+ /// raea: what Cmd+Z puts back (undo.rs)+ pub history: super::undo::History,}// FIXME: Implement Clone for PlainEditor@@ -102,9 +104,42 @@ impl TextInputData {scroll_offset: 0.0,placeholder: None,mask: None,+ history: super::undo::History::default(),}}+ /// raea: the field as it is now, for its history+ pub fn snap(&self) -> super::undo::Snap {+ let sel = self.editor.raw_selection();+ super::undo::Snap { text: self.editor.raw_text().to_string(), anchor: sel.anchor().index(), focus: sel.focus().index() }+ }++ /// raea: a change was made (`before` is the field before it): kept for Cmd+Z when the words changed+ pub fn note_change(&mut self, before: super::undo::Snap, run: Option<super::undo::Run>) {+ if self.editor.raw_text() != before.text {+ let after = self.editor.raw_selection().focus().index();+ self.history.record(before, after, run);+ }+ }++ /// raea: Cmd+Z (or with `redo`, Cmd+Shift+Z): the field as it was before its last step, caret and selection too;+ /// None when there's nothing to go back to+ pub fn step_history(+ &mut self,+ font_ctx: &mut FontContext,+ layout_ctx: &mut LayoutContext<TextBrush>,+ redo: bool,+ ) -> Option<GeneratedTextInputEvent> {+ let now = self.snap();+ let to = if redo { self.history.redo(now) } else { self.history.undo(now) }?;+ self.editor.set_text(&to.text);+ let mut driver = self.editor.driver(font_ctx, layout_ctx);+ driver.refresh_layout();+ driver.select_byte_range(to.anchor, to.focus);+ self.sync_mask(font_ctx, layout_ctx);+ Some(GeneratedTextInputEvent::Input)+ }+/// raea: a password field's bullets made again for its text, when they've changedpub fn sync_mask(&mut self, font_ctx: &mut FontContext, layout_ctx: &mut LayoutContext<TextBrush>) {let Some(mask) = self.mask.as_mut() else { return };@@ -122,6 +157,8 @@ impl TextInputData {text: &str,) {if self.editor.text() != text {+ // raea: a value a script sets is a new start (undo.rs)+ self.history.clear();self.editor.set_text(text);// raea: a value set by script puts the caret at its end, as Chrome's value setter does (a combobox's// chosen option, written into its field)diff --git a/vendor/blitz-dom/src/node/undo.rs b/vendor/blitz-dom/src/node/undo.rsnew file mode 100644index 00000000..b27627af--- /dev/null+++ b/vendor/blitz-dom/src/node/undo.rs@@ -0,0 +1,136 @@+//! raea: a text field's history, for Cmd+Z and Cmd+Shift+Z (Ctrl+Z and Ctrl+Y elsewhere). Blitz kept none, so undo in+//! any box of raea's window did nothing (a tester, 11-16). Before each change the field's text and selection are kept;+//! a run of typing, or of deleting, with the caret where the last change left it, is one step, as the Mac's own fields+//! group it, and anything else (a paste, a cut, a new line, the caret moved first) starts the next. A value a script+//! sets is a new start: the history goes, as Chrome drops it.++/// The field as it was: its words and its selection, as byte offsets.+#[derive(Clone, Debug, PartialEq, Eq)]+pub struct Snap {+ pub text: String,+ pub anchor: usize,+ pub focus: usize,+}++/// What a change was, for grouping: a run of typing or of deleting is one step.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum Run {+ Typing,+ Deleting,+}++/// Steps kept: enough for any box in the app, small enough to never matter.+const KEEP: usize = 200;++#[derive(Default, Debug)]+pub struct History {+ undo: Vec<Snap>,+ redo: Vec<Snap>,+ /// the run the last change was in, and where it left the caret+ run: Option<(Run, usize)>,+ /// the field as it was when a composition (an input method's marked text) began: its commit is one step from there+ pub composing: Option<Snap>,+}++impl History {+ /// A change happened: `before` is the field before it, `after` where the caret is now, `run` what kind of change.+ pub fn record(&mut self, before: Snap, after: usize, run: Option<Run>) {+ let joins = matches!((run, self.run), (Some(r), Some((last, at))) if r == last && at == before.focus);+ if !joins {+ self.undo.push(before);+ if self.undo.len() > KEEP {+ self.undo.remove(0);+ }+ }+ self.redo.clear();+ self.run = run.map(|r| (r, after));+ }++ /// Cmd+Z: the field before its last step, given the field now (kept for Cmd+Shift+Z).+ pub fn undo(&mut self, now: Snap) -> Option<Snap> {+ let to = self.undo.pop()?;+ self.redo.push(now);+ self.run = None;+ Some(to)+ }++ /// Cmd+Shift+Z: the step Cmd+Z last took back.+ pub fn redo(&mut self, now: Snap) -> Option<Snap> {+ let to = self.redo.pop()?;+ self.undo.push(now);+ self.run = None;+ Some(to)+ }++ /// A value set by script: nothing before it can be put back.+ pub fn clear(&mut self) {+ *self = Self::default();+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn snap(text: &str) -> Snap {+ Snap { text: text.into(), anchor: text.len(), focus: text.len() }+ }++ #[test]+ fn a_run_of_typing_is_one_step() {+ let mut h = History::default();+ h.record(snap(""), 1, Some(Run::Typing));+ h.record(snap("h"), 2, Some(Run::Typing));+ h.record(snap("hi"), 3, Some(Run::Typing));+ assert_eq!(h.undo(snap("hi!")), Some(snap("")));+ assert_eq!(h.undo(snap("")), None);+ }++ #[test]+ fn deleting_after_typing_is_its_own_step() {+ let mut h = History::default();+ h.record(snap(""), 1, Some(Run::Typing));+ h.record(snap("a"), 2, Some(Run::Typing));+ h.record(snap("ab"), 1, Some(Run::Deleting));+ assert_eq!(h.undo(snap("a")), Some(snap("ab")));+ assert_eq!(h.undo(snap("ab")), Some(snap("")));+ }++ #[test]+ fn typing_somewhere_else_starts_a_new_step() {+ let mut h = History::default();+ h.record(snap(""), 1, Some(Run::Typing));+ // the caret was moved to 0 before the next letter+ h.record(Snap { text: "a".into(), anchor: 0, focus: 0 }, 1, Some(Run::Typing));+ assert_eq!(h.undo(snap("ba")).map(|s| s.text), Some("a".into()));+ }++ #[test]+ fn a_paste_is_a_step_of_its_own() {+ let mut h = History::default();+ h.record(snap(""), 1, Some(Run::Typing));+ h.record(snap("a"), 6, None);+ h.record(snap("ahello"), 7, Some(Run::Typing));+ assert_eq!(h.undo(snap("ahello!")).map(|s| s.text), Some("ahello".into()));+ assert_eq!(h.undo(snap("ahello")).map(|s| s.text), Some("a".into()));+ }++ #[test]+ fn redo_puts_back_what_undo_took_and_a_new_change_drops_it() {+ let mut h = History::default();+ h.record(snap(""), 5, None);+ assert_eq!(h.undo(snap("hello")), Some(snap("")));+ assert_eq!(h.redo(snap("")), Some(snap("hello")));+ assert_eq!(h.undo(snap("hello")), Some(snap("")));+ h.record(snap(""), 1, Some(Run::Typing));+ assert_eq!(h.redo(snap("x")), None);+ }++ #[test]+ fn a_script_value_clears_it() {+ let mut h = History::default();+ h.record(snap(""), 1, Some(Run::Typing));+ h.clear();+ assert_eq!(h.undo(snap("a")), None);+ }+}