use tuirealm::{
AttrValue, Attribute, Props,
props::Style,
ratatui::layout::{Constraint, Layout, Rect},
};
pub fn draw_area_in_relative(parent: Rect, width: u16, height: u16) -> Rect {
let new_area = Layout::vertical([
Constraint::Percentage((100 - height) / 2),
Constraint::Percentage(height),
Constraint::Percentage((100 - height) / 2),
])
.split(parent);
Layout::horizontal([
Constraint::Percentage((100 - width) / 2),
Constraint::Percentage(width),
Constraint::Percentage((100 - width) / 2),
])
.split(new_area[1])[1]
}
pub fn draw_area_in_absolute(parent: Rect, width: u16, height: u16) -> Rect {
let new_area = Layout::vertical([
Constraint::Length((parent.height - height) / 2),
Constraint::Length(height),
Constraint::Length((parent.height - height) / 2),
])
.split(parent);
Layout::horizontal([
Constraint::Length((parent.width - width) / 2),
Constraint::Length(width),
Constraint::Length((parent.width - width) / 2),
])
.split(new_area[1])[1]
}
pub fn draw_area_top_right_absolute(parent: Rect, width: u16, height: u16) -> Rect {
let new_area = Layout::vertical([
Constraint::Length(1),
Constraint::Length(height),
Constraint::Length(parent.height - height - 1),
])
.split(parent);
Layout::horizontal([
Constraint::Length(parent.width - width - 1),
Constraint::Length(width),
Constraint::Length(1),
])
.split(new_area[1])[1]
}
pub fn get_style(props: &Props) -> Style {
let mut style = Style::default();
if let Some(fg) = props
.get_ref(Attribute::Foreground)
.and_then(AttrValue::as_color)
{
style = style.fg(fg);
}
if let Some(bg) = props
.get_ref(Attribute::Background)
.and_then(AttrValue::as_color)
{
style = style.bg(bg);
}
if let Some(modifiers) = props
.get_ref(Attribute::TextProps)
.and_then(AttrValue::as_text_modifiers)
{
style = style.add_modifier(modifiers);
}
style
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_utils_ui_draw_area_in() {
let area: Rect = Rect::new(0, 0, 1024, 512);
let child: Rect = draw_area_in_relative(area, 75, 30);
assert_eq!(child, Rect::new(123, 179, 768, 154));
}
}