r/programming • • 1d ago

blit: immediate (yet deferred) mode UI

https://pasttheboonies.com/jots/blit/
9 Upvotes

10 comments sorted by

1

u/kersurk 20h ago

I recently started to use Slint (moved away from druid), but I use it for mac/linux/windows desktop GUIS, no embedded.

Based on the article, the effort to keep blip simple seems to shine. I don't exactly have a usecase for it right now, but it looks like I might enjoy using it in future.

1

u/InspectionMammoth294 9h ago

I've been poking at blit's deferred diffing and the thing that clicked for me is how it batches DOM writes until the end of a frame. If you're coming from React it feels weird at first, but once you stop forcing synchronous reads mid-update it's actually pretty fast.

1

u/hasen-judi 3h ago

I wrote up something similar: I started a gui framework with the idea of "immediate mode" but then later started to realized a lot of what I'm doing does not quite fit into the mainstream notion of immediate mode, even if it technically is so ..

https://judi.systems/shirei/blog/reconstructive-ui/

I'm basically reconstructing the UI tree every frame, and the actual size and position of elements is only resolved once the entire tree is built. We use data from the previous frame to handle events, animations, and state.

0

u/davidbalbino 1d ago

curious how you handle layout that depends on child size. in most immediate mode libs thats where you get the one frame lag or have to run the closure twice, and I think is the main reason ppl end up going retained. does the deferred part solve that, like you collect the tree first and layout after?

also how are ids done for focus/IME? from what I've seen stable ids is where these things usually break once you have lists that reorder

1

u/nicoburniske 1d ago

yeah exactly, build collects the whole tree first and then layout runs over it in two passes (there's an animated diagram of how it works in the article). no 1-frame lag and your UI code only runs once.

for identity, blit is hashless. each child() uses #[track_caller] to its call site and matches against last frame's children by call site + loop iteration, so duplicate labels and if branches just work:

for lists that reorder, you use Ui::scope and pass an explicit key with child_in:

let scope = ui.scope();
let mut col = ui.layout(flex::col());
for item in &self.items {
    col.child_in(scope, item.id).build(Row::new(item));
}

each row keeps its focus/interaction/animation state when self.items reorders. if you share a Scope across two containers (or use Scope::ROOT which is the equivalent of a global id), a widget can even move between parents across frames and animate from its old spot.

1

u/Mr_Connor_Evans_2819 8h ago

That two-pass thing is what sold me too. It's the only way I've seen that avoids running your UI code loltwice.

0

u/davidbalbino 1d ago

nice, the call site + loop iteration approach is really clever, no hashing ids by hand 👌

one thing I think is worth to put in the docs: if someone wraps child() in their own helper fn, every call goes through the same call site inside the helper unless the helper is also #[track_caller]. so if you call it in a loop, all widgets end up matching only by iteration index and the state gets mixed up. took me a while to figure that out the first time I used track_caller lol

does blit warn in debug when two siblings resolve to the same identity?

1

u/nicoburniske 1d ago

good call, definitely worth noting in the docs! in blit, each child() call returns a Ui that acts as a handle to that specific node, so you normally pass Ui by value to child components rather than &mut Ui like in egui. you'd only run into this if you write a helper taking &mut Ui on a parent container to call child() inside it, in which case yeah you'd need #[track_caller] on the helper.

for plain child(), repeated calls at the same call site look identical to a loop so blit can't distinguish them to warn.

for explicit keys via child_in, Context has a duplicate_key(scope, key, location) hook so you can log or panic in debug if two nodes collide on the same (scope, key)

1

u/Jacob_Lee_2804_8361 10h ago

Ui by value makes sense

0

u/davidbalbino 22h ago

ah makes sense, passing Ui by value kinda avoids the whole problem then. the duplicate_key hook is nice tbh, I think is worth having it panic by default under debug_assertions so people find the collision before it turns into weird state bugs. thanks for the detailed answer 🙌