Skip to main content

ChatViewport

Struct ChatViewport 

Source
pub struct ChatViewport {
    pub scroll_offset: usize,
    pub scroll_target: usize,
    pub scroll_pos: f32,
    pub scrollbar_thumb_top: f32,
    pub scrollbar_thumb_size: f32,
    pub auto_scroll: bool,
    pub width: u16,
    pub message_heights: Vec<usize>,
    pub message_heights_width: u16,
    pub dirty_from: Option<usize>,
    pub height_prefix_sums: Vec<usize>,
    pub prefix_sums_width: u16,
}
Expand description

Single owner of all chat layout state: scroll, per-message heights, and prefix sums.

Consolidates state previously scattered across App (scroll fields, prefix sums), ChatMessage (cached_visual_height/cached_visual_width), and BlockCache (wrapped_height/wrapped_width). Per-block heights remain on BlockCache via set_height() / height_at(), but the viewport owns the validity width that governs whether those caches are considered current. On resize, on_frame() zeroes message heights and clears prefix sums, causing the next update_visual_heights() pass to re-measure every message using ground-truth Paragraph::line_count().

Fields§

§scroll_offset: usize

Rendered scroll offset (rounded from scroll_pos).

§scroll_target: usize

Target scroll offset requested by user input or auto-scroll.

§scroll_pos: f32

Smooth scroll position (fractional) for animation.

§scrollbar_thumb_top: f32

Smoothed scrollbar thumb top row (fractional) for animation.

§scrollbar_thumb_size: f32

Smoothed scrollbar thumb height (fractional) for animation.

§auto_scroll: bool

Whether to auto-scroll to bottom on new content.

§width: u16

Current terminal width. Set by on_frame() each render cycle.

§message_heights: Vec<usize>

Visual height (in terminal rows) of each message, indexed by message position. Zeroed on resize; rebuilt by measure_message_height().

§message_heights_width: u16

Width at which message_heights was last computed.

§dirty_from: Option<usize>

Oldest message index whose cached height may be stale.

§height_prefix_sums: Vec<usize>

Cumulative heights: height_prefix_sums[i] = sum of heights 0..=i. Enables O(log n) binary search for first visible message and O(1) total height.

§prefix_sums_width: u16

Width at which prefix sums were last computed.

Implementations§

Source§

impl ChatViewport

Source

pub fn new() -> Self

Create a new viewport with default scroll state (auto-scroll enabled).

Source

pub fn on_frame(&mut self, width: u16)

Called at top of each render frame. Detects width change and invalidates all cached heights so they get re-measured at the new width.

Source

pub fn message_height(&self, idx: usize) -> usize

Get the cached visual height for message idx. Returns 0 if not yet computed.

Source

pub fn set_message_height(&mut self, idx: usize, h: usize)

Set the visual height for message idx, growing the vec if needed.

Does NOT update message_heights_width - the caller must call mark_heights_valid() after the full re-measurement pass completes.

Source

pub fn mark_heights_valid(&mut self)

Mark all message heights as valid at the current width. Call after update_visual_heights() finishes re-measuring.

Source

pub fn mark_message_dirty(&mut self, idx: usize)

Mark cached heights dirty from idx onward.

Source

pub fn rebuild_prefix_sums(&mut self)

Rebuild prefix sums from message_heights. O(1) fast path when width unchanged and only the last message changed (streaming).

Source

pub fn total_message_height(&self) -> usize

Total height of all messages (O(1) via prefix sums).

Source

pub fn cumulative_height_before(&self, idx: usize) -> usize

Cumulative height of messages 0..idx (O(1) via prefix sums).

Source

pub fn find_first_visible(&self, scroll_offset: usize) -> usize

Binary search for the first message whose cumulative range overlaps scroll_offset.

Source

pub fn scroll_up(&mut self, lines: usize)

Scroll up by lines. Disables auto-scroll.

Source

pub fn scroll_down(&mut self, lines: usize)

Scroll down by lines. Auto-scroll re-engagement handled by render.

Source

pub fn engage_auto_scroll(&mut self)

Re-engage auto-scroll (stick to bottom).

Trait Implementations§

Source§

impl Default for ChatViewport

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more