pub struct Viewport {
pub top_row: usize,
pub top_col: usize,
pub width: u16,
pub height: u16,
pub wrap: Wrap,
pub text_width: u16,
pub tab_width: u16,
}Expand description
Where the buffer is scrolled to and how big the visible area is.
Viewport is an input to crate::Buffer::ensure_cursor_visible,
not a derived value. The host writes top_row, top_col, width, and
height per render frame; the buffer clamps the cursor inside the
declared area.
top_row and top_col are the first visible row / column; top_col is
a char index, matching Position.
wrap and text_width together drive soft-wrap-aware scrolling and
motion. text_width is the cell width of the text area (i.e. width
minus any gutter the host renders) so the buffer can compute screen-line
splits without duplicating gutter logic.
scroll_off is not a field on Viewport itself; the host computes it
and adjusts top_row before handing the viewport to
crate::Buffer::ensure_cursor_visible.
Wrap::None / crate::Wrap::Char / crate::Wrap::Word change
which screen-row arithmetic the buffer uses. Switching mid-session is
supported but the host must call
crate::Buffer::ensure_cursor_visible afterwards.
Fields§
§top_row: usize§top_col: usize§width: u16§height: u16§wrap: WrapSoft-wrap mode the renderer + scroll math is using. Default
is Wrap::None (no wrap, horizontal scroll via top_col).
text_width: u16Cell width of the text area (after the host’s gutter is
subtracted from the editor area). Used by wrap-aware scroll
and motion code; ignored when wrap == Wrap::None. Set to 0
before the first frame; wrap math falls back to no-op then.
tab_width: u16Cells per \t expansion stop. The renderer uses this to align
tab characters; cursor_screen_pos uses it to map char column to
visual column. 0 is treated as the renderer’s fallback (4) so
hosts that don’t publish a value still render legibly.
Implementations§
Source§impl Viewport
impl Viewport
pub const fn new() -> Self
Sourcepub fn effective_tab_width(self) -> usize
pub fn effective_tab_width(self) -> usize
Effective tab width — falls back to 4 when tab_width == 0 so
uninitialized viewports still expand tabs sensibly.
Sourcepub fn bottom_row(self) -> usize
pub fn bottom_row(self) -> usize
Last document row that’s currently on screen (inclusive).
Returns top_row when height == 0 so callers don’t have
to special-case the pre-first-draw state.
Sourcepub fn contains(self, pos: Position) -> bool
pub fn contains(self, pos: Position) -> bool
True when pos lies inside the current viewport rect.
Sourcepub fn ensure_visible(&mut self, pos: Position)
pub fn ensure_visible(&mut self, pos: Position)
Adjust top_row / top_col so pos is visible, scrolling by
the minimum amount needed. Used after motions and after
content edits that move the cursor.