@@ -4,7 +4,10 @@ import { WIDGET_PRIORITY } from '@kitware/vtk.js/Widgets/Core/AbstractWidget/Con
44import { useToolSelectionStore } from ' @/src/store/tools/toolSelection' ;
55import { useToolStore } from ' @/src/store/tools' ;
66import { Tools } from ' @/src/store/tools/types' ;
7- import { vtkAnnotationToolWidget } from ' @/src/vtk/ToolWidgetUtils/types' ;
7+ import type {
8+ vtkAnnotationToolWidget ,
9+ vtkAnnotationWidgetState ,
10+ } from ' @/src/vtk/ToolWidgetUtils/types' ;
811import { inject } from ' vue' ;
912import { VtkViewContext } from ' @/src/components/vtk/context' ;
1013
@@ -16,6 +19,17 @@ const toolStore = useToolStore();
1619
1720const PLACING_TOOLS = [Tools .Ruler , Tools .Rectangle , Tools .Polygon ];
1821
22+ const isAnnotationWidgetState = (
23+ widgetState : unknown
24+ ): widgetState is vtkAnnotationWidgetState => {
25+ const candidate = widgetState as Partial <vtkAnnotationWidgetState > | null ;
26+ return (
27+ !! candidate &&
28+ typeof candidate .getId === ' function' &&
29+ typeof candidate .getToolType === ' function'
30+ );
31+ };
32+
1933onVTKEvent (
2034 view .interactor ,
2135 ' onLeftButtonPress' ,
@@ -28,9 +42,16 @@ onVTKEvent(
2842 const withModifiers = !! (event .shiftKey || event .controlKey );
2943 const selectedData = view .widgetManager .getSelectedData ();
3044 if (' widget' in selectedData ) {
31- // clicked in empty space.
32- const widget = selectedData .widget as vtkAnnotationToolWidget ;
33- const widgetState = widget .getWidgetState ();
45+ const widget =
46+ selectedData .widget as Partial <vtkAnnotationToolWidget > | null ;
47+ const widgetState = widget ?.getWidgetState ?.();
48+ if (! isAnnotationWidgetState (widgetState )) {
49+ if (! withModifiers ) {
50+ selectionStore .clearSelection ();
51+ }
52+ return ;
53+ }
54+
3455 const id = widgetState .getId ();
3556 const type = widgetState .getToolType ();
3657 // preserve if we've used shift or ctrl
0 commit comments