fp_tui 0.2.2

A very basic tui library
Documentation
use std::rc::Rc;
use std::cell::RefCell;

use crate::common::{BoundingBox, Position, Dimensions};
use crate::{ContainerWidget, PositioningAlgorithm, Widget, DisplayBuffer};

/// #### Description
/// A basic container widget
/// 
pub struct FrameWidget {
    /// #### Description
    /// The bounding box of the frame widget
    /// 
    bounding_box: BoundingBox,

    /// #### Description
    /// holds all the child nodes and each of their positioning algorithms
    /// 
    children: Vec<(Rc<RefCell<dyn Widget>>, Box<dyn PositioningAlgorithm>)>
}

impl FrameWidget {
    /// #### Description
    /// Creates a new frame widget
    /// 
    /// #### Arguments
    /// * `x`: i32 - The x position of the frame
    /// * `y`: i32 - The y position of the frame
    /// * `width`: u32 - The width of the frame
    /// * `height`: u32 - The height of the frame
    /// 
    /// #### Returns
    /// [Rc]<[RefCell]<[FrameWidget]>> - A boxed frame widget
    /// 
    pub fn new(x: i32, y: i32, width: u32, height: u32) -> Rc<RefCell<Self>> {
        return Rc::new(RefCell::new(
            Self {
                bounding_box: BoundingBox::new(
                    Position::new(x, y),
                    Dimensions::new(width, height)
                ),
                children: vec![]
            }
        ));
    }
}

impl Widget for FrameWidget {
    fn draw(
            &mut self,
            positioning_algorithm: &Box<dyn PositioningAlgorithm>,
            display_buffer: &mut DisplayBuffer
        ) {

        let frame_abs_pos = positioning_algorithm.calculate_position(
            self.bounding_box.get_position()
        );

        for (widget, pos_alg) in &mut self.children {
            pos_alg.set_parent_position(frame_abs_pos);
            widget.borrow_mut().draw(pos_alg, display_buffer);
        }
    }

    fn get_bounding_box(&self) -> BoundingBox {
        return self.bounding_box;
    }
}

impl ContainerWidget for FrameWidget {
    fn add_child(
            &mut self,
            positioning_algorithm: Box<dyn PositioningAlgorithm>,
            widget: Rc<RefCell<dyn Widget>>
        ) {
        self.children.push((widget, positioning_algorithm));
    }
}