Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement line-height snapping and full line rendering #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lib/src/ui/render.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
var _stickToBottom = true;

void _onScroll() {
_stickToBottom = _scrollOffset >= _maxScrollExtent;
final lineHeight = _painter.cellSize.height;
_stickToBottom = (_maxScrollExtent - _offset.pixels).abs() < lineHeight / 2;
markNeedsLayout();
_notifyEditableRect();
}
Expand Down Expand Up @@ -209,12 +210,13 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
size = constraints.biggest;

_updateViewportSize();

_updateScrollOffset();

if (_stickToBottom) {
_offset.correctBy(_maxScrollExtent - _scrollOffset);
_offset.correctBy(_maxScrollExtent - _offset.pixels);
}

_notifyEditableRect();
}

/// Total height of the terminal in pixels. Includes scrollback buffer.
Expand All @@ -224,8 +226,9 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
/// The distance from the top of the terminal to the top of the viewport.
// double get _scrollOffset => _offset.pixels;
double get _scrollOffset {
// return _offset.pixels ~/ _painter.cellSize.height * _painter.cellSize.height;
return _offset.pixels;
final lineHeight = _painter.cellSize.height;
// Snap the scroll offset to the nearest multiple of lineHeight
return (_offset.pixels ~/ lineHeight) * lineHeight;
}

/// The height of a terminal line in pixels. This includes the line spacing.
Expand Down Expand Up @@ -373,7 +376,15 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
}

double get _maxScrollExtent {
return max(_terminalHeight - _viewportHeight, 0.0);
final totalLines = _terminal.buffer.lines.length;
final totalHeight = totalLines * _painter.cellSize.height;
final viewportHeight = _viewportHeight;
final maxExtent = totalHeight - viewportHeight;

final lineHeight = _painter.cellSize.height;
final adjustedMaxExtent = (maxExtent / lineHeight).ceil() * lineHeight;

return max(0.0, adjustedMaxExtent);
}

double get _lineOffset {
Expand All @@ -400,15 +411,14 @@ class RenderTerminal extends RenderBox with RelayoutWhenSystemFontsChangeMixin {

void _paint(PaintingContext context, Offset offset) {
final canvas = context.canvas;

final lines = _terminal.buffer.lines;
final charHeight = _painter.cellSize.height;

final firstLineOffset = _scrollOffset - _padding.top;
final lastLineOffset = _scrollOffset + size.height + _padding.bottom;
final lastLineOffset = _scrollOffset + size.height - _padding.bottom;

final firstLine = firstLineOffset ~/ charHeight;
final lastLine = lastLineOffset ~/ charHeight;
final firstLine = (firstLineOffset / charHeight).ceil();
final lastLine = (lastLineOffset / charHeight).floor() - 1;

final effectFirstLine = firstLine.clamp(0, lines.length - 1);
final effectLastLine = lastLine.clamp(0, lines.length - 1);
Expand Down