Skip to main content

TUI

Struct TUI 

Source
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

Source

pub fn new(terminal: Box<dyn Terminal>) -> Self

Create a new TUI backed by the given terminal.

Source

pub fn terminal(&self) -> &dyn Terminal

Borrow the underlying terminal.

Source

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.

Source

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.

Source

pub fn clear_children(&mut self)

Remove all children and reset focus.

Source

pub fn add_overlay(&mut self, overlay: Overlay)

Add an overlay on top of the main UI.

Source

pub fn clear_overlays(&mut self)

Remove all overlays.

Source

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.

Source

pub fn dismiss_modal(&mut self)

Dismiss the currently open modal, restoring previous focus.

Source

pub fn modal_active(&self) -> bool

Returns true if a modal is currently open.

Source

pub fn set_layout(&mut self, layout: Layout)

Set a layout for splitting the terminal area among children in the bottom layer.

Source

pub fn clear_layout(&mut self)

Clear the layout, reverting to vertical stacking.

Source

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.

Source

pub fn add_layer(&mut self, layer: Layer) -> usize

Add a new layer on top of the stack and return its index.

Source

pub fn insert_layer(&mut self, index: usize, layer: Layer)

Insert a layer at the given index.

Source

pub fn remove_layer(&mut self, index: usize) -> Option<Layer>

Remove the layer at the given index.

Source

pub fn layer_mut(&mut self, index: usize) -> Option<&mut Layer>

Borrow the layer at the given index mutably.

Source

pub fn layer_count(&self) -> usize

Return the number of layers.

Source

pub fn set_focused_layer(&mut self, index: usize)

Move input focus to the given layer.

Source

pub fn stop(&mut self) -> Result<()>

Restore the terminal (leave alternate screen, disable raw mode, show cursor).

Source

pub fn render_frame(&mut self) -> Result<()>

Render one frame to the terminal.

  1. Queries terminal size.
  2. Decides RenderStrategy (first render, full redraw on resize, or diff).
  3. Renders all layers front-to-back into a composite screen buffer, culling cells hidden by higher layers.
  4. Renders overlays and modal on top of the layer stack.
  5. Deletes stale terminal images.
  6. Writes the result through the Renderer.
  7. Positions the hardware cursor.
Source

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.

Auto Trait Implementations§

§

impl !RefUnwindSafe for TUI

§

impl !Send for TUI

§

impl !Sync for TUI

§

impl !UnwindSafe for TUI

§

impl Freeze for TUI

§

impl Unpin for TUI

§

impl UnsafeUnpin for TUI

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, 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, 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.