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
feat(widget): add render_ref method to Widget trait #16
Conversation
harrypunk said: Super helpful feature. I'm looking for a way of rendering a dynamic widget. |
kroonhorstdino said: Would definitely like to see this feature merged! Would make my life so much easier, right now you have to work around so much limitations in that regard. |
I ran into a similar issue with the same root cause (rendering consumes the widget, which makes this call sometimes annoying - particularly as a rust newb). I'd be wary of this implementation however as it breaks all custom widgets, unless I'm mistaken? |
Widget is a trait. Why not have a RefWidget trait also? This would allow backwards compatability with the existing widgets, and allow for rendering by ref. I think though, a bigger thing that's a bit confusing at first about the way ratatui is designed is that widgets aren't supposed to be a place to store state - (I'm not sure of the exact right wording here but heres a try) They are basically the builder pattern for all the arguments to A similar thing is true for several of the small structs and enums that the the widgets consume for arguments (e.g. When writing the code it can feel expensive... "why am i calling this all the time?" and similar questions come up. One of the places this gets a bit hairy is that |
I'm going to suggest that this one be deferred until we resolve the answers to: |
This issue was closed due to it becoming stale. Feel free to reopen and work on this. |
1 similar comment
This issue was closed due to it becoming stale. Feel free to reopen and work on this. |
Many widgets can be rendered without changing their state. This commit implements The `Widget` trait for various references to widgets and changes their implementations to be immutable. This allows us to render widgets without consuming them by passing a ref to the widget when calling `Frame::render_widget()`. ```rust // this might be stored in a struct let paragraph = Paragraph::new("Hello world!"); let [left, right] = area.split(&Layout::horizontal([20, 20])); frame.render_widget(¶graph, left); frame.render_widget(¶graph, right); // we can reuse the widget ``` - Clear - Block - Tabs - Sparkline - Paragraph - Gauge - Calendar Other widgets will be implemented in follow up commits. Fixes: #164 Replaces PRs: #122 and #16 Enables: #132 Validated as a viable working solution by: #836
Many widgets can be rendered without changing their state. This commit implements The `Widget` trait for various references to widgets and changes their implementations to be immutable. This allows us to render widgets without consuming them by passing a ref to the widget when calling `Frame::render_widget()`. ```rust // this might be stored in a struct let paragraph = Paragraph::new("Hello world!"); let [left, right] = area.split(&Layout::horizontal([20, 20])); frame.render_widget(¶graph, left); frame.render_widget(¶graph, right); // we can reuse the widget ``` - Clear - Block - Tabs - Sparkline - Paragraph - Gauge - Calendar Other widgets will be implemented in follow up commits. Fixes: #164 Replaces PRs: #122 and #16 Enables: #132 Validated as a viable working solution by: #836
Many widgets can be rendered without changing their state. This commit implements The `Widget` trait for references to widgets and changes their implementations to be immutable. This allows us to render widgets without consuming them by passing a ref to the widget when calling `Frame::render_widget()`. ```rust // this might be stored in a struct let paragraph = Paragraph::new("Hello world!"); let [left, right] = area.split(&Layout::horizontal([20, 20])); frame.render_widget(¶graph, left); frame.render_widget(¶graph, right); // we can reuse the widget ``` Implemented for all widgets except BarChart (which has an implementation that modifies the internal state and requires a rewrite to fix. Other widgets will be implemented in follow up commits. Fixes: #164 Replaces PRs: #122 and #16 Enables: #132 Validated as a viable working solution by: #836
Description
This PR adds
Terminal::render_widget_ref()
and(Stateful)Widget::render_ref()
functions that take Widgets by reference instead of taking ownership. This allows to call render on a Trait object with unknown size with is useful where the exact object implementing Widget is unknown or depends on some kind of condition. I decided not to change therender_widget()
andrender()
signatures as to keep backwards compatibility but sadly all custom widgets are still broken because they are now required to implementrender_ref()
instead. This is just a proof of concept PR and I would appreciate any help on getting it up to the standards since this is my first time contributing to a big rust project. I made this to fix issue #526 I posted yesterday and I'm already using this PR in a project of mine.Checklist