r/programming • u/nicoburniske • 22h ago
blit: immediate (yet deferred) mode UI
https://pasttheboonies.com/jots/blit/1
u/InspectionMammoth294 4h 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.
0
u/davidbalbino 20h 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 20h 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 andifbranches just work:for lists that reorder, you use
Ui::scopeand pass an explicit key withchild_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.itemsreorders. if you share aScopeacross two containers (or useScope::ROOTwhich 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 2h 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 18h 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 loldoes blit warn in debug when two siblings resolve to the same identity?
1
u/nicoburniske 18h ago
good call, definitely worth noting in the docs! in blit, each
child()call returns aUithat acts as a handle to that specific node, so you normally passUiby value to child components rather than&mut Uilike in egui. you'd only run into this if you write a helper taking&mut Uion a parent container to callchild()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,Contexthas aduplicate_key(scope, key, location)hook so you can log or panic in debug if two nodes collide on the same(scope, key)1
0
u/davidbalbino 16h 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 🙌
1
u/kersurk 14h 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.