From 65fd1c04ed47d781bf86685e15d51c28ee29e7fb Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Mon, 6 May 2024 10:24:41 +0200 Subject: [PATCH] refactor!: reuse text style of tree item BREAKING CHANGE: TreeItem style is gone --- src/lib.rs | 15 +++++++++------ src/tree_item.rs | 10 ---------- 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6977698..9ed7e75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -276,8 +276,8 @@ where height, }; - let item_style = self.style.patch(item.style); - buf.set_style(area, item_style); + let text = &item.text; + let item_style = text.style; let is_selected = state.selected == *identifier; let after_highlight_symbol_x = if has_selection { @@ -314,10 +314,13 @@ where x }; - let max_element_width = area.width.saturating_sub(after_depth_x - x); - for (j, line) in item.text.lines.iter().enumerate() { - buf.set_line(after_depth_x, y + j as u16, line, max_element_width); - } + let text_area = Rect { + x: after_depth_x, + width: area.width.saturating_sub(after_depth_x - x), + ..area + }; + text.render(text_area, buf); + if is_selected { buf.set_style(area, self.highlight_style); } diff --git a/src/tree_item.rs b/src/tree_item.rs index e2c846b..ac929f1 100644 --- a/src/tree_item.rs +++ b/src/tree_item.rs @@ -1,6 +1,5 @@ use std::collections::HashSet; -use ratatui::style::Style; use ratatui::text::Text; /// One item inside a [`Tree`](crate::Tree). @@ -38,7 +37,6 @@ use ratatui::text::Text; pub struct TreeItem<'a, Identifier> { pub(super) identifier: Identifier, pub(super) text: Text<'a>, - pub(super) style: Style, pub(super) children: Vec, } @@ -55,7 +53,6 @@ where Self { identifier, text: text.into(), - style: Style::new(), children: Vec::new(), } } @@ -83,7 +80,6 @@ where Ok(Self { identifier, text: text.into(), - style: Style::new(), children, }) } @@ -112,12 +108,6 @@ where self.text.height() } - #[must_use] - pub const fn style(mut self, style: Style) -> Self { - self.style = style; - self - } - /// Add a child to the `TreeItem`. /// /// # Errors