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

state: adds offset access and manipulation #497

Closed

Conversation

wookietreiber
Copy link
Contributor

Description

Adds offset access and manipulation. Fixes #495.

Testing guidelines

I have a few structs that keep the table state. I use this trait ScrollableTable for each of these to attach actions like scroll up/down, etc. I've added this fn recenter to test offset manipulation. ListState should be pretty much analogously.

Note that I haven't added tests per se, because I think that these kinds of accessor/mutator functions are quite obvious.

use tui::layout::Rect;
use tui::widgets::TableState;

pub trait ScrollableTable {
    fn size(&self) -> Rect;
    fn state(&self) -> &TableState;
    fn state_mut(&mut self) -> &mut TableState;

    fn recenter(&mut self) {
        if let Some(selected) = self.state().selected() {
            let height = self.size().height as usize;

            let offset = self.state_mut().offset_mut();
            *offset = selected.saturating_sub(height / 2);
        }
    }
}

Checklist

@wookietreiber
Copy link
Contributor Author

cargo make does not seem to be compatible with rustc 1.56.1:

error: failed to compile `cargo-make v0.35.8`, intermediate artifacts can be found at `/tmp/cargo-install52QPLl`

Caused by:
  package `os_str_bytes v6.2.0` cannot be built because it requires rustc 1.57.0 or newer, while the currently active rustc version is 1.56.1
Error: Process completed with exit code 101.

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.

manipulating ListState/TableState offset
2 participants