简体   繁体   中英

How would I handle certain, specific submorphs differently using TableLayout?

I have a morph. This morph is designed to hold a row of submorphs, which I use a TableLayout layout policy for and it works well.

However, I want this morph to have a specific submorph that is always positioned directly below the morph. This specific submorph is determined by an instance variable of the morph.

In this example, the main morph is cyan, and the submorphs are red, green and blue. The purple morph is currently not a submorph of the main morph, but it demonstrates where I want it to be as a submorph.

Let's say that the main morph has the purple morph in one of its instance variables, and the purple morph is also a submorph of the main morph. How would I treat that submorph differently when organizing the morph's contents using TableLayout?

I haven't tried much of anything yet. This is partially because I wouldn't know how to try, and partially because this seems like a common enough use case that there might be a solution better than anything I could come up with.

To me, the first obvious question would be: Why do you want purple to be a submorph of main ? Grouping morphs based on common properties usually helps organizing and layouting them. In general, it's a widespread practice in Morphic to use composition extensively.

Preliminary note: As you did not provide a code sample, I will work with the following baseline script:

main := Morph new
    color: Color cyan;
    borderWidth: 2;
    borderColor: Color black.
main
    changeTableLayout;
    listDirection: #leftToRight;
    hResizing: #shrinkWrap;
    vResizing: #shrinkWrap;
    cellGap: 5.
main addAllMorphs:
    {Morph new color: Color red; borderWidth: 2; borderColor: Color black.
    Morph new color: Color green; borderWidth: 2; borderColor: Color black.
    Morph new color: Color blue; borderWidth: 2; borderColor: Color black}.
purple := Morph new color: Color magenta.

main openInWorld.

purple openInWorld.
purple topLeft: main bottomLeft.

原来的情况

Solution 1: Composition

If you use composition, you can arrange both main and purple in another table-layouted transparent container:

outer := Morph new
    changeTableLayout;
    beTransparent;
    hResizing: #shrinkWrap;
    vResizing: #shrinkWrap;
    cellPositioning: #topLeft;
    yourself.
outer addAllMorphs: {main. purple}.
outer openInWorld.

解决方案 1:组合

Solution 2: Exclude single morph from its owner layout policy

Just for sake of completion, it is also possible to exclude a single morph from its owner layout policy as you requested. This can be done by enabling the #disableLayout property on the relevant submorph:

purple disableLayout: true.
main vResizing: #rigid; height: 44.
main addMorph: purple.
purple topLeft: main bottomLeft.

解决方案 2:从其所有者布局策略中排除单个变形

However, I would not consider this solution idiomatic in most situations:

  1. You are foregoing composition (as described above) which increases the overall layout complexity.
  2. You are deliberately positioning a morph outside of the bounds of this owner. While Morphic generally supports this (see #fullBounds ), this is often unexpected and may make certain inspection and debugging tasks harder. For instance, to invoke a halo on the purple morph in this example, you need to either press Shift + Blue , or turn on the preference "Halo encloses full bounds". Note that since Squeak 6.0, you can also press Ctrl + Blue to override this preference once.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM