pub struct Context { /* private fields */ }Expand description
The main rendering context passed to your closure each frame.
Provides all methods for building UI: text, containers, widgets, and event
handling. You receive a &mut Context on every frame and describe what to
render by calling its methods. SLT collects those calls, lays them out with
flexbox, diffs against the previous frame, and flushes only changed cells.
§Example
slt::run(|ui: &mut slt::Context| {
if ui.key('q') { ui.quit(); }
ui.text("Hello, world!").bold();
});Implementations§
Source§impl Context
impl Context
Sourcepub fn widget<W: Widget>(&mut self, w: &mut W) -> W::Response
pub fn widget<W: Widget>(&mut self, w: &mut W) -> W::Response
Render a custom Widget.
Calls Widget::ui with this context and returns the widget’s response.
Sourcepub fn interaction(&mut self) -> Response
pub fn interaction(&mut self) -> Response
Allocate a click/hover interaction slot and return the Response.
Use this in custom widgets to detect mouse clicks and hovers without wrapping content in a container. Each call reserves one slot in the hit-test map, so the call order must be stable across frames.
Sourcepub fn register_focusable(&mut self) -> bool
pub fn register_focusable(&mut self) -> bool
Register a widget as focusable and return whether it currently has focus.
Call this in custom widgets that need keyboard focus. Each call increments the internal focus counter, so the call order must be stable across frames.
Sourcepub fn text(&mut self, s: impl Into<String>) -> &mut Self
pub fn text(&mut self, s: impl Into<String>) -> &mut Self
Render a text element. Returns &mut Self for style chaining.
§Example
use slt::Color;
ui.text("hello").bold().fg(Color::Cyan);Sourcepub fn text_wrap(&mut self, s: impl Into<String>) -> &mut Self
pub fn text_wrap(&mut self, s: impl Into<String>) -> &mut Self
Render a text element with word-boundary wrapping.
Long lines are broken at word boundaries to fit the container width.
Style chaining works the same as Context::text.
Sourcepub fn dim(&mut self) -> &mut Self
pub fn dim(&mut self) -> &mut Self
Apply dim styling to the last rendered text element.
Also sets the foreground color to the theme’s text_dim color if no
explicit foreground has been set.
Sourcepub fn strikethrough(&mut self) -> &mut Self
pub fn strikethrough(&mut self) -> &mut Self
Apply strikethrough to the last rendered text element.
Sourcepub fn fg(&mut self, color: Color) -> &mut Self
pub fn fg(&mut self, color: Color) -> &mut Self
Set the foreground color of the last rendered text element.
Sourcepub fn bg(&mut self, color: Color) -> &mut Self
pub fn bg(&mut self, color: Color) -> &mut Self
Set the background color of the last rendered text element.
Sourcepub fn styled(&mut self, s: impl Into<String>, style: Style) -> &mut Self
pub fn styled(&mut self, s: impl Into<String>, style: Style) -> &mut Self
Render a text element with an explicit Style applied immediately.
Equivalent to calling text(s) followed by style-chain methods, but
more concise when you already have a Style value.
Sourcepub fn wrap(&mut self) -> &mut Self
pub fn wrap(&mut self) -> &mut Self
Enable word-boundary wrapping on the last rendered text element.
Sourcepub fn col_gap(&mut self, gap: u32, f: impl FnOnce(&mut Context)) -> Response
pub fn col_gap(&mut self, gap: u32, f: impl FnOnce(&mut Context)) -> Response
Create a vertical (column) container with a gap between children.
gap is the number of blank rows inserted between each child.
Sourcepub fn row_gap(&mut self, gap: u32, f: impl FnOnce(&mut Context)) -> Response
pub fn row_gap(&mut self, gap: u32, f: impl FnOnce(&mut Context)) -> Response
Create a horizontal (row) container with a gap between children.
gap is the number of blank columns inserted between each child.
Sourcepub fn container(&mut self) -> ContainerBuilder<'_>
pub fn container(&mut self) -> ContainerBuilder<'_>
Create a container with a fluent builder.
Use this for borders, padding, grow, constraints, and titles. Chain
configuration methods on the returned ContainerBuilder, then call
.col() or .row() to finalize.
§Example
use slt::Border;
ui.container()
.border(Border::Rounded)
.pad(1)
.title("My Panel")
.col(|ui| {
ui.text("content");
});Sourcepub fn scrollable(&mut self, state: &mut ScrollState) -> ContainerBuilder<'_>
pub fn scrollable(&mut self, state: &mut ScrollState) -> ContainerBuilder<'_>
Create a scrollable container. Handles wheel scroll and drag-to-scroll automatically.
Pass a ScrollState to persist scroll position across frames. The state
is updated in-place with the current scroll offset and bounds.
§Example
let mut scroll = ScrollState::new();
ui.scrollable(&mut scroll).col(|ui| {
for i in 0..100 {
ui.text(format!("Line {i}"));
}
});Sourcepub fn bordered(&mut self, border: Border) -> ContainerBuilder<'_>
pub fn bordered(&mut self, border: Border) -> ContainerBuilder<'_>
Shortcut for container().border(border).
Returns a ContainerBuilder pre-configured with the given border style.
Sourcepub fn grow(&mut self, value: u16) -> &mut Self
pub fn grow(&mut self, value: u16) -> &mut Self
Set the flex-grow factor of the last rendered text element.
A value of 1 causes the element to expand and fill remaining space
along the main axis.
Sourcepub fn align(&mut self, align: Align) -> &mut Self
pub fn align(&mut self, align: Align) -> &mut Self
Set the text alignment of the last rendered text element.
Sourcepub fn spacer(&mut self) -> &mut Self
pub fn spacer(&mut self) -> &mut Self
Render an invisible spacer that expands to fill available space.
Useful for pushing siblings to opposite ends of a row or column.
Sourcepub fn text_input(&mut self, state: &mut TextInputState) -> &mut Self
pub fn text_input(&mut self, state: &mut TextInputState) -> &mut Self
Render a single-line text input. Auto-handles cursor, typing, and backspace.
The widget claims focus via Context::register_focusable. When focused,
it consumes character, backspace, arrow, Home, and End key events.
§Example
let mut input = TextInputState::with_placeholder("Search...");
ui.text_input(&mut input);
// input.value holds the current textSourcepub fn spinner(&mut self, state: &SpinnerState) -> &mut Self
pub fn spinner(&mut self, state: &SpinnerState) -> &mut Self
Render an animated spinner.
The spinner advances one frame per tick. Use SpinnerState::dots or
SpinnerState::line to create the state, then chain style methods to
color it.
Sourcepub fn toast(&mut self, state: &mut ToastState) -> &mut Self
pub fn toast(&mut self, state: &mut ToastState) -> &mut Self
Render toast notifications. Calls state.cleanup(tick) automatically.
Expired messages are removed before rendering. If there are no active
messages, nothing is rendered and self is returned unchanged.
Sourcepub fn textarea(
&mut self,
state: &mut TextareaState,
visible_rows: u32,
) -> &mut Self
pub fn textarea( &mut self, state: &mut TextareaState, visible_rows: u32, ) -> &mut Self
Render a multi-line text area with the given number of visible rows.
When focused, handles character input, Enter (new line), Backspace, arrow keys, Home, and End. The cursor is rendered as a block character.
Sourcepub fn progress(&mut self, ratio: f64) -> &mut Self
pub fn progress(&mut self, ratio: f64) -> &mut Self
Render a progress bar (20 chars wide). ratio is clamped to 0.0..=1.0.
Uses block characters (█ filled, ░ empty). For a custom width use
Context::progress_bar.
Sourcepub fn progress_bar(&mut self, ratio: f64, width: u32) -> &mut Self
pub fn progress_bar(&mut self, ratio: f64, width: u32) -> &mut Self
Render a progress bar with a custom character width.
ratio is clamped to 0.0..=1.0. width is the total number of
characters rendered.
Sourcepub fn list(&mut self, state: &mut ListState) -> &mut Self
pub fn list(&mut self, state: &mut ListState) -> &mut Self
Render a selectable list. Handles Up/Down (and k/j) navigation when focused.
The selected item is highlighted with the theme’s primary color. If the list is empty, nothing is rendered.
Sourcepub fn table(&mut self, state: &mut TableState) -> &mut Self
pub fn table(&mut self, state: &mut TableState) -> &mut Self
Render a data table with column headers. Handles Up/Down selection when focused.
Column widths are computed automatically from header and cell content. The selected row is highlighted with the theme’s selection colors.
Sourcepub fn tabs(&mut self, state: &mut TabsState) -> &mut Self
pub fn tabs(&mut self, state: &mut TabsState) -> &mut Self
Render a tab bar. Handles Left/Right navigation when focused.
The active tab is rendered in the theme’s primary color. If the labels list is empty, nothing is rendered.
Render a clickable button. Returns true when activated via Enter, Space, or mouse click.
The button is styled with the theme’s primary color when focused and the accent color when hovered.
Sourcepub fn checkbox(
&mut self,
label: impl Into<String>,
checked: &mut bool,
) -> &mut Self
pub fn checkbox( &mut self, label: impl Into<String>, checked: &mut bool, ) -> &mut Self
Render a checkbox. Toggles the bool on Enter, Space, or click.
The checked state is shown with the theme’s success color. When focused,
a ▸ prefix is added.
Sourcepub fn toggle(&mut self, label: impl Into<String>, on: &mut bool) -> &mut Self
pub fn toggle(&mut self, label: impl Into<String>, on: &mut bool) -> &mut Self
Render an on/off toggle switch.
Toggles on when activated via Enter, Space, or click. The switch
renders as ●━━ ON or ━━● OFF colored with the theme’s success or
dim color respectively.
Sourcepub fn separator(&mut self) -> &mut Self
pub fn separator(&mut self) -> &mut Self
Render a horizontal divider line.
The line is drawn with the theme’s border color and expands to fill the container width.
Sourcepub fn help(&mut self, bindings: &[(&str, &str)]) -> &mut Self
pub fn help(&mut self, bindings: &[(&str, &str)]) -> &mut Self
Render a help bar showing keybinding hints.
bindings is a slice of (key, action) pairs. Keys are rendered in the
theme’s primary color; actions in the dim text color. Pairs are separated
by a · character.
Sourcepub fn key(&self, c: char) -> bool
pub fn key(&self, c: char) -> bool
Check if a character key was pressed this frame.
Returns true if the key event has not been consumed by another widget.
Sourcepub fn key_code(&self, code: KeyCode) -> bool
pub fn key_code(&self, code: KeyCode) -> bool
Check if a specific key code was pressed this frame.
Returns true if the key event has not been consumed by another widget.
Sourcepub fn key_mod(&self, c: char, modifiers: KeyModifiers) -> bool
pub fn key_mod(&self, c: char, modifiers: KeyModifiers) -> bool
Check if a character key with specific modifiers was pressed this frame.
Returns true if the key event has not been consumed by another widget.
Sourcepub fn mouse_down(&self) -> Option<(u32, u32)>
pub fn mouse_down(&self) -> Option<(u32, u32)>
Return the position of a left mouse button down event this frame, if any.
Returns None if no unconsumed mouse-down event occurred.
Sourcepub fn mouse_pos(&self) -> Option<(u32, u32)>
pub fn mouse_pos(&self) -> Option<(u32, u32)>
Return the current mouse cursor position, if known.
The position is updated on every mouse move or click event. Returns
None until the first mouse event is received.
Sourcepub fn scroll_down(&self) -> bool
pub fn scroll_down(&self) -> bool
Check if an unconsumed scroll-down event occurred this frame.
Sourcepub fn set_theme(&mut self, theme: Theme)
pub fn set_theme(&mut self, theme: Theme)
Change the theme for subsequent rendering.
All widgets rendered after this call will use the new theme’s colors.
Sourcepub fn tick(&self) -> u64
pub fn tick(&self) -> u64
Get the current tick count (increments each frame).
Useful for animations and time-based logic. The tick starts at 0 and increases by 1 on every rendered frame.
Sourcepub fn debug_enabled(&self) -> bool
pub fn debug_enabled(&self) -> bool
Return whether the layout debugger is enabled.
The debugger is toggled with F12 at runtime.