1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
use std::any::Any; use crate::description::BoxedDescription; use crate::geom::{Position, Rect, Size}; use crate::view::{Layout, WidgetCache, WidgetKey, WidgetTree}; use crate::Description; pub struct Box { widgets: Vec<BoxedDescription>, } impl Box { pub fn new() -> Box { Box { widgets: Vec::new(), } } pub fn append<D: Description + 'static>(mut self, desc: D) -> Self { self.widgets.push(BoxedDescription::new(desc)); self } } impl Description for Box { fn key(&self) -> Option<WidgetKey> { None } fn apply(self, _: &mut dyn Any) -> Result<(), Self> where Self: Sized, { panic!("Box can't be persisted") } fn create(self, cache: &mut WidgetCache) -> WidgetTree { let children = self .widgets .into_iter() .map(|desc| cache.build(desc)) .collect::<Vec<_>>(); cache.factory().new_layout(BoxLayout {}, children) } } struct BoxLayout {} impl Layout for BoxLayout { fn layout(&self, children: &mut [WidgetTree], _: Size) { let mut y = 0f32; for child in children { let size = child.size_hint(); child.set_rect(Rect::new(Position::new(0.0, y), size)); y += size.height; } } fn size_hint(&self, children: &[WidgetTree]) -> Size { let mut height = 0f32; let mut width = 0f32; for child in children { let size = child.size_hint(); height += size.height; width = width.max(size.width); } Size::new(width, height) } }