Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new CollectionBuilder constructor using an AttributesBundle #1081

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ module TestUI_Widgets =
)

static member Stack<'msg, 'marker when 'marker :> IMarker>() =
CollectionBuilder<'msg, TestStackMarker, 'marker>(TestStackKey, StackList.empty(), Attributes.Container.Children)


CollectionBuilder<'msg, TestStackMarker, 'marker>(
TestStackKey,
Attributes.Container.Children,
AttributesBundle(StackList.empty(), ValueNone, ValueNone)
)

[<Extension>]
type CollectionBuilderExtensions =
Expand Down
30 changes: 23 additions & 7 deletions src/Fabulous/Builders.fs
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,45 @@ type Content<'msg> = { Widgets: MutStackArray1.T<Widget> }
type CollectionBuilder<'msg, 'marker, 'itemMarker> =
struct
val WidgetKey: WidgetKey
val Scalars: StackList<ScalarAttribute>
val Attr: WidgetCollectionAttributeDefinition
val Scalars: AttributesBundle
edgarfgp marked this conversation as resolved.
Show resolved Hide resolved

new(widgetKey: WidgetKey, scalars: StackList<ScalarAttribute>, attr: WidgetCollectionAttributeDefinition) =
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalars: AttributesBundle) =
{ WidgetKey = widgetKey
Scalars = scalars
Attr = attr }
edgarfgp marked this conversation as resolved.
Show resolved Hide resolved

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition) =
{ WidgetKey = widgetKey
Scalars = StackList.empty()
Scalars = AttributesBundle(StackList.empty(), ValueNone, ValueNone)
Attr = attr }

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalar: ScalarAttribute) =
{ WidgetKey = widgetKey
Scalars = StackList.one scalar
Scalars = AttributesBundle(StackList.one scalar, ValueNone, ValueNone)
Attr = attr }

new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
{ WidgetKey = widgetKey
Scalars = StackList.two(scalarA, scalarB)
Scalars = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone)
Attr = attr }

member inline x.Run(c: Content<'msg>) =
let struct (scalars, widgets, widgetCollections) = x.Scalars

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 +186,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.Scalars

CollectionBuilder<'msg, 'marker, 'itemMarker>(
x.WidgetKey,
x.Attr,
struct (StackList.add(&scalarAttributes, attr), widgetAttributes, widgetCollectionAttributes)
)

end

Expand Down
Loading