~tm85/lintuini

281900fe61d7df4c18826576a3ed56064e914e45 — TerraMaster85 26 days ago e3ca25d main
fmt: all
A .rustfmt.toml => .rustfmt.toml +2 -0
@@ 0,0 1,2 @@
max_width = 80
edition = "2024"

M examples/empty_container.rs => examples/empty_container.rs +1 -1
@@ 1,6 1,6 @@
extern crate lintuini;
use lintuini::elements::Element;
use lintuini::elements::Container;
use lintuini::elements::Element;

use crossterm::event;


M src/elements/container.rs => src/elements/container.rs +8 -7
@@ 1,11 1,9 @@
use ratatui_core::layout;
use ratatui_core::terminal::Frame;

use ratatui_widgets::block;

use crate::error;
use crate::elements::{Element, ElementWithChildren};
use crate::elements::macros::*;
use crate::elements::{Element, ElementWithChildren};
use crate::error;

#[derive(Default)]
pub struct Container<'a> {


@@ 17,7 15,7 @@ pub struct Container<'a> {

#[derive(Clone, Default)]
struct ContainerWidgets<'a> {
    block0: block::Block<'a>,
    block0: ratatui_widgets::block::Block<'a>,
}

basic_config_methods!(Container<'_>);


@@ 29,7 27,11 @@ impl Element for Container<'_> {
        self.constraint
    }

    fn render(&self, frm: &mut Frame, rect: layout::Rect) -> Result<(), error::LintuiniError> {
    fn render(
        &self,
        frm: &mut Frame,
        rect: layout::Rect,
    ) -> Result<(), error::LintuiniError> {
        frm.render_widget(&self.widgets.block0, rect);

        for (el, r) in self.children.iter().zip(&*self.layout.split(rect)) {


@@ 39,4 41,3 @@ impl Element for Container<'_> {
        Ok(())
    }
}


M src/elements/element.rs => src/elements/element.rs +9 -2
@@ 5,11 5,18 @@ use crate::error;

pub trait Element {
    fn constraint(&self) -> layout::Constraint;
    fn render(&self, frm: &mut Frame, rect: layout::Rect) -> Result<(), error::LintuiniError>;
    fn render(
        &self,
        frm: &mut Frame,
        rect: layout::Rect,
    ) -> Result<(), error::LintuiniError>;
}

pub trait ElementWithChildren: Element {
    fn add_child(&mut self, child: impl Into<Box<dyn Element>>) -> Result<(), error::LintuiniError>;
    fn add_child(
        &mut self,
        child: impl Into<Box<dyn Element>>,
    ) -> Result<(), error::LintuiniError>;
    fn children(&self) -> &[Box<dyn Element>];
    fn children_mut(&mut self) -> &mut [Box<dyn Element>];
}

M src/elements/macros.rs => src/elements/macros.rs +19 -10
@@ 2,7 2,10 @@ macro_rules! element_with_children {
    ($el:ty) => {
        impl ElementWithChildren for $el {
            #[inline(always)]
            fn add_child(&mut self, child: impl Into<Box<dyn Element>>) -> Result<(), crate::error::LintuiniError>{
            fn add_child(
                &mut self,
                child: impl Into<Box<dyn Element>>,
            ) -> Result<(), crate::error::LintuiniError> {
                self.children.push(child.into());
                self.regenerate_layout();
                Ok(())


@@ 18,12 21,12 @@ macro_rules! element_with_children {
                self.children.as_mut_slice()
            }
        }
    }
    };
}
pub(crate) use element_with_children;

macro_rules! basic_config_methods {
    ($el:ty) => {    
    ($el:ty) => {
        impl $el {
            #[inline(always)]
            pub fn new() -> Self {


@@ 31,29 34,35 @@ macro_rules! basic_config_methods {
            }

            #[inline(always)]
            pub fn constraint(&mut self, constraint: ratatui_core::layout::Constraint) -> &mut Self {
            pub fn constraint(
                &mut self,
                constraint: ratatui_core::layout::Constraint,
            ) -> &mut Self {
                self.constraint = constraint;
                self
            }

            // beware the owned self!
            #[inline(always)]
            pub fn direction(mut self, direction: ratatui_core::layout::Direction) -> Self {
            pub fn direction(
                mut self,
                direction: ratatui_core::layout::Direction,
            ) -> Self {
                self.layout = self.layout.direction(direction);
                self.regenerate_layout();
                self
            }

            pub fn regenerate_layout(&mut self) -> &mut Self {
                if self.children.len() == 0 { return self; }
                if self.children.len() == 0 {
                    return self;
                }
                self.layout = self.layout.clone().constraints(
                    self.children
                        .iter()
                        .map(|el| el.constraint())
                    self.children.iter().map(|el| el.constraint()),
                );
                self
            }
        }
    }
    };
}
pub(crate) use basic_config_methods;

M src/elements/mod.rs => src/elements/mod.rs +0 -1
@@ 5,4 5,3 @@ pub mod element;
pub use element::*;

pub mod macros;


M src/error.rs => src/error.rs +1 -3
@@ 15,6 15,4 @@ pub enum ElementError {
}

#[derive(Debug, Error)]
pub enum RenderError {
    
}
pub enum RenderError {}

M src/tests.rs => src/tests.rs +1 -0
@@ 0,0 1,1 @@