tuigui 0.23.0

An easy-to-use, highly extensible, and speedy TUI library.
Documentation
use crate::preludes::widget_creation::*;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
/// Margin containers draw their child widget with a gap around it
pub struct MarginContainer<W: Widget> {
    /// Number of rows above child
    pub top: u16,
    /// Number of rows below child
    pub bottom: u16,
    /// Number of columns to the left of the child
    pub left: u16,
    /// Number of columns to the right of the child
    pub right: u16,
    /// Child widget
    pub widget: W,

    widget_data: WidgetData,
}

impl<W: Widget> MarginContainer<W> {
    pub fn new(
        top: u16,
        right: u16,
        bottom: u16,
        left: u16,
        widget: W,
    ) -> Self {
        Self {
            top,
            bottom,
            left,
            right,
            widget,
            widget_data: WidgetData::new(),
        }
    }
}

impl<W: Widget> Widget for MarginContainer<W> {
    fn draw(&mut self, canvas: &mut Canvas, state_frame: Option<&EventStateFrame>) {
        if canvas.is_visible() == false {
            return;
        }

        let subtract = Size::new(self.left + self.right, self.top + self.bottom);

        let size: Size;

        if canvas.transform.size.rows < subtract.rows || canvas.transform.size.cols < subtract.cols {
            size = subtract;
        } else {
            size = canvas.transform.size - subtract;
        }

        let mut inner = canvas.new_child(Transform::new(
            Position::new(self.left as i16, self.top as i16),
            size,
        ));

        self.widget.draw(&mut inner, state_frame);
    }

    fn widget_info(&self) -> WidgetInfo {
        let child_info = self.widget.widget_info();

        let size_info: WidgetSizeInfo;

        let addition = Size::new(
            self.left + self.right,
            self.top + self.bottom,
        );

        if addition == Size::new(0, 0) {
            size_info = child_info.size_info;
        }

        else {
            size_info = match child_info.size_info {
                WidgetSizeInfo::Dynamic { min, max } => {
                    let new_min = Some(min.unwrap_or(Size::new(0, 0)) + addition);

                    let new_max = match max {
                        Some(s) => Some(s + addition),
                        None => None,
                    };

                    WidgetSizeInfo::Dynamic {
                        min: new_min,
                        max: new_max,
                    }
                },
                WidgetSizeInfo::Fixed(fixed_size) => WidgetSizeInfo::Fixed(
                    fixed_size + addition,
                ),
            };
        }

        return WidgetInfo {
            size_info,
        };
    }

    fn widget_data(&mut self) -> &mut WidgetData {
        return &mut self.widget_data;
    }
}