From d5b92aa5f03b51dc64fc982a7bf357efc6731907 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Sun, 20 Oct 2024 22:38:08 -0400 Subject: [PATCH 1/2] Add types and methods for intrinsic sizing protocol --- crates/yakui-core/src/layout/mod.rs | 15 +++++++++- crates/yakui-core/src/types.rs | 18 ++++++++++++ crates/yakui-core/src/widget.rs | 45 ++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/crates/yakui-core/src/layout/mod.rs b/crates/yakui-core/src/layout/mod.rs index 30e4df14..da33a876 100644 --- a/crates/yakui-core/src/layout/mod.rs +++ b/crates/yakui-core/src/layout/mod.rs @@ -10,7 +10,8 @@ use crate::event::EventInterest; use crate::geometry::{Constraints, Rect}; use crate::id::WidgetId; use crate::input::{InputState, MouseInterest}; -use crate::widget::LayoutContext; +use crate::types::Axis; +use crate::widget::{IntrinsicSizeContext, LayoutContext}; /// Contains information on how each widget in the DOM is laid out and what /// events they're interested in. @@ -199,6 +200,18 @@ impl LayoutDom { size } + /// Calculates the intrinsic size of the given widget along the given axis. + pub fn intrinsic_size(&self, dom: &Dom, id: WidgetId, axis: Axis) -> f32 { + dom.enter(id); + let dom_node = dom.get(id).unwrap(); + + let context = IntrinsicSizeContext { dom, layout: self }; + let size = dom_node.widget.intrinsic_size(context, axis); + + dom.exit(id); + size + } + /// Enables clipping for the currently active widget. pub fn enable_clipping(&mut self, dom: &Dom) { self.clip_stack.push(dom.current()); diff --git a/crates/yakui-core/src/types.rs b/crates/yakui-core/src/types.rs index c6cd9591..eb309674 100644 --- a/crates/yakui-core/src/types.rs +++ b/crates/yakui-core/src/types.rs @@ -256,3 +256,21 @@ impl Pivot { pub const BOTTOM_CENTER: Self = Self::new(0.5, 1.0); pub const BOTTOM_RIGHT: Self = Self::new(1.0, 1.0); } + +/// Defines an axis usable by the UI. +#[allow(missing_docs)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Axis { + X, + Y, +} + +impl Axis { + /// Return the component of a [`Vec2`] that matches this axis. + pub fn select(self, v: Vec2) -> f32 { + match self { + Axis::X => v.x, + Axis::Y => v.y, + } + } +} diff --git a/crates/yakui-core/src/widget.rs b/crates/yakui-core/src/widget.rs index a601e692..b77bb6c0 100644 --- a/crates/yakui-core/src/widget.rs +++ b/crates/yakui-core/src/widget.rs @@ -9,10 +9,11 @@ use crate::dom::Dom; use crate::event::EventResponse; use crate::event::{EventInterest, WidgetEvent}; use crate::geometry::{Constraints, FlexFit}; +use crate::id::WidgetId; use crate::input::{InputState, NavDirection}; use crate::layout::LayoutDom; use crate::paint::PaintDom; -use crate::{Flow, WidgetId}; +use crate::types::{Axis, Flow}; /// Trait that's automatically implemented for all widget props. /// @@ -38,6 +39,26 @@ impl<'dom> LayoutContext<'dom> { self.layout .calculate(self.dom, self.input, widget, constraints) } + + /// Calculates the intrinsic size for the given widget on the given axis. + pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis) -> f32 { + self.layout.intrinsic_size(self.dom, widget, axis) + } +} + +/// Information available to a widget during the layout phase. +#[non_exhaustive] +#[allow(missing_docs)] +pub struct IntrinsicSizeContext<'dom> { + pub dom: &'dom Dom, + pub layout: &'dom LayoutDom, +} + +impl<'dom> IntrinsicSizeContext<'dom> { + /// Calculates the intrinsic size for the given widget on the given axis. + pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis) -> f32 { + self.layout.intrinsic_size(self.dom, widget, axis) + } } /// Information available to a widget during the paint phase. @@ -134,6 +155,21 @@ pub trait Widget: 'static + fmt::Debug { constraints.constrain_min(size) } + /// Tells the intrinsic size on one axis of the object, which is its size + /// along that axis if the widget were given unbounded constraints on the + /// other axis. + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32 { + let node = ctx.dom.get_current(); + let mut size: f32 = 0.0; + + for &child in &node.children { + let child_size = ctx.intrinsic_size(child, axis); + size = size.max(child_size); + } + + size + } + /// Paint the widget based on its current state. /// /// The default implementation will paint all of the widget's children. @@ -180,6 +216,9 @@ pub trait ErasedWidget: Any + fmt::Debug { /// See [`Widget::layout`]. fn layout(&self, ctx: LayoutContext<'_>, constraints: Constraints) -> Vec2; + /// See [`Widget::intrinsic_size`]. + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32; + /// See [`Widget::flex`]. fn flex(&self) -> (u32, FlexFit); @@ -207,6 +246,10 @@ where ::layout(self, ctx, constraints) } + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32 { + ::intrinsic_size(self, ctx, axis) + } + fn flex(&self) -> (u32, FlexFit) { ::flex(self) } From 5c21a4dd959a4afd8637015555f3feee5b43bf07 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Mon, 21 Oct 2024 00:28:39 -0400 Subject: [PATCH 2/2] Add cross-axis extent, stub implementations for ColoredBox and List --- crates/yakui-core/src/layout/mod.rs | 4 +- crates/yakui-core/src/types.rs | 11 +++++ crates/yakui-core/src/widget.rs | 22 +++++----- .../yakui-widgets/src/widgets/colored_box.rs | 15 ++++++- crates/yakui-widgets/src/widgets/list.rs | 41 ++++++++++++++++++- 5 files changed, 76 insertions(+), 17 deletions(-) diff --git a/crates/yakui-core/src/layout/mod.rs b/crates/yakui-core/src/layout/mod.rs index da33a876..caa19863 100644 --- a/crates/yakui-core/src/layout/mod.rs +++ b/crates/yakui-core/src/layout/mod.rs @@ -201,12 +201,12 @@ impl LayoutDom { } /// Calculates the intrinsic size of the given widget along the given axis. - pub fn intrinsic_size(&self, dom: &Dom, id: WidgetId, axis: Axis) -> f32 { + pub fn intrinsic_size(&self, dom: &Dom, id: WidgetId, axis: Axis, extent: f32) -> f32 { dom.enter(id); let dom_node = dom.get(id).unwrap(); let context = IntrinsicSizeContext { dom, layout: self }; - let size = dom_node.widget.intrinsic_size(context, axis); + let size = dom_node.widget.intrinsic_size(context, axis, extent); dom.exit(id); size diff --git a/crates/yakui-core/src/types.rs b/crates/yakui-core/src/types.rs index eb309674..1bfda033 100644 --- a/crates/yakui-core/src/types.rs +++ b/crates/yakui-core/src/types.rs @@ -273,4 +273,15 @@ impl Axis { Axis::Y => v.y, } } + + /// Tells whether this axis indicates the same axis as a given [`Direction`]. + pub fn is_direction(self, dir: Direction) -> bool { + match (self, dir) { + (Axis::X, Direction::Right) => true, + (Axis::Y, Direction::Down) => true, + + (Axis::X, Direction::Down) => false, + (Axis::Y, Direction::Right) => false, + } + } } diff --git a/crates/yakui-core/src/widget.rs b/crates/yakui-core/src/widget.rs index b77bb6c0..6da40d73 100644 --- a/crates/yakui-core/src/widget.rs +++ b/crates/yakui-core/src/widget.rs @@ -41,8 +41,8 @@ impl<'dom> LayoutContext<'dom> { } /// Calculates the intrinsic size for the given widget on the given axis. - pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis) -> f32 { - self.layout.intrinsic_size(self.dom, widget, axis) + pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis, extent: f32) -> f32 { + self.layout.intrinsic_size(self.dom, widget, axis, extent) } } @@ -56,8 +56,8 @@ pub struct IntrinsicSizeContext<'dom> { impl<'dom> IntrinsicSizeContext<'dom> { /// Calculates the intrinsic size for the given widget on the given axis. - pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis) -> f32 { - self.layout.intrinsic_size(self.dom, widget, axis) + pub fn intrinsic_size(&self, widget: WidgetId, axis: Axis, extent: f32) -> f32 { + self.layout.intrinsic_size(self.dom, widget, axis, extent) } } @@ -156,14 +156,14 @@ pub trait Widget: 'static + fmt::Debug { } /// Tells the intrinsic size on one axis of the object, which is its size - /// along that axis if the widget were given unbounded constraints on the - /// other axis. - fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32 { + /// along that axis if the widget is provided the given `extent` as the max + /// size along the other axis. + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis, extent: f32) -> f32 { let node = ctx.dom.get_current(); let mut size: f32 = 0.0; for &child in &node.children { - let child_size = ctx.intrinsic_size(child, axis); + let child_size = ctx.intrinsic_size(child, axis, extent); size = size.max(child_size); } @@ -217,7 +217,7 @@ pub trait ErasedWidget: Any + fmt::Debug { fn layout(&self, ctx: LayoutContext<'_>, constraints: Constraints) -> Vec2; /// See [`Widget::intrinsic_size`]. - fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32; + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis, extent: f32) -> f32; /// See [`Widget::flex`]. fn flex(&self) -> (u32, FlexFit); @@ -246,8 +246,8 @@ where ::layout(self, ctx, constraints) } - fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis) -> f32 { - ::intrinsic_size(self, ctx, axis) + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis, extent: f32) -> f32 { + ::intrinsic_size(self, ctx, axis, extent) } fn flex(&self) -> (u32, FlexFit) { diff --git a/crates/yakui-widgets/src/widgets/colored_box.rs b/crates/yakui-widgets/src/widgets/colored_box.rs index efd65cee..69ff423b 100644 --- a/crates/yakui-widgets/src/widgets/colored_box.rs +++ b/crates/yakui-widgets/src/widgets/colored_box.rs @@ -1,7 +1,7 @@ use yakui_core::geometry::{Color, Constraints, Vec2}; use yakui_core::paint::PaintRect; -use yakui_core::widget::{LayoutContext, PaintContext, Widget}; -use yakui_core::Response; +use yakui_core::widget::{IntrinsicSizeContext, LayoutContext, PaintContext, Widget}; +use yakui_core::{Axis, Response}; use crate::util::{widget, widget_children}; @@ -82,6 +82,17 @@ impl Widget for ColoredBoxWidget { input.constrain_min(size) } + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis, extent: f32) -> f32 { + let node = ctx.dom.get_current(); + let mut size = axis.select(self.props.min_size); + + for &child in &node.children { + size = size.max(ctx.intrinsic_size(child, axis, extent)); + } + + size + } + fn paint(&self, mut ctx: PaintContext<'_>) { let node = ctx.dom.get_current(); let layout_node = ctx.layout.get(ctx.dom.current()).unwrap(); diff --git a/crates/yakui-widgets/src/widgets/list.rs b/crates/yakui-widgets/src/widgets/list.rs index 8cb9b935..7181e6e3 100644 --- a/crates/yakui-widgets/src/widgets/list.rs +++ b/crates/yakui-widgets/src/widgets/list.rs @@ -1,6 +1,8 @@ use yakui_core::geometry::{Constraints, FlexFit, Vec2}; -use yakui_core::widget::{LayoutContext, Widget}; -use yakui_core::{CrossAxisAlignment, Direction, Flow, MainAxisAlignment, MainAxisSize, Response}; +use yakui_core::widget::{IntrinsicSizeContext, LayoutContext, Widget}; +use yakui_core::{ + Axis, CrossAxisAlignment, Direction, Flow, MainAxisAlignment, MainAxisSize, Response, +}; use crate::util::widget_children; @@ -285,4 +287,39 @@ impl Widget for ListWidget { container_size } + + fn intrinsic_size(&self, ctx: IntrinsicSizeContext<'_>, axis: Axis, extent: f32) -> f32 { + let node = ctx.dom.get_current(); + + if axis.is_direction(self.props.direction) { + // main axis layout + let mut total_flex = 0.0; + let mut inflexible_space = 0.0; + + for &child_id in &node.children { + let child_size = ctx.intrinsic_size(child_id, axis, extent); + let child = ctx.dom.get(child_id).unwrap(); + + let (flex, _) = child.widget.flex(); + + if flex > 0 { + // TODO + } else { + inflexible_space += child_size; + } + } + + inflexible_space + } else { + // cross axis layout + let mut max_size: f32 = 0.0; + + for &child_id in &node.children { + let child_size = ctx.intrinsic_size(child_id, axis, extent); + max_size = max_size.max(child_size); + } + + max_size + } + } }