# blit
blit is an experimental immediate-mode ui toolkit for rust.
the core is ~5k lines of `no_std`, zero-dependency rust with a fast, composable api for layout, interaction and animation.
https://github.com/user-attachments/assets/0350260c-592f-4337-b541-9762faf7a96d
## try it
terminal:
```sh
nix develop --command cargo run --example tui-demo
```
desktop + CPU:
```sh
nix develop --command cargo run --release --example desktop-demo
```
desktop + GPU:
```sh
nix develop --command cargo run -p blit-demo --no-default-features --features gpu --release --example desktop-demo
```
## example
each frame, blit:
- builds widgets into a tree of nodes
- solves layouts and assigns final rectangles
- paints each node (usually recording draw commands) with its resolved area
```rust
use blit::KeyboardEvent;
use blit_tui::{
Ui,
interact,
color::Color,
layout::{Align, Justify, flex},
text::{Text, HorizontalAlign, TextAttributes, TextOptions},
widgets::block::{Block, Border},
};
fn main() -> std::io::Result<()> {
blit_tui::run(|ui| {
// layout arranges the root's children in a column
// pad insets children while painted content keeps the full box
let mut root = ui.layout(flex::column().gap(1)).pad(1);
let quit = root
// the header sizes itself to its contents
.child()
// the header returns whether its button was clicked
.build(|ui: Ui<'_>| {
let mut header = ui
.layout(flex::row().align(Align::Start).justify(Justify::SpaceBetween))
.pad(1);
// insert draws content directly on the node
header.insert(Block::new().border(Border::new(Color::BLUE)));
// title
header
.child()
.insert(Text::new("hello from blit!").attributes(TextAttributes::BOLD));
header
.child()
.item(flex::item().width(8).height(1))
.build(|mut ui: Ui<'_>| {
let press = ui.interact(interact::press());
ui.insert(Block::new().background(Color::BLUE));
ui.insert(
Text::new("quit")
.options(TextOptions::new().horizontal_align(HorizontalAlign::Center)),
);
press.clicked
})
});
let quit_key = root.interact(interact::keyboard(|input| match input {
KeyboardEvent::Text("q") => Some(()),
_ => None,
}));
if quit || quit_key.is_some() {
root.context_mut().quit = true;
}
})
}
```
## install
tui:
```sh
cargo add blit-ui blit-tui
```
desktop (pick `cpu` or `gpu`):
```sh
cargo add blit-ui blit-gui blit-gui-widget blit-text-parley
cargo add blit-desktop --features cpu
```
`blit-ui` exposes its library as `blit`, so import it with `use blit::...`.
## crates
- `blit-ui`: the core
- `blit-widget`: layouts, widgets and interactions
- `blit-tui`: runs blit in a terminal
- `blit-desktop`: runs blit in a native window (macos + wayland)
for custom backends:
- `blit-gui`: 2d drawing commands
- `blit-cpu` and `blit-gpu`: renderers for `blit-gui`
- `blit-text-parley`: text layout with parley
- `blit-gui-widget`: widgets that need `blit-gui`, like text input
## acknowledgments
blit wouldn't exist without the people who shared their work and ideas so openly.
- [clay](https://github.com/nicbarker/clay) by nic barker, the biggest inspiration
- [dear imgui](https://github.com/ocornut/imgui) by omar cornut
- [egui](https://github.com/emilk/egui) by emil ernerfeldt
- [hashless imgui](https://www.youtube.com/watch?v=h4GQfQmNi_4) by anton mikhailov
thank you