Extending widgetstate with new fields

I’m currently extending the existing image cropping widget with some extra additions of my own. My question is now how I would go by extending the existing state with an extra field.
Normally I would go by making a StateGenerator() which creates a new state using the vtkStateBuilder.

But I now can’t overwrite the existing state, but I would like to add a field to the existing model.widgetState of the vtkImageCroppingWidget.
I tried using
const newState = vtkStateBuilder .createBuilder() .addField({ name: 'slices', initialValue: [0, 0, 0] }) .build(); model.widgetState.bindState(newState, 'extendedState');

But for some reason this just creates multiple substates which are never destroyed even after the widget is destroyed? And also do not contain the fields from the existing imageCroppingWidget state.

Is there a better/easier way to add certain fields to the existing state? Or am I missing something obvious?

Kind regards,
Emil

IIRC there is no way to extend an existing state object with new fields once you’ve instantiated it. However, you can choose to extend vtkImageCroppingWidget and create your own model.widgetState by copying the one used by ImageCroppingWidget and using addField.

I’m already extending the vtkImageCroppingWidget, but how would you go about copying the existing state into my own model.widgetState such that all fields that the base vtkImageCroppingWidget would need are still there?

You can copy this state file, edit it to fit your needs, import it and set it on model.widgetState. Unfortunately the final constructed state is exported from the state.js file, rather than the builder object. Maybe it’s a good idea to export just the builder object so people can extend it further if they need to…