Contains all UI components and the Component trait.
pub trait Component {
fn render(&self, renderer: &mut dyn Renderer, theme: &Theme);
fn handle_event(&mut self, event: &Event);
}Root container for applications.
pub struct Window {
pub title: String,
pub width: u32,
pub height: u32,
children: Vec<Box<dyn Component>>,
}
impl Window {
pub fn new(title: String, width: u32, height: u32) -> Self
pub fn add_child(&mut self, child: Box<dyn Component>)
}Vertical stack layout.
pub struct VStack {
spacing: f32,
padding: f32,
border: f32,
children: Vec<Box<dyn Component>>,
}
impl VStack {
pub fn new(spacing: f32) -> Self
pub fn padding(self, padding: f32) -> Self
pub fn border(self, border: f32) -> Self
pub fn add_child(&mut self, child: Box<dyn Component>)
}Horizontal stack layout.
pub struct HStack {
spacing: f32,
padding: f32,
border: f32,
children: Vec<Box<dyn Component>>,
}
impl HStack {
pub fn new(spacing: f32) -> Self
pub fn padding(self, padding: f32) -> Self
pub fn border(self, border: f32) -> Self
pub fn add_child(&mut self, child: Box<dyn Component>)
}2D grid layout.
pub struct Grid {
rows: usize,
cols: usize,
spacing: f32,
children: Vec<Vec<Option<Box<dyn Component>>>>,
}
impl Grid {
pub fn new(rows: usize, cols: usize, spacing: f32) -> Self
pub fn set_child(&mut self, row: usize, col: usize, child: Box<dyn Component>)
}Container with optional styling.
pub struct Panel {
border_width: f32,
padding: f32,
child: Option<Box<dyn Component>>,
}
impl Panel {
pub fn new(border_width: f32, padding: f32) -> Self
pub fn child(self, child: Box<dyn Component>) -> Self
}Clickable button.
pub struct Button {
label: String,
padding: f32,
border: f32,
on_click: Option<Box<dyn FnMut()>>,
}
impl Button {
pub fn new(label: String) -> Self
pub fn padding(self, padding: f32) -> Self
pub fn border(self, border: f32) -> Self
pub fn on_click<F>(self, f: F) -> Self
where F: FnMut() + 'static
}Text display component.
pub struct Label {
text: Binding<String>,
padding: f32,
}
impl Label {
pub fn new(text: Binding<String>) -> Self
pub fn padding(self, padding: f32) -> Self
}On/off switch.
pub struct Toggle {
is_on: Binding<bool>,
on_toggle: Option<Box<dyn FnMut(bool)>>,
}
impl Toggle {
pub fn new(is_on: Binding<bool>) -> Self
pub fn on_toggle<F>(self, f: F) -> Self
where F: FnMut(bool) + 'static
}Text input field.
pub struct Input {
text: Binding<String>,
placeholder: String,
}
impl Input {
pub fn new(text: Binding<String>, placeholder: String) -> Self
}State management system.
Reactive state container.
pub struct State<T> {
value: RefCell<T>,
subscribers: RefCell<Vec<Box<dyn Fn()>>>,
}
impl<T: Clone + 'static> State<T> {
pub fn new(initial: T) -> Self
pub fn get(&self) -> T
pub fn set(&self, new_value: T)
pub fn binding(&self) -> Binding<T>
}Shared reference to state.
pub struct Binding<T> {
state: Rc<State<T>>,
}
impl<T: Clone> Binding<T> {
pub fn get(&self) -> T
pub fn set(&self, new_value: T)
}Rendering engine.
Abstract rendering interface.
pub trait Renderer {
fn draw_text(&mut self, text: &str, x: f32, y: f32);
fn draw_rect(&mut self, x: f32, y: f32, w: f32, h: f32);
fn draw_line(&mut self, x1: f32, y1: f32, x2: f32, y2: f32);
fn set_color(&mut self, r: u8, g: u8, b: u8);
fn clear(&mut self);
fn present(&mut self);
}SDL2-based rendering engine.
pub struct SDLEngine {
window: sdl2::video::Window,
canvas: sdl2::render::Canvas<sdl2::video::Window>,
event_pump: sdl2::EventPump,
ttf_context: sdl2::ttf::Sdl2TtfContext,
}
impl SDLEngine {
pub fn new(title: &str, width: u32, height: u32) -> Result<Self, String>
pub fn run(&mut self, root_component: Box<dyn Component>, theme: &Theme) -> Result<(), String>
}Theming system.
Color and font configuration.
pub struct Theme {
pub primary_color: (u8, u8, u8),
pub secondary_color: (u8, u8, u8),
pub background_color: (u8, u8, u8),
pub text_color: (u8, u8, u8),
pub font_size: u32,
}
impl Theme {
pub fn default() -> Self
}pub enum Event {
Click { x: f32, y: f32 },
Hover { x: f32, y: f32 },
KeyPress(char),
Drag { dx: f32, dy: f32 },
Resize { width: u32, height: u32 },
}Most functions return Result<T, String> for error handling. SDL2 errors are propagated as strings.