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): add offset() and offset_mut() for table and list state #12

Merged
merged 3 commits into from
May 20, 2023

Conversation

orhun
Copy link
Member

@orhun orhun commented Feb 12, 2023

Upstream: #497

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

@orhun
Copy link
Member Author

orhun commented Feb 12, 2023

wookietreiber said: 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.

@mindoodoo mindoodoo added the Type: Enhancement New feature or request label Feb 14, 2023
@joshka joshka added the Type: Imported from tui-rs This issue / PR was Imported from tui-rs - kept for posterity / ease of finding historical ideas label May 5, 2023
@joshka joshka changed the title state: adds offset access and manipulation feat(widgets) add offset{,_mut}() for table and list state May 7, 2023
@joshka joshka changed the title feat(widgets) add offset{,_mut}() for table and list state feat(widgets): add offset() and offset_mut() for table and list state May 7, 2023
Copy link
Member

@joshka joshka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding the with_offset / with_selected methods to TableState to make building state in a single statement easy, but this is fine without it.

@orhun
Copy link
Member Author

orhun commented May 20, 2023

Updated the implementation so that Table and List state implementations are identical. Makes one think if we should have one struct such as e.g. WidgetState for both and deprecate the separate usage. 🤔

@joshka
Copy link
Member

joshka commented May 20, 2023

That's kinda what the scroll RFC talks about.

@joshka joshka merged commit bc66a27 into main May 20, 2023
@joshka joshka deleted the 497/offset-manipulation branch May 26, 2023 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement New feature or request Type: Imported from tui-rs This issue / PR was Imported from tui-rs - kept for posterity / ease of finding historical ideas
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants