Use new with_* naming scheme
All builder-like functions are now named with_*. There is also now a way to set each property imperatively with only a mutable reference. The only widgets I haven't yet converted to this style are the Join* widgets; they're a bit harder to figure out an appropriate API for.
This commit is contained in:
parent
cb483431cc
commit
607c11fea4
11 changed files with 125 additions and 90 deletions
|
|
@ -11,14 +11,14 @@ fn widget() -> impl Widget<io::Error> {
|
|||
.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) {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ use crate::{AsyncWidget, Frame, Pos, Size, Style, Widget};
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Background<I> {
|
||||
inner: I,
|
||||
style: Style,
|
||||
pub inner: I,
|
||||
pub style: Style,
|
||||
}
|
||||
|
||||
impl<I> Background<I> {
|
||||
|
|
@ -16,7 +16,7 @@ impl<I> Background<I> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn style(mut self, style: Style) -> Self {
|
||||
pub fn with_style(mut self, style: Style) -> Self {
|
||||
self.style = style;
|
||||
self
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,9 +88,9 @@ impl Default for BorderLook {
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Border<I> {
|
||||
inner: I,
|
||||
look: BorderLook,
|
||||
style: Style,
|
||||
pub inner: I,
|
||||
pub look: BorderLook,
|
||||
pub style: Style,
|
||||
}
|
||||
|
||||
impl<I> Border<I> {
|
||||
|
|
@ -102,12 +102,12 @@ impl<I> Border<I> {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,25 +4,25 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget};
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Cursor<I> {
|
||||
inner: I,
|
||||
at: Pos,
|
||||
pub inner: I,
|
||||
pub position: Pos,
|
||||
}
|
||||
|
||||
impl<I> Cursor<I> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -340,8 +340,8 @@ impl Default for EditorState {
|
|||
pub struct Editor<'a> {
|
||||
state: &'a mut EditorState,
|
||||
highlighted: Styled,
|
||||
hidden: Option<Styled>,
|
||||
focus: bool,
|
||||
pub hidden: Option<Styled>,
|
||||
pub focus: bool,
|
||||
}
|
||||
|
||||
impl Editor<'_> {
|
||||
|
|
@ -349,29 +349,45 @@ impl Editor<'_> {
|
|||
self.state
|
||||
}
|
||||
|
||||
pub fn highlight<F>(mut self, highlight: F) -> Self
|
||||
pub fn text(&self) -> &Styled {
|
||||
&self.highlighted
|
||||
}
|
||||
|
||||
pub fn highlight<F>(&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<F>(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(("<hidden>", Style::new().grey().italic()))
|
||||
}
|
||||
|
||||
pub fn hidden_with_placeholder<S: Into<Styled>>(mut self, placeholder: S) -> Self {
|
||||
pub fn with_hidden<S: Into<Styled>>(mut self, placeholder: S) -> Self {
|
||||
self.hidden = Some(placeholder.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_hidden_default_placeholder(self) -> Self {
|
||||
self.with_hidden(("<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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget};
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Float<I> {
|
||||
inner: I,
|
||||
pub inner: I,
|
||||
horizontal: Option<f32>,
|
||||
vertical: Option<f32>,
|
||||
}
|
||||
|
|
@ -18,49 +18,68 @@ impl<I> Float<I> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn horizontal(mut self, position: f32) -> Self {
|
||||
assert!((0.0..=1.0).contains(&position));
|
||||
self.horizontal = Some(position);
|
||||
pub fn horizontal(&self) -> Option<f32> {
|
||||
self.horizontal
|
||||
}
|
||||
|
||||
pub fn set_horizontal(&mut self, position: Option<f32>) {
|
||||
if let Some(position) = position {
|
||||
assert!((0.0..=1.0).contains(&position));
|
||||
}
|
||||
self.horizontal = position;
|
||||
}
|
||||
|
||||
pub fn vertical(&self) -> Option<f32> {
|
||||
self.vertical
|
||||
}
|
||||
|
||||
pub fn set_vertical(&mut self, position: Option<f32>) {
|
||||
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) {
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ use crate::{AsyncWidget, Frame, Size, Widget};
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Layer<I1, I2> {
|
||||
below: I1,
|
||||
above: I2,
|
||||
pub below: I1,
|
||||
pub above: I2,
|
||||
}
|
||||
|
||||
impl<I1, I2> Layer<I1, I2> {
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@ use crate::{AsyncWidget, Frame, Pos, Size, Widget};
|
|||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Padding<I> {
|
||||
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<I> Padding<I> {
|
||||
|
|
@ -22,36 +22,36 @@ impl<I> Padding<I> {
|
|||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ use async_trait::async_trait;
|
|||
use crate::{AsyncWidget, Frame, Size, Widget};
|
||||
|
||||
pub struct Resize<I> {
|
||||
inner: I,
|
||||
min_width: Option<u16>,
|
||||
min_height: Option<u16>,
|
||||
max_width: Option<u16>,
|
||||
max_height: Option<u16>,
|
||||
pub inner: I,
|
||||
pub min_width: Option<u16>,
|
||||
pub min_height: Option<u16>,
|
||||
pub max_width: Option<u16>,
|
||||
pub max_height: Option<u16>,
|
||||
}
|
||||
|
||||
impl<I> Resize<I> {
|
||||
|
|
@ -21,22 +21,22 @@ impl<I> Resize<I> {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue