~tm85/lintuini

e3d8c2115570e47d29b67c4aa810b0a68a998a50 — TerraMaster85 30 days ago 44f71ca
feat: misguided attempt #2
12 files changed, 112 insertions(+), 154 deletions(-)

M Cargo.lock
M Cargo.toml
A src/.lib.rs.swp
A src/elements/element.rs
A src/elements/frame.rs
A src/elements/mod.rs
A src/elements/root.rs
M src/error.rs
M src/lib.rs
D src/widgets/frame.rs
D src/widgets/mod.rs
D src/widgets/widget.rs
M Cargo.lock => Cargo.lock +7 -0
@@ 44,6 44,12 @@ dependencies = [
]

[[package]]
name = "critical-section"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"

[[package]]
name = "either"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 149,6 155,7 @@ checksum = "cbb175c433c8e28a809d1f5773a2ae96e68c0ce40db865cbab1020bf33ae479c"
dependencies = [
 "bitflags",
 "compact_str",
 "critical-section",
 "hashbrown 0.17.1",
 "itertools",
 "kasuari",

M Cargo.toml => Cargo.toml +1 -1
@@ 4,4 4,4 @@ version = "0.1.0"
edition = "2024"

[dependencies]
ratatui-core = "0.1.2"
ratatui-core = { version = "0.1.2", features = ["layout-cache"] }

A src/.lib.rs.swp => src/.lib.rs.swp +0 -0
A src/elements/element.rs => src/elements/element.rs +8 -0
@@ 0,0 1,8 @@
use ratatui_core::layout;

use crate::error;

pub trait Element {
    fn constraint(&self) -> &layout::Constraint;
    fn render(&self);
}

A src/elements/frame.rs => src/elements/frame.rs +85 -0
@@ 0,0 1,85 @@
use ratatui_core::layout;
use ratatui_core::style;
use ratatui_core::symbols;
use ratatui_core::widgets::Widget;

use super::element::Element;
use crate::error;

pub struct VFrame<'a> {
    children: Vec<Box<dyn Element>>,
    constraint: layout::Constraint,
    fill: style::Style,
}

impl Element for Frame {
    fn add_child(&mut self, child: Box<dyn Element>) -> Result<(), error::LintuiniError>{

        self.children.push(child);
        Ok(())
    }
    #[inline(always)]
    fn children(&self) -> &[Box<dyn Element>] {
        self.children.as_slice()
    }
    #[inline(always)]
    fn children_mut(&mut self) -> &mut [Box<dyn Element>] {
        self.children.as_mut_slice()
    }

    #[inline(always)]
    fn constraint(&self) -> &layout::Constraint {
        &self.constraint
    }
}

#[derive(Default)]
pub struct FrameBuilder<'a> {
    fill: style::Style,
    constraint: layout::Constraint,
    border: Option<BorderStyle<'a>>,
}

impl FrameBuilder<'_> {
    #[inline(always)]
    fn new() -> Self {
        FrameBuilder::default()
    }
    #[inline(always)]
    fn fill(mut self, style: style::Style) -> Self {
        self.fill = style;
        self
    }
    #[inline(always)]
    fn border_style(mut self, style: style::Style) -> Self {
        match self.border {
            Some(mut bs) => { bs.style = style; },
            None => { self.border = Some(BorderStyle { style: style, ..Default::default() }) },
        }
        self
    }
    #[inline(always)]
    fn border_charset(mut self, charset: symbols::border::Set<'_>) -> Self {
        match self.border {
            Some(mut bs) => { bs.charset = charset; },
            None => { self.border = Some(BorderStyle { charset: charset, ..Default::default() }) },
        }
        self
    }

    #[inline(always)]
    fn build<'a>(self) -> Frame<'a> {
        Frame {
            border: self.border.clone(),
            constraint: self.constraint,
            children: Vec::new(),
            fill: self.fill,
        }
    }
}

#[derive(Clone, Copy, Default)]
pub struct BorderStyle<'a> {
    style: style::Style,
    charset: symbols::border::Set<'a>,
}

A src/elements/mod.rs => src/elements/mod.rs +4 -0
@@ 0,0 1,4 @@
pub mod frame;

pub mod element;
pub use element::*;

A src/elements/root.rs => src/elements/root.rs +2 -0
@@ 0,0 1,2 @@
pub struct Root {
    

M src/error.rs => src/error.rs +3 -6
@@ 1,15 1,12 @@
#[derive(Debug)]
pub enum WitError {
    WidgetError(WidgetError),
    CompositorError(CompositorError),
pub enum LintuiniError {
    ElementError(ElementError),
}

#[derive(Debug)]
pub enum WidgetError {
pub enum ElementError {
    SetSize,
    AddChild,
    SetPos,
    SetDisplayMode,
}

#[derive(Debug)]

M src/lib.rs => src/lib.rs +2 -2
@@ 1,6 1,6 @@
pub mod widgets;
pub mod elements;

pub mod error;

#[cfg(test)]
mod tests;
mod test;

D src/widgets/frame.rs => src/widgets/frame.rs +0 -43
@@ 1,43 0,0 @@
use super::widget::{Widget, DisplayMode, WidgetSize, WidgetPos};
use crate::error;

pub struct Frame {
    size: WidgetSize,
    pos: WidgetPos,
    display_mode: DisplayMode,
    children: Vec<Box<dyn Widget>>,
}

impl Widget for Frame {
    fn set_size(&mut self, new_size: WidgetSize) -> Result<(), error::WitError>{
        self.size = new_size;
        Ok(())
    }
    fn size(&self) -> WidgetSize {
        self.size
    }

    fn add_child(&mut self, child: Box<dyn Widget>) -> Result<(), error::WitError>{
        self.children.push(child);
        Ok(())
    }
    fn children(&self) -> &[Box<dyn Widget>] {
        self.children.as_slice()
    }

    fn set_pos(&mut self, new_pos: WidgetPos) -> Result<(), error::WitError>{
        self.pos = new_pos;
        Ok(())
    }
    fn pos(&self) -> WidgetPos {
        self.pos
    }
    
    fn set_display_mode(&mut self, new_display_mode: DisplayMode) -> Result<(), error::WitError>{
        self.display_mode = new_display_mode;
        Ok(())
    }
    fn display_mode(&self) -> DisplayMode {
        self.display_mode
    }
}

D src/widgets/mod.rs => src/widgets/mod.rs +0 -4
@@ 1,4 0,0 @@
pub mod frame;

pub mod widget;
pub use widget::*;

D src/widgets/widget.rs => src/widgets/widget.rs +0 -98
@@ 1,98 0,0 @@
use crate::error;

pub trait Widget {
    fn set_size(&mut self, size: WidgetSize) -> Result<(), error::WitError>;
    fn size(&self) -> WidgetSize;
    fn add_child(&mut self, child: Box<dyn Widget>) -> Result<(), error::WitError>;
    fn children(&self) -> &[Box<dyn Widget>];
    fn set_pos(&mut self, pos: WidgetPos) -> Result<(), error::WitError>;
    fn pos(&self) -> WidgetPos;
    fn set_display_mode(&mut self, display: DisplayMode) -> Result<(), error::WitError>;
    fn display_mode(&self) -> DisplayMode;

    fn set_static(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Static) }
    fn is_static(&self) -> bool { matches!(self.display_mode(), DisplayMode::Static) }
    fn set_floating(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Floating) }
    fn is_floating(&self) -> bool { matches!(self.display_mode(), DisplayMode::Floating) }
    fn hide(&mut self) -> Result<(), error::WitError> { self.set_display_mode(DisplayMode::Hidden) }
    fn is_hidden(&self) -> bool { matches!(self.display_mode(), DisplayMode::Hidden) }
}

// should write some macros to make this nicer

#[derive(Clone, Copy)]
pub struct WidgetSize(WidgetSizeDimn, WidgetSizeDimn);

impl WidgetSize {
    #[inline(always)]
    pub fn x(&self) -> WidgetSizeDimn {
        self.0
    }
    #[inline(always)]
    pub fn y(&self) -> WidgetSizeDimn {
        self.1
    }
}

impl From<WidgetSize> for (u16, u16) {
    #[inline(always)]
    fn from(size: WidgetSize) -> Self {
        (size.0.into(), size.1.into())
    }
}

#[derive(Clone, Copy)]
pub enum WidgetSizeDimn {
    Static(u16),
    Fill,
    Fit,
}

impl From<WidgetSizeDimn> for u16 {
    #[inline(always)]
    fn from(dim: WidgetSizeDimn) -> Self {
        match dim {
            WidgetSizeDimn::Static(x) => x,
            WidgetSizeDimn::Fill => todo!(),
            WidgetSizeDimn::Fit => todo!(),
        }
    }
}

#[derive(Clone, Copy)]
pub struct WidgetPos(WidgetPosDimn, WidgetPosDimn);

impl WidgetPos {
    #[inline(always)]
    pub fn x(&self) -> WidgetPosDimn {
        self.0
    }
    #[inline(always)]
    pub fn y(&self) -> WidgetPosDimn {
        self.1
    }
}

impl Default for WidgetPos {
    fn default() -> WidgetPos { 
        WidgetPos(WidgetPosDimn::FloatStart, WidgetPosDimn::FloatStart)
    }
}

#[derive(Clone, Copy)]
pub enum WidgetPosDimn {
    Static(u16),
    FloatStart,
    FloatEnd,
}

impl Default for WidgetPosDimn {
    fn default() -> Self { Self::FloatStart }
}

#[derive(Clone, Copy)]
pub enum DisplayMode {
    Static,
    Floating,
    Hidden,
}