diff --git a/output/src/content.rs b/output/src/content.rs index 7a633f2..2925259 100644 --- a/output/src/content.rs +++ b/output/src/content.rs @@ -16,17 +16,11 @@ pub trait HasContent { fn content (&self) -> impl Content; } - -pub struct Bounded(pub O::Area, pub D); -impl> HasContent for Bounded { - fn content (&self) -> impl Content { &self.1 } -} - -impl> Draw for Bounded { - fn draw (&self, to: &mut O) { - let area = to.area(); - *to.area_mut() = self.0; - self.1.draw(to); - *to.area_mut() = area; - } -} +//impl> Draw for T { + //fn draw (&self, to: &mut O) { + //let area = to.area(); + //*to.area_mut() = self.0; + //self.content().draw(to); + //*to.area_mut() = area; + //} +//} diff --git a/output/src/draw.rs b/output/src/draw.rs index cfeefa5..12ef066 100644 --- a/output/src/draw.rs +++ b/output/src/draw.rs @@ -1,10 +1,30 @@ use crate::*; /// Drawable with dynamic dispatch. -pub trait Draw { fn draw (&self, to: &mut O); } -impl Draw for () { fn draw (&self, to: &mut O) {} } -impl Draw for fn(&mut O) { fn draw (&self, to: &mut O) { (*self)(to) } } -impl Draw for Box> { fn draw (&self, to: &mut O) { (**self).draw(to) } } -impl> Draw for &D { fn draw (&self, to: &mut O) { (*self).draw(to) } } -impl> Draw for &mut D { fn draw (&self, to: &mut O) { (**self).draw(to) } } -impl> Draw for Option { fn draw (&self, to: &mut O) { if let Some(draw) = self { draw.draw(to) } } } +pub trait Draw { + fn draw (&self, to: &mut O); +} +impl Draw for () { + fn draw (&self, _: &mut O) {} +} +impl Draw for fn(&mut O) { + fn draw (&self, to: &mut O) { (*self)(to) } +} +impl Draw for Box> { + fn draw (&self, to: &mut O) { (**self).draw(to) } +} +impl> Draw for &D { + fn draw (&self, to: &mut O) { (*self).draw(to) } +} +impl> Draw for &mut D { + fn draw (&self, to: &mut O) { (**self).draw(to) } +} +impl> Draw for Arc { + fn draw (&self, to: &mut O) { (**self).draw(to) } +} +impl> Draw for RwLock { + fn draw (&self, to: &mut O) { self.read().unwrap().draw(to) } +} +impl> Draw for Option { + fn draw (&self, to: &mut O) { if let Some(draw) = self { draw.draw(to) } } +} diff --git a/output/src/layout.rs b/output/src/layout.rs index 0776da4..3130385 100644 --- a/output/src/layout.rs +++ b/output/src/layout.rs @@ -11,15 +11,27 @@ mod layout_stack; //pub use self::layout_stack::*; /// Drawable area of display. pub trait Layout { - fn x (&self, to: O::Area) -> O::Unit { to.x() } - fn y (&self, to: O::Area) -> O::Unit { to.y() } - fn min_w (&self, to: O::Area) -> O::Unit { 0.into() } - fn max_w (&self, to: O::Area) -> O::Unit { to.w() } + fn x (&self, to: O::Area) -> O::Unit { + to.x() + } + fn y (&self, to: O::Area) -> O::Unit { + to.y() + } + fn min_w (&self, _to: O::Area) -> O::Unit { + 0.into() + } + fn max_w (&self, to: O::Area) -> O::Unit { + to.w() + } fn w (&self, to: O::Area) -> O::Unit { to.w().max(self.min_w(to)).min(self.max_w(to)) } - fn min_h (&self, to: O::Area) -> O::Unit { 0.into() } - fn max_h (&self, to: O::Area) -> O::Unit { to.h() } + fn min_h (&self, _to: O::Area) -> O::Unit { + 0.into() + } + fn max_h (&self, to: O::Area) -> O::Unit { + to.h() + } fn h (&self, to: O::Area) -> O::Unit { to.h().max(self.min_h(to)).min(self.max_h(to)) } @@ -64,6 +76,18 @@ impl> Layout for &mut L { fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) } } +impl> Layout for Arc { + fn x (&self, a: O::Area) -> O::Unit { (**self).x(a) } + fn y (&self, a: O::Area) -> O::Unit { (**self).y(a) } + fn w (&self, a: O::Area) -> O::Unit { (**self).w(a) } + fn min_w (&self, a: O::Area) -> O::Unit { (**self).min_w(a) } + fn max_w (&self, a: O::Area) -> O::Unit { (**self).max_w(a) } + fn h (&self, a: O::Area) -> O::Unit { (**self).h(a) } + fn min_h (&self, a: O::Area) -> O::Unit { (**self).min_h(a) } + fn max_h (&self, a: O::Area) -> O::Unit { (**self).max_h(a) } + fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) } +} + impl Layout for Box> { fn x (&self, a: O::Area) -> O::Unit { (**self).x(a) } fn y (&self, a: O::Area) -> O::Unit { (**self).y(a) } @@ -76,6 +100,18 @@ impl Layout for Box> { fn layout (&self, a: O::Area) -> O::Area { (**self).layout(a) } } +impl> Layout for RwLock { + fn x (&self, a: O::Area) -> O::Unit { self.read().unwrap().x(a) } + fn y (&self, a: O::Area) -> O::Unit { self.read().unwrap().y(a) } + fn w (&self, a: O::Area) -> O::Unit { self.read().unwrap().w(a) } + fn min_w (&self, a: O::Area) -> O::Unit { self.read().unwrap().min_w(a) } + fn max_w (&self, a: O::Area) -> O::Unit { self.read().unwrap().max_w(a) } + fn h (&self, a: O::Area) -> O::Unit { self.read().unwrap().h(a) } + fn min_h (&self, a: O::Area) -> O::Unit { self.read().unwrap().min_h(a) } + fn max_h (&self, a: O::Area) -> O::Unit { self.read().unwrap().max_h(a) } + fn layout (&self, a: O::Area) -> O::Area { self.read().unwrap().layout(a) } +} + impl> Layout for Option { fn x (&self, to: O::Area) -> O::Unit { self.as_ref().map(|c|c.x(to)).unwrap_or(to.x()) @@ -106,3 +142,20 @@ impl> Layout for Option { .unwrap_or([to.x(), to.y(), 0.into(), 0.into()].into()) } } + +pub struct Bounded(pub O::Area, pub D); + +impl> HasContent for Bounded { + fn content (&self) -> impl Content { + &self.1 + } +} + +impl> Draw for Bounded { + fn draw (&self, to: &mut O) { + let area = to.area(); + *to.area_mut() = self.0; + self.1.draw(to); + *to.area_mut() = area; + } +} diff --git a/output/src/widget.rs b/output/src/widget.rs index 44e3257..e8fc4c4 100644 --- a/output/src/widget.rs +++ b/output/src/widget.rs @@ -1,4 +1,4 @@ mod widget_border; pub use self::widget_border::*; -mod widget_field; pub use self::widget_field::*; +mod widget_form; pub use self::widget_form::*; mod widget_style; pub use self::widget_style::*; mod widget_tryptich; pub use self::widget_tryptich::*; diff --git a/output/src/widget/widget_field.rs b/output/src/widget/widget_field.rs deleted file mode 100644 index 6bedc02..0000000 --- a/output/src/widget/widget_field.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::*; - -pub struct FieldH(pub Theme, pub Label, pub Value); -impl, V: Content> HasContent for FieldH { - fn content (&self) -> impl Content { Bsp::e(&self.1, &self.2) } -} - -pub struct FieldV(pub Theme, pub Label, pub Value); -impl, V: Content> HasContent for FieldV { - fn content (&self) -> impl Content { Bsp::s(&self.1, &self.2) } -} diff --git a/output/src/widget/widget_form.rs b/output/src/widget/widget_form.rs new file mode 100644 index 0000000..960c77d --- /dev/null +++ b/output/src/widget/widget_form.rs @@ -0,0 +1,55 @@ +use crate::*; + +pub struct FieldH(pub Theme, pub Label, pub Value); +impl, V: Content> HasContent for FieldH { + fn content (&self) -> impl Content { Bsp::e(&self.1, &self.2) } +} +impl, V: Content> Layout for FieldH { + fn layout (&self, to: O::Area) -> O::Area { self.content().layout(to) } +} +impl, V: Content> Draw for FieldH { + fn draw (&self, to: &mut O) { self.content().draw(to) } +} + +pub struct FieldV(pub Theme, pub Label, pub Value); +impl, V: Content> HasContent for FieldV { + fn content (&self) -> impl Content { Bsp::s(&self.1, &self.2) } +} +impl, V: Content> Layout for FieldV { + fn layout (&self, to: O::Area) -> O::Area { self.content().layout(to) } +} +impl, V: Content> Draw for FieldV { + fn draw (&self, to: &mut O) { self.content().draw(to) } +} + +// TODO: +pub struct Field { + pub direction: Direction, + pub label: Option, + pub label_fg: Option, + pub label_bg: Option, + pub label_align: Option, + pub value: Option, + pub value_fg: Option, + pub value_bg: Option, + pub value_align: Option, +} +impl Field { + pub fn new (direction: Direction) -> Field { + Field:: { + direction, + label: None, label_fg: None, label_bg: None, label_align: None, + value: None, value_fg: None, value_bg: None, value_align: None, + } + } + pub fn label ( + self, label: Option, align: Option, fg: Option, bg: Option + ) -> Field { + Field:: { label, label_fg: fg, label_bg: bg, label_align: align, ..self } + } + pub fn value ( + self, value: Option, align: Option, fg: Option, bg: Option + ) -> Field { + Field:: { value, value_fg: fg, value_bg: bg, value_align: align, ..self } + } +} diff --git a/tui/src/tui_content.rs b/tui/src/tui_content.rs index 7c4285d..f1c812b 100644 --- a/tui/src/tui_content.rs +++ b/tui/src/tui_content.rs @@ -11,7 +11,6 @@ mod tui_border; pub use self::tui_border::*; mod tui_button; pub use self::tui_button::*; mod tui_color; pub use self::tui_color::*; mod tui_error; pub use self::tui_error::*; -mod tui_field; pub use self::tui_field::*; mod tui_phat; pub use self::tui_phat::*; mod tui_repeat; pub use self::tui_repeat::*; mod tui_scroll; pub use self::tui_scroll::*; diff --git a/tui/src/tui_content/tui_field.rs b/tui/src/tui_content/tui_field.rs deleted file mode 100644 index 38ecb2d..0000000 --- a/tui/src/tui_content/tui_field.rs +++ /dev/null @@ -1,72 +0,0 @@ -use crate::*; - -impl Draw for FieldH { - fn draw (&self, to: &mut TuiOut) { to.place(&self.content()) } -} -impl Draw for FieldV { - fn draw (&self, to: &mut TuiOut) { to.place(&self.content()) } -} - -// TODO: -pub struct Field { - pub direction: Direction, - pub label: Option, - pub label_fg: Option, - pub label_bg: Option, - pub label_align: Option, - pub value: Option, - pub value_fg: Option, - pub value_bg: Option, - pub value_align: Option, -} -impl, U: Content> HasContent for Field { - fn content (&self) -> impl Content { - "TODO" - } -} - -impl Field { - pub fn new (direction: Direction) -> Field<(), ()> { - Field { - direction, - label: None, - label_fg: None, - label_bg: None, - label_align: None, - value: None, - value_fg: None, - value_bg: None, - value_align: None, - } - } - pub fn label ( - self, - label: Option, - align: Option, - fg: Option, - bg: Option - ) -> Field { - Field { - label, - label_fg: fg, - label_bg: bg, - label_align: align, - ..self - } - } - pub fn value ( - self, - value: Option, - align: Option, - fg: Option, - bg: Option - ) -> Field { - Field { - value, - value_fg: fg, - value_bg: bg, - value_align: align, - ..self - } - } -} diff --git a/tui/src/tui_content/tui_string.rs b/tui/src/tui_content/tui_string.rs index 781802c..eae8982 100644 --- a/tui/src/tui_content/tui_string.rs +++ b/tui/src/tui_content/tui_string.rs @@ -1,12 +1,35 @@ use crate::*; use crate::ratatui::prelude::Position; use unicode_width::{UnicodeWidthStr, UnicodeWidthChar}; -impl Layout for &str { fn layout (&self, to: [u16;4]) -> [u16;4] { to.center_xy([width_chars_max(to.w(), self), 1]) } } -impl Draw for &str { fn draw (&self, to: &mut TuiOut) { let [x, y, w, ..] = self.layout(to.area()); to.text(&self, x, y, w) } } -impl Layout for String { fn layout (&self, to: [u16;4]) -> [u16;4] { self.as_str().layout(to) } } -impl Draw for String { fn draw (&self, to: &mut TuiOut) { self.as_str().draw(to) } } -impl Layout for Arc { fn layout (&self, to: [u16;4]) -> [u16;4] { self.as_ref().layout(to) } } -impl Draw for Arc { fn draw (&self, to: &mut TuiOut) { self.as_ref().draw(to) } } + +impl Draw for &str { + fn draw (&self, to: &mut TuiOut) { + let [x, y, w, ..] = self.layout(to.area()); + to.text(&self, x, y, w) + } +} +impl Draw for String { + fn draw (&self, to: &mut TuiOut) { + self.as_str().draw(to) + } +} +impl Draw for Arc { + fn draw (&self, to: &mut TuiOut) { self.as_ref().draw(to) } +} + +impl Layout for &str { + fn layout (&self, to: [u16;4]) -> [u16;4] { to.center_xy([width_chars_max(to.w(), self), 1]) } +} +impl Layout for String { + fn layout (&self, to: [u16;4]) -> [u16;4] { + self.as_str().layout(to) + } +} +impl Layout for Arc { + fn layout (&self, to: [u16;4]) -> [u16;4] { + self.as_ref().layout(to) + } +} fn width_chars_max (max: u16, text: impl AsRef) -> u16 { let mut width: u16 = 0;