-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide a builder pattern interface to the construction layout tree #275
Comments
I'm on board :) I think this is very useful when using this crate more directly. |
Additions: What alternative(s) have you considered?Using NodeRef(s) might be a better way to get information about individual nodes from the tree builder. The idea comes from Yew's NodeRef. Both interior mutability and initialization tests are provided. |
Inspired by this discussion on Discord, I have come to the opinion that an API that automatically creates a Something like: Style::build(|style| {
style.width = points(100.);
style.height = points(200.);
}); where the Style::build(|style| style.width(100.).height(200.)) where the Style::build((width(100.), height(200.)) |
I really like this pattern! |
I think something like the following would be quite easy to create: let root_node = Style::column()
.width(800)
.height(100)
.with_children(|tree| {
Style::leaf().width(800).height(100).build(&mut tree);
Style::leaf().width(800).flex_grow(1.0).build(&mut tree);
})
.build(&mut tree); This is the same as the example in the README. It's quite an improvement! |
A couple of other API idea that I think would be doable. With nested builders, closures taking let root_node_id = taffy.new_flex_column(
|style| style.width(800).height(600),
|cx| {
cx.new_leaf(|style| style.height(100));
cx.new_leaf(|style| style.flex_grow(1.0));
},
); Using a macro ( let root_node_id = taffy.new_flex_column(style! {width: 800 px, height: 600 px}, |cx| {
cx.new_leaf(style! {height: 800 px});
cx.new_leaf(style! {flex_grow: 1.0});
}); |
The builder approach is my favorite here stylistically :) Would like to avoid macros if possible, and closures can be rough for beginners. |
What problem does this solve or what need does it fill?
Provides a more compact, write-friendly, ergonomic interface
What solution would you like?
What alternative(s) have you considered?
Since many anonymous nodes are used, a method of result feedback is needed, such as having the node save a callback function, or a Trait that provides result feedback
Additional context
The sample code comes from the Yoga home page, a Java package called litho. But it can be translated quite directly to Rust code.
The text was updated successfully, but these errors were encountered: