Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

feat: add stateful widgets #210

Merged
merged 1 commit into from
Feb 23, 2020
Merged

feat: add stateful widgets #210

merged 1 commit into from
Feb 23, 2020

Conversation

fdehau
Copy link
Owner

@fdehau fdehau commented Dec 15, 2019

Most widgets can be drawn directly based on the input parameters. However, some
features may require some kind of associated state to be implemented.

For example, the List widget can highlight the item currently selected. This
can be translated in an offset, which is the number of elements to skip in
order to have the selected item within the viewport currently allocated to this
widget. The widget can therefore only provide the following behavior: whenever
the selected item is out of the viewport scroll to a predefined position (make
the selected item the last viewable item or the one in the middle).
Nonetheless, if the widget has access to the last computed offset then it can
implement a natural scrolling experience where the last offset is reused until
the selected item is out of the viewport.

To allow such behavior within the widgets, this commit introduces the following
changes:

  • Add a StatefulWidget trait with an associated State type. Widgets that
    can take advantage of having a "memory" between two draw calls needs to
    implement this trait.
  • Add a render_stateful_widget method on Frame where the associated
    state is given as a parameter.

The chosen approach is thus to let the developers manage their widgets' states
themselves as they are already responsible for the lifecycle of the wigets
(given that the crate exposes an immediate mode api).

The following changes were also introduced:

  • Widget::render has been deleted. Developers should use Frame::render_widget
    instead.
  • Widget::background has been deleted. Developers should use Buffer::set_background
    instead.
  • SelectableList has been deleted. Developers can directly use List where
    SelectableList features have been back-ported.

@clevinson
Copy link

clevinson commented Dec 20, 2019

Awesome! This will also make the "reverse scrolling" behavior I implemented for the paragraph widget a lot easier to work with (I can now have doubly bounded scroll boundaries!). I'll try to refactor that using stateful widgets and PR if there's anything that seems useful.

@fdehau fdehau force-pushed the stateful-widgets branch 2 times, most recently from 27cf16f to a924d4f Compare February 23, 2020 17:02
@fdehau fdehau marked this pull request as ready for review February 23, 2020 18:12
Most widgets can be drawn directly based on the input parameters. However, some
features may require some kind of associated state to be implemented.

For example, the `List` widget can highlight the item currently selected. This
can be translated in an offset, which is the number of elements to skip in
order to have the selected item within the viewport currently allocated to this
widget. The widget can therefore only provide the following behavior: whenever
the selected item is out of the viewport scroll to a predefined position (make
the selected item the last viewable item or the one in the middle).
Nonetheless, if the widget has access to the last computed offset then it can
implement a natural scrolling experience where the last offset is reused until
the selected item is out of the viewport.

To allow such behavior within the widgets, this commit introduces the following
changes:
- Add a `StatefulWidget` trait with an associated `State` type. Widgets that
can take advantage of having a "memory" between two draw calls needs to
implement this trait.
- Add a `render_stateful_widget` method on `Frame` where the associated
state is given as a parameter.

The chosen approach is thus to let the developers manage their widgets' states
themselves as they are already responsible for the lifecycle of the wigets
(given that the crate exposes an immediate mode api).

The following changes were also introduced:

- `Widget::render` has been deleted. Developers should use `Frame::render_widget`
instead.
- `Widget::background` has been deleted. Developers should use `Buffer::set_background`
instead.
- `SelectableList` has been deleted. Developers can directly use `List` where
`SelectableList` features have been back-ported.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants