From 9e93b3ecd1c1e2ed75f289dcae9f4136f8fea354 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Fri, 23 Feb 2024 23:42:41 +0100 Subject: [PATCH] fixup! feat: support for a scrollbar --- .github/workflows/rust.yml | 1 + Cargo.toml | 8 ++++++++ src/lib.rs | 23 +++++++++++++++-------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index b9fcc94..dd7250a 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -40,6 +40,7 @@ jobs: - -D clippy::pedantic features: - "" # default features + - --all-features include: # Check future versions and maybe get some glances on soon to be lints - toolchain: beta diff --git a/Cargo.toml b/Cargo.toml index ae70eb8..cf8e2af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,14 @@ categories = ["command-line-interface"] include = ["src/**/*", "README.md"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +default = [] +experimental_scrollbar = [] + +[[example]] +name = "example" +required-features = ["experimental_scrollbar"] + [dependencies] ratatui = { version = "0.26", default-features = false } unicode-width = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 76a335f..1ca1fe8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,9 +11,9 @@ The user interaction state (like the current selection) is stored in the [`TreeS use std::collections::HashSet; use ratatui::buffer::Buffer; -use ratatui::layout::{Corner, Margin, Rect}; +use ratatui::layout::{Corner, Rect}; use ratatui::style::Style; -use ratatui::widgets::{Block, Scrollbar, ScrollbarState, StatefulWidget, Widget}; +use ratatui::widgets::{Block, StatefulWidget, Widget}; use unicode_width::UnicodeWidthStr; mod flatten; @@ -60,8 +60,10 @@ pub struct Tree<'a, Identifier> { items: Vec>, block: Option>, - scrollbar: Option>, - scrollbar_margin: Margin, + #[cfg(feature = "experimental_scrollbar")] + scrollbar: Option>, + #[cfg(feature = "experimental_scrollbar")] + scrollbar_margin: ratatui::layout::Margin, start_corner: Corner, /// Style used as a base style for the widget style: Style, @@ -103,8 +105,10 @@ where Ok(Self { items, block: None, + #[cfg(feature = "experimental_scrollbar")] scrollbar: None, - scrollbar_margin: Margin::new(0, 0), + #[cfg(feature = "experimental_scrollbar")] + scrollbar_margin: ratatui::layout::Margin::new(0, 0), start_corner: Corner::TopLeft, style: Style::new(), highlight_style: Style::new(), @@ -122,14 +126,16 @@ where self } + #[cfg(feature = "experimental_scrollbar")] #[must_use] - pub const fn scrollbar(mut self, scrollbar: Option>) -> Self { + pub const fn scrollbar(mut self, scrollbar: Option>) -> Self { self.scrollbar = scrollbar; self } + #[cfg(feature = "experimental_scrollbar")] #[must_use] - pub const fn scrollbar_margin(mut self, margin: Margin) -> Self { + pub const fn scrollbar_margin(mut self, margin: ratatui::layout::Margin) -> Self { self.scrollbar_margin = margin; self } @@ -251,8 +257,9 @@ where state.offset = start; state.ensure_selected_in_view_on_next_render = false; + #[cfg(feature = "experimental_scrollbar")] if let Some(scrollbar) = self.scrollbar { - let mut scrollbar_state = ScrollbarState::new(visible.len()) + let mut scrollbar_state = ratatui::widgets::ScrollbarState::new(visible.len()) .position(start) .viewport_content_length(available_height); let scrollbar_area = full_area.inner(&self.scrollbar_margin);