phetsims / sun

User-interface components for PhET simulations, built on top of Scenery.
http://scenerystack.org/
MIT License
4 stars 12 forks source link

Components with "content" should refrain from Decorator Pattern #860

Open marlitas opened 11 months ago

marlitas commented 11 months ago

Components that handle the layout of their children such as: ButtonNode, AccordionBox, Panel, Carousel, etc should not use the decorator pattern. Adding children to these components outside of the "content" or "contentNode" seems like an anti-pattern. We would like to discourage these Node subclasses from adding children through options or mutation.

Affected ButtonNode Subclasses:

Affected AccordionBox Subclasses:

Affected Panel Subclasses/Usages:

marlitas commented 11 months ago

Dev Meeting 12/7/23

JO: Any objections to refactoring to remove these usages?

pixelzoom commented 11 months ago

I'll be happy to address refactoring any in the sims that I'm responsible for. I see 2 so far in the list above, which I'll take care of now.

pixelzoom commented 11 months ago

Some other common-code UI components that have "content" (not a complete list) ...

marlitas commented 11 months ago

Thanks @pixelzoom! I'll keep going through components to make a comprehensive list. I will plan on refactoring the common code components that need this work done.

pixelzoom commented 11 months ago

In case it's useful, I added this at the end of relevant constructors to identify culprits:

    this.childrenChangedEmitter.addListener( () => console.log( new Error( 'children changed' ).stack ) );
marlitas commented 11 months ago

So far none of these components have raised any errors:

I'll continue checking components that use the "content" pattern, especially those that extend sizable.

marlitas commented 11 months ago

Tested components:

marlitas commented 3 months ago

This would be good to put on the radar again. I would love some help as I don't think I have the bandwidth to complete myself. I'll add it to the dev board to discuss in the next meeting and see if I can wrangle a co-conspirator.

jessegreenberg commented 3 months ago

Recommendation from developer meeting:

I will take a look at the common code components this iteration, and also review the assertions that were proposed here.

It is possible that some of these have already been fixed.

pixelzoom commented 3 months ago

I will take a look at the common code components this iteration, and also review the assertions that were proposed here.

It is possible that some of these have already been fixed.

From https://github.com/phetsims/sun/issues/860#issue-2031266798, the common-code components are:

  • [ ] RectangularRadioButton (sun)
  • [ ] LevelSelectionButton (vegas)

LevelSelectionButton is already fixed. @marlitas said the problem was fixed in https://github.com/phetsims/vegas/commit/f605de47e2d8c5ede7d12b668d637865cda80e5f. And I recently (7/31/24) added support for dynamic layout.

I don't know about RectangularRadioButton.

jessegreenberg commented 2 months ago

Here is what I have done so far -

While working on this, it was pointed out that some components should allow having children added to them (like Slider and RichText), because the implementation of their layout does not make assumptions about children.

However, @jonathanolson suggested that all common code components should discourage adding children to them because a) it is difficult to review the implementation of each and decide whether assumptions are made and b) adding children to UI components makes usages less maintainable.

We need to check in at developer meeting to decide next steps.

jonathanolson commented 2 months ago

Dev meeting decision:

@jessegreenberg thoughts on what would be good to do now?

marlitas commented 2 months ago

@jessegreenberg I will have some time again to finish this off. Would you like to meet and discuss next steps?