From ecc08f3f7324e50efcc63617bafcb8cf382efc57 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 22 Nov 2023 06:21:44 -0800 Subject: [PATCH] Stack gutters honor IntrinsicPadding --- examples/focus-order.rs | 5 ++--- examples/login.rs | 5 ++--- src/widgets/stack.rs | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/examples/focus-order.rs b/examples/focus-order.rs index ec9362b..0f726f0 100644 --- a/examples/focus-order.rs +++ b/examples/focus-order.rs @@ -67,9 +67,8 @@ fn main() -> gooey::Result { .into_columns(); username_row - .pad() - .and(password_row.pad()) - .and(buttons.pad()) + .and(password_row) + .and(buttons) .into_rows() .contain() .width(Lp::points(300)..Lp::points(600)) diff --git a/examples/login.rs b/examples/login.rs index 5ca2f9a..93e95ea 100644 --- a/examples/login.rs +++ b/examples/login.rs @@ -46,9 +46,8 @@ fn main() -> gooey::Result { .into_columns(); username_field - .pad() - .and(password_field.pad()) - .and(buttons.pad()) + .and(password_field) + .and(buttons) .into_rows() .contain() .width(Lp::inches(3)..Lp::inches(6)) diff --git a/src/widgets/stack.rs b/src/widgets/stack.rs index 7f5a805..5094440 100644 --- a/src/widgets/stack.rs +++ b/src/widgets/stack.rs @@ -4,12 +4,14 @@ use std::ops::{Bound, Deref}; use alot::{LotId, OrderedLots}; +use intentional::Cast; use kludgine::figures::units::{Lp, UPx}; use kludgine::figures::{ Fraction, IntoSigned, IntoUnsigned, Point, Rect, Round, ScreenScale, Size, }; use crate::context::{AsEventContext, EventContext, GraphicsContext, LayoutContext}; +use crate::styles::components::IntrinsicPadding; use crate::styles::Dimension; use crate::value::{Generation, IntoValue, Value}; use crate::widget::{Children, ManagedWidget, Widget, WidgetRef}; @@ -157,6 +159,7 @@ impl Widget for Stack { let content_size = self.layout.update( available_space, + context.get(&IntrinsicPadding).into_upx(context.gfx.scale()), context.gfx.scale(), |child_index, constraints, persist| { let mut context = context.for_other(&self.synced_children[child_index]); @@ -400,13 +403,15 @@ impl Layout { pub fn update( &mut self, available: Size, + gutter: UPx, scale: Fraction, mut measure: impl FnMut(usize, Size, bool) -> Size, ) -> Size { let (space_constraint, other_constraint) = self.orientation.split_size(available); let available_space = space_constraint.max(); + let gutter_space = gutter.saturating_mul(UPx::new((self.children.len() - 1).cast::())); let allocated_space = - self.allocated_space.0 + self.allocated_space.1.into_upx(scale).ceil(); + self.allocated_space.0 + self.allocated_space.1.into_upx(scale).ceil() + gutter_space; let mut remaining = available_space.saturating_sub(allocated_space); // If our `other_constraint` is not known, we will need to give child // widgets an opportunity to lay themselves out in the full area. This @@ -490,7 +495,7 @@ impl Layout { let mut offset = UPx::ZERO; for index in 0..self.children.len() { self.layouts[index].offset = offset; - offset += self.layouts[index].size; + offset += self.layouts[index].size + gutter; if needs_final_layout { self.orientation.split_size(measure( index, @@ -576,8 +581,11 @@ mod tests { flex.push(child.dimension, Fraction::ONE); } - let computed_size = - flex.update(available, Fraction::ONE, |index, constraints, _persist| { + let computed_size = flex.update( + available, + UPx::ZERO, + Fraction::ONE, + |index, constraints, _persist| { let (measured_constraint, _other_constraint) = orientation.split_size(constraints); let child = &children[index]; let maximum_measured = measured_constraint.max(); @@ -594,7 +602,8 @@ mod tests { _ => (child.size, child.other), }; orientation.make_size(measured, other) - }); + }, + ); assert_eq!(computed_size, expected_size); let mut offset = UPx::ZERO; for ((index, &child), &expected) in flex.iter().enumerate().zip(expected) {