Skip to content

Commit da92948

Browse files
authored
Merge pull request #1081 from fabulous-dev/use-attribute-bundle
Add new `CollectionBuilder` constructor using an `AttributesBundle`
2 parents f592997 + 05cc057 commit da92948

3 files changed

Lines changed: 35 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
_No unreleased changes_
1111

12+
## [3.0.0-pre5] - 2024-05-17
13+
14+
### Added
15+
- Add new `CollectionBuilder` constructor using an `AttributesBundle` by @edgarfgp in https://github.com/fabulous-dev/Fabulous/pull/1081
16+
1217
## [3.0.0-pre4] - 2024-04-19
1318

1419
### Added
@@ -134,7 +139,8 @@ _No unreleased changes_
134139
### Changed
135140
- 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)
136141

137-
[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/3.0.0-pre4...HEAD
142+
[unreleased]: https://github.com/fabulous-dev/Fabulous/compare/3.0.0-pre5...HEAD
143+
[3.0.0-pre5]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre5
138144
[3.0.0-pre4]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre4
139145
[3.0.0-pre3]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre3
140146
[3.0.0-pre2]: https://github.com/fabulous-dev/Fabulous/releases/tag/3.0.0-pre2

src/Fabulous.Tests/APISketchTests/TestUI.Widgets.fs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,6 @@ module TestUI_Widgets =
157157
static member Stack<'msg, 'marker when 'marker :> IMarker>() =
158158
CollectionBuilder<'msg, TestStackMarker, 'marker>(TestStackKey, StackList.empty(), Attributes.Container.Children)
159159

160-
161-
162160
[<Extension>]
163161
type CollectionBuilderExtensions =
164162
[<Extension>]

src/Fabulous/Builders.fs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,36 +125,50 @@ type Content<'msg> = { Widgets: MutStackArray1.T<Widget> }
125125
type CollectionBuilder<'msg, 'marker, 'itemMarker> =
126126
struct
127127
val WidgetKey: WidgetKey
128-
val Scalars: StackList<ScalarAttribute>
129128
val Attr: WidgetCollectionAttributeDefinition
129+
val Attributes: AttributesBundle
130130

131131
new(widgetKey: WidgetKey, scalars: StackList<ScalarAttribute>, attr: WidgetCollectionAttributeDefinition) =
132132
{ WidgetKey = widgetKey
133-
Scalars = scalars
133+
Attributes = AttributesBundle(scalars, ValueNone, ValueNone)
134134
Attr = attr }
135135

136136
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition) =
137137
{ WidgetKey = widgetKey
138-
Scalars = StackList.empty()
138+
Attributes = AttributesBundle(StackList.empty(), ValueNone, ValueNone)
139+
Attr = attr }
140+
141+
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, attributes: AttributesBundle) =
142+
{ WidgetKey = widgetKey
143+
Attributes = attributes
139144
Attr = attr }
140145

141146
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalar: ScalarAttribute) =
142147
{ WidgetKey = widgetKey
143-
Scalars = StackList.one scalar
148+
Attributes = AttributesBundle(StackList.one scalar, ValueNone, ValueNone)
144149
Attr = attr }
145150

146151
new(widgetKey: WidgetKey, attr: WidgetCollectionAttributeDefinition, scalarA: ScalarAttribute, scalarB: ScalarAttribute) =
147152
{ WidgetKey = widgetKey
148-
Scalars = StackList.two(scalarA, scalarB)
153+
Attributes = AttributesBundle(StackList.two(scalarA, scalarB), ValueNone, ValueNone)
149154
Attr = attr }
150155

151156
member inline x.Run(c: Content<'msg>) =
157+
let struct (scalars, widgets, widgetCollections) = x.Attributes
158+
152159
let attrValue =
153160
match MutStackArray1.toArraySlice &c.Widgets with
154161
| ValueNone -> ArraySlice.emptyWithNull()
155162
| ValueSome slice -> slice
156163

157-
WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(x.Scalars, ValueNone, ValueSome [| x.Attr.WithValue(attrValue) |]))
164+
let widgetCollAttr = x.Attr.WithValue(attrValue)
165+
166+
let widgetCollections =
167+
match widgetCollections with
168+
| ValueNone -> ValueSome([| widgetCollAttr |])
169+
| ValueSome widgetCollectionAttributes -> ValueSome(Array.appendOne widgetCollAttr widgetCollectionAttributes)
170+
171+
WidgetBuilder<'msg, 'marker>(x.WidgetKey, AttributesBundle(scalars, widgets, widgetCollections))
158172

159173
member inline _.Combine(a: Content<'msg>, b: Content<'msg>) : Content<'msg> =
160174
let res = MutStackArray1.combineMut(&a.Widgets, b.Widgets)
@@ -177,7 +191,14 @@ type CollectionBuilder<'msg, 'marker, 'itemMarker> =
177191

178192
[<EditorBrowsable(EditorBrowsableState.Never)>]
179193
member inline x.AddScalar(attr: ScalarAttribute) =
180-
CollectionBuilder<'msg, 'marker, 'itemMarker>(x.WidgetKey, StackList.add(&x.Scalars, attr), x.Attr)
194+
let struct (scalarAttributes, widgetAttributes, widgetCollectionAttributes) =
195+
x.Attributes
196+
197+
CollectionBuilder<'msg, 'marker, 'itemMarker>(
198+
x.WidgetKey,
199+
x.Attr,
200+
struct (StackList.add(&scalarAttributes, attr), widgetAttributes, widgetCollectionAttributes)
201+
)
181202

182203
end
183204

0 commit comments

Comments
 (0)