|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use anathema_geometry::Size; |
| 4 | +use anathema_state::States; |
| 5 | +use anathema_store::tree::root_node; |
| 6 | +use anathema_templates::Document; |
| 7 | +use anathema_value_resolver::{AttributeStorage, Attributes, Scope}; |
| 8 | + |
| 9 | +use crate::components::ComponentRegistry; |
| 10 | +use crate::layout::{LayoutCtx, Viewport}; |
| 11 | +use crate::{eval_blueprint, Components, Factory, FloatingWidgets, GlyphMap, LayoutForEach, Widget, WidgetTree, WidgetTreeView}; |
| 12 | + |
| 13 | +pub struct TestCase {} |
| 14 | + |
| 15 | +pub fn with_template<F>(tpl: &str, f: F) |
| 16 | +where |
| 17 | + F: Fn(WidgetTreeView<'_, '_>), |
| 18 | +{ |
| 19 | + let mut tree = WidgetTree::empty(); |
| 20 | + let mut doc = Document::new(tpl); |
| 21 | + let (blueprint, globals) = doc.compile().unwrap(); |
| 22 | + let globals = Box::leak(Box::new(globals)); |
| 23 | + let blueprint = Box::leak(Box::new(blueprint)); |
| 24 | + |
| 25 | + let mut factory = Factory::new(); |
| 26 | + factory.register_default::<Many>("many"); |
| 27 | + factory.register_default::<Text>("text"); |
| 28 | + |
| 29 | + let mut states = States::new(); |
| 30 | + let mut attribute_storage = AttributeStorage::empty(); |
| 31 | + |
| 32 | + let mut components = Components::new(); |
| 33 | + let mut component_registry = ComponentRegistry::new(); |
| 34 | + |
| 35 | + let mut viewport = Viewport::new((80, 25)); |
| 36 | + |
| 37 | + let mut floating = FloatingWidgets::empty(); |
| 38 | + let mut glyph_map = GlyphMap::empty(); |
| 39 | + |
| 40 | + let mut layout_ctx = LayoutCtx::new( |
| 41 | + globals, |
| 42 | + &factory, |
| 43 | + &mut states, |
| 44 | + &mut attribute_storage, |
| 45 | + &mut components, |
| 46 | + &mut component_registry, |
| 47 | + &mut floating, |
| 48 | + &mut glyph_map, |
| 49 | + &mut viewport, |
| 50 | + ); |
| 51 | + |
| 52 | + let mut ctx = layout_ctx.eval_ctx(None); |
| 53 | + let scope = Scope::root(); |
| 54 | + |
| 55 | + eval_blueprint(blueprint, &mut ctx, &scope, root_node(), &mut tree.view_mut()).unwrap(); |
| 56 | + |
| 57 | + let filter = crate::layout::LayoutFilter::fixed(); |
| 58 | + let mut for_each = LayoutForEach::new(tree.view_mut(), &scope, filter, None); |
| 59 | + for_each.each(&mut layout_ctx, |ctx, widget, children| { |
| 60 | + _ = widget.layout(children, ctx.viewport.constraints(), ctx)?; |
| 61 | + Ok(ControlFlow::Break(())) |
| 62 | + }).unwrap(); |
| 63 | + |
| 64 | + |
| 65 | + f(tree.view_mut()); |
| 66 | +} |
| 67 | + |
| 68 | +// ----------------------------------------------------------------------------- |
| 69 | +// - Test widgets - |
| 70 | +// ----------------------------------------------------------------------------- |
| 71 | + |
| 72 | +#[derive(Debug, Default)] |
| 73 | +pub(crate) struct Many; |
| 74 | + |
| 75 | +impl Widget for Many { |
| 76 | + fn layout<'bp>( |
| 77 | + &mut self, |
| 78 | + mut children: crate::LayoutForEach<'_, 'bp>, |
| 79 | + constraints: crate::layout::Constraints, |
| 80 | + id: crate::WidgetId, |
| 81 | + ctx: &mut LayoutCtx<'_, 'bp>, |
| 82 | + ) -> crate::error::Result<anathema_geometry::Size> { |
| 83 | + let mut size = Size::ZERO; |
| 84 | + children.each(ctx, |ctx, child, children| { |
| 85 | + let child_size: Size = child.layout(children, constraints, ctx)?.into(); |
| 86 | + size.width = size.width.max(child_size.width); |
| 87 | + size.height += child_size.height; |
| 88 | + Ok(ControlFlow::Continue(())) |
| 89 | + }); |
| 90 | + |
| 91 | + Ok(size) |
| 92 | + } |
| 93 | + |
| 94 | + fn position<'bp>( |
| 95 | + &mut self, |
| 96 | + children: crate::ForEach<'_, 'bp, crate::layout::PositionFilter>, |
| 97 | + id: crate::WidgetId, |
| 98 | + attribute_storage: &AttributeStorage<'bp>, |
| 99 | + ctx: crate::layout::PositionCtx, |
| 100 | + ) { |
| 101 | + // |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Debug, Default)] |
| 106 | +pub(crate) struct Text; |
| 107 | + |
| 108 | +impl Widget for Text { |
| 109 | + fn layout<'bp>( |
| 110 | + &mut self, |
| 111 | + children: crate::LayoutForEach<'_, 'bp>, |
| 112 | + constraints: crate::layout::Constraints, |
| 113 | + id: crate::WidgetId, |
| 114 | + ctx: &mut LayoutCtx<'_, 'bp>, |
| 115 | + ) -> crate::error::Result<anathema_geometry::Size> { |
| 116 | + Ok(Size::ZERO) |
| 117 | + } |
| 118 | + |
| 119 | + fn position<'bp>( |
| 120 | + &mut self, |
| 121 | + children: crate::ForEach<'_, 'bp, crate::layout::PositionFilter>, |
| 122 | + id: crate::WidgetId, |
| 123 | + attribute_storage: &AttributeStorage<'bp>, |
| 124 | + ctx: crate::layout::PositionCtx, |
| 125 | + ) { |
| 126 | + } |
| 127 | +} |
| 128 | + |
0 commit comments