ohif-viewer/modes/segmentation/src/utils/setUpAutoTabSwitchHandler.ts
Joe Boccanfuso 35bfc9b0cc
feature(segmentation): Add contour tools and utilities in a side panel (#5517)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Dan Rukas <dan.rukas@gmail.com>
2025-10-29 14:15:52 -04:00

57 lines
1.9 KiB
TypeScript

/**
* Sets up auto tab switching for when the first segmentation is added into the viewer.
*/
export default function setUpAutoTabSwitchHandler({
segmentationService,
viewportGridService,
panelService,
}) {
const autoTabSwitchEvents = [
segmentationService.EVENTS.SEGMENTATION_MODIFIED,
segmentationService.EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED,
];
// Initially there are no segmentations, so we should switch the tab whenever the first segmentation is added.
let shouldSwitchTab = true;
const unsubscribeAutoTabSwitchEvents = autoTabSwitchEvents
.map(eventName =>
segmentationService.subscribe(eventName, () => {
const segmentations = segmentationService.getSegmentations();
if (!segmentations.length) {
// If all the segmentations are removed, then the next time a segmentation is added, we should switch the tab.
shouldSwitchTab = true;
return;
}
const activeViewportId = viewportGridService.getActiveViewportId();
const activeRepresentation = segmentationService
.getSegmentationRepresentations(activeViewportId)
?.find(representation => representation.active);
if (activeRepresentation && shouldSwitchTab) {
shouldSwitchTab = false;
switch (activeRepresentation.type) {
case 'Labelmap':
panelService.activatePanel(
'@ohif/extension-cornerstone.panelModule.panelSegmentationWithToolsLabelMap',
true
);
break;
case 'Contour':
panelService.activatePanel(
'@ohif/extension-cornerstone.panelModule.panelSegmentationWithToolsContour',
true
);
break;
}
}
})
)
.map(subscription => subscription.unsubscribe);
return { unsubscribeAutoTabSwitchEvents };
}