Skip to content
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(widgets)!: make widgets optionally reusable #122

Closed
wants to merge 1 commit into from

Commits on Jun 2, 2023

  1. feat(widget)!: render functions now accept references

    In some situations, ratatui's render functions can make it difficult to
    implement specific behavior or can make it difficult and inefficient to
    reuse widgets. E.g. for a list, you have to clone either the vec or it's
    items again each frame to make the new list object. Another example is
    rendering a widget in a loop, where you have to clone the widget each
    time.
    
    This commit makes it so the `Widget`/`StatefulWidget``::render`
    functions only take `self` by immutable reference, meaning they're not
    consumed and can be reused.
    
    The `Frame::render_{,stateful_}widget` functions take `widget` by value
    though. To allow these functions to be called with references or values,
    we added a new trait `AsWidgetRef` which is implemented for `&W` and `W`
    where `W: Widget`/`StatefulWidget. This trait is then used to call the
    `render` functions. Thus backwards compatibility is maintained for the
    `Frame` API.
    
    This also removes any calls to `take()` in the `render` functions since
    this cannot happen when the widget is passed by reference.
    
    BREAKING CHANGE: `Widget`/`StatefulWidget`'s `render` functions now take
    `self` by immutable reference instead of by value. To update your custom
    widgets simply add `&` before the `self` parameter in the `render`
    function.
    bolshoytoster authored and joshka committed Jun 2, 2023
    Configuration menu
    Copy the full SHA
    faa0444 View commit details
    Browse the repository at this point in the history