setTextFormatted method

bool setTextFormatted(
  1. Iterable<TextFormatted> textFormatted, {
  2. bool scrollToBottom = true,
  3. bool force = false,
})

Sets this RichEdit text with textFormatted elements.

Implementation

bool setTextFormatted(Iterable<TextFormatted> textFormatted,
    {bool scrollToBottom = true, bool force = false}) {
  var newTextList = textFormatted.toList();

  if (newTextList.isEmpty) {
    setWindowText('');
    _onlyTextFormatted = true;
    _allTextFormatted = null;
    if (scrollToBottom) {
      this.scrollToBottom();
    }
    return true;
  }

  if (!force && _onlyTextFormatted) {
    var allTextFormatted = _allTextFormatted ?? [];
    if (allTextFormatted.isNotEmpty) {
      if (_listEquality.equals(newTextList, allTextFormatted)) {
        if (scrollToBottom) {
          this.scrollToBottom();
        }
        return false;
      }

      var tailSz = newTextList.length - allTextFormatted.length;
      if (tailSz > 0) {
        var headSz = newTextList.length - tailSz;
        assert(headSz > 0);

        var newHead = newTextList.sublist(0, headSz);
        var allTextFormattedHead = allTextFormatted.sublist(0, headSz);

        // Head equals: add only tail (new lines):
        if (_listEquality.equals(newHead, allTextFormattedHead)) {
          var tail = newTextList.sublist(headSz);
          assert(tail.isNotEmpty);
          appendAllTextFormatted(tail, scrollToBottom: scrollToBottom);
          return true;
        }
      }
    }
  }

  setWindowText('');
  _onlyTextFormatted = false;

  appendAllTextFormatted(newTextList, scrollToBottom: scrollToBottom);

  _onlyTextFormatted = true;
  _allTextFormatted = newTextList;

  return true;
}