简体   繁体   English

我将如何使用 TableLayout 以不同的方式处理某些特定的子变形?

[英]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.这个变形被设计用来容纳一排子变形,我使用了一个 TableLayout 布局策略,它工作得很好。

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?在使用 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 ?对我来说,第一个明显的问题是:为什么你希望purple成为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.一般来说,在 Morphic 中广泛使用组合是一种普遍的做法。

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解决方案 1:组合

If you use composition, you can arrange both main and purple in another table-layouted transparent container:如果您使用组合,您可以在另一个表格布局的透明容器中安排mainpurple

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解决方案 2:从其所有者布局策略中排除单个变形

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:这可以通过在相关子变形上启用#disableLayout属性来完成:

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.虽然 Morphic 通常支持这一点(参见#fullBounds ),但这通常是出乎意料的,并且可能会使某些检查和调试任务变得更加困难。 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".例如,要在此示例中调用紫色变形上的光环,您需要按Shift + Blue或打开首选项“光环包围完整边界”。 Note that since Squeak 6.0, you can also press Ctrl + Blue to override this preference once.请注意,从 Squeak 6.0 开始,您还可以按Ctrl + Blue一次覆盖此首选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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