# ui/uiinputfield (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/uiinputfield/

Input field. (UIInputField, BackgroundLayerRenderer, OverlayLayerRenderer of the original ui/uiinputfield.js)
- Draws text, cursor, selection, IME composition text and its underline, and the focus border directly onto the canvas, and sets the cursor and selection by pressing and dragging.
- The original is a node inheriting WorldNode, with text drawn by two UILabel child nodes (value, placeholder) and the cursor and border drawn by an overlay child node.
  The Rust version is a component like the other widgets. Attach it with node_tree.add_component(node_id, UIInputField::new()).
  It contains a UIControl, creates the content node in attach_to_tree and turns the host interactable. The pivot and anchor are set to the top left as in the original.
  Text is drawn with two contained Text instances (value, placeholder), and the background (the original Paint component) is also drawn with a contained Paint.
  The layer order is the same as the original: background → selection → text → composition underline → cursor → focus border. One component draws them all at once, so
  even if scene code calls set_text outside tick, it shows up immediately that frame. (splitting into child nodes would lag one frame while moving the state)
- Input is received from the engine's text input queue (core/textinput.rs). The attach()ed field with focus takes it out in tick.
  - Desktop: winit KeyboardInput and Ime events. IME is enabled only when a field with use_dom_input (the original setUseDOMInput) on has focus.
  - Web: as in the original, a hidden DOM <input> is placed over each field. (platform/web/textinput.rs) The DOM owns the value and selection, and the field follows.
    Fields with use_dom_input off receive window keydown like the original canvas mode.
- Differences from the original:
  - Events are Box<dyn FnMut(&mut UIInputField, &mut ComponentContext)>. Original callbacks receive only the value. (the value is field.get_text())
  - setFont(FontFace) became set_font_family(registered font name). setEngine was not ported. (the engine comes from the context)
  - Character positions in the public functions (get_cursor_index, get_selection_start, get_selection_range, move_cursor, measure_index_at_local_x) and max_length
    count in UTF-16 units as in the original. Internally they are held in characters (char) so surrogate pairs (characters like emoji) are not split.
    So a UTF-16 position in the middle of a surrogate pair is treated as after that character. (the original does the same) With max_length, inserted text is cut to the remaining length like a Blink input box.
  - Pressed position → character position is found from the character boundaries measured during draw. (the original measures each time with an offscreen 2D canvas)
  - On desktop, fields with use_dom_input on also lose focus when pressing outside the field, like the original DOM mode. There is only one focus in the engine.
    (in the original canvas mode, the previous field keeps focus even when another field is pressed. The Rust version keeps only the field that gained focus last)
  - When focus is lost, text being composed is committed into the value. (same as what browsers do for DOM inputs)
  - The context menu (right button) is drawn by the engine like the original. (UIInputFieldContextMenu, the same on the web and the desktop) It is created as the last child node of the root and drawn on top.
    The items are cut, copy, paste and select all, as in the original. Fields that let the DOM input be pressed directly (use_direct_touch) also get this menu instead of the browser edit-field menu, as in the original.
    Those fields select a word with a finger long press (650ms, within 15 DIP) like Chrome desktop touch and show touch handles and the quick menu. (Windows decides the menu on release)
  - Desktop DOM input fields undo and redo like the original DOM input box. (Ctrl+Z, Ctrl+Shift+Z, Ctrl+Y, Cmd on Mac, Blink's grouping rules, TextEditBuffer)
  - Desktop single-line DOM input fields, like a Blink input box, remove trailing line breaks from inserted text and replace middle line breaks with spaces, and up and page up move to the text start, down and page down to the text end.
  - With set_multiline(true), it is a multiline input like Chrome textarea. (not in the original engine, where the original uses a DOM textarea through the textarea preset of Pane)
    Text is split by line break characters and width, like white-space: pre-wrap, overflow-wrap: break-word. (ui/textarealayout.rs, line break positions follow Unicode line breaking like Chrome)
    Enter inserts a line (in the character insertion undo group), up and down keep the horizontal position from the first vertical move (up on the first line goes to the text start, down on the last line to the text end),
    Home and End go to the start and end of the visible line (also Cmd + Left, Right on Mac), Ctrl + Home, End (Cmd + Up, Down on Mac) go to the text start and end,
    PageUp, PageDown (with Option on Mac) move to the farthest line within 87.5% of the visible height (on Mac, the larger of that and height - 40) and bring that line to the top.
    CR and CRLF in inserted text become LF. After edits and moves it scrolls to keep the cursor visible (to the edge except on macOS, to the center on macOS), and it scrolls with the wheel and thumb dragging.
    A normal line height is computed from the first font's ascent, descent and line gap like Chrome. With classic scroll bars (Windows, Linux, the macOS classic setting), the scroll bar takes away text width when overflowing, like Chrome.
    On macOS, Home and End leave the cursor and scroll to the start and end like Chrome on macOS (line start and end if there is nothing to scroll), and PageUp, PageDown leave the cursor and scroll by one screen.
  - Fields imitating DOM input (use_dom_input, multiline) select a word (with the trailing space on Windows) on a double click and a paragraph on a triple click like Chrome, and dragging extends by that unit.
    Repeated presses are within 500ms (the system double-click time on macOS) and within 2 DIP horizontally and vertically, like Chrome aura. The web uses the mousedown detail counted by the browser.
  - Cursor movement and Delete work by grapheme cluster (extended grapheme cluster) like Chrome, and Backspace follows Blink's BackspaceStateMachine (one code point, emoji sequences whole).
    For web DOM input, a hidden `<textarea>` handles text, composition and undo, and keys that move by visible lines (up and down, page, Home, End) are handled by the field using the engine layout.
  - Word movement and deletion (Ctrl on Windows and Linux, Option on macOS, with Left, Right, Backspace, Delete) follow Blink NextWordPosition, PreviousWordPosition like Chrome's textarea.
    (Right goes to the start of the next word only on Windows, and to the end of the current word on macOS and Linux. On macOS, an extending selection does not cross the anchor) For web DOM input, the browser does it.
  - Turning off (set_active(false)) or removing the node stops tick, so focus, IME and the web's hidden <input> go away together. (the original goes away only when detached)
  - getRenderState was not ported. (there is only one drawing component, so there is nothing to pass)
  - UI documents (UIDocument) write and read it in the original node form when registered with register_node_type. (node type "UIInputField", background Paint, draw layers, placeholder and value UILabel children,
    overlay child. UIDocumentNode in uidocument.rs) The content node the Rust version creates is not written to the document.
    The positions and sizes of the text labels and overlay are written from the node size last placed by tick (text_layout_size), like the original refreshTextPositions.

Example: Undo and redo consecutively typed characters at once

```rust
use vanilla::{TextEditBuffer, TextEditKind};

let mut buffer = TextEditBuffer::new();
buffer.set_text("abc");
buffer.begin_edit(TextEditKind::InsertText);
buffer.replace_selection("d");
buffer.begin_edit(TextEditKind::InsertText);
buffer.replace_selection("e");
assert_eq!(buffer.get_text(), "abcde");

assert!(buffer.undo());
assert_eq!(buffer.get_text(), "abc");
assert!(buffer.redo());
assert_eq!(buffer.get_text(), "abcde");
assert!(!buffer.can_redo());
```

## UIInputFieldEvent (type)

Input field event. (takes the field and the context. the value is field.get_text())

```rust
pub type UIInputFieldEvent = Box<dyn FnMut(&mut UIInputField, &mut ComponentContext)>
```

## TextEditBuffer (struct) (Clone, Debug, Default)

Edit buffer. (value, composition text, cursor, selection start. #value / #composition / #cursor_index / #selection_start of the original UIInputField and the edit functions)
- Positions are in characters. If the cursor and the selection start differ, the range between them is the selection. (the cursor may be in front)
- max_length is in UTF-16 units like the original maxLength. (the same count as maxlength of the web DOM <input>)
- Undo and redo: in the original DOM input field, the browser does it. Grouping is the same as Blink. (consecutively typed characters go at once, moving the cursor or changing the selection breaks it,
  a new edit clears the redo list, setting the value in code empties the lists) The editing side calls begin_edit before changing it.

Example: Cut to the maximum length, replace the selected text, then Backspace

```rust
use vanilla::TextEditBuffer;

let mut buffer = TextEditBuffer::new();
buffer.set_max_length(5);
buffer.set_text("hello world");
assert_eq!(buffer.get_text(), "hello");

buffer.set_selection(0, 5);
assert_eq!(buffer.get_selection_text(), "hello");
buffer.replace_selection("hi");
assert_eq!(buffer.get_text(), "hi");
assert_eq!(buffer.get_cursor_index(), 2);
assert!(buffer.delete_backward());
assert_eq!(buffer.get_text(), "h");
```

- `pub fn new() -> TextEditBuffer` (constructor, since 0.3.2): Constructor.
  - returns TextEditBuffer: An empty buffer. (the value and composition are empty and the maximum length is 0)
- `pub fn set_text(&mut self, text: &str)` (method, since 0.3.2): Sets the value. (cut to max_length with the cursor at the end. The original setText)
  - `text` &str: Text to set. (also clears the undo and redo lists and the text being composed)
- `pub fn begin_edit(&mut self, edit_kind: TextEditKind)` (method, since 0.4.0): Signals the start of an edit. (called before changing. if it is a new group, pushes the current value onto the undo list and clears the redo list)
  - `edit_kind` TextEditKind: Undo group kind of the edit to make.
- `pub fn close_edit_group(&mut self)` (method, since 0.4.0): Closes the open group. (the next edit is a new group)
- `pub fn can_undo(&self) -> bool` (method, since 0.4.0): Whether there is something to undo.
  - returns bool: true if there is a group to undo.
- `pub fn can_redo(&self) -> bool` (method, since 0.4.0): Whether there is something to redo.
  - returns bool: true if there is a group to redo.
- `pub fn undo(&mut self) -> bool` (method, since 0.4.0): Undoes. (true if done)
  - returns bool: true if undone. false if there is nothing to undo.
- `pub fn redo(&mut self) -> bool` (method, since 0.4.0): Redoes. (true if done)
  - returns bool: true if redone. false if there is nothing to redo.
- `pub fn get_text(&self) -> &str` (method, since 0.3.2): Returns the value.
  - returns &str: The entered text. (text being composed is not included)
- `pub fn set_value_from_platform(&mut self, value: &str, cursor_index: usize)` (method, since 0.3.2): Replaces with the value given by the platform. (not clamped. sets the cursor and collapses the selection. original handleDOMInputEvent)
  - `value` &str: The value of the platform input box.
  - `cursor_index` usize: Cursor position. (in characters, the end if past the end of the value)
- `pub fn set_max_length(&mut self, max_length: usize)` (method, since 0.3.2): Sets the maximum length. (UTF-16 units, 0 for no limit. as in the original, the current value is not clamped)
  - `max_length` usize: Maximum character count. (UTF-16 units, no limit if 0, the current value is not cut)
- `pub fn get_max_length(&self) -> usize` (method, since 0.3.2): Returns the maximum length.
  - returns usize: Maximum character count. (0 means no limit)
- `pub fn set_composition(&mut self, composition: &str)` (method, since 0.3.2): Sets the composition text.
  - `composition` &str: Text being composed by the IME. (none if empty)
- `pub fn get_composition(&self) -> &str` (method, since 0.3.2): Returns the composition text.
  - returns &str: Text being composed by the IME. (empty if none)
- `pub fn get_cursor_index(&self) -> usize` (method, since 0.3.2): Cursor position.
  - returns usize: Cursor position. (in characters, 0 ~ the character count of the value)
- `pub fn get_selection_start(&self) -> usize` (method, since 0.3.2): Selection start position. (the side that stays fixed when extending the selection)
  - returns usize: The fixed end of the selection. (in characters, no selection if equal to the cursor position)
- `pub fn set_selection(&mut self, selection_start: usize, cursor_index: usize)` (method, since 0.3.2): Sets the selection. (clamped if past the end)
  - `selection_start` usize: The fixed end of the selection. (in characters, the end if past the end of the value)
  - `cursor_index` usize: Cursor position. (in characters, the end if past the end of the value)
- `pub fn has_selection(&self) -> bool` (method, since 0.3.2): Whether there is a selection.
  - returns bool: True if text is selected.
- `pub fn get_selection_range(&self) -> Option<(usize, usize)>` (method, since 0.3.2): Selection range. (front, back. None if none)
  - returns Option<(usize, usize)>: Selection range (start, end). (in characters, start is the smaller one, None if there is no selection)
- `pub fn get_selection_text(&self) -> String` (method, since 0.3.2): Selected text.
  - returns String: The selected text. (empty if none)
- `pub fn replace_selection(&mut self, text: &str)` (method, since 0.3.2): Replaces the selection with text. (inserted at the cursor if there is no selection. The original replaceSelection)
  - With max_length, inserts text only up to the remaining UTF-16 length like a Blink input box (FilterBeforeTextInserted in text_field_input_type.cc). (characters are not split)
  - `text` &str: Text to insert. (an empty string deletes the selection)
- `pub fn delete_character_at(&mut self, character_index: usize)` (method, since 0.3.2): Deletes one character. (one grapheme cluster from that position, does not split emoji or combining marks, like the Chrome Delete key. The original deleteCharAt)
  - `character_index` usize: Index of the character to delete. (in characters)
- `pub fn delete_range(&mut self, start_index: usize, end_index: usize)` (method, since 0.4.0): Deletes a text range. (character positions, the cursor is pulled to the deleted position and the selection collapses. The original deleteRange)
  - `start_index` usize: Start of the range to delete. (in characters)
  - `end_index` usize: End of the range to delete. (in characters, does nothing if it is not greater than the start)
- `pub fn delete_backward(&mut self) -> bool` (method, since 0.3.2): Backspace. (the selection if there is one, otherwise the range before the cursor decided by Blink's BackspaceStateMachine. true if changed)
  - returns bool: true if deleted. false if there is no selection and the cursor is at the start of the text.
- `pub fn delete_forward(&mut self) -> bool` (method, since 0.3.2): Forward delete. (the selection if there is one, otherwise the grapheme cluster after the cursor. true if changed)
  - returns bool: true if deleted. false if there is no selection and the cursor is at the end of the text.
- `pub fn move_cursor(&mut self, new_index: usize, is_selection_extended: bool)` (method, since 0.3.2): Moves the cursor. (clamped into the text. Collapses the selection unless extending it. The original moveCursor)
  - `new_index` usize: Index to move to. (in characters, clamped to 0 ~ the character count of the value)
  - `is_selection_extended` bool: If true, keeps the fixed end of the selection and extends the selection.
- `pub fn select_all(&mut self)` (method, since 0.3.2): Selects all. (the original selectAll)
- `pub fn get_text_before_cursor(&self) -> &str` (method, since 0.3.2): Text before the cursor.
  - returns &str: The text of the value up to the cursor.
- `pub fn get_composition_start(&self) -> usize` (method, since 0.4.0): Position in the value where the composition goes. (the start of the selection while composing, otherwise the cursor. Original #getCompositionStart)
  - When composing over a selection, committing replaces the selection. The shown text is [before the selection, the composition, after the selection].
  - returns usize: Character position.
- `pub fn get_composition_end(&self) -> usize` (method, since 0.4.0): End in the value that the composition replaces up to. (the end of the selection while composing, otherwise the cursor. Original #getCompositionEnd)
  - returns usize: Character position.
- `pub fn get_display_text_before_cursor(&self) -> String` (method, since 0.4.0): The shown text up to the cursor. (while composing, the value before where the composition goes plus the composition; otherwise the value before the cursor)
  - returns String: The shown text up to the cursor.
- `pub fn build_display_text(&self) -> String` (method, since 0.3.2): Shown text. (the composition inserted at the cursor; when composing over a selection, the selection replaced by the composition. Original getDisplayText)
  - returns String: Shown text. (the value as is when not composing)

## UIInputField (struct) (Default, Component, UIDocumentNode)

Input field.

Example: Attach a name input field and press it to give it focus

```rust
use vanilla::{Engine, EngineConfiguration, NodeTree, TouchKind, UIInputField, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let field_id = node_tree.create_child(root_id, "NameField");
node_tree[field_id].set_local_position(Vector2::create(100.0, 50.0));
node_tree[field_id].set_content_size(Vector2::create(300.0, 56.0));
let input_field = node_tree.add_component(field_id, UIInputField::new()).expect("input field");
input_field.set_use_dom_input(true);
input_field.set_placeholder("Name");
input_field.set_max_length(8);
input_field.set_text("Vanilla Engine");
assert_eq!(input_field.get_text(), "Vanilla ");
input_field.move_cursor(0, true);
assert_eq!(input_field.get_selection_range(), Some((0, 8)));
input_field.attach();

node_tree.dispatch_touch(field_id, &engine, TouchKind::Press, Vector2::create(150.0, 70.0));
assert!(node_tree[field_id].get_component::<UIInputField>().expect("input field").is_focused());
```

- `pub fn new() -> UIInputField` (constructor, since 0.3.2): Constructor. (defaults are the same as the original: 28px, padding 16, white background with rounding 8, text #222222, placeholder #999999, cursor #5b8def, border #f97316 3px)
  - returns UIInputField: A new input field. (the value and placeholder are empty and it has no focus)
- `pub fn get_uicontrol(&self) -> &UIControl` (method, since 0.3.2): Returns the embedded control.
  - returns &UIControl: The embedded UIControl.
- `pub fn get_uicontrol_mut(&mut self) -> &mut UIControl` (method, since 0.3.2): Returns the embedded control. (mutable)
  - returns &mut UIControl: The embedded UIControl.
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: Content node created under the host node. None before being attached to a tree.
- `pub fn get_field_id(&self) -> u64` (method, since 0.3.2): Field id. (the value that refers to this field in the engine's text input line)
  - returns u64: Field id. (from 1)
- `pub fn set_use_dom_input(&mut self, use_dom_input: bool)` (method, since 0.3.2): Sets whether to use platform text input. (original setUseDOMInput. must be on for Korean IME, mobile keyboards, and web system paste. off by default)
  - `use_dom_input` bool: true to receive through platform text input. (hidden DOM input on the web, IME on desktop)
- `pub fn is_using_dom_input(&self) -> bool` (method, since 0.3.2): Whether platform text input is used.
  - returns bool: True if the DOM input is used.
- `pub fn set_use_touch_end_focus(&mut self, use_touch_end_focus: bool)` (method, since 0.3.2): Sets whether to also give focus on release. (web DOM input only. for browsers that do not show the keyboard on the first touch, off by default)
  - `use_touch_end_focus` bool: If true, also gives focus on touchend. (default false)
- `pub fn is_using_touch_end_focus(&self) -> bool` (method, since 0.3.2): Whether focus is also given on release.
  - returns bool: True if focus is also given on touchend.
- `pub fn set_use_direct_touch(&mut self, use_direct_touch: bool)` (method, since 0.3.2): Sets whether the DOM input is pressed directly over the field. (meaningful for fields with use_dom_input on, off by default)
  - Web: as in the original, a transparent DOM input receives presses at the field's position and opens focus and the keyboard through the browser's default behavior. (for webviews that do not accept focus given by code)
    The right button on that field opens the same engine context menu as other fields (cut, copy, paste, select all) instead of the browser edit-field menu, as in the original.
    The finger long press is what Chrome shows for that DOM input.
  - Desktop: the engine does what Chrome does for that DOM input in the original. The right button opens the same engine context menu,
    and a 650ms finger long press (within 15 DIP) selects the word and shows touch handles and the quick menu. (Chrome desktop touch; Windows decides the menu on release)
  - `use_direct_touch` bool: If true, the DOM input receives presses at the field's position directly. (on desktop it does the long press like that DOM input, false by default)
- `pub fn is_using_direct_touch(&self) -> bool` (method, since 0.3.2): Whether the DOM input is pressed directly.
  - returns bool: True if the DOM input takes presses directly.
- `pub fn set_multiline(&mut self, is_multiline: bool)` (method, since 0.4.0): Sets multiline. (like Chrome textarea. Web DOM input uses a hidden `<textarea>`. Not a property of the original field, so excluded from reflection)
  - `is_multiline` bool: true for multiline, false for single line. (initially false, changing it resets the scroll)
- `pub fn is_multiline(&self) -> bool` (method, since 0.4.0): Whether it is multiline.
  - returns bool: true if multiline.
- `pub fn set_line_height(&mut self, line_height: Option<f64>)` (method, since 0.4.0): Sets the line height. (multiline fields, CSS normal if None. Like Chrome SimpleFontData, adds the rounded ascent, descent and line gap of the first font)
  - `line_height` Option<f64>: Line height. (content size units, CSS normal if None)
- `pub fn get_line_height(&self) -> f64` (method, since 0.4.0): Line height. (multiline fields)
  - returns f64: Line height. (if not set, the value measured as CSS normal, or the font size times 1.2 rounded if no font could be read at all)
- `pub fn set_tab_size(&mut self, tab_size: f64)` (method, since 0.4.0): Sets the tab size. (multiline fields, width in spaces. CSS tab-size, default 8)
  - `tab_size` f64: Tab size. (width in spaces, negative becomes 0, default 8)
- `pub fn get_tab_size(&self) -> f64` (method, since 0.4.0): Tab size.
  - returns f64: Tab size. (width in spaces)
- `pub fn get_scroll_top(&self) -> f64` (method, since 0.4.0): Vertical scroll offset. (multiline fields, the original textarea scrollTop)
  - returns f64: The amount scrolled up. (content size units)
- `pub fn set_scroll_top(&mut self, scroll_top: f64)` (method, since 0.4.0): Sets the vertical scroll offset. (multiline fields, clamped to within the end of the text on the next draw)
  - `scroll_top` f64: The amount to scroll up. (content size units, negative becomes 0)
- `pub fn can_undo(&self) -> bool` (method, since 0.4.0): Whether there is something to undo.
  - returns bool: true if there is a group to undo.
- `pub fn can_redo(&self) -> bool` (method, since 0.4.0): Whether there is something to redo.
  - returns bool: true if there is a group to redo.
- `pub fn set_text(&mut self, text: &str)` (method, since 0.3.2): Sets the value. (cut to max_length with the cursor at the end. Does not call the change event. The original setText)
  - Drops the composition if composing. (the composition was inserted into the value before the change; if kept, the field stays composing when no commit comes)
  - `text` &str: Text to set. (also clears the undo and redo lists and the text being composed)
- `pub fn get_text(&self) -> &str` (method, since 0.3.2): Returns the value.
  - returns &str: The entered text. (text being composed is not included)
- `pub fn get_display_text(&self) -> String` (method, since 0.3.2): Returns the shown text. (the composition inserted at the cursor of the value; when composing over a selection, the selection replaced by the composition. Original getDisplayText)
  - returns String: Shown text. (the value as is when not composing)
- `pub fn get_composition(&self) -> &str` (method, since 0.3.2): Returns the text being composed.
  - returns &str: Text being composed by the IME. (empty if none)
- `pub fn is_composing(&self) -> bool` (method, since 0.3.2): Whether IME composition is in progress.
  - returns bool: True while composing.
- `pub fn set_placeholder(&mut self, placeholder: &str)` (method, since 0.3.2): Sets the placeholder text. (shown when the value is empty)
  - `placeholder` &str: Text shown when the value is empty.
- `pub fn get_placeholder(&self) -> &str` (method, since 0.3.2): Returns the placeholder text.
  - returns &str: Placeholder text.
- `pub fn set_max_length(&mut self, max_length: usize)` (method, since 0.3.2): Sets the maximum character count. (UTF-16 units as in the original, 0 for no limit)
  - `max_length` usize: Maximum character count. (UTF-16 units, no limit if 0, the current value is not cut)
- `pub fn get_max_length(&self) -> usize` (method, since 0.3.2): Returns the maximum character count.
  - returns usize: Maximum character count. (0 means no limit)
- `pub fn set_font_size(&mut self, font_size: f64)` (method, since 0.3.2): Sets the font size.
  - `font_size` f64: Font size. (content size units, default 28)
- `pub fn get_font_size(&self) -> f64` (method, since 0.3.2): Returns the font size.
  - returns f64: Font size.
- `pub fn set_font_family(&mut self, font_family: &str)` (method, since 0.3.2): Sets the font name. (in place of the original setFont. a name registered with Graphic, the default font if none)
  - `font_family` &str: Font name registered with Graphic. (initially "default", the engine default font)
- `pub fn get_font_family(&self) -> &str` (method, since 0.3.2): Returns the font name.
  - returns &str: Font name. (initially "default")
- `pub fn set_padding(&mut self, padding: f64)` (method, since 0.3.2): Sets the inner padding. (left padding of the text)
  - `padding` f64: Left and right inner padding. (content size units, default 16, also top and bottom for multiline fields)
- `pub fn get_padding(&self) -> f64` (method, since 0.3.2): Returns the inner padding.
  - returns f64: Left and right inner padding.
- `pub fn set_background_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the background color.
  - `color` impl Into<Color>: Background color. (default white)
- `pub fn get_background_color(&self) -> Color` (method, since 0.3.2): Returns the background color.
  - returns Color: Background color.
- `pub fn set_round_size(&mut self, round_size: f64)` (method, since 0.3.2): Sets the background rounding. (the focus border follows it)
  - `round_size` f64: Corner radius of the background and the focus border. (default 8)
- `pub fn get_round_size(&self) -> f64` (method, since 0.3.2): Returns the background rounding.
  - returns f64: Corner radius of the background.
- `pub fn set_text_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the text color.
  - `color` impl Into<Color>: Color of the value text. (default #222222)
- `pub fn get_text_color(&self) -> Color` (method, since 0.3.2): Returns the text color.
  - returns Color: Color of the value text.
- `pub fn set_placeholder_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the placeholder text color.
  - `color` impl Into<Color>: Color of the placeholder text. (default #999999)
- `pub fn get_placeholder_color(&self) -> Color` (method, since 0.3.2): Returns the placeholder text color.
  - returns Color: Color of the placeholder text.
- `pub fn set_cursor_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the cursor color. (the composition underline uses the same color. same as the original)
  - `color` impl Into<Color>: Color of the cursor and the composition underline. (default #5b8def)
- `pub fn get_cursor_color(&self) -> Color` (method, since 0.3.2): Returns the cursor color.
  - returns Color: Cursor color.
- `pub fn set_focus_border_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the focus border color.
  - `color` impl Into<Color>: Focus border color. (default #f97316)
- `pub fn set_focus_border_width(&mut self, focus_border_width: f64)` (method, since 0.3.2): Sets the focus border thickness. (not drawn if 0)
  - `focus_border_width` f64: Focus border width. (not drawn if 0 or less, default 3)
- `pub fn set_selection_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the selection color.
  - `color` impl Into<Color>: Color to fill the selection with. (default blue with alpha 0.35)
- `pub fn set_on_change(&mut self, change_event: UIInputFieldEvent)` (method, since 0.3.2): Sets the change event. (when user input changes the value. The original setOnChange)
  - `change_event` UIInputFieldEvent: Function called whenever user input changes the value. (takes the field and the context, the value is `field.get_text()`, not called when changed with set_text)
- `pub fn set_on_submit(&mut self, submit_event: UIInputFieldEvent)` (method, since 0.3.2): Sets the submit event. (Enter. The original setOnSubmit)
  - `submit_event` UIInputFieldEvent: Function called when Enter is pressed while focused. (single-line fields only, takes the field and the context, the value is `field.get_text()`)
- `pub fn attach(&mut self)` (method, since 0.3.2): Starts taking input. (original attach: DOM input or canvas keyboard, context menu)
- `pub fn detach(&mut self)` (method, since 0.3.2): Stops taking input. (original detach. a field that was using DOM input also loses focus and the composition text)
- `pub fn is_attached(&self) -> bool` (method, since 0.3.2): Whether it has started taking input.
  - returns bool: true after attach and before detach.
- `pub fn focus(&mut self)` (method, since 0.3.2): Gives focus. (original focus. platform input comes to this field from the next frame)
- `pub fn blur(&mut self)` (method, since 0.3.2): Removes focus. (original blur. commits any text being composed. when called from scene code, the changed event is not called)
- `pub fn is_focused(&self) -> bool` (method, since 0.3.2): Whether it has focus.
  - returns bool: True if it has focus.
- `pub fn select_all(&mut self)` (method, since 0.3.2): Selects all.
- `pub fn has_selection(&self) -> bool` (method, since 0.3.2): Whether there is a selection.
  - returns bool: True if text is selected.
- `pub fn get_selection_range(&self) -> Option<(usize, usize)>` (method, since 0.3.2): Selection range. (front, back. UTF-16 units as in the original, None if none)
  - returns Option<(usize, usize)>: Selection range (start, end). (UTF-16 units, start is the smaller one, None if there is no selection)
- `pub fn get_selection_text(&self) -> String` (method, since 0.3.2): Selected text.
  - returns String: The selected text. (empty if none)
- `pub fn get_cursor_index(&self) -> usize` (method, since 0.3.2): Cursor position. (UTF-16 units as in the original)
  - returns usize: Cursor position. (character position in the value, 0 ~ value length)
- `pub fn get_selection_start(&self) -> usize` (method, since 0.3.2): Selection start position. (UTF-16 units as in the original)
  - returns usize: The fixed end of the selection. (no selection if equal to the cursor position)
- `pub fn move_cursor(&mut self, new_index: usize, is_selection_extended: bool)` (method, since 0.3.2): Moves the cursor. (the original moveCursor, positions in UTF-16 units)
  - `new_index` usize: Index to move to. (UTF-16 units, clamped to 0 to the value length, an index inside a surrogate pair is taken as after that character)
  - `is_selection_extended` bool: If true, keeps the fixed end of the selection and extends the selection.
- `pub fn replace_selection(&mut self, context: &mut ComponentContext, text: &str)` (method, since 0.3.2): Replaces the selection with text and calls the change event. (the original replaceSelection)
  - `context` &mut ComponentContext: Component context to pass to the change event.
  - `text` &str: Text to insert. (an empty string deletes the selection)
- `pub fn cut(&mut self, context: &mut ComponentContext)` (method, since 0.3.2): Cuts. (writes the selection to the clipboard and deletes it)
  - `context` &mut ComponentContext: Component context that provides the engine for writing the clipboard and is passed to the change event.
- `pub fn copy(&self, engine: &Engine)` (method, since 0.3.2): Copies. (writes the selection to the clipboard)
  - `engine` &Engine: Engine to write the clipboard with.
- `pub fn paste(&mut self, context: &mut ComponentContext)` (method, since 0.3.2): Pastes. (desktop reads the clipboard on the spot. the web reads it asynchronously as in the original, and once read, puts it into this field's text input line and pastes it on the next tick)
  - `context` &mut ComponentContext: Component context that provides the engine for reading the clipboard and is passed to the change event. (unused on the web)
- `pub fn request_menu_action(&mut self, menu_action: UIInputFieldMenuAction)` (method, since 0.3.2): Requests a context menu item. (called by the context menu. done with this field's context on the next tick)
  - `menu_action` UIInputFieldMenuAction: Item to perform. (a UIInputFieldMenuAction value)
- `pub fn measure_index_at_local_x(&self, local_x: f64) -> usize` (method, since 0.3.2): Node x (relative to the content area) → character position. (UTF-16 units as in the original)
  - `local_x` f64: X inside the node. (0 at the left of the content area)
  - returns usize: The grapheme cluster boundary nearest to that point. (UTF-16 units, 0 to the value length, the value length if not yet measured by drawing)

## Functions and constants

- `pub fn find_character_index_at_offset(character_offsets: &[f64], target_x: f64) -> usize` (method, since 0.3.2): Finds the character position closest to the pressed x from a list of character boundary x values. (the comparison in the original measureIndexAtLocalX)
  - `character_offsets[i]` is the width of the first i characters. (0 for index 0)
  - `character_offsets` &[f64]: List of widths of the first i characters. (0 for index 0, character count + 1 entries)
  - `target_x` f64: Pressed x. (0 at the left of the first character)
  - returns usize: Position of the nearest character boundary. (0 ~ character count)

## UIInputFieldMenuAction (enum) (Clone, Copy, Debug, PartialEq, Eq)

Context menu item. (cut / copy / paste / select all of the original UIInputFieldMenuAction, received by request_menu_action)

- `Cut`: Cut.
- `Copy`: Copy.
- `Paste`: Paste.
- `SelectAll`: Selects all.

## TextEditKind (enum) (Clone, Copy, Debug, PartialEq, Eq)

Undo group kind. (Blink TypingCommand and paste)

- `InsertText`: Insert text. (joins the open group regardless of its kind if there is one, and the group becomes insert text)
- `DeleteBackward`: Delete backward. (joins only if the open group is delete backward, otherwise a new group)
- `DeleteForward`: Delete forward. (joins only if the open group is delete forward, otherwise a new group)
- `Paste`: Paste, cut, and deleting a selection with the word deletion keys. (Blink ReplaceSelectionCommand, DeleteSelectionCommand, so always a separate group, and character insertion after it is a new group too)
- `DeleteWordBackward`: Delete word backward. (Blink always opens a new TypingCommand, so it is a new group, and Backspace and character insertion after it join this group)
- `DeleteWordForward`: Delete word forward. (always a new group, and forward delete and character insertion after it join this group)
