use ratatui_core::layout::Rect;
use super::geometry::Size;
use super::style::{StyleSheet, Theme};
use super::surface::Surface;
pub struct RenderCtx<'a> {
pub theme: &'a Theme,
pub sheet: StyleSheet,
pub focused: bool,
}
impl<'a> RenderCtx<'a> {
pub fn new(theme: &'a Theme) -> Self {
Self {
theme,
sheet: StyleSheet::from_theme(theme),
focused: false,
}
}
pub fn with_sheet(mut self, sheet: StyleSheet) -> Self {
self.sheet = sheet;
self
}
pub fn with_focus(&self, focused: bool) -> RenderCtx<'a> {
RenderCtx {
theme: self.theme,
sheet: self.sheet,
focused,
}
}
}
pub trait View {
fn measure(&self, available: Size) -> Size;
fn render(&self, area: Rect, surface: &mut Surface, ctx: &RenderCtx);
}
pub type Element = Box<dyn View>;
impl View for Element {
fn measure(&self, available: Size) -> Size {
(**self).measure(available)
}
fn render(&self, area: Rect, surface: &mut Surface, ctx: &RenderCtx) {
(**self).render(area, surface, ctx)
}
}
pub fn element<V: View + 'static>(view: V) -> Element {
Box::new(view)
}