Improve Rust API for UI
Closed this issue · 3 comments
Right now the code follows pretty much the way the C API works with some improvements which is similar to what's in the C++ API.
Some ideas:
- Have something similar to "with-open-file" which is used in Common Lisp for example:
(with-open-file (stream "/some/file/name.txt")
(format t "~a~%" (read-line stream)))
Something like
ui.with_item_size(20.0, || {
// Some code
});
Other ideas:
- Use builders more. This could improve cases where where above isn't needed and you could do
ui.text("foo").color(Color::from_rgb(0xff, 0, 0).build();
cc @SlNPacifist
I can't see full picture right now, but several helpers would be useful.
ImGui has lots of paired set/pop functions that set context. This includes push_style_var/pop_style_var, push_style_color/pop_style_color, push_id/pop_id, etc. Context builder could be used like this:
ui.context()
.style(style1)
.color(color2)
.exec(|| {
your code here;
});
Second big issue with current are buffers in text inputs. Whenever one wants to use input, he has to create new buffer and perform manual conversion. This could be done much easier if Ui had some wrapper that would do all the memory allocation and string conversion.
Third thing are functions with optional parameters. I guess builders are better for functions with optional parameters.
Agreed. I tried this yesterday:
https://github.com/emoon/ProDBG/blob/master/api/rust/prodbg/src/ui.rs#L732
So using this work like this
ui.tree_node("foo").show(|ui| {
ui.text("bar");
});
The ui.text(...)
call will only be made if user unfolds the tree (and pop_tree
) gets auto called.
Switching Qt and C++ (for now, will be reevaluated when there are some good Rust bindings for Qt) so closing.