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(())
}