use ratatui::{
Frame,
layout::Rect,
style::{Color, Style},
widgets::{Block, BorderType, Borders},
};
use a2ui_base::model::component_context::ComponentContext;
use crate::component_impl::TuiComponent;
pub struct CardComponent;
impl TuiComponent for CardComponent {
fn name(&self) -> &'static str {
"Card"
}
fn render(
&self,
ctx: &ComponentContext,
area: Rect,
frame: &mut Frame,
render_child: &mut dyn FnMut(&str, Rect, &mut Frame, &str),
_measure_child: &mut dyn FnMut(&str, &str, u16) -> Option<u16>,
) {
let comp_model = match ctx.components.get(&ctx.component_id) {
Some(m) => m,
None => return,
};
let inner = crate::layout_engine::padded_content(area);
if inner.width == 0 || inner.height == 0 {
return;
}
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(Color::DarkGray))
.style(Style::default());
let child_area = block.inner(inner);
frame.render_widget(block, inner);
if let Some(child_id) = comp_model.child() {
if child_area.width > 0 && child_area.height > 0 {
render_child(&child_id, child_area, frame, "");
}
}
}
fn natural_height(
&self,
ctx: &ComponentContext,
available_width: u16,
measure_child: &mut dyn FnMut(&str, &str, u16) -> Option<u16>,
) -> Option<u16> {
let comp_model = ctx.components.get(&ctx.component_id)?;
let child_id = comp_model.child()?;
let inner_width = available_width.saturating_sub(4);
let child_h = measure_child(&child_id, "", inner_width)?;
Some(child_h.saturating_add(4))
}
}