r/ForgeYourProject • u/IncomeOk7237 • 19h ago
WIP kilim update: undo/redo in text fields, and a keystroke in a 750 KB text area went from 7 ms to 0.12 ms
Small update on kilim, the retained-mode GUI library I'm writing for the iyi language (a Crystal fork). Two things landed on main this morning. They're not in a tagged release yet, v0.3.0 is still the latest.
Undo and redo
Text fields and the text area now have Ctrl/Cmd+Z to undo, and Ctrl+Y or Ctrl/Cmd+Shift+Z to redo.
I didn't want to copy the whole text on every step, because that gets ugly fast with long notes. So each change is stored as a diff: where it happened, what was removed, what was added, and the caret and selection from before.
Grouping was the part I thought about most:
- typing merges into one step per word
- a run of Backspace or Delete is one step
- moving the caret ends the group
- typing over a selection is one step, and undo brings the selection back
- setting the text from code clears the history
- it keeps at most 200 steps
Rewrapping only what changed
Before this, every keystroke in a TextArea joined, split and hashed the whole text and rewrapped all of it. Fine for a short note, not fine for a big one.
Now TextEdit keeps a version counter and remembers the last edit as {from, old length, new length}. The text area rewraps only the paragraphs around that range and shifts the lines after it. A full rewrap still happens when you set the text, resize, or anything else.
On 10,000 paragraphs (~750 KB), a keystroke went from about 7 ms to 0.12 ms in the release core benchmark.
Repo: https://github.com/Tuntii/kilim
Question for anyone who's built a text widget or editor: how do you decide undo grouping? Word at a time felt natural to me, but I've seen editors that group by time instead (pause for a bit and a new step starts). Which one feels right when you're actually typing?