use kahlo::{math::PixelSideOffsets, prelude::*}; use crate::{ component::Component, layout::{LayoutNode, LayoutNodeAccess, LayoutNodeContainer, SizePolicy}, theme::Theme, ui::UIHandle, window::RenderFormat, }; use super::Widget; pub struct Frame { layout: LayoutNode, child: Option>>, _ghost: std::marker::PhantomData, } impl Frame { pub fn new(uih: &UIHandle) -> Self { let mut nnode = uih.new_layout_node(); nnode .set_height_policy(SizePolicy::expanding(1)) .set_width_policy(SizePolicy::expanding(1)); Self { layout: nnode, child: None, _ghost: Default::default(), } } pub fn set_child(&mut self, child: Box>) { self.child = Some(child); self.layout.relayout(); } } impl LayoutNodeContainer for Frame { fn layout_node(&self) -> &LayoutNode { &self.layout } fn layout_child(&self, ndx: usize) -> Option { if ndx == 0 { Some(self.child.as_ref()?.layout_node()) } else { None } } fn layout_child_count(&self) -> usize { if self.child.is_some() { 1 } else { 0 } } } impl Widget for Frame { fn poll( &mut self, _uih: &mut UIHandle, _input_state: Option<&crate::input::InputState>, ) -> Vec { vec![] } fn layout_node(&self) -> LayoutNodeAccess { LayoutNodeAccess::new(self) } fn layout_node_mut(&mut self) -> &mut LayoutNode { &mut self.layout } fn render(&self, theme: &Theme, target: &mut kahlo::BitmapMut) { let area = self.layout.render_area().unwrap(); target.fill_region(area, theme.border); target.fill_region( area.inner_box(PixelSideOffsets::new_all_same(theme.border_width as i32)), theme.panel, ); } }