fixup! feat: support for a scrollbar

This commit is contained in:
EdJoPaTo
2024-02-23 23:42:41 +01:00
parent 355d18bf41
commit 9e93b3ecd1
3 changed files with 24 additions and 8 deletions
+1
View File
@@ -40,6 +40,7 @@ jobs:
- -D clippy::pedantic - -D clippy::pedantic
features: features:
- "" # default features - "" # default features
- --all-features
include: include:
# Check future versions and maybe get some glances on soon to be lints # Check future versions and maybe get some glances on soon to be lints
- toolchain: beta - toolchain: beta
+8
View File
@@ -11,6 +11,14 @@ categories = ["command-line-interface"]
include = ["src/**/*", "README.md"] include = ["src/**/*", "README.md"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # 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] [dependencies]
ratatui = { version = "0.26", default-features = false } ratatui = { version = "0.26", default-features = false }
unicode-width = "0.1" unicode-width = "0.1"
+15 -8
View File
@@ -11,9 +11,9 @@ The user interaction state (like the current selection) is stored in the [`TreeS
use std::collections::HashSet; use std::collections::HashSet;
use ratatui::buffer::Buffer; use ratatui::buffer::Buffer;
use ratatui::layout::{Corner, Margin, Rect}; use ratatui::layout::{Corner, Rect};
use ratatui::style::Style; use ratatui::style::Style;
use ratatui::widgets::{Block, Scrollbar, ScrollbarState, StatefulWidget, Widget}; use ratatui::widgets::{Block, StatefulWidget, Widget};
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
mod flatten; mod flatten;
@@ -60,8 +60,10 @@ pub struct Tree<'a, Identifier> {
items: Vec<TreeItem<'a, Identifier>>, items: Vec<TreeItem<'a, Identifier>>,
block: Option<Block<'a>>, block: Option<Block<'a>>,
scrollbar: Option<Scrollbar<'a>>, #[cfg(feature = "experimental_scrollbar")]
scrollbar_margin: Margin, scrollbar: Option<ratatui::widgets::Scrollbar<'a>>,
#[cfg(feature = "experimental_scrollbar")]
scrollbar_margin: ratatui::layout::Margin,
start_corner: Corner, start_corner: Corner,
/// Style used as a base style for the widget /// Style used as a base style for the widget
style: Style, style: Style,
@@ -103,8 +105,10 @@ where
Ok(Self { Ok(Self {
items, items,
block: None, block: None,
#[cfg(feature = "experimental_scrollbar")]
scrollbar: None, scrollbar: None,
scrollbar_margin: Margin::new(0, 0), #[cfg(feature = "experimental_scrollbar")]
scrollbar_margin: ratatui::layout::Margin::new(0, 0),
start_corner: Corner::TopLeft, start_corner: Corner::TopLeft,
style: Style::new(), style: Style::new(),
highlight_style: Style::new(), highlight_style: Style::new(),
@@ -122,14 +126,16 @@ where
self self
} }
#[cfg(feature = "experimental_scrollbar")]
#[must_use] #[must_use]
pub const fn scrollbar(mut self, scrollbar: Option<Scrollbar<'a>>) -> Self { pub const fn scrollbar(mut self, scrollbar: Option<ratatui::widgets::Scrollbar<'a>>) -> Self {
self.scrollbar = scrollbar; self.scrollbar = scrollbar;
self self
} }
#[cfg(feature = "experimental_scrollbar")]
#[must_use] #[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.scrollbar_margin = margin;
self self
} }
@@ -251,8 +257,9 @@ where
state.offset = start; state.offset = start;
state.ensure_selected_in_view_on_next_render = false; state.ensure_selected_in_view_on_next_render = false;
#[cfg(feature = "experimental_scrollbar")]
if let Some(scrollbar) = self.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) .position(start)
.viewport_content_length(available_height); .viewport_content_length(available_height);
let scrollbar_area = full_area.inner(&self.scrollbar_margin); let scrollbar_area = full_area.inner(&self.scrollbar_margin);