What it brings
Use weirdnet-text for the rules behind a text field (an address bar, a prompt box, a chat input) when you draw the field yourself in a toolkit with no text widget of its own, such as GPUI. The rules live outside the toolkit, so they are tested headless and every app's inputs behave the same.
What it is
The behaviour behind a text field, kept out of the UI toolkit so it can be tested headless and shared by every app’s inputs. The app’s widget draws the field and feeds it keys and input-method events; TextField decides what they do.
How it works
TextField holds the text, the selection (and which end of it moves) and any text an input method has marked
while composing. Offsets are UTF-8 byte offsets, always on grapheme cluster boundaries, so the caret never lands inside an
accented letter or an emoji with a skin tone.
- Editing:
replace(None, text)types or pastes over the selection (or marked text) and puts the caret after it;backspaceanddeleteremove one grapheme cluster or the selection. - Moving and selecting:
left,right,home,end,move_to;select_left,select_right,select_to(the selection can reverse),select_all,selected_text,cursor. - Completions:
append_selected(suffix)adds a suggestion selected, so typing replaces it, Backspace removes it and Right accepts it.set_text_selectedreplaces the text and selects it, as an address bar does on focus. - Input methods:
replace_and_markinserts composing text as marked, with a selection inside it;unmarkcommits it.len_utf16,offset_to_utf16,offset_from_utf16,range_to_utf16,range_from_utf16andset_selection_utf16convert for platform input APIs, which count UTF-16 units. - Line breaks:
TextField::new()is one line and turns line breaks into spaces;TextField::multiline()keeps them and normalises\r\nand\rto\n.
Key types and modules
TextField::new(one line: pasted line breaks become spaces) andTextField::multiline(line breaks kept,\r\nnormalised).- Typing and pasting at the caret (
replace), Backspace and Delete by whole grapheme clusters, Left, Right, Home and End with and without Shift, select-all andselected_text. append_selected: an inline completion that typing replaces, Backspace removes and Right accepts.set_text_selected: replace the text and select it, as an address bar does on focus.replace_and_mark,marked,unmark: input-method composition;offset_to_utf16,range_from_utf16and friends for platform input APIs.
Feature flags
None: everything is on by default.
Usage
In Cargo.toml:
[dependencies]
weirdnet-text = "0.1.1"Then (built and run against the published crate when this page was made):
// Editing rules for a text field, tested without any UI toolkit.
use weirdnet_text::TextField;
fn main() {
let mut field = TextField::new();
for ch in "git".chars() {
field.replace(None, &ch.to_string()); // typing inserts at the caret
}
field.append_selected("hub.com"); // an inline completion, selected
field.right(); // Right accepts it
assert_eq!((field.text(), field.cursor()), ("github.com", 10));
// Backspace removes a whole grapheme cluster, not one byte or code point.
let mut emoji = TextField::new();
emoji.replace(None, "hi 👋🏽");
emoji.backspace();
assert_eq!(emoji.text(), "hi ");
// One-line fields turn pasted line breaks into spaces; multiline ones keep them.
let mut chat = TextField::multiline();
chat.replace(None, "line one\r\nline two");
assert_eq!(chat.text(), "line one\nline two");
}
Where it fits
- Used by Morph and Bragi (moving onto it).
- Extracted from Morph.
- License: BSD-3-Clause.
morph-ui text module. Offsets are UTF-8 byte offsets on grapheme boundaries. The GPUI widget that uses it stays in each app: GPUI is not on crates.io at the revision the apps use, so a shared widget crate can’t be published yet.