ViewportModel class

A viewport widget for scrollable content.

The viewport manages a scrollable view of content that may be larger than the available display area. It supports vertical and horizontal scrolling with keyboard and mouse input.

Example

class DocViewerModel implements Model {
  final ViewportModel viewport;
  final String content;

  DocViewerModel({ViewportModel? viewport, this.content = ''})
      : viewport = (viewport ?? ViewportModel(width: 80, height: 24))
          ..setContent(content);

  @override
  (Model, Cmd?) update(Msg msg) {
    // Handle window resize
    if (msg is WindowSizeMsg) {
      return (
        DocViewerModel(
          viewport: viewport.copyWith(
            width: msg.width,
            height: msg.height - 2, // Leave room for status
          ),
          content: content,
        ),
        null,
      );
    }

    final (newViewport, cmd) = viewport.update(msg);
    return (
      DocViewerModel(viewport: newViewport as ViewportModel, content: content),
      cmd,
    );
  }

  @override
  String view() => '''
${viewport.view()}
${(viewport.scrollPercent * 100).toInt()}%
''';
}
Inheritance
Implementers

Constructors

ViewportModel({int width = 80, int? height = 24, int gutter = 0, int yOffset = 0, int xOffset = 0, bool mouseWheelEnabled = true, int mouseWheelDelta = 3, int horizontalStep = 6, bool softWrap = false, bool fillHeight = false, bool showLineNumbers = false, GutterFunc? leftGutterFunc, Style? style, Style? highlightStyle, Style? selectedHighlightStyle, Style styleLineFunc(int lineIndex)?, List<HighlightInfo>? highlights, int currentHighlightIndex = -1, (int, int)? selectionStart, (int, int)? selectionEnd, DateTime? lastClickTime, (int, int)? lastClickPos, ViewportKeyMap? keyMap, List<String>? lines, List<String>? wrappedLines, List<String>? originalLines})
Creates a new viewport model.

Properties

atBottom bool
Whether the viewport is at the bottom.
no setter
atTop bool
Whether the viewport is at the top.
no setter
currentHighlightIndex int
The index of the currently focused highlight.
final
fillHeight bool
Whether to fill to the height of the viewport with empty lines.
final
gutter int
Left gutter (spaces) applied to each rendered line. Also reduces available content width.
final
hashCode int
The hash code for this object.
no setterinherited
height int?
Height of the viewport in rows. If null, the viewport will show all lines and will not scroll vertically.
final
highlights List<HighlightInfo>
Returns the current highlights.
no setter
highlightStyle Style
HighlightStyle highlights the ranges set with SetHighlights.
final
horizontalScrollPercent double
Returns the horizontal scroll percentage (0.0 to 1.0).
no setter
horizontalStep int
Number of columns to scroll left/right. 0 disables horizontal scrolling.
final
internalLines List<String>
Internal lines (unwrapped).
no setter
internalOriginalLines List<String>
Internal original lines (before styling).
no setter
internalWrappedLines List<String>
Internal wrapped lines.
no setter
keyMap ViewportKeyMap
Key bindings for navigation.
final
lastClickPos → (int, int)?
The position of the last mouse click.
final
lastClickTime DateTime?
The time of the last mouse click.
final
leftGutterFunc GutterFunc?
A function that returns the gutter for a given line index. If provided, gutter is ignored for rendering but still used for width calculations.
final
lines List<String>
The content lines.
no setter
mouseWheelDelta int
Number of lines to scroll per mouse wheel tick.
final
mouseWheelEnabled bool
Whether mouse wheel scrolling is enabled.
final
pastBottom bool
Whether the viewport is scrolled past the bottom.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scrollPercent double
Returns the scroll percentage (0.0 to 1.0).
no setter
selectedHighlightStyle Style
SelectedHighlightStyle highlights the highlight range focused during navigation.
final
selectionEnd → (int, int)?
The end of the selection (x, y) in content coordinates.
final
selectionStart → (int, int)?
The start of the selection (x, y) in content coordinates.
final
showLineNumbers bool
Whether to show line numbers in the gutter.
final
softWrap bool
Whether to wrap lines that exceed the viewport width.
final
style Style
Style applies a lipgloss style to the viewport. Realistically, it's most useful for setting borders, margins and padding.
final
styleLineFunc Style Function(int lineIndex)?
StyleLineFunc allows to return a Style for each line. The argument is the line index.
final
totalLineCount int
Total number of lines in the content.
no setter
visibleLineCount int
Number of visible lines.
no setter
width int
Width of the viewport in columns.
final
xOffset int
Horizontal scroll offset (0 = left).
final
yOffset int
Vertical scroll offset (0 = top).
final

Methods

calculateLine(int yoffset) → (int, int, int)
calculateLine taking soft wrapping into account, returns the total viewable lines and the real-line index for the given yoffset, as well as the virtual line offset.
clearHighlights() ViewportModel
Clears previously set highlights.
clearSelection() ViewportModel
Clears the current selection.
copyWith({int? width, int? height, int? yOffset, int? xOffset, bool? mouseWheelEnabled, int? mouseWheelDelta, int? horizontalStep, ViewportKeyMap? keyMap, List<String>? lines, int? gutter, bool? softWrap, bool? fillHeight, bool? showLineNumbers, GutterFunc? leftGutterFunc, Style? style, Style? highlightStyle, Style? selectedHighlightStyle, Style styleLineFunc(int lineIndex)?, List<HighlightInfo>? highlights, int? currentHighlightIndex, Object? selectionStart = undefined, Object? selectionEnd = undefined, DateTime? lastClickTime, (int, int)? lastClickPos}) ViewportModel
Creates a copy with the given fields replaced.
ensureVisible(int line, int colstart, int colend) ViewportModel
Ensures that the given line and column are in the viewport.
getSelectedText() String
Returns the currently selected text.
gotoBottom() ViewportModel
Goes to the bottom of the content.
gotoTop() ViewportModel
Goes to the top of the content.
halfPageDown() ViewportModel
Moves down by half a page.
halfPageUp() ViewportModel
Moves up by half a page.
highlightNext() ViewportModel
Moves the viewport to the next highlight.
highlightPrev() ViewportModel
Moves the viewport to the previous highlight.
init() Cmd?
Returns an optional command to execute on program startup.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pageDown() ViewportModel
Moves down by one page.
pageUp() ViewportModel
Moves up by one page.
scrollDown(int n) ViewportModel
Scrolls down by the given number of lines.
scrollLeft(int n) ViewportModel
Scrolls left by the given number of columns.
scrollRight(int n) ViewportModel
Scrolls right by the given number of columns.
scrollUp(int n) ViewportModel
Scrolls up by the given number of lines.
selectAll() ViewportModel
Selects all text in the viewport.
setContent(String content) ViewportModel
Sets the content of the viewport.
setHighlights(List<List<int>> matches) ViewportModel
Sets ranges of characters to highlight. For instance, [[2, 10], [20, 30]] will highlight characters 2 to 10 and 20 to 30. Note that highlights are not expected to transpose each other, and are also expected to be in order.
setXOffset(int n) ViewportModel
Sets the X offset (clamped to valid range).
setYOffset(int n) ViewportModel
Sets the Y offset (clamped to valid range).
toString() String
A string representation of this object.
inherited
update(Msg msg) → (ViewportModel, Cmd?)
Updates the component state in response to a message.
override
view() String
Renders the current model state for display.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited