Use the Tui layout engine in egui
mainTo use egui_taffy, initialize a tui instance from an egui::Ui object. You can then use a builder pattern to define styles and add elements.
Key capabilities:
- Reserve Space: Use
.reserve_available_space()to allow the layout to occupy the full available window space. - Styling: Use
.style(Style { ... })to set layout properties likeflex_direction,align_items, orflex_wrap. You can also use.mut_style(|style| { ... })to modify specific fields. - Adding Elements:
- Standard egui UI: Use
.ui(|ui| { ... })to pass a standardegui::Uiblock. - Direct Widgets: Use
.ui_add(widget)for widgets implementing theTuiWidgettrait. - Helpers: Use convenience methods like
.label("text")or.separator(). - Borders: Use
.add_with_border(|tui| { ... })to add elements with a custom background/border.
- Standard egui UI: Use
- Layout Types: Supports CSS Block, Flexbox, and Grid layouts.
egui::Window::new("Flex wrap demo").show(ctx, |ui| {
tui(ui, ui.id().with("demo"))
.reserve_available_space()
.style(Style {
flex_direction: taffy::FlexDirection::Column,
align_items: Some(taffy::AlignItems::Stretch),
..default_style()
})
.show(|tui| {
// Add egui ui as node
tui.ui(|ui| {
ui.label("Hello from egui ui!");
ui.button("Egui button");
});
// Add egui widgets directly
tui.ui_add(egui::Label::new("label"));
// Use helper functions
tui.separator();
tui.label("Text");
// Custom style via builder
tui.style(Style {
align_self: Some(taffy::AlignItems::Center),
..Default::default()
})
.label("Centered text");
// Modify existing style
tui.style(default_style())
.mut_style(|style| {
style.align_self = Some(taffy::AlignItems::End);
})
.label("Right aligned text");
// Add with border
tui.add_with_border(|tui| {
tui.label("Text with border");
});
});
});