render method

  1. @override
void render(
  1. Rect area,
  2. Buffer buffer,
  3. RenderContext ctx
)
override

Implementation

@override
void render(Rect area, Buffer buffer, RenderContext ctx) {
  if (area.isEmpty) return;
  final isFocused = ctx.isFocused(id);
  final filtered = _derived();

  final headerLines = _headerLines();
  final dataArea = headerLines == 0
      ? area
      : Rect(area.x, area.y + headerLines, area.width,
          area.height - headerLines);

  if (filtered.isEmpty) {
    if (showHeader && area.height >= 2) {
      final widths = _computeWidths(area.width);
      final vis = _visibleColumns(widths, area.width);
      _renderHeader(area, buffer, ctx, widths, vis);
    }
    final emptyArea = headerLines > 0 && area.height > headerLines
        ? Rect(area.x, area.y + headerLines, area.width,
            area.height - headerLines)
        : area;
    _renderEmpty(emptyArea, buffer, ctx);
    return;
  }

  state.activeRow = state.activeRow.clamp(0, filtered.length - 1);
  state.activeColumn = state.activeColumn.clamp(0, columns.length - 1);

  final widths = _computeWidths(area.width);
  final vis = _visibleColumns(widths, area.width);

  if (state.activeColumn < vis.firstVisible) {
    state.horizontalScroll = state.activeColumn;
  } else if (state.activeColumn > vis.lastVisible) {
    state.horizontalScroll = state.activeColumn;
    while (state.horizontalScroll > 0) {
      final candidate = state.horizontalScroll;
      state.horizontalScroll = candidate - 1;
      final visAttempt = _visibleColumns(widths, area.width);
      if (visAttempt.lastVisible < state.activeColumn) {
        state.horizontalScroll = candidate;
        break;
      }
    }
  }

  final visibleCols = _visibleColumns(widths, area.width);
  final visibleRows = dataArea.height;
  if (visibleRows <= 0) return;

  if (state.activeRow < state.verticalScroll) {
    state.verticalScroll = state.activeRow;
  } else if (state.activeRow >= state.verticalScroll + visibleRows) {
    state.verticalScroll = state.activeRow - visibleRows + 1;
  }
  final maxRowOffset =
      (filtered.length - visibleRows).clamp(0, filtered.length);
  state.verticalScroll = state.verticalScroll.clamp(0, maxRowOffset);

  if (showHeader) {
    _renderHeader(area, buffer, ctx, widths, visibleCols);
  }

  _renderRows(
    dataArea,
    buffer,
    ctx,
    filtered,
    widths,
    visibleCols,
    visibleRows,
    isFocused,
  );

  if (showScrollIndicators) {
    _renderScrollIndicators(area, buffer, ctx, visibleCols);
  }
}