Skip to content

Commit

Permalink
Merge pull request #1081 from fabulous-dev/use-attribute-bundle
Browse files Browse the repository at this point in the history
Add new `CollectionBuilder` constructor using an `AttributesBundle`
  • Loading branch information
Edgar Gonzalez committed May 17, 2024
2 parents f592997 + 05cc057 commit da92948
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)



[<Extension>]
type CollectionBuilderExtensions =
[<Extension>]
Expand Down
35 changes: 28 additions & 7 deletions src/Fabulous/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,50 @@ type Content<'msg> = { Widgets: MutStackArray1.T<Widget> }
type CollectionBuilder<'msg, 'marker, 'itemMarker> =
struct
val WidgetKey: WidgetKey
val Scalars: StackList<ScalarAttribute>
val Attr: WidgetCollectionAttributeDefinition
val Attributes: AttributesBundle

new(widgetKey: WidgetKey, scalars: StackList<ScalarAttribute>, 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)
Expand All @@ -177,7 +191,14 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker> =

[<EditorBrowsable(EditorBrowsableState.Never)>]
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