When a class defines methods with the pragma magritteDescription
as exemplified in How to define a Magritte form field, the message Object>>#asGtMagritteViewModel
asGtMagritteViewModel
^ GtMagritteViewModel forObject: self
will produce a view model that can then be converted into a BlElement
Object subclass: #BlElement
uses: TBlTransformable + TBlEventTarget + TBlDebug
instanceVariableNames: 'spaceReference parent children bounds measuredBounds boundsCache eventDispatcher constraints layout transformation taskQueue errorHandler userData visuals flags'
classVariableNames: ''
package: 'Bloc-Basic'
using GtMagritteViewModel>>#asElement
asElement
| aFrame |
aFrame := BrFrame new.
aFrame
hFitContentLimited;
vFitContent;
withAsyncFutureDo: [ :anElementFuture |
anElementFuture
whenPending: [ :theContainer |
theContainer
addChild: (BrLabel new
fitContent;
aptitude: (BrGlamorousLabelAptitude new
foreground: BrGlamorousColors disabledButtonTextColor);
text: 'Preparing...');
constraintsDo: [ :c |
c frame horizontal alignCenter.
c frame vertical alignCenter ] ];
whenError: [ :theContainer :anError |
theContainer userData at: self class gtMementoSymbol put: anError.
theContainer
removeChildren;
addChild: (anError asDebuggableElement
hFitContentLimited;
vFitContent);
constraintsDo: [ :c |
c frame horizontal alignCenter.
c frame vertical alignCenter ] ];
whenSuccess: [ :theFrame :aMemento |
| aStencil aContainer |
theFrame userData at: self class gtMementoSymbol put: aMemento.
theFrame removeChildren.
aStencil := aMemento magritteDescription blocElementContainerStencil
asStencilBuilder: GtMagritteFormStencilBuilder.
aContainer := aStencil
memento: aMemento;
asElement.
theFrame addAptitude: (BrLayoutResizerAptitude new inherit: aContainer).
theFrame addChild: aContainer.
GtMagritteElementBuilder new
form: theFrame;
memento: aMemento;
addButtons: self actions;
build.
self onElementCreated cull: theFrame ] ].
aFrame asyncFuture
executionConfiguration: GtMagritteBuilderUtility executionConfigutation.
aFrame asyncFuture future: self mementoFuture.
^ aFrame
.
Let’s use this example once again on GtClassCreationForm
GtBehaviorCreationForm subclass: #GtClassCreationForm
instanceVariableNames: 'superclassName classVars pools'
classVariableNames: ''
package: 'GToolkit-Pharo-Coder-UI-Forms - Models'
.
Putting all that we’ve learned so far together, a form for adding a class can be produced through calling:
GtClassCreationForm new asGtMagritteViewModel asElement
This can be used within elements and stencils to cohabitate models and their respective UIs.