extract tui support code to tek_tui

This commit is contained in:
🪞👃🪞 2025-01-05 10:50:32 +01:00
parent 1a9077427c
commit 1faf5bb6df
22 changed files with 477 additions and 450 deletions

86
tui/src/tui_style.rs Normal file
View file

@ -0,0 +1,86 @@
use crate::*;
pub trait TuiStyle {
fn fg <R: Content<Tui>> (color: Color, w: R) -> Foreground<R> {
Foreground(color, w)
}
fn bg <R: Content<Tui>> (color: Color, w: R) -> Background<R> {
Background(color, w)
}
fn fg_bg <R: Content<Tui>> (fg: Color, bg: Color, w: R) -> Background<Foreground<R>> {
Background(bg, Foreground(fg, w))
}
fn bold <R: Content<Tui>> (on: bool, w: R) -> Bold<R> {
Bold(on, w)
}
fn border <R: Content<Tui>, S: BorderStyle> (style: S, w: R) -> Bordered<S, R> {
Bordered(style, w)
}
}
impl TuiStyle for Tui {}
pub struct Bold<R: Content<Tui>>(pub bool, R);
impl<R: Content<Tui>> Content<Tui> for Bold<R> {
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<R: Content<Tui>>(pub Color, R);
impl<R: Content<Tui>> Content<Tui> for Foreground<R> {
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<R: Content<Tui>>(pub Color, R);
impl<R: Content<Tui>> Content<Tui> for Background<R> {
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<R: Content<Tui>>(pub Option<Style>, pub R);
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: Render<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<R: Content<Tui>> TuiStyle for R {}
//impl<S: BorderStyle> Content<Tui> for Border<S> {
//}
//impl<S: BorderStyle, R: Content<Tui>> Content<Tui> for Bordered<S, R> {
//fn content (&self) -> impl Render<Tui> {
//let content: &dyn Content<Tui> = &self.1;
//lay! { content.padding_xy(1, 1), Border(self.0) }.fill_xy()
//}
//}