diff --git a/anathema-default-widgets/src/expand.rs b/anathema-default-widgets/src/expand.rs index afa445c3..d1f2a3d8 100644 --- a/anathema-default-widgets/src/expand.rs +++ b/anathema-default-widgets/src/expand.rs @@ -65,7 +65,53 @@ impl Widget for Expand { #[cfg(test)] mod test { + use crate::testing::TestRunner; #[test] - fn expand() {} + fn hstack_expand_nospace() { + let tpl = " + border + container [width:12] + hstack + expand + text 'this is text' + "; + + let expected = " + ╔══════════════╗ + ║┌────────────┐║ + ║│this is text│║ + ║│ │║ + ║└────────────┘║ + ╚══════════════╝ + "; + + let mut runner = TestRunner::new(tpl, (14, 4)); + let mut runner = runner.instance(); + runner.render_assert(expected); + } + + #[test] + fn vstack_expand_nospace() { + let tpl = " + border + container [height:1] + vstack + expand + text 'this is text' + "; + + let expected = " + ╔═════════════════╗ + ║┌───────────────┐║ + ║│this is text │║ + ║└───────────────┘║ + ║ ║ + ╚═════════════════╝ + "; + + let mut runner = TestRunner::new(tpl, (17, 4)); + let mut runner = runner.instance(); + runner.render_assert(expected); + } } diff --git a/anathema-default-widgets/src/layout/expand.rs b/anathema-default-widgets/src/layout/expand.rs index b32acefa..d3eb9e8a 100644 --- a/anathema-default-widgets/src/layout/expand.rs +++ b/anathema-default-widgets/src/layout/expand.rs @@ -16,21 +16,13 @@ const DEFAULT_FACTOR: u16 = 1; /// Panics when called with more weights than the total number of available size. /// Allocates a minimum of one to each weight. fn distribute_size(weights: &[u16], mut total: u16) -> Vec { - assert!( - total >= weights.len() as u16, - "{total} is not larger than {}", - weights.len() - ); - let mut indexed = weights .iter() .copied() .enumerate() - .map(|(i, w)| (i, w, 1u16)) + .map(|(i, w)| (i, w, 0u16)) .collect::>(); - total -= weights.len() as u16; - fn pop(n: &mut u16) -> bool { if let Some(nn) = n.checked_sub(1) { *n = nn; diff --git a/anathema-default-widgets/src/layout/many.rs b/anathema-default-widgets/src/layout/many.rs index 7a1278b0..3b1e1c99 100644 --- a/anathema-default-widgets/src/layout/many.rs +++ b/anathema-default-widgets/src/layout/many.rs @@ -115,16 +115,13 @@ impl Many { } })?; - // Apply spacer and expand if the layout is constrained and we have remaining space - if !self.unconstrained && !self.used_size.no_space_left() { - let constraints = self.used_size.to_constraints(); - let expanded_size = expand::layout_all_expansions(&mut children, constraints, self.axis, ctx)?; - self.used_size.apply(expanded_size); - - let constraints = self.used_size.to_constraints(); - let spacer_size = spacers::layout_all_spacers(&mut children, constraints, self.axis, ctx)?; - self.used_size.apply(spacer_size); - } + let constraints = self.used_size.to_constraints(); + let expanded_size = expand::layout_all_expansions(&mut children, constraints, self.axis, ctx)?; + self.used_size.apply(expanded_size); + + let constraints = self.used_size.to_constraints(); + let spacer_size = spacers::layout_all_spacers(&mut children, constraints, self.axis, ctx)?; + self.used_size.apply(spacer_size); size.width = self.used_size.inner.width.max(max_constraints.min_width); size.height = (self.used_size.inner.height).max(max_constraints.min_height); diff --git a/anathema-default-widgets/src/spacer.rs b/anathema-default-widgets/src/spacer.rs index d427462d..580a7458 100644 --- a/anathema-default-widgets/src/spacer.rs +++ b/anathema-default-widgets/src/spacer.rs @@ -33,3 +33,56 @@ impl Widget for Spacer { // The spacer widget has no children } } + +#[cfg(test)] +mod test { + use crate::testing::TestRunner; + + #[test] + fn hstack_spacer_nospace() { + let tpl = " + border + container [width:12] + hstack + spacer + text 'this is text' + "; + + let expected = " + ╔══════════════╗ + ║┌────────────┐║ + ║│this is text│║ + ║└────────────┘║ + ║ ║ + ╚══════════════╝ + "; + + let mut runner = TestRunner::new(tpl, (14, 4)); + let mut runner = runner.instance(); + runner.render_assert(expected); + } + + #[test] + fn vstack_spacer_nospace() { + let tpl = " + border + container [height:1] + vstack + spacer + text 'this is text' + "; + + let expected = " + ╔═════════════════╗ + ║┌────────────┐ ║ + ║│this is text│ ║ + ║└────────────┘ ║ + ║ ║ + ╚═════════════════╝ + "; + + let mut runner = TestRunner::new(tpl, (17, 4)); + let mut runner = runner.instance(); + runner.render_assert(expected); + } +} diff --git a/anathema-runtime/src/runtime/mod.rs b/anathema-runtime/src/runtime/mod.rs index 34ddec9c..25a80b2b 100644 --- a/anathema-runtime/src/runtime/mod.rs +++ b/anathema-runtime/src/runtime/mod.rs @@ -3,7 +3,7 @@ use std::time::{Duration, Instant}; use anathema_backend::{Backend, WidgetCycle}; use anathema_geometry::Size; -use anathema_state::{clear_all_changes, clear_all_subs, drain_changes, Changes, StateId, States}; +use anathema_state::{Changes, StateId, States, clear_all_changes, clear_all_subs, drain_changes}; use anathema_store::tree::root_node; use anathema_templates::blueprints::Blueprint; use anathema_templates::{Document, Globals}; @@ -17,8 +17,8 @@ use anathema_widgets::layout::{LayoutCtx, Viewport}; use anathema_widgets::query::Children; use anathema_widgets::tabindex::{Index, TabIndex}; use anathema_widgets::{ - eval_blueprint, update_widget, Component, Components, Factory, FloatingWidgets, GlyphMap, WidgetContainer, - WidgetId, WidgetKind, WidgetTree, + Component, Components, Factory, FloatingWidgets, GlyphMap, WidgetContainer, WidgetId, WidgetKind, WidgetTree, + eval_blueprint, update_widget, }; use flume::Receiver; use notify::RecommendedWatcher; diff --git a/anathema-state/src/value/mod.rs b/anathema-state/src/value/mod.rs index 9c4fbe9a..d90fb7af 100644 --- a/anathema-state/src/value/mod.rs +++ b/anathema-state/src/value/mod.rs @@ -12,12 +12,12 @@ pub use self::list::List; pub use self::map::Map; pub use self::maybe::{Maybe, Nullable}; use crate::states::State; -use crate::store::subscriber::{subscribe, unsubscribe, SubKey}; +use crate::store::subscriber::{SubKey, subscribe, unsubscribe}; use crate::store::values::{ - drop_value, get_unique, make_shared, new_value, return_owned, return_shared, try_make_shared, OwnedValue, + OwnedValue, drop_value, get_unique, make_shared, new_value, return_owned, return_shared, try_make_shared, }; -use crate::store::watchers::{monitor, queue_monitor, Watcher}; -use crate::store::{changed, ValueKey}; +use crate::store::watchers::{Watcher, monitor, queue_monitor}; +use crate::store::{ValueKey, changed}; use crate::{Change, Subscriber}; mod list; diff --git a/anathema-store/src/tree/view.rs b/anathema-store/src/tree/view.rs index e70b8c0d..74eca6b6 100644 --- a/anathema-store/src/tree/view.rs +++ b/anathema-store/src/tree/view.rs @@ -27,12 +27,7 @@ impl<'tree, T> TreeView<'tree, T> { } pub fn view_mut(&mut self) -> TreeView<'_, T> { - TreeView::new( - self.offset, - &mut self.layout, - &mut self.values, - &mut self.removed_values, - ) + TreeView::new(self.offset, self.layout, self.values, self.removed_values) } pub fn get_mut(&mut self, value_id: ValueId) -> Option<&mut T> { diff --git a/anathema-templates/src/components.rs b/anathema-templates/src/components.rs index 8765259c..a61e2dd9 100644 --- a/anathema-templates/src/components.rs +++ b/anathema-templates/src/components.rs @@ -6,6 +6,7 @@ use anathema_store::smallmap::SmallMap; use anathema_store::stack::Stack; use anathema_store::storage::Storage; +use crate::Lexer; use crate::blueprints::Blueprint; use crate::error::{Error, Result}; use crate::statements::eval::Scope; @@ -14,7 +15,6 @@ use crate::statements::{Context, Statements}; use crate::strings::{StringId, Strings}; use crate::token::Tokens; use crate::variables::Variables; -use crate::Lexer; pub trait ToSourceKind { fn to_path(self) -> SourceKind; diff --git a/anathema-templates/src/statements/mod.rs b/anathema-templates/src/statements/mod.rs index 8c06a30e..f500ae8d 100644 --- a/anathema-templates/src/statements/mod.rs +++ b/anathema-templates/src/statements/mod.rs @@ -1,12 +1,12 @@ use anathema_store::smallmap::SmallMap; +use crate::ComponentBlueprintId; use crate::blueprints::Blueprint; use crate::components::{AssocEventMapping, ComponentTemplates}; use crate::error::Result; use crate::expressions::Expression; use crate::strings::{StringId, Strings}; use crate::variables::Variables; -use crate::ComponentBlueprintId; mod const_eval; pub(crate) mod eval;