decodeInputEvent function
Decodes a TuiEvent from wire bytes.
Implementation
TuiEvent decodeInputEvent(Uint8List bytes) {
final r = _Reader(bytes);
final tag = r.u8();
final TuiEvent event;
switch (tag) {
case _evKey:
final code = _readKeyCode(r);
final mods = _readModifiers(r);
final type = r.enumValue(KeyEventType.values);
final (position, synthesized) = _readKeyExtension(r);
event = KeyEvent(
code,
modifiers: mods,
type: type,
position: position,
synthesized: synthesized,
);
case _evBatch:
KeyEvent? key;
if (r.boolean()) {
final code = _readKeyCode(r);
final mods = _readModifiers(r);
final type = r.enumValue(KeyEventType.values);
final (position, synthesized) = _readKeyExtension(r);
key = KeyEvent(
code,
modifiers: mods,
type: type,
position: position,
synthesized: synthesized,
);
}
final text = r.boolean() ? r.str() : null;
if (key == null && text == null) {
throw const RemoteCodecException('batch carries neither key nor text');
}
final seconds = r.u32();
final micros = r.u32();
if (micros >= Duration.microsecondsPerSecond) {
throw const RemoteCodecException('batch timestamp remainder overflow');
}
final sequence = r.u32();
event = InputBatch(
key: key,
committedText: text,
timeStamp: Duration(seconds: seconds, microseconds: micros),
sequence: sequence,
);
case _evText:
event = TextInputEvent(r.str(), repeat: r.boolean());
case _evComposition:
final kind = r.enumValue(TextCompositionEventKind.values);
final text = r.boolean() ? r.str() : null;
event = switch (kind) {
TextCompositionEventKind.update => TextCompositionEvent.update(
text ?? '',
),
TextCompositionEventKind.commit => TextCompositionEvent.commit(text),
TextCompositionEventKind.cancel => const TextCompositionEvent.cancel(),
};
case _evMouse:
final kind = r.enumValue(MouseEventKind.values);
final button = r.enumValue(MouseButton.values);
final col = r.i32();
final row = r.i32();
final mods = _readModifiers(r);
event = MouseEvent(
kind: kind,
button: button,
col: col,
row: row,
modifiers: mods,
);
case _evPaste:
final text = r.str();
final phase = r.enumValue(PasteEventPhase.values);
event = phase == PasteEventPhase.single
? PasteEvent(text)
: PasteEvent.segment(text, phase: phase, pasteId: r.u32());
case _evResize:
event = ResizeEvent(CellSize(r.u16(), r.u16()));
case _evSignal:
event = SignalEvent(r.enumValue(AppSignal.values));
default:
throw RemoteCodecException('unknown input-event tag $tag');
}
r.expectEnd();
return event;
}