terminalMain function

void terminalMain(
  1. SendPort port
)

main entry for the terminal isolate

Implementation

void terminalMain(SendPort port) async {
  final rp = ReceivePort();
  port.send(rp.sendPort);

  Terminal? _terminal;
  var _needNotify = true;

  await for (var msg in rp) {
    // process incoming commands
    final _IsolateCommand action = msg[0];
    switch (action) {
      case _IsolateCommand.sendPort:
        port = msg[1];
        break;
      case _IsolateCommand.init:
        final TerminalInitData initData = msg[1];
        _terminal = Terminal(
            backend: initData.backend,
            onTitleChange: (String title) {
              port.send([_IsolateEvent.titleChanged, title]);
            },
            onIconChange: (String icon) {
              port.send([_IsolateEvent.iconChanged, icon]);
            },
            onBell: () {
              port.send([_IsolateEvent.bell]);
            },
            platform: initData.platform,
            theme: initData.theme,
            maxLines: initData.maxLines);
        _terminal.addListener(() {
          if (_needNotify) {
            port.send([_IsolateEvent.notifyChange]);
            _needNotify = false;
          }
        });
        _terminal.backendExited
            .then((value) => port.send([_IsolateEvent.exit, value]));
        port.send([_IsolateEvent.notifyChange]);
        break;
      case _IsolateCommand.write:
        _terminal?.write(msg[1]);
        break;
      case _IsolateCommand.refresh:
        _terminal?.refresh();
        break;
      case _IsolateCommand.clearSelection:
        _terminal?.clearSelection();
        break;
      case _IsolateCommand.selectAll:
        _terminal?.selectAll();
        break;
      case _IsolateCommand.mouseTap:
        _terminal?.onMouseTap(msg[1]);
        break;
      case _IsolateCommand.mouseDoubleTap:
        _terminal?.onMouseDoubleTap(msg[1]);
        break;
      case _IsolateCommand.mousePanStart:
        _terminal?.onPanStart(msg[1]);
        break;
      case _IsolateCommand.mousePanUpdate:
        _terminal?.onPanUpdate(msg[1]);
        break;
      case _IsolateCommand.setScrollOffsetFromBottom:
        _terminal?.setScrollOffsetFromBottom(msg[1]);
        break;
      case _IsolateCommand.resize:
        _terminal?.resize(msg[1], msg[2], msg[3], msg[4]);
        break;
      case _IsolateCommand.onInput:
        _terminal?.backend?.write(msg[1]);
        break;
      case _IsolateCommand.keyInput:
        _terminal?.keyInput(
          msg[1],
          ctrl: msg[2],
          alt: msg[3],
          shift: msg[4],
          mac: msg[5],
          character: msg[6],
        );
        break;
      case _IsolateCommand.requestNewStateWhenDirty:
        if (_terminal == null) {
          break;
        }
        if (_terminal.dirty) {
          final newState = TerminalState(
            _terminal.scrollOffsetFromBottom,
            _terminal.scrollOffsetFromTop,
            _terminal.buffer.height,
            _terminal.invisibleHeight,
            _terminal.viewHeight,
            _terminal.viewWidth,
            _terminal.selection!,
            _terminal.getSelectedText(),
            _terminal.theme.background,
            _terminal.cursorX,
            _terminal.cursorY,
            _terminal.showCursor,
            _terminal.theme.cursor,
            _terminal
                .getVisibleLines()
                .map((bl) => BufferLine.withDataFrom(bl))
                .toList(growable: false),
            _terminal.composingString,
            _terminal.userSearchResult,
            _terminal.userSearchPattern,
            _terminal.userSearchOptions,
            _terminal.isUserSearchActive,
          );
          port.send([_IsolateEvent.newState, newState]);
          _needNotify = true;
        }
        break;
      case _IsolateCommand.paste:
        _terminal?.paste(msg[1]);
        break;
      case _IsolateCommand.terminateBackend:
        _terminal?.terminateBackend();
        break;
      case _IsolateCommand.updateComposingString:
        _terminal?.updateComposingString(msg[1]);
        break;
      case _IsolateCommand.updateSearchPattern:
        _terminal?.userSearchPattern = msg[1];
        break;
      case _IsolateCommand.updateSearchOptions:
        _terminal?.userSearchOptions = msg[1];
        break;
      case _IsolateCommand.updateCurrentSearchHit:
        _terminal?.currentSearchHit = msg[1];
        break;
      case _IsolateCommand.updateIsUserSearchActive:
        _terminal?.isUserSearchActive = msg[1];
        break;
    }
  }
}