Deletes the selected widget

When there are many widgets on the page and I want to delete the selected widget, is there any way to get the selected widget?
my code:

const delHandle = () => {
        const selectedIndex = ?;// selected widget
        const widgets = scene.widgetManager.getWidgets();
        if (!widgets.length) return;
        scene.widgetManager.removeWidget(selectedIndex]);       
 };

You can listen on widgetState.onModified(callback) to see if the state is active (widgetState.getActive()).

In my tests, when I click or change the size of the widget, its state is always false.What’s wrong with my code?

widgets.circleWidget = vtkEllipseWidget.newInstance({
            modifierBehavior: {
                None: {
                [BehaviorCategory.PLACEMENT]: ShapeBehavior[BehaviorCategory.PLACEMENT].CLICK_AND_DRAG,
                [BehaviorCategory.POINTS]: ShapeBehavior[BehaviorCategory.POINTS].RADIUS,
                [BehaviorCategory.RATIO]: ShapeBehavior[BehaviorCategory.RATIO].FREE,
                },
            },
 });

widgets.circleWidget.getWidgetState().onModified((widget) => {
      console.log(widget.getActive());
});

Ah, that might be due to a sub-state being active, rather than the root state.

After thinking about this more, I don’t think there is a concept of a “selected widget” in the sense of clicking on a widget to make it active. The vtkWidgetManager only provides information about the current widget selection underneath the mouse cursor. If you want the click-to-persist-selection functionality, I think you will have to listen for widget interaction.

let selectedWidget = null;
widget.onStartInteractionEvent(() => {
 selectedWidget = widget;
 // optionally change UI to reflect the selection
})