Four LayoutPolicies are provided, two in the Morphic-Layout packages, two in some Polymorph package. They seem to be limited to one-dimension proportional layout, and different flavors of row or column layout.
The LayoutPolicies are invoked on several morph events: change of dimensions or position, adding or removing submorphs, also when content is dropped. They can also be invoked explicitly by sending the message layoutChanged to the morph.
I guess that a LayoutPolicy could be used to perform different dynamic changes on the submorphs of a morph: hide/show submorphs, change colors, change editable/non editable settings, etc..
Fortunately, it seems to be almost very easy to implement a new LayoutPolicy: it suffices to redefine the method layout:in:. The first argument is the morph whose submorphs are to be arranged, and the second some sort of reference box for the morph. You just arrange the submorphs modifying their bounds, position or extent. You can also modify the bounds of the morph itself. I guess that some forms of modifying the morph bounds can imply entering into a layout-computing loop, I found that the method layoutBounds: seems to be safe (a note about bounds in Morphic is to be released shortly).
Other method that one would possibly like to redefine is minExtentOf:in:, whose arguments are the same as layout:in:.It should return the minimal extent needed for the morph to encompass its submorphs, given the way the policy arrange the submorphs. I am still not quite sure about what is this method used for.
Other methods which can be redefined, but I guess most of the times can be ignored, are:
- flushLayoutCache, which is sent in order to let the policy to clear information which should not be mantained between different layout computations.
- indexForInserting:at:, which is used when something is drop onto the morph in a drag/drop operation.
- isTableLayout, which I think is better to respect the default behaviour which is to answer false.
LayoutPolicy subclass: #TrivialTwoSubmorphsPolicy
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Carlos-Sandbox'
layout: aMorph in: newBounds
"Compute the layout for the given morph based on the new bounds"
| submorphs aStream |
submorphs := aMorph submorphs.
"first submorph"
submorphs isEmpty ifTrue: [ ^self ].
submorphs first position: aMorph position + (20@5).
"second submorph"
(submorphs size < 2) ifTrue: [ ^self ].
submorphs second position: aMorph position + (20@35).
"the remaining submorphs"
(submorphs size < 3) ifTrue: [ ^self ].
aStream := ReadStream on: (aMorph submorphs).
aStream next; next.
[ aStream atEnd ] whileFalse: [ aStream next hide ].
minExtentOf: aMorph in: newBounds
"Return the minimal size aMorph's children would require given the new bounds.
It is not required to override this method
in order to obtain a minimal yet working subclass of LayoutPolicy"
^aMorph bounds
Why implementing a new LayoutPolicy is only almost very easy? Stay tuned!
No comments:
Post a Comment