use super::theme; use crate::ui::{ component::{Child, Component, Event, EventCtx}, display, geometry::{Insets, Rect}, }; pub struct Frame { area: Rect, border: Insets, title: U, content: Child, } impl Frame where T: Component, U: AsRef, { pub fn new(title: U, content: T) -> Self { Self { title, area: Rect::zero(), border: theme::borders_scroll(), content: Child::new(content), } } pub fn with_border(mut self, border: Insets) -> Self { self.border = border; self } pub fn inner(&self) -> &T { self.content.inner() } } impl Component for Frame where T: Component, U: AsRef, { type Msg = T::Msg; fn place(&mut self, bounds: Rect) -> Rect { const TITLE_SPACE: i32 = theme::BUTTON_SPACING; let (title_area, content_area) = bounds .inset(self.border) .split_top(theme::FONT_BOLD.text_height()); let title_area = title_area.inset(Insets::left(theme::CONTENT_BORDER)); let content_area = content_area.inset(Insets::top(TITLE_SPACE)); self.area = title_area; self.content.place(content_area); bounds } fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option { self.content.event(ctx, event) } fn paint(&mut self) { display::text( self.area.bottom_left(), self.title.as_ref(), theme::FONT_BOLD, theme::GREY_LIGHT, theme::BG, ); self.content.paint(); } fn bounds(&self, sink: &mut dyn FnMut(Rect)) { sink(self.area); self.content.bounds(sink); } } #[cfg(feature = "ui_debug")] impl crate::trace::Trace for Frame where T: crate::trace::Trace, U: crate::trace::Trace + AsRef, { fn trace(&self, t: &mut dyn crate::trace::Tracer) { t.open("Frame"); t.field("title", &self.title); t.field("content", &self.content); t.close(); } }