All files / extensions/cornerstone/src/utils getLegacyViewportType.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 5/5
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65    204x                                               72490x 72490x         4447x         24504x                             39897x 39897x               2819x 2819x    
import { Enums as csEnums, Types as csTypes } from '@cornerstonejs/core';
 
const { ViewportType } = csEnums;
 
type ViewportLike = {
  requestedType?: csEnums.ViewportType;
  type?: csEnums.ViewportType;
};
 
/**
 * Returns the legacy/requested viewport type, transparent across GenericViewport
 * compatibility remapping.
 *
 * When `rendering.useGenericViewport` is enabled, a viewport requested as
 * `STACK` or `ORTHOGRAPHIC` has runtime `viewport.type === PLANAR_NEXT`, and a
 * `VOLUME_3D` request has `VOLUME_3D_NEXT`. The rendering engine records the
 * original request on `viewport.requestedType`, which this reads. Falls back to
 * `viewport.type` for legacy/non-remapped viewports (and older cornerstone builds
 * that do not populate `requestedType`).
 *
 * Use this instead of `viewport instanceof StackViewport/VolumeViewport/...` or
 * raw `viewport.type === ViewportType.X` when branching on the legacy viewport
 * type. For "does this viewport support operation X" questions, prefer the
 * cornerstone capability guards (`utilities.viewportSupports*`) instead.
 */
export function getLegacyViewportType(viewport: unknown): csEnums.ViewportType | undefined {
  const vp = viewport as ViewportLike | null | undefined;
  return vp?.requestedType ?? vp?.type;
}
 
/** Legacy STACK viewport (image stack). Replaces `instanceof StackViewport`. */
export function isStackViewportType(viewport: unknown): viewport is csTypes.IStackViewport {
  return getLegacyViewportType(viewport) === ViewportType.STACK;
}
 
/** Legacy ORTHOGRAPHIC (MPR) viewport. Replaces `instanceof VolumeViewport`. */
export function isOrthographicViewportType(viewport: unknown): viewport is csTypes.IVolumeViewport {
  return getLegacyViewportType(viewport) === ViewportType.ORTHOGRAPHIC;
}
 
/**
 * 3D volume viewport. Replaces `instanceof VolumeViewport3D`.
 *
 * Matches both the legacy `VOLUME_3D` type and the native ("next") `VOLUME_3D_NEXT`.
 * Under `useNextViewports`, OHIF requests `VOLUME_3D_NEXT` directly, so cornerstone
 * leaves `requestedType` unset and `getLegacyViewportType` returns the native type
 * (cornerstone only rewrites `requestedType` back to the legacy type for its own
 * compat remap of a legacy `VOLUME_3D` request). Checking only `VOLUME_3D` would miss
 * native 3D viewports and misroute them through the planar (getViewReference/getViewState)
 * branch, and would leave 3D gates such as `is3DVolume` false.
 */
export function isVolume3DViewportType(viewport: unknown): viewport is csTypes.IVolumeViewport {
  const legacyType = getLegacyViewportType(viewport);
  return legacyType === ViewportType.VOLUME_3D || legacyType === ViewportType.VOLUME_3D_NEXT;
}
 
/**
 * Legacy ORTHOGRAPHIC or VOLUME_3D viewport (i.e. a `BaseVolumeViewport`).
 * Replaces `instanceof BaseVolumeViewport`.
 */
export function isVolumeViewportType(viewport: unknown): viewport is csTypes.IVolumeViewport {
  const legacyType = getLegacyViewportType(viewport);
  return legacyType === ViewportType.ORTHOGRAPHIC || legacyType === ViewportType.VOLUME_3D;
}