pub struct TUI { /* private fields */ }Expand description
Top-level TUI manager.
Owns the terminal, a list of mounted components, overlays, and a
Renderer that performs differential drawing. Only one component
receives focus at a time; it is the sole recipient of input events.
§Example
use photon_ui::{
TUI,
TestTerminal,
components::Text,
};
let mut tui = TUI::new(Box::new(TestTerminal::new(80, 24)));
tui.mount(Box::new(Text::new("Hello", 0, 0)));
tui.render_frame().unwrap();Implementations§
Source§impl TUI
impl TUI
Sourcepub fn mount(&mut self, component: Box<dyn Component>)
pub fn mount(&mut self, component: Box<dyn Component>)
Add a component to the TUI.
The component is appended to the bottom layer. If no component currently has focus, the new component receives focus automatically.
Sourcepub fn set_focus(&mut self, index: usize)
pub fn set_focus(&mut self, index: usize)
Move focus to the component at index in the bottom layer.
The previously focused component, if any, is unfocused first.
Sourcepub fn clear_children(&mut self)
pub fn clear_children(&mut self)
Remove all children and reset focus.
Sourcepub fn add_overlay(&mut self, overlay: Overlay)
pub fn add_overlay(&mut self, overlay: Overlay)
Add an overlay on top of the main UI.
Sourcepub fn clear_overlays(&mut self)
pub fn clear_overlays(&mut self)
Remove all overlays.
Sourcepub fn show_modal(&mut self, modal: Box<dyn Component>)
pub fn show_modal(&mut self, modal: Box<dyn Component>)
Show a modal dialog on top of the main UI.
The modal captures all input until it is dismissed. Focus is moved to the modal content automatically. When dismissed, focus returns to the previously focused layer.
Sourcepub fn dismiss_modal(&mut self)
pub fn dismiss_modal(&mut self)
Dismiss the currently open modal, restoring previous focus.
Sourcepub fn modal_active(&self) -> bool
pub fn modal_active(&self) -> bool
Returns true if a modal is currently open.
Sourcepub fn set_layout(&mut self, layout: Layout)
pub fn set_layout(&mut self, layout: Layout)
Set a layout for splitting the terminal area among children in the bottom layer.
Sourcepub fn clear_layout(&mut self)
pub fn clear_layout(&mut self)
Clear the layout, reverting to vertical stacking.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset the TUI for a fresh page / screen.
Clears all layers, overlays, and layout, and schedules a full screen redraw so no stale content or ANSI attributes bleed through.
Sourcepub fn add_layer(&mut self, layer: Layer) -> usize
pub fn add_layer(&mut self, layer: Layer) -> usize
Add a new layer on top of the stack and return its index.
Sourcepub fn insert_layer(&mut self, index: usize, layer: Layer)
pub fn insert_layer(&mut self, index: usize, layer: Layer)
Insert a layer at the given index.
Sourcepub fn remove_layer(&mut self, index: usize) -> Option<Layer>
pub fn remove_layer(&mut self, index: usize) -> Option<Layer>
Remove the layer at the given index.
Sourcepub fn layer_mut(&mut self, index: usize) -> Option<&mut Layer>
pub fn layer_mut(&mut self, index: usize) -> Option<&mut Layer>
Borrow the layer at the given index mutably.
Sourcepub fn layer_count(&self) -> usize
pub fn layer_count(&self) -> usize
Return the number of layers.
Sourcepub fn set_focused_layer(&mut self, index: usize)
pub fn set_focused_layer(&mut self, index: usize)
Move input focus to the given layer.
Sourcepub fn stop(&mut self) -> Result<()>
pub fn stop(&mut self) -> Result<()>
Restore the terminal (leave alternate screen, disable raw mode, show cursor).
Sourcepub fn render_frame(&mut self) -> Result<()>
pub fn render_frame(&mut self) -> Result<()>
Render one frame to the terminal.
- Queries terminal size.
- Decides
RenderStrategy(first render, full redraw on resize, or diff). - Renders all layers front-to-back into a composite screen buffer, culling cells hidden by higher layers.
- Renders overlays and modal on top of the layer stack.
- Deletes stale terminal images.
- Writes the result through the
Renderer. - Positions the hardware cursor.
Sourcepub fn handle_input(&mut self, event: &Event)
pub fn handle_input(&mut self, event: &Event)
Dispatch an event to the focused component, falling back to other
components if the focused one returns crate::InputResult::Ignored.
Also handles Tab to cycle focus between focusable components.