encodeInputEvent function
Encodes a TuiEvent to wire bytes. Throws RemoteCodecException for an event kind not carried by the serve protocol.
Implementation
Uint8List encodeInputEvent(TuiEvent event) {
final w = _Writer();
switch (event) {
case TerminalFocusEvent():
// A local terminal window signal, not a peer input event: a remote
// peer reports its own focus through its own driver.
throw const RemoteCodecException(
'terminal focus events are not carried by the serve protocol',
);
case KeyEvent e:
w.u8(_evKey);
_writeKeyPayload(w, e);
_writeKeyExtension(w, e);
case InputBatch e:
// RFC 0020 §5: the correlated key+text unit.
w.u8(_evBatch);
w.boolean(e.key != null);
if (e.key != null) {
_writeKeyPayload(w, e.key!);
_writeKeyExtension(w, e.key!);
}
w.boolean(e.committedText != null);
if (e.committedText != null) w.str(e.committedText!);
// Timestamp as whole-seconds + micros-remainder u32 pair: varint would
// overflow the reader at ~36 minutes of monotonic clock, and dart2js
// (the serve client — a real batch encoder come P3) cannot shift-encode
// values ≥ 2^32. Two u32s are JS-safe and good for 136 years.
w.u32(e.timeStamp.inSeconds);
w.u32(e.timeStamp.inMicroseconds % Duration.microsecondsPerSecond);
w.u32(e.sequence);
case TextInputEvent e:
w.u8(_evText);
w.str(e.text);
w.boolean(e.repeat);
case TextCompositionEvent e:
w.u8(_evComposition);
w.u8(e.kind.index);
w.boolean(e.text != null);
if (e.text != null) w.str(e.text!);
case MouseEvent e:
w.u8(_evMouse);
w
..u8(e.kind.index)
..u8(e.button.index)
..i32(e.col)
..i32(e.row);
_writeModifiers(w, e.modifiers);
case PasteEvent e:
w.u8(_evPaste);
w.str(e.text);
// A complete paste (the only kind browser peers originate) is its phase
// byte alone; a segment also names the paste it belongs to.
w.u8(e.phase.index);
if (e.phase != PasteEventPhase.single) w.u32(e.pasteId!);
case ResizeEvent e:
w.u8(_evResize);
w
..u16(e.size.cols)
..u16(e.size.rows);
case SignalEvent e:
// A host relaying a termination request (e.g. the serve process was
// asked to shut the session down) forwards it like any other event.
w.u8(_evSignal);
w.u8(e.signal.index);
}
return w.take();
}