pub struct ViewContext {
pub size: Size,
pub theme: Arc<Theme>,
/* private fields */
}Expand description
The size this render call may draw into, plus the Theme in effect.
A ViewContext describes an allocated size, not the terminal size. When a parent component renders a child, it is responsible for narrowing the context to the child’s slot before calling Component::render. Children should treat size as authoritative — only the root component should assume the full terminal width.
§Size contract
- Parents allocate sizes and pass size-scoped contexts to children.
- Children render into the size they were given. They do not negotiate width upward.
- Composition (stacking, side-by-side layout, padding) happens on
Frame, not by reaching back into the parent’sViewContext.
§Construction
use tui::ViewContext;
// Default theme
let ctx = ViewContext::new((80, 24));
// Custom theme
use tui::Theme;
let ctx = ViewContext::new_with_theme((120, 40), Theme::default());§Size helpers
When rendering a child into part of the parent’s size, derive a new context with one of:
with_size((w, h))— Replace the entire size.with_width(w)— Replace just the width, keep the height.with_height(h)— Replace just the height, keep the width.inset(insets)— Shrink the size byInsetson each edge. Saturates at zero on each axis.
All helpers preserve the theme and (when the syntax feature is enabled) the syntax highlighter.
use tui::{Insets, ViewContext};
let parent = ViewContext::new((80, 24));
// Render a child in a left half of width 40.
let left = parent.with_width(40);
// Render a child inside a 2-column horizontal padding.
let padded = parent.inset(Insets::horizontal(2));§Size
Size holds the dimensions of an allocated render area as width (columns) and height (rows), both u16. Implements From<(u16, u16)> for convenient construction.
§Other methods
highlighter()— Access theSyntaxHighlighter(requires featuresyntax).
§See also
Frame— The render artifact returned byComponent::render. Composition primitives likefit,indent,vstack, andhstackoperate on frames, not on the parent context.Insets— Edge insets used byinset().Theme— The semantic color palette.Renderer— CreatesViewContextautomatically from its own state.
Fields§
§size: SizeThe size this context allows the component to draw into. This is not the terminal size — parents pass child contexts whose size is the slice of the terminal allocated to the child.
theme: Arc<Theme>Implementations§
Source§impl ViewContext
impl ViewContext
pub fn new(size: impl Into<Size>) -> Self
pub fn new_with_theme(size: impl Into<Size>, theme: Theme) -> Self
pub fn highlighter(&self) -> &SyntaxHighlighter
Sourcepub fn with_size(&self, size: impl Into<Size>) -> Self
pub fn with_size(&self, size: impl Into<Size>) -> Self
Clone this context with a new size, preserving theme state.
Sourcepub fn with_width(&self, width: u16) -> Self
pub fn with_width(&self, width: u16) -> Self
Clone this context with the width replaced, preserving height and theme.
Sourcepub fn with_height(&self, height: u16) -> Self
pub fn with_height(&self, height: u16) -> Self
Clone this context with the height replaced, preserving width and theme.
Trait Implementations§
Source§impl Clone for ViewContext
impl Clone for ViewContext
Source§fn clone(&self) -> ViewContext
fn clone(&self) -> ViewContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ViewContext
impl RefUnwindSafe for ViewContext
impl Send for ViewContext
impl Sync for ViewContext
impl Unpin for ViewContext
impl UnsafeUnpin for ViewContext
impl UnwindSafe for ViewContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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