Skip to main content

Backend

Trait Backend 

Source
pub trait Backend {
    // Required methods
    fn size(&self) -> (u32, u32);
    fn buffer_mut(&mut self) -> &mut Buffer;
    fn flush(&mut self) -> Result<()>;
}
Expand description

Rendering backend for SLT.

Implement this trait to render SLT UIs to custom targets — alternative terminals, GUI embeds, test harnesses, WASM canvas, etc.

The built-in terminal backend (run(), run_with()) handles setup, teardown, and event polling automatically. For custom backends, pair this trait with AppState and frame() to drive the render loop yourself.

§Example

use slt::{Backend, AppState, Buffer, Rect, RunConfig, Context, Event};

struct MyBackend {
    buffer: Buffer,
}

impl Backend for MyBackend {
    fn size(&self) -> (u32, u32) {
        (self.buffer.area.width, self.buffer.area.height)
    }
    fn buffer_mut(&mut self) -> &mut Buffer {
        &mut self.buffer
    }
    fn flush(&mut self) -> std::io::Result<()> {
        // Render self.buffer to your target
        Ok(())
    }
}

fn main() -> std::io::Result<()> {
    let mut backend = MyBackend {
        buffer: Buffer::empty(Rect::new(0, 0, 80, 24)),
    };
    let mut state = AppState::new();
    let config = RunConfig::default();

    loop {
        let events: Vec<Event> = vec![]; // Collect your own events
        if !slt::frame(&mut backend, &mut state, &config, &events, &mut |ui| {
            ui.text("Hello from custom backend!");
        })? {
            break;
        }
    }
    Ok(())
}

Required Methods§

Source

fn size(&self) -> (u32, u32)

Returns the current display size as (width, height) in cells.

Source

fn buffer_mut(&mut self) -> &mut Buffer

Returns a mutable reference to the display buffer.

SLT writes the UI into this buffer each frame. After frame() returns, call flush() to present the result.

Source

fn flush(&mut self) -> Result<()>

Flush the buffer contents to the display.

Called automatically at the end of each frame() call. Implementations should present the current buffer to the user — by writing ANSI escapes, drawing to a canvas, updating a texture, etc.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Backend for InlineTerminal

Available on crate feature crossterm only.
Source§

impl Backend for Terminal

Available on crate feature crossterm only.