Skip to content

Commit f4b4d69

Browse files
committed
fix(useInteractorStyle): set style immediately
Do not wait for a react render to set the interactor style. Additionally, do not handle reference counting externally set styles since we do not control their lifecycle.
1 parent 4fcc192 commit f4b4d69

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

src/core/modules/useInteractorStyle.ts

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ import vtkInteractorStyle from '@kitware/vtk.js/Interaction/Style/InteractorStyl
2424
import vtkInteractorStyleManipulator from '@kitware/vtk.js/Interaction/Style/InteractorStyleManipulator';
2525
import vtkRenderWindowInteractor from '@kitware/vtk.js/Rendering/Core/RenderWindowInteractor';
2626
import deepEqual from 'deep-equal';
27-
import { useCallback, useEffect, useState } from 'react';
27+
import { useCallback, useEffect } from 'react';
2828
import deletionRegistry from '../../utils/DeletionRegistry';
2929
import useComparableEffect from '../../utils/useComparableEffect';
3030
import useGetterRef from '../../utils/useGetterRef';
31-
import useUnmount from '../../utils/useUnmount';
3231

3332
interface PanSettings extends IMouseCameraTrackballPanManipulatorInitialValues {
3433
action: 'Pan';
@@ -117,53 +116,43 @@ export function useInteractorStyleManipulatorSettings(
117116
export function useInteractorStyle(
118117
getInteractor: () => vtkRenderWindowInteractor | null
119118
) {
120-
const [externalStyle, setExternalStyle] = useState<vtkInteractorStyle | null>(
121-
null
122-
);
123-
124119
const [styleRef, getStyle] = useGetterRef<vtkInteractorStyle>(() => {
125-
setExternalStyle(null);
126120
const style = vtkInteractorStyleManipulator.newInstance();
127121
deletionRegistry.register(style, () => style.delete());
128122
return style;
129123
});
130124

125+
const setStyle = useCallback(
126+
(style: vtkInteractorStyle) => {
127+
const interactor = getInteractor();
128+
if (!interactor) return;
129+
interactor.setInteractorStyle(style ?? styleRef.current);
130+
},
131+
[getInteractor, styleRef]
132+
);
133+
131134
useEffect(() => {
132135
const interactor = getInteractor();
133136
if (!interactor) return;
134137

135-
const style = externalStyle ?? getStyle();
136-
interactor.setInteractorStyle(style);
137-
138138
deletionRegistry.incRefCount(interactor);
139-
deletionRegistry.incRefCount(style);
139+
if (styleRef.current) {
140+
setStyle(styleRef.current);
141+
}
140142

141143
return () => {
142-
if (interactor.getInteractorStyle() === style) {
143-
interactor.setInteractorStyle(null);
144-
}
145-
deletionRegistry.decRefCount(interactor);
146-
deletionRegistry.incRefCount(style);
147-
};
148-
});
149-
150-
useUnmount(() => {
151-
if (styleRef.current && !externalStyle) {
152-
deletionRegistry.markForDeletion(styleRef.current);
153-
styleRef.current = null;
154-
}
155-
});
144+
if (styleRef.current) {
145+
if (interactor.getInteractorStyle() === styleRef.current) {
146+
interactor.setInteractorStyle(null);
147+
}
156148

157-
const setStyle = useCallback(
158-
(style: vtkInteractorStyle) => {
159-
if (!externalStyle && styleRef.current) {
160149
deletionRegistry.markForDeletion(styleRef.current);
150+
styleRef.current = null;
161151
}
162-
styleRef.current = style;
163-
setExternalStyle(style);
164-
},
165-
[externalStyle, styleRef]
166-
);
152+
153+
deletionRegistry.decRefCount(interactor);
154+
};
155+
}, [getInteractor, styleRef, setStyle]);
167156

168157
return [getStyle, setStyle] as const;
169158
}

0 commit comments

Comments
 (0)