diff --git a/CHANGELOG.md b/CHANGELOG.md index ef3c6c0aa..157eaa5d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 _No unreleased changes_ +## [3.0.0-pre5] - 2024-05-17 + +### Added +- Add new `CollectionBuilder` constructor using an `AttributesBundle` by @edgarfgp in https://github.com/fabulous-dev/Fabulous/pull/1081 + ## [3.0.0-pre4] - 2024-04-19 ### Added @@ -134,7 +139,8 @@ _No unreleased changes_ ### Changed - Fabulous.XamarinForms & Fabulous.MauiControls have been moved been out of the Fabulous repository. Find them in their own repositories: [https://github.com/fabulous-dev/Fabulous.XamarinForms](https://github.com/fabulous-dev/Fabulous.XamarinForms) / [https://github.com/fabulous-dev/Fabulous.MauiControls](https://github.com/fabulous-dev/Fabulous.MauiControls) -[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/3.0.0-pre4...HEAD +[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/3.0.0-pre5...HEAD +[3.0.0-pre5]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre5 [3.0.0-pre4]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre4 [3.0.0-pre3]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre3 [3.0.0-pre2]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre2 diff --git a/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs b/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs index 71543f877..be031185c 100644 --- a/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs +++ b/src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs @@ -157,8 +157,6 @@ module TestUI_Widgets = static member Stack<'msg, 'marker when 'marker :> IMarker>() = CollectionBuilder<'msg, TestStackMarker, 'marker>(TestStackKey, StackList.empty(), Attributes.Container.Children) - - [] type CollectionBuilderExtensions = [] diff --git a/src/Fabulous/Builders.fs b/src/Fabulous/Builders.fs index e9c883252..68df45e17 100644 --- a/src/Fabulous/Builders.fs +++ b/src/Fabulous/Builders.fs @@ -125,36 +125,50 @@ type Content<'msg> = { Widgets: MutStackArray1.T } type CollectionBuilder<'msg, 'marker, 'itemMarker> = struct val WidgetKey: WidgetKey - val Scalars: StackList val Attr: WidgetCollectionAttributeDefinition + val Attributes: AttributesBundle new(widgetKey: WidgetKey, scalars: StackList, attr: WidgetCollectionAttributeDefinition) = { WidgetKey = widgetKey - Scalars = scalars + Attributes = AttributesBundle(scalars, ValueNone, ValueNone) Attr = attr } new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition) = { WidgetKey = widgetKey - Scalars = StackList.empty() + Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone) + Attr = attr } + + new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, attributes: AttributesBundle) = + { WidgetKey = widgetKey + Attributes = attributes Attr = attr } new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalar: ScalarAttribute) = { WidgetKey = widgetKey - Scalars = StackList.one scalar + Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone) Attr = attr } new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalarA: ScalarAttribute, scalarB: ScalarAttribute) = { WidgetKey = widgetKey - Scalars = StackList.two(scalarA, scalarB) + Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone) Attr = attr } member inline x.Run(c: Content<'msg>) = + let struct (scalars, widgets, widgetCollections) = x.Attributes + let attrValue = match MutStackArray1.toArraySlice &c.Widgets with | ValueNone -> ArraySlice.emptyWithNull() | ValueSome slice -> slice - WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(x.Scalars, ValueNone, ValueSome [| x.Attr.WithValue(attrValue) |])) + let widgetCollAttr = x.Attr.WithValue(attrValue) + + let widgetCollections = + match widgetCollections with + | ValueNone -> ValueSome([| widgetCollAttr |]) + | ValueSome widgetCollectionAttributes -> ValueSome(Array.appendOne widgetCollAttr widgetCollectionAttributes) + + WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(scalars, widgets, widgetCollections)) member inline _.Combine(a: Content<'msg>, b: Content<'msg>) : Content<'msg> = let res = MutStackArray1.combineMut(&a.Widgets, b.Widgets) @@ -177,7 +191,14 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker> = [] member inline x.AddScalar(attr: ScalarAttribute) = - CollectionBuilder<'msg, 'marker, 'itemMarker>(x.WidgetKey, StackList.add(&x.Scalars, attr), x.Attr) + let struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes) = + x.Attributes + + CollectionBuilder<'msg, 'marker, 'itemMarker>( + x.WidgetKey, + x.Attr, + struct (StackList.add(&scalarAttributes, attr), widgetAttributes, widgetCollectionAttributes) + ) end