tek/src/style.rs

86 lines
2.7 KiB
Rust

use crate::*;
pub trait TuiStyle {
fn fg <W: Content<Tui>> (color: Color, w: W) -> Foreground<W> {
Foreground(color, w)
}
fn bg <W: Content<Tui>> (color: Color, w: W) -> Background<W> {
Background(color, w)
}
fn fg_bg <W: Content<Tui>> (fg: Color, bg: Color, w: W) -> Background<Foreground<W>> {
Background(bg, Foreground(fg, w))
}
fn bold <W: Content<Tui>> (on: bool, w: W) -> Bold<W> {
Bold(on, w)
}
fn border <W: Content<Tui>, S: BorderStyle> (style: S, w: W) -> Bordered<S, W> {
Bordered(style, w)
}
}
impl TuiStyle for Tui {}
pub struct Bold<W: Content<Tui>>(pub bool, W);
impl<W: Content<Tui>> Content<Tui> for Bold<W> {
fn content (&self) -> impl Render<Tui> { &self.1 }
fn render (&self, to: &mut TuiOut) {
to.fill_bold(to.area(), self.0);
self.1.render(to)
}
}
pub struct Foreground<W: Content<Tui>>(pub Color, W);
impl<W: Content<Tui>> Content<Tui> for Foreground<W> {
fn content (&self) -> impl Render<Tui> { &self.1 }
fn render (&self, to: &mut TuiOut) {
to.fill_fg(to.area(), self.0);
self.1.render(to)
}
}
pub struct Background<W: Content<Tui>>(pub Color, W);
impl<W: Content<Tui>> Content<Tui> for Background<W> {
fn content (&self) -> impl Render<Tui> { &self.1 }
fn render (&self, to: &mut TuiOut) {
to.fill_bg(to.area(), self.0);
self.1.render(to)
}
}
pub struct Styled<T: Content<Tui>>(pub Option<Style>, pub T);
impl Content<Tui> for Styled<&str> {
fn content (&self) -> impl Render<Tui> { &self.1 }
fn render (&self, to: &mut TuiOut) {
// FIXME
let [x, y, ..] = to.area();
//let [w, h] = self.min_size(to.area().wh())?.unwrap();
to.blit(&self.1, x, y, None)
}
}
//pub trait TuiStyle: Content<Tui> + Sized {
//fn fg (self, color: Color) -> impl Render<Tui> {
//Layers::new(move |add|{ add(&Foreground(color))?; add(&self) })
//}
//fn bg (self, color: Color) -> impl Render<Tui> {
//Layers::new(move |add|{ add(&Background(color))?; add(&self) })
//}
//fn bold (self, on: bool) -> impl Render<Tui> {
//Layers::new(move |add|{ add(&Bold(on))?; add(&self) })
//}
//fn border <S: BorderStyle> (self, style: S) -> impl Render<Tui> {
//Bordered(style, self)
//}
//}
//impl<W: Content<Tui>> TuiStyle for W {}
//impl<S: BorderStyle> Content<Tui> for Border<S> {
//}
//impl<S: BorderStyle, W: Content<Tui>> Content<Tui> for Bordered<S, W> {
//fn content (&self) -> impl Render<Tui> {
//let content: &dyn Content<Tui> = &self.1;
//lay! { content.padding_xy(1, 1), Border(self.0) }.fill_xy()
//}
//}