Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-tui/src/widgets/block.rs')
| -rw-r--r-- | helix-tui/src/widgets/block.rs | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/helix-tui/src/widgets/block.rs b/helix-tui/src/widgets/block.rs index ee7aa757..98f84abe 100644 --- a/helix-tui/src/widgets/block.rs +++ b/helix-tui/src/widgets/block.rs @@ -1,15 +1,14 @@ use crate::{ buffer::Buffer, symbols::line, - text::Spans, + text::{Span, Spans}, widgets::{Borders, Widget}, }; use helix_view::graphics::{Rect, Style}; /// Border render type. Defaults to [`BorderType::Plain`]. -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum BorderType { - #[default] Plain, Rounded, Double, @@ -27,6 +26,12 @@ impl BorderType { } } +impl Default for BorderType { + fn default() -> BorderType { + BorderType::Plain + } +} + /// Base widget to be used with all upper level ones. It may be used to display a box border around /// the widget and/or add a title. /// @@ -58,22 +63,6 @@ pub struct Block<'a> { } impl<'a> Block<'a> { - pub const fn new() -> Self { - Self { - title: None, - borders: Borders::empty(), - border_style: Style::new(), - border_type: BorderType::Plain, - style: Style::new(), - } - } - - pub const fn bordered() -> Self { - let mut block = Self::new(); - block.borders = Borders::ALL; - block - } - pub fn title<T>(mut self, title: T) -> Block<'a> where T: Into<Spans<'a>>, @@ -82,22 +71,34 @@ impl<'a> Block<'a> { self } - pub const fn border_style(mut self, style: Style) -> Block<'a> { + #[deprecated( + since = "0.10.0", + note = "You should use styling capabilities of `text::Spans` given as argument of the `title` method to apply styling to the title." + )] + pub fn title_style(mut self, style: Style) -> Block<'a> { + if let Some(t) = self.title { + let title = String::from(&t); + self.title = Some(Spans::from(Span::styled(title, style))); + } + self + } + + pub fn border_style(mut self, style: Style) -> Block<'a> { self.border_style = style; self } - pub const fn style(mut self, style: Style) -> Block<'a> { + pub fn style(mut self, style: Style) -> Block<'a> { self.style = style; self } - pub const fn borders(mut self, flag: Borders) -> Block<'a> { + pub fn borders(mut self, flag: Borders) -> Block<'a> { self.borders = flag; self } - pub const fn border_type(mut self, border_type: BorderType) -> Block<'a> { + pub fn border_type(mut self, border_type: BorderType) -> Block<'a> { self.border_type = border_type; self } @@ -123,7 +124,7 @@ impl<'a> Block<'a> { } } -impl Widget for Block<'_> { +impl<'a> Widget for Block<'a> { fn render(self, area: Rect, buf: &mut Buffer) { if area.area() == 0 { return; @@ -417,7 +418,9 @@ mod tests { // All borders assert_eq!( - Block::bordered().inner(Rect::default()), + Block::default() + .borders(Borders::ALL) + .inner(Rect::default()), Rect { x: 0, y: 0, @@ -427,7 +430,7 @@ mod tests { "all borders, width=0, height=0" ); assert_eq!( - Block::bordered().inner(Rect { + Block::default().borders(Borders::ALL).inner(Rect { x: 0, y: 0, width: 1, @@ -442,7 +445,7 @@ mod tests { "all borders, width=1, height=1" ); assert_eq!( - Block::bordered().inner(Rect { + Block::default().borders(Borders::ALL).inner(Rect { x: 0, y: 0, width: 2, @@ -457,7 +460,7 @@ mod tests { "all borders, width=2, height=2" ); assert_eq!( - Block::bordered().inner(Rect { + Block::default().borders(Borders::ALL).inner(Rect { x: 0, y: 0, width: 3, |