Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text/RichText constructor and setTextProperty take different Property types. #1446

Closed
pixelzoom opened this issue Sep 1, 2022 · 8 comments
Closed

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Sep 1, 2022

The textProperty parameter to Text/RichText constructors differs from setTextProperty:

  public constructor( text: string | number | TReadOnlyProperty<string>, options?: TextOptions ) {
...
  public setTextProperty( newTarget: TProperty<string> | null ): this { 

This makes it impossible to provide a DerivedProperty to Text/RichText via setTextProperty. Shouldn't setTextProperty also take a TReadOnlyProperty<string>?

I have an immediate need for this in molecule-polarity RealMoleculeViewer.ts:

    const moleculeText = new RichText( '', {
      font: FONT,
      maxWidth: 200
    } );

    moleculeProperty.link( molecule => {
      moleculeText.textProperty = new DerivedProperty(
        [ molecule.fullNameProperty, moleculePolarityStrings.pattern.symbolNameStringProperty ],
        ( fullName, patternString ) => StringUtils.fillIn( patternString, {
          symbol: molecule.symbol,
          name: fullName
        } ) )
    } );

The assignment to moleculeText.textProperty results in this tsc error:

TS2322: Type 'DerivedProperty<string, string, string, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>' is not assignable to type 'TProperty'.   Type 'DerivedProperty<string, string, string, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>' is not assignable to type '{ set(value: string): void; value: string; }'.     Property 'set' is protected in type 'DerivedProperty<string, string, string, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown, unknown>' but public in type '{ set(value: string): void; value: string; }'.

I can workaround this in RealMoleculeViewer.ts by creating a new RichText node instead of calling setTextProperty. But that will be trouble when I need to instrument for PhET-iO.

@pixelzoom pixelzoom changed the title Text/RichText constructor and setTextProperty takes different Property types. Text/RichText constructor and setTextProperty take different Property types. Sep 1, 2022
pixelzoom added a commit to phetsims/molecule-polarity that referenced this issue Sep 1, 2022
@jonathanolson
Copy link
Contributor

I was told to follow the pattern with Node.visibleProperty (I didn't invent this pattern!)

moleculeText.mutate( { textProperty: ... } ) was proposed as the way of handling this.

Perhaps we can improve this pattern @zepumph? Thoughts @pixelzoom?

@jonathanolson
Copy link
Contributor

Node's visibleProperty is TReadOnlyProperty in the options, TProperty in the setVisibleProperty() definition.

@pixelzoom
Copy link
Contributor Author

Thoughts @pixelzoom?

It's really weird, and problematic in practice, that the constructor takes a (readonly) TReadOnlyProperty<string>, while setTextProperty takes a (writeable) TProperty | null ). What is it about setTextProperty` that require a writeable Property?

@pixelzoom pixelzoom removed their assignment Sep 1, 2022
@jonathanolson
Copy link
Contributor

What is it about setTextProperty` that require a writeable Property?

My understanding was that we want to be able to access things that TProperty provides from getters, e.g.

text.textProperty.value = would be illegal if it's a TReadOnlyProperty.

That doesn't sound like a good argument to me, and perhaps we can change them all to TReadOnlyProperty?

@zepumph
Copy link
Member

zepumph commented Oct 20, 2022

How about this @jonathanolson? If you agree with it than I can apply it to all Node Properties over in phetsims/axon#391

@zepumph
Copy link
Member

zepumph commented Nov 3, 2022

Just a ping here. Then I can proceed with phetsims/axon#391.

@zepumph
Copy link
Member

zepumph commented Nov 10, 2022

I discussed with @jonathanolson and we like the above commit. @pixelzoom you can now set a DerivedProperty onto Text.stringProperty. Feel free to close!

@zepumph zepumph assigned pixelzoom and unassigned jonathanolson Nov 10, 2022
@pixelzoom
Copy link
Contributor Author

👍🏻 closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants