# platform/web/textinput (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/web/textinput/

Web text input. (hidden DOM <input> → engine text input queue, right clicks, canvas mode keys, clipboard)
- A port of the DOM side of the original UIInputField. In the original each field creates its own <input> and attaches listeners.
  In the Rust version fields post their state (position, value, selection, focus) to the engine every frame, and here one hidden <input> is matched to each attached DOM input field.
  - The DOM owns the value and selection. input / compositionstart / compositionupdate / compositionend / Enter keydown / focus / blur are
    put into the text input queue with that field id (replaceValue, composition, focus), and native moves like arrows are put in by polling the selection every frame. (the original tick)
  - When the field side changes the value or cursor (set_text, pressed position, cut), the state's value_revision increases and it is written back to the <input> here. (deferred during composition)
  - iOS shows the keyboard only if focus() is called inside a user gesture. As in the original, synchronous handlers are attached to canvas touchstart / mousedown (touchend if set),
    which check against the previous frame's field positions and focus / blur on the spot.
  - A right click on a field blocks the browser menu like the original and is put in as a context menu request (set_context_menu_request_position), the same as the desktop right button.
    The field opens the menu drawn by the engine (UIInputFieldContextMenu). A right click on that menu also blocks the browser menu.
  - Fields with use_dominput off convert window keydown into editing keys and characters, like the original canvas mode.
- DOM event listeners do not borrow the engine. They go into a pending queue (PENDING_EVENT_LIST) and move to the engine at the start of the next frame.
  (because focus() called by apply_frame fires focus / blur events on the spot)
- Differences from the original: the hidden <input> exists only while the field runs its tick. (it goes away when the node is turned off or removed) Cursor moves deferred with setTimeout are the same as the original.

Example: Write to the clipboard and read asynchronously (web cannot read on the spot, so it receives through a callback)

```rust
use vanilla::platform;

platform::write_clipboard_text("vanilla");
platform::read_clipboard_text_with_callback(Box::new(|text: String| {
	println!("{}", text);
}));
```

## Functions and constants

- `pub fn write_clipboard_text(text: &str)` (method, since 0.3.2): Writes text to the system clipboard. (navigator.clipboard.writeText, failure ignored. the original writeClipboard)
  - `text` &str: Text to write.
- `pub fn read_clipboard_text() -> Option<String>` (method, since 0.3.2): Reads text from the system clipboard. (web is asynchronous and cannot read on the spot. Read with read_clipboard_text_with_callback or request_clipboard_paste)
  - returns Option<String>: Always None.
- `pub fn read_clipboard_text_with_callback(callback: Box<dyn FnOnce(String)>)` (method, since 0.4.0): Reads the system clipboard text asynchronously and passes it to a callback. (navigator.clipboard.readText().then of the original readClipboard, empty text if it cannot be read or is refused)
  - `callback` Box<dyn FnOnce(String)>: Function to receive the text read. (called once after readText finishes)
- `pub fn read_optional_clipboard_text_with_callback(callback: Box<dyn FnOnce(Option<String>)>)` (method, since 0.4.0): Reads the system clipboard text asynchronously and passes it to a callback. (the original engine.readClipboardText, None in place of the original null if there is no clipboard or it is refused)
  - `callback` Box<dyn FnOnce(Option<String>)>: Function to receive the text read. (called once after readText finishes, called on the spot with None if navigator.clipboard is missing)
