output: refactor Content and Render traits

This commit is contained in:
🪞👃🪞 2025-09-06 08:46:52 +03:00
parent 74b3af2212
commit 1c21a85f27
24 changed files with 346 additions and 613 deletions

View file

@ -1,60 +1,70 @@
use crate::*;
pub trait TuiStyle {
fn fg <R: Content<TuiOut>> (color: Color, w: R) -> Foreground<R> {
fn fg <R: Render<TuiOut>> (color: Color, w: R) -> Foreground<R> {
Foreground(color, w)
}
fn bg <R: Content<TuiOut>> (color: Color, w: R) -> Background<R> {
fn bg <R: Render<TuiOut>> (color: Color, w: R) -> Background<R> {
Background(color, w)
}
fn fg_bg <R: Content<TuiOut>> (fg: Color, bg: Color, w: R) -> Background<Foreground<R>> {
fn fg_bg <R: Render<TuiOut>> (fg: Color, bg: Color, w: R) -> Background<Foreground<R>> {
Background(bg, Foreground(fg, w))
}
fn modify <R: Content<TuiOut>> (enable: bool, modifier: Modifier, w: R) -> Modify<R> {
fn modify <R: Render<TuiOut>> (enable: bool, modifier: Modifier, w: R) -> Modify<R> {
Modify(enable, modifier, w)
}
fn bold <R: Content<TuiOut>> (enable: bool, w: R) -> Modify<R> {
fn bold <R: Render<TuiOut>> (enable: bool, w: R) -> Modify<R> {
Self::modify(enable, Modifier::BOLD, w)
}
fn border <R: Content<TuiOut>, S: BorderStyle> (enable: bool, style: S, w: R) -> Bordered<S, R> {
fn border <R: Render<TuiOut>, S: BorderStyle> (enable: bool, style: S, w: R) -> Bordered<S, R> {
Bordered(enable, style, w)
}
}
impl TuiStyle for Tui {}
pub struct Foreground<R: Content<TuiOut>>(pub Color, pub R);
impl<R: Content<TuiOut>> Content<TuiOut> for Foreground<R> {
fn content (&self) -> impl Render<TuiOut> { &self.1 }
pub struct Foreground<R>(pub Color, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Foreground<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.fill_fg(to.area(), self.0);
self.1.render(to)
let area = self.layout(to.area());
to.fill_fg(area, self.0);
to.place(area, &self.1);
}
}
pub struct Background<R: Content<TuiOut>>(pub Color, pub R);
impl<R: Content<TuiOut>> Content<TuiOut> for Background<R> {
fn content (&self) -> impl Render<TuiOut> { &self.1 }
pub struct Background<R>(pub Color, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Background<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.fill_bg(to.area(), self.0);
self.1.render(to)
let area = self.layout(to.area());
to.fill_bg(area, self.0);
to.place(area, &self.1);
}
}
pub struct Modify<R: Content<TuiOut>>(pub bool, pub Modifier, pub R);
impl<R: Content<TuiOut>> Content<TuiOut> for Modify<R> {
fn content (&self) -> impl Render<TuiOut> { &self.2 }
pub struct Modify<R: Render<TuiOut>>(pub bool, pub Modifier, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Modify<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.2.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.fill_mod(to.area(), self.0, self.1);
self.2.render(to)
}
}
pub struct Styled<R: Content<TuiOut>>(pub Option<Style>, pub R);
impl<R: Content<TuiOut>> Content<TuiOut> for Styled<R> {
fn content (&self) -> impl Render<TuiOut> { &self.1 }
pub struct Styled<R: Render<TuiOut>>(pub Option<Style>, pub R);
impl<R: Render<TuiOut>> Render<TuiOut> for Styled<R> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
self.1.layout(to)
}
fn render (&self, to: &mut TuiOut) {
to.place(self.content().layout(to.area()), &self.content());
to.place(self.1.layout(to.area()), &self.1);
// TODO write style over area
}
}