diff --git a/examples/hello_world_widgets.rs b/examples/hello_world_widgets.rs index c80f10d..000cf91 100644 --- a/examples/hello_world_widgets.rs +++ b/examples/hello_world_widgets.rs @@ -11,14 +11,14 @@ fn widget() -> impl Widget { .then("Press any key to exit", Style::new().on_dark_blue()); Text::new(styled) .padding() - .horizontal(1) + .with_horizontal(1) .border() - .look(BorderLook::LINE_DOUBLE) - .style(Style::new().dark_red()) + .with_look(BorderLook::LINE_DOUBLE) + .with_style(Style::new().dark_red()) .background() - .style(Style::new().on_yellow().opaque()) + .with_style(Style::new().on_yellow().opaque()) .float() - .all(0.5) + .with_all(0.5) } fn render_frame(term: &mut Terminal) { diff --git a/src/widgets/background.rs b/src/widgets/background.rs index edd1fdf..d72add6 100644 --- a/src/widgets/background.rs +++ b/src/widgets/background.rs @@ -4,8 +4,8 @@ use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget}; #[derive(Debug, Clone, Copy)] pub struct Background { - inner: I, - style: Style, + pub inner: I, + pub style: Style, } impl Background { @@ -16,7 +16,7 @@ impl Background { } } - pub fn style(mut self, style: Style) -> Self { + pub fn with_style(mut self, style: Style) -> Self { self.style = style; self } diff --git a/src/widgets/border.rs b/src/widgets/border.rs index 588f738..3ee2b4a 100644 --- a/src/widgets/border.rs +++ b/src/widgets/border.rs @@ -88,9 +88,9 @@ impl Default for BorderLook { #[derive(Debug, Clone, Copy)] pub struct Border { - inner: I, - look: BorderLook, - style: Style, + pub inner: I, + pub look: BorderLook, + pub style: Style, } impl Border { @@ -102,12 +102,12 @@ impl Border { } } - pub fn look(mut self, look: BorderLook) -> Self { + pub fn with_look(mut self, look: BorderLook) -> Self { self.look = look; self } - pub fn style(mut self, style: Style) -> Self { + pub fn with_style(mut self, style: Style) -> Self { self.style = style; self } diff --git a/src/widgets/cursor.rs b/src/widgets/cursor.rs index 5f79ed8..47dca56 100644 --- a/src/widgets/cursor.rs +++ b/src/widgets/cursor.rs @@ -4,25 +4,25 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget}; #[derive(Debug, Clone, Copy)] pub struct Cursor { - inner: I, - at: Pos, + pub inner: I, + pub position: Pos, } impl Cursor { pub fn new(inner: I) -> Self { Self { inner, - at: Pos::ZERO, + position: Pos::ZERO, } } - pub fn at(mut self, pos: Pos) -> Self { - self.at = pos; + pub fn with_position(mut self, position: Pos) -> Self { + self.position = position; self } - pub fn at_xy(self, x: i32, y: i32) -> Self { - self.at(Pos::new(x, y)) + pub fn with_position_xy(self, x: i32, y: i32) -> Self { + self.with_position(Pos::new(x, y)) } } @@ -41,7 +41,7 @@ where fn draw(self, frame: &mut Frame) -> Result<(), E> { self.inner.draw(frame)?; - frame.show_cursor(self.at); + frame.show_cursor(self.position); Ok(()) } } @@ -62,7 +62,7 @@ where async fn draw(self, frame: &mut Frame) -> Result<(), E> { self.inner.draw(frame).await?; - frame.show_cursor(self.at); + frame.show_cursor(self.position); Ok(()) } } diff --git a/src/widgets/editor.rs b/src/widgets/editor.rs index e433e88..e587a0a 100644 --- a/src/widgets/editor.rs +++ b/src/widgets/editor.rs @@ -340,8 +340,8 @@ impl Default for EditorState { pub struct Editor<'a> { state: &'a mut EditorState, highlighted: Styled, - hidden: Option, - focus: bool, + pub hidden: Option, + pub focus: bool, } impl Editor<'_> { @@ -349,29 +349,45 @@ impl Editor<'_> { self.state } - pub fn highlight(mut self, highlight: F) -> Self + pub fn text(&self) -> &Styled { + &self.highlighted + } + + pub fn highlight(&mut self, highlight: F) where F: FnOnce(&str) -> Styled, { self.highlighted = highlight(&self.state.text); assert_eq!(self.state.text, self.highlighted.text()); + } + + pub fn with_highlight(mut self, highlight: F) -> Self + where + F: FnOnce(&str) -> Styled, + { + self.highlight(highlight); self } - pub fn focus(mut self, active: bool) -> Self { - self.focus = active; + pub fn with_visible(mut self) -> Self { + self.hidden = None; self } - pub fn hidden(self) -> Self { - self.hidden_with_placeholder(("", Style::new().grey().italic())) - } - - pub fn hidden_with_placeholder>(mut self, placeholder: S) -> Self { + pub fn with_hidden>(mut self, placeholder: S) -> Self { self.hidden = Some(placeholder.into()); self } + pub fn with_hidden_default_placeholder(self) -> Self { + self.with_hidden(("", Style::new().grey().italic())) + } + + pub fn with_focus(mut self, active: bool) -> Self { + self.focus = active; + self + } + fn wrapped_cursor(cursor_idx: usize, break_indices: &[usize]) -> (usize, usize) { let mut row = 0; let mut line_idx = cursor_idx; diff --git a/src/widgets/empty.rs b/src/widgets/empty.rs index 86881e2..fe6cfad 100644 --- a/src/widgets/empty.rs +++ b/src/widgets/empty.rs @@ -4,7 +4,7 @@ use crate::{AsyncWidget, Frame, Size, Widget}; #[derive(Debug, Default, Clone, Copy)] pub struct Empty { - size: Size, + pub size: Size, } impl Empty { @@ -12,17 +12,17 @@ impl Empty { Self { size: Size::ZERO } } - pub fn width(mut self, width: u16) -> Self { + pub fn with_width(mut self, width: u16) -> Self { self.size.width = width; self } - pub fn height(mut self, height: u16) -> Self { + pub fn with_height(mut self, height: u16) -> Self { self.size.height = height; self } - pub fn size(mut self, size: Size) -> Self { + pub fn with_size(mut self, size: Size) -> Self { self.size = size; self } diff --git a/src/widgets/float.rs b/src/widgets/float.rs index d3eff4b..b22c108 100644 --- a/src/widgets/float.rs +++ b/src/widgets/float.rs @@ -4,7 +4,7 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget}; #[derive(Debug, Clone, Copy)] pub struct Float { - inner: I, + pub inner: I, horizontal: Option, vertical: Option, } @@ -18,49 +18,68 @@ impl Float { } } - pub fn horizontal(mut self, position: f32) -> Self { - assert!((0.0..=1.0).contains(&position)); - self.horizontal = Some(position); + pub fn horizontal(&self) -> Option { + self.horizontal + } + + pub fn set_horizontal(&mut self, position: Option) { + if let Some(position) = position { + assert!((0.0..=1.0).contains(&position)); + } + self.horizontal = position; + } + + pub fn vertical(&self) -> Option { + self.vertical + } + + pub fn set_vertical(&mut self, position: Option) { + if let Some(position) = position { + assert!((0.0..=1.0).contains(&position)); + } + self.vertical = position; + } + + pub fn with_horizontal(mut self, position: f32) -> Self { + self.set_horizontal(Some(position)); self } - pub fn vertical(mut self, position: f32) -> Self { - assert!((0.0..=1.0).contains(&position)); - self.vertical = Some(position); + pub fn with_vertical(mut self, position: f32) -> Self { + self.set_vertical(Some(position)); self } - pub fn all(self, position: f32) -> Self { - assert!((0.0..=1.0).contains(&position)); - self.horizontal(position).vertical(position) + pub fn with_all(self, position: f32) -> Self { + self.with_horizontal(position).with_vertical(position) } - pub fn left(self) -> Self { - self.horizontal(0.0) + pub fn with_left(self) -> Self { + self.with_horizontal(0.0) } - pub fn right(self) -> Self { - self.horizontal(1.0) + pub fn with_right(self) -> Self { + self.with_horizontal(1.0) } - pub fn top(self) -> Self { - self.vertical(0.0) + pub fn with_top(self) -> Self { + self.with_vertical(0.0) } - pub fn bottom(self) -> Self { - self.vertical(1.0) + pub fn with_bottom(self) -> Self { + self.with_vertical(1.0) } - pub fn center_h(self) -> Self { - self.horizontal(0.5) + pub fn with_center_h(self) -> Self { + self.with_horizontal(0.5) } - pub fn center_v(self) -> Self { - self.vertical(0.5) + pub fn with_center_v(self) -> Self { + self.with_vertical(0.5) } - pub fn center(self) -> Self { - self.all(0.5) + pub fn with_center(self) -> Self { + self.with_all(0.5) } fn push_inner(&self, frame: &mut Frame, size: Size, mut inner_size: Size) { diff --git a/src/widgets/layer.rs b/src/widgets/layer.rs index 289f854..f8da993 100644 --- a/src/widgets/layer.rs +++ b/src/widgets/layer.rs @@ -4,8 +4,8 @@ use crate::{AsyncWidget, Frame, Size, Widget}; #[derive(Debug, Clone, Copy)] pub struct Layer { - below: I1, - above: I2, + pub below: I1, + pub above: I2, } impl Layer { diff --git a/src/widgets/padding.rs b/src/widgets/padding.rs index a8f30b0..656aec8 100644 --- a/src/widgets/padding.rs +++ b/src/widgets/padding.rs @@ -4,11 +4,11 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget}; #[derive(Debug, Clone, Copy)] pub struct Padding { - inner: I, - left: u16, - right: u16, - top: u16, - bottom: u16, + pub inner: I, + pub left: u16, + pub right: u16, + pub top: u16, + pub bottom: u16, } impl Padding { @@ -22,36 +22,36 @@ impl Padding { } } - pub fn left(mut self, amount: u16) -> Self { + pub fn with_left(mut self, amount: u16) -> Self { self.left = amount; self } - pub fn right(mut self, amount: u16) -> Self { + pub fn with_right(mut self, amount: u16) -> Self { self.right = amount; self } - pub fn top(mut self, amount: u16) -> Self { + pub fn with_top(mut self, amount: u16) -> Self { self.top = amount; self } - pub fn bottom(mut self, amount: u16) -> Self { + pub fn with_bottom(mut self, amount: u16) -> Self { self.bottom = amount; self } - pub fn horizontal(self, amount: u16) -> Self { - self.left(amount).right(amount) + pub fn with_horizontal(self, amount: u16) -> Self { + self.with_left(amount).with_right(amount) } - pub fn vertical(self, amount: u16) -> Self { - self.top(amount).bottom(amount) + pub fn with_vertical(self, amount: u16) -> Self { + self.with_top(amount).with_bottom(amount) } - pub fn all(self, amount: u16) -> Self { - self.horizontal(amount).vertical(amount) + pub fn with_all(self, amount: u16) -> Self { + self.with_horizontal(amount).with_vertical(amount) } fn pad_size(&self) -> Size { diff --git a/src/widgets/resize.rs b/src/widgets/resize.rs index bf80801..5adad8a 100644 --- a/src/widgets/resize.rs +++ b/src/widgets/resize.rs @@ -3,11 +3,11 @@ use async_trait::async_trait; use crate::{AsyncWidget, Frame, Size, Widget}; pub struct Resize { - inner: I, - min_width: Option, - min_height: Option, - max_width: Option, - max_height: Option, + pub inner: I, + pub min_width: Option, + pub min_height: Option, + pub max_width: Option, + pub max_height: Option, } impl Resize { @@ -21,22 +21,22 @@ impl Resize { } } - pub fn min_width(mut self, width: u16) -> Self { + pub fn with_min_width(mut self, width: u16) -> Self { self.min_width = Some(width); self } - pub fn min_height(mut self, height: u16) -> Self { + pub fn with_min_height(mut self, height: u16) -> Self { self.min_height = Some(height); self } - pub fn max_width(mut self, width: u16) -> Self { + pub fn with_max_width(mut self, width: u16) -> Self { self.max_width = Some(width); self } - pub fn max_height(mut self, height: u16) -> Self { + pub fn with_max_height(mut self, height: u16) -> Self { self.max_height = Some(height); self } diff --git a/src/widgets/text.rs b/src/widgets/text.rs index 3c2ceee..5acdb8e 100644 --- a/src/widgets/text.rs +++ b/src/widgets/text.rs @@ -4,8 +4,8 @@ use crate::{AsyncWidget, Frame, Pos, Size, Styled, Widget, WidthDb}; #[derive(Debug, Clone)] pub struct Text { - styled: Styled, - wrap: bool, + pub styled: Styled, + pub wrap: bool, } impl Text { @@ -16,8 +16,8 @@ impl Text { } } - pub fn wrap(mut self, wrap: bool) -> Self { - self.wrap = wrap; + pub fn with_wrap(mut self, active: bool) -> Self { + self.wrap = active; self }