copper3d 3.9.3 → 3.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +113 -5
  2. package/dist/Loader/copperNrrdLoader.d.ts +74 -0
  3. package/dist/Loader/copperNrrdLoader.js +182 -8
  4. package/dist/Loader/copperNrrdLoader.js.map +1 -1
  5. package/dist/Loader/nrrdWorker.d.ts +1 -0
  6. package/dist/Loader/nrrdWorker.js +29 -0
  7. package/dist/Loader/nrrdWorker.js.map +1 -0
  8. package/dist/Loader/nrrdWorkerCore.d.ts +87 -0
  9. package/dist/Loader/nrrdWorkerCore.js +223 -0
  10. package/dist/Loader/nrrdWorkerCore.js.map +1 -0
  11. package/dist/Utils/segmentation/DrawToolCore.d.ts +20 -0
  12. package/dist/Utils/segmentation/DrawToolCore.js +78 -47
  13. package/dist/Utils/segmentation/DrawToolCore.js.map +1 -1
  14. package/dist/Utils/segmentation/NrrdTools.d.ts +66 -0
  15. package/dist/Utils/segmentation/NrrdTools.js +110 -0
  16. package/dist/Utils/segmentation/NrrdTools.js.map +1 -1
  17. package/dist/Utils/segmentation/tools/DataLoader.d.ts +48 -1
  18. package/dist/Utils/segmentation/tools/DataLoader.js +128 -30
  19. package/dist/Utils/segmentation/tools/DataLoader.js.map +1 -1
  20. package/dist/Utils/segmentation/tools/DragSliceTool.js +10 -3
  21. package/dist/Utils/segmentation/tools/DragSliceTool.js.map +1 -1
  22. package/dist/Utils/segmentation/tools/ToolHost.d.ts +9 -1
  23. package/dist/bundle.esm.js +518 -94
  24. package/dist/bundle.umd.js +519 -93
  25. package/dist/index.d.ts +5 -3
  26. package/dist/index.js +3 -2
  27. package/dist/index.js.map +1 -1
  28. package/dist/types/Loader/copperNrrdLoader.d.ts +74 -0
  29. package/dist/types/Loader/nrrdWorker.d.ts +1 -0
  30. package/dist/types/Loader/nrrdWorkerCore.d.ts +87 -0
  31. package/dist/types/Utils/segmentation/DrawToolCore.d.ts +20 -0
  32. package/dist/types/Utils/segmentation/NrrdTools.d.ts +66 -0
  33. package/dist/types/Utils/segmentation/tools/DataLoader.d.ts +48 -1
  34. package/dist/types/Utils/segmentation/tools/ToolHost.d.ts +9 -1
  35. package/dist/types/index.d.ts +5 -3
  36. package/package.json +1 -1
@@ -0,0 +1,87 @@
1
+ /** A message this module hands back to the caller, in the exact shape `postMessage` expects
2
+ * (payload + a transfer list containing every `Transferable` inside it). */
3
+ export type WorkerEmit = (message: NrrdWorkerMessage, transfer?: Transferable[]) => void;
4
+ export interface NrrdLoadCommand {
5
+ cmd: "load";
6
+ id: string;
7
+ url: string;
8
+ segmentation: boolean;
9
+ knownMinMax?: [number, number];
10
+ }
11
+ export interface NrrdAbortCommand {
12
+ cmd: "abort";
13
+ id: string;
14
+ }
15
+ export type NrrdWorkerCommand = NrrdLoadCommand | NrrdAbortCommand;
16
+ /** Everything `rehydrateVolume` (main thread, `copperNrrdLoader.ts`) needs to reconstruct a
17
+ * real `Volume` -- values already computed by the real `NRRDLoader.parse()` above, copied
18
+ * out verbatim, never re-derived. `buffer` is the volume's own pixel data, transferable. */
19
+ export interface NrrdWorkerPayload {
20
+ xLength: number;
21
+ yLength: number;
22
+ zLength: number;
23
+ /** `headerObject.type`, e.g. "short" -- accepted by `Volume`'s own constructor switch. */
24
+ type: string;
25
+ header: Record<string, unknown>;
26
+ spacing: [number, number, number];
27
+ axisOrder: string[];
28
+ RASDimensions: [number, number, number];
29
+ segmentation: boolean;
30
+ min: number;
31
+ max: number;
32
+ windowLow: number;
33
+ windowHigh: number;
34
+ lowerThreshold: number;
35
+ upperThreshold: number;
36
+ matrixElements: number[];
37
+ inverseMatrixElements: number[];
38
+ buffer: ArrayBuffer;
39
+ }
40
+ export type NrrdWorkerMessage = {
41
+ id: string;
42
+ type: "progress";
43
+ loaded: number;
44
+ total: number;
45
+ } | {
46
+ id: string;
47
+ type: "loaded";
48
+ payload: NrrdWorkerPayload;
49
+ } | {
50
+ id: string;
51
+ type: "error";
52
+ name: string;
53
+ message: string;
54
+ };
55
+ interface PendingUrlFetch {
56
+ bufferPromise: Promise<ArrayBuffer>;
57
+ controller: AbortController;
58
+ attached: number;
59
+ }
60
+ export interface NrrdWorkerState {
61
+ pendingByUrl: Map<string, PendingUrlFetch>;
62
+ /** Which URL each still-attached caller id belongs to, so `handleAbortMessage` can find its
63
+ * shared entry, and so a load whose buffer arrives after the caller already detached can
64
+ * tell it has nothing left to do. */
65
+ callerToUrl: Map<string, string>;
66
+ }
67
+ export declare function createNrrdWorkerState(): NrrdWorkerState;
68
+ /**
69
+ * Handles one `{cmd: "load"}` command. Dedupes the underlying fetch by URL exactly like
70
+ * `copperNrrdLoader.ts`'s old `sharedFetchNrrdArrayBuffer` did (one real request per URL,
71
+ * shared by every attached caller) -- only the transport moved, not the sharing rule. Once
72
+ * the shared bytes arrive, this caller runs its OWN `loader.parse()` with its OWN
73
+ * `knownMinMax`, independently of every other attached caller -- parsing is cheap relative to
74
+ * fetch+gunzip and was never shared even before this change (see `copperNrrdLoader.ts`'s doc
75
+ * comment on `sharedFetchNrrdArrayBuffer`), so a second caller's different `knownMinMax` can
76
+ * never be silently handed the first caller's window.
77
+ */
78
+ export declare function handleLoadMessage(state: NrrdWorkerState, msg: NrrdLoadCommand, emit: WorkerEmit): Promise<void>;
79
+ /**
80
+ * Handles one `{cmd: "abort"}` command. The caller's own promise on the main thread is
81
+ * already rejected by the time this arrives (see `copperNrrdLoader.ts`'s `loadViaWorker` --
82
+ * it rejects locally the instant the caller's `AbortSignal` fires, exactly like the old
83
+ * `sharedFetchNrrdArrayBuffer` did). This only updates the shared bookkeeping: the real
84
+ * network transfer stops only once every attached caller has dropped off.
85
+ */
86
+ export declare function handleAbortMessage(state: NrrdWorkerState, msg: NrrdAbortCommand): void;
87
+ export {};
@@ -0,0 +1,223 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { NRRDLoader } from "three/examples/jsm/loaders/NRRDLoader";
11
+ import { Volume } from "three/examples/jsm/misc/Volume.js";
12
+ /**
13
+ * Runs entirely off the main thread: fetch, gunzip (via `NRRDLoader.parse`'s own
14
+ * `fflate.gunzipSync`) and the NRRD header/typed-array parse. This module has no DOM
15
+ * dependency -- `NRRDLoader.parse` and `Volume` only touch `ArrayBuffer`s and three's math
16
+ * classes (`Matrix4`, `Vector3`), never `document` -- so it is safe to import both inside a
17
+ * Web Worker and, for tests, inside the main jsdom realm (see `__tests__/helpers/
18
+ * fakeNrrdWorker.ts`, which drives this same module in-process instead of through a real
19
+ * `postMessage` boundary).
20
+ *
21
+ * Structured as plain functions over an explicit `NrrdWorkerState`, with an injected `emit`
22
+ * callback instead of a bare `self.postMessage`, so the real worker entry (`nrrdWorker.ts`)
23
+ * and the in-process test double can share the exact same logic.
24
+ */
25
+ const loader = new NRRDLoader();
26
+ /**
27
+ * Mirrors `copperNrrdLoader.ts`'s own `installMinMaxOverride` -- same lever, same reason
28
+ * (three exposes no hook between "Volume built" and "computeMinMax() called", only the
29
+ * method itself), just installed against *this* module's own `Volume` class. Worker and main
30
+ * thread each get their own bundle of `three/examples/jsm/misc/Volume.js`, so patching the
31
+ * prototype here never touches the main thread's copy.
32
+ */
33
+ let pendingMinMax = null;
34
+ const MINMAX_OVERRIDE_INSTALLED = Symbol("copper-worker-minmax-override");
35
+ function installMinMaxOverride() {
36
+ const proto = Volume.prototype;
37
+ if (proto[MINMAX_OVERRIDE_INSTALLED])
38
+ return;
39
+ const original = proto.computeMinMax;
40
+ proto.computeMinMax = function computeMinMaxOrSupplied() {
41
+ if (pendingMinMax) {
42
+ const [min, max] = pendingMinMax;
43
+ this.min = min;
44
+ this.max = max;
45
+ return pendingMinMax;
46
+ }
47
+ return original.call(this);
48
+ };
49
+ proto[MINMAX_OVERRIDE_INSTALLED] = true;
50
+ }
51
+ export function createNrrdWorkerState() {
52
+ installMinMaxOverride();
53
+ return { pendingByUrl: new Map(), callerToUrl: new Map() };
54
+ }
55
+ /**
56
+ * Fetches `url` to completion, reporting `{loaded, total}` progress. No abort handling of its
57
+ * own -- the caller passes a signal it controls (the shared per-URL `AbortController`).
58
+ */
59
+ function fetchRawBuffer(url, signal, onProgress) {
60
+ return __awaiter(this, void 0, void 0, function* () {
61
+ const response = yield fetch(url, { signal });
62
+ if (!response.ok) {
63
+ throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
64
+ }
65
+ const total = Number(response.headers.get("content-length")) || 0;
66
+ if (!response.body) {
67
+ return response.arrayBuffer();
68
+ }
69
+ const reader = response.body.getReader();
70
+ const chunks = [];
71
+ let loaded = 0;
72
+ for (;;) {
73
+ const { done, value } = yield reader.read();
74
+ if (done)
75
+ break;
76
+ chunks.push(value);
77
+ loaded += value.byteLength;
78
+ onProgress(loaded, total);
79
+ }
80
+ const merged = new Uint8Array(loaded);
81
+ let offset = 0;
82
+ for (const chunk of chunks) {
83
+ merged.set(chunk, offset);
84
+ offset += chunk.byteLength;
85
+ }
86
+ return merged.buffer;
87
+ });
88
+ }
89
+ /** Turns a real, just-parsed `Volume` into a transferable, structured-clone-safe payload.
90
+ * Every field is a value the real `NRRDLoader.parse()` already computed -- this only unwraps
91
+ * it from class instances (`Matrix4`) and non-cloneable fields (`header.__array`, a
92
+ * constructor function) that cannot cross the worker/main-thread boundary as-is. */
93
+ function buildPayload(volume) {
94
+ const header = Object.assign({}, volume.header);
95
+ delete header.__array;
96
+ return {
97
+ xLength: volume.xLength,
98
+ yLength: volume.yLength,
99
+ zLength: volume.zLength,
100
+ type: volume.header.type,
101
+ header,
102
+ spacing: volume.spacing,
103
+ axisOrder: volume.axisOrder,
104
+ RASDimensions: volume.RASDimensions,
105
+ segmentation: volume.segmentation,
106
+ min: volume.min,
107
+ max: volume.max,
108
+ windowLow: volume.windowLow,
109
+ windowHigh: volume.windowHigh,
110
+ lowerThreshold: volume.lowerThreshold,
111
+ upperThreshold: volume.upperThreshold,
112
+ matrixElements: Array.from(volume.matrix.elements),
113
+ inverseMatrixElements: Array.from(volume.inverseMatrix.elements),
114
+ buffer: volume.data.buffer,
115
+ };
116
+ }
117
+ function errorMessage(err) {
118
+ if (err instanceof Error)
119
+ return { name: err.name, message: err.message };
120
+ return { name: "Error", message: String(err) };
121
+ }
122
+ /**
123
+ * Handles one `{cmd: "load"}` command. Dedupes the underlying fetch by URL exactly like
124
+ * `copperNrrdLoader.ts`'s old `sharedFetchNrrdArrayBuffer` did (one real request per URL,
125
+ * shared by every attached caller) -- only the transport moved, not the sharing rule. Once
126
+ * the shared bytes arrive, this caller runs its OWN `loader.parse()` with its OWN
127
+ * `knownMinMax`, independently of every other attached caller -- parsing is cheap relative to
128
+ * fetch+gunzip and was never shared even before this change (see `copperNrrdLoader.ts`'s doc
129
+ * comment on `sharedFetchNrrdArrayBuffer`), so a second caller's different `knownMinMax` can
130
+ * never be silently handed the first caller's window.
131
+ */
132
+ export function handleLoadMessage(state, msg, emit) {
133
+ return __awaiter(this, void 0, void 0, function* () {
134
+ const { id, url, segmentation, knownMinMax } = msg;
135
+ let entry = state.pendingByUrl.get(url);
136
+ if (!entry) {
137
+ const controller = new AbortController();
138
+ const bufferPromise = fetchRawBuffer(url, controller.signal, (loaded, total) => {
139
+ for (const [callerId, callerUrl] of state.callerToUrl) {
140
+ if (callerUrl === url)
141
+ emit({ id: callerId, type: "progress", loaded, total });
142
+ }
143
+ }).finally(() => {
144
+ if (state.pendingByUrl.get(url) === entry)
145
+ state.pendingByUrl.delete(url);
146
+ });
147
+ entry = { bufferPromise, controller, attached: 0 };
148
+ state.pendingByUrl.set(url, entry);
149
+ }
150
+ entry.attached++;
151
+ state.callerToUrl.set(id, url);
152
+ const detach = () => {
153
+ entry.attached--;
154
+ if (entry.attached <= 0) {
155
+ entry.controller.abort();
156
+ if (state.pendingByUrl.get(url) === entry)
157
+ state.pendingByUrl.delete(url);
158
+ }
159
+ };
160
+ let buffer;
161
+ try {
162
+ buffer = yield entry.bufferPromise;
163
+ }
164
+ catch (err) {
165
+ // A caller already detached (via an explicit abort message) has its own `attached`
166
+ // decrement done there -- doing it again here would double-count. Its promise on the
167
+ // main thread was also already rejected locally, so there is nothing left to emit.
168
+ const stillAttached = state.callerToUrl.has(id);
169
+ if (stillAttached) {
170
+ detach();
171
+ state.callerToUrl.delete(id);
172
+ emit(Object.assign({ id, type: "error" }, errorMessage(err)));
173
+ }
174
+ return;
175
+ }
176
+ const stillAttached = state.callerToUrl.has(id);
177
+ if (stillAttached) {
178
+ detach();
179
+ state.callerToUrl.delete(id);
180
+ }
181
+ if (!stillAttached)
182
+ return; // detached (aborted) while the fetch was in flight
183
+ try {
184
+ pendingMinMax = knownMinMax !== null && knownMinMax !== void 0 ? knownMinMax : null;
185
+ let volume;
186
+ try {
187
+ loader.setSegmentation(segmentation);
188
+ volume = loader.parse(buffer);
189
+ }
190
+ finally {
191
+ pendingMinMax = null;
192
+ }
193
+ const payload = buildPayload(volume);
194
+ emit({ id, type: "loaded", payload }, [payload.buffer]);
195
+ }
196
+ catch (err) {
197
+ emit(Object.assign({ id, type: "error" }, errorMessage(err)));
198
+ }
199
+ });
200
+ }
201
+ /**
202
+ * Handles one `{cmd: "abort"}` command. The caller's own promise on the main thread is
203
+ * already rejected by the time this arrives (see `copperNrrdLoader.ts`'s `loadViaWorker` --
204
+ * it rejects locally the instant the caller's `AbortSignal` fires, exactly like the old
205
+ * `sharedFetchNrrdArrayBuffer` did). This only updates the shared bookkeeping: the real
206
+ * network transfer stops only once every attached caller has dropped off.
207
+ */
208
+ export function handleAbortMessage(state, msg) {
209
+ const url = state.callerToUrl.get(msg.id);
210
+ if (!url)
211
+ return;
212
+ state.callerToUrl.delete(msg.id);
213
+ const entry = state.pendingByUrl.get(url);
214
+ if (!entry)
215
+ return;
216
+ entry.attached--;
217
+ if (entry.attached <= 0) {
218
+ entry.controller.abort();
219
+ if (state.pendingByUrl.get(url) === entry)
220
+ state.pendingByUrl.delete(url);
221
+ }
222
+ }
223
+ //# sourceMappingURL=nrrdWorkerCore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nrrdWorkerCore.js","sourceRoot":"","sources":["../../src/Loader/nrrdWorkerCore.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,uCAAuC,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAE3D;;;;;;;;;;;;GAYG;AAEH,MAAM,MAAM,GAAQ,IAAI,UAAU,EAAE,CAAC;AAErC;;;;;;GAMG;AACH,IAAI,aAAa,GAA4B,IAAI,CAAC;AAClD,MAAM,yBAAyB,GAAG,MAAM,CAAC,+BAA+B,CAAC,CAAC;AAE1E,SAAS,qBAAqB;IAC5B,MAAM,KAAK,GAAG,MAAM,CAAC,SAAgB,CAAC;IACtC,IAAI,KAAK,CAAC,yBAAyB,CAAC;QAAE,OAAO;IAC7C,MAAM,QAAQ,GAAG,KAAK,CAAC,aAAa,CAAC;IACrC,KAAK,CAAC,aAAa,GAAG,SAAS,uBAAuB;QACpD,IAAI,aAAa,EAAE,CAAC;YAClB,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,aAAa,CAAC;YACjC,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;YACf,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;YACf,OAAO,aAAa,CAAC;QACvB,CAAC;QACD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7B,CAAC,CAAC;IACF,KAAK,CAAC,yBAAyB,CAAC,GAAG,IAAI,C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CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC"}
@@ -47,6 +47,26 @@ export declare class DrawToolCore {
47
47
  aiAssistTool: AiAssistTool;
48
48
  /** Slice index recorded when paintOnCanvas() starts, guards stale-click */
49
49
  private paintSliceIndex;
50
+ /**
51
+ * While true, no input may modify a mask: the pencil, brush, eraser, sphere and
52
+ * sphere-brush branches below all no-op. Slice scrubbing, zoom, pan and the crosshair
53
+ * stay live, because this exists for the window while a case's images are still
54
+ * arriving -- the reader may look, but must not annotate.
55
+ *
56
+ * A UI that merely greys its own buttons does not achieve this: a tool selected before
57
+ * the load began stays armed, and `afterLoadSlice` clears the undo stack every time a
58
+ * slice lands, so a stroke made during loading cannot be taken back.
59
+ */
60
+ private annotationSuspended;
61
+ setAnnotationSuspended(suspended: boolean): void;
62
+ isAnnotationSuspended(): boolean;
63
+ /**
64
+ * Left/right button press on the drawing canvas.
65
+ *
66
+ * Named rather than inlined into `draw()` so the suspension gate below can be exercised
67
+ * directly: it is the single point that decides whether an input may modify a mask.
68
+ */
69
+ onCanvasPointerDown(e: MouseEvent): void;
50
70
  /** Wheel event dispatch mode — replaces manual wheel add/remove (Phase 2) */
51
71
  private activeWheelMode;
52
72
  /**
@@ -22,6 +22,70 @@ import { UndoManager } from "./core";
22
22
  * Created by NrrdTools which passes in a shared CanvasState.
23
23
  */
24
24
  export class DrawToolCore {
25
+ setAnnotationSuspended(suspended) {
26
+ this.annotationSuspended = suspended;
27
+ }
28
+ isAnnotationSuspended() {
29
+ return this.annotationSuspended;
30
+ }
31
+ /**
32
+ * Left/right button press on the drawing canvas.
33
+ *
34
+ * Named rather than inlined into `draw()` so the suspension gate below can be exercised
35
+ * directly: it is the single point that decides whether an input may modify a mask.
36
+ */
37
+ onCanvasPointerDown(e) {
38
+ if (this.drawingTool.isActive || this.panTool.isActive) {
39
+ this.state.protectedData.ctxes.drawingLayerMasterCtx.closePath();
40
+ return;
41
+ }
42
+ if (this.paintSliceIndex !== this.state.protectedData.mainPreSlices.index) {
43
+ this.paintSliceIndex = this.state.protectedData.mainPreSlices.index;
44
+ }
45
+ // Suppress wheel only when starting a draw operation
46
+ if (this.eventRouter.getMode() === 'draw') {
47
+ this.activeWheelMode = 'none';
48
+ }
49
+ if (e.button === 0) {
50
+ // Every branch that can write into a mask is gated; the crosshair below is a
51
+ // read-only probe and stays available, as do pan (button 2) and the wheel.
52
+ if (this.annotationSuspended && !this.eventRouter.isCrosshairEnabled()) {
53
+ return;
54
+ }
55
+ if (this.eventRouter.getMode() === 'aiAssist') {
56
+ this.aiAssistTool.onPointerDown(e);
57
+ }
58
+ else if (this.eventRouter.getMode() === 'draw') {
59
+ this.drawingTool.onPointerDown(e);
60
+ }
61
+ else if (this.eventRouter.isCrosshairEnabled()) {
62
+ this.state.nrrd_states.interaction.cursorPageX =
63
+ e.offsetX / this.state.nrrd_states.view.sizeFactor;
64
+ this.state.nrrd_states.interaction.cursorPageY =
65
+ e.offsetY / this.state.nrrd_states.view.sizeFactor;
66
+ this.enableCrosshair();
67
+ }
68
+ else if (this.state.gui_states.mode.sphereBrush && !this.eventRouter.isCrosshairEnabled()) {
69
+ // SphereBrush: direct click (no Shift needed)
70
+ this.activeWheelMode = 'sphereBrush';
71
+ this.sphereBrushTool.onSphereBrushClick(e);
72
+ }
73
+ else if (this.state.gui_states.mode.sphereEraser && !this.eventRouter.isCrosshairEnabled()) {
74
+ // SphereEraser: direct click (Shift handled at mode level)
75
+ this.activeWheelMode = 'sphereBrush';
76
+ this.sphereBrushTool.onSphereEraserClick(e);
77
+ }
78
+ else if (this.state.gui_states.mode.sphere && !this.eventRouter.isCrosshairEnabled()) {
79
+ this.handleSphereClick(e);
80
+ }
81
+ }
82
+ else if (e.button === 2) {
83
+ this.panTool.onPointerDown(e);
84
+ }
85
+ else {
86
+ return;
87
+ }
88
+ }
25
89
  constructor(container, state) {
26
90
  this.drawingPrameters = {
27
91
  handleOnDrawingMouseDown: (ev) => { },
@@ -50,6 +114,17 @@ export class DrawToolCore {
50
114
  this.undoManager = new UndoManager();
51
115
  /** Slice index recorded when paintOnCanvas() starts, guards stale-click */
52
116
  this.paintSliceIndex = 0;
117
+ /**
118
+ * While true, no input may modify a mask: the pencil, brush, eraser, sphere and
119
+ * sphere-brush branches below all no-op. Slice scrubbing, zoom, pan and the crosshair
120
+ * stay live, because this exists for the window while a case's images are still
121
+ * arriving -- the reader may look, but must not annotate.
122
+ *
123
+ * A UI that merely greys its own buttons does not achieve this: a tool selected before
124
+ * the load began stays armed, and `afterLoadSlice` clears the undo stack every time a
125
+ * slice lands, so a stroke made during loading cannot be taken back.
126
+ */
127
+ this.annotationSuspended = false;
53
128
  /** Wheel event dispatch mode — replaces manual wheel add/remove (Phase 2) */
54
129
  this.activeWheelMode = 'zoom';
55
130
  /**
@@ -430,55 +505,11 @@ export class DrawToolCore {
430
505
  this.drawingTool.createBrushTrackingHandler();
431
506
  // drawing move — delegated to DrawingTool
432
507
  this.drawingPrameters.handleOnDrawingMouseMove = (e) => {
433
- this.drawingTool.onPointerMove(e);
434
- };
435
- this.drawingPrameters.handleOnDrawingMouseDown = (e) => {
436
- if (this.drawingTool.isActive || this.panTool.isActive) {
437
- this.state.protectedData.ctxes.drawingLayerMasterCtx.closePath();
508
+ if (this.annotationSuspended)
438
509
  return;
439
- }
440
- if (this.paintSliceIndex !== this.state.protectedData.mainPreSlices.index) {
441
- this.paintSliceIndex = this.state.protectedData.mainPreSlices.index;
442
- }
443
- // Suppress wheel only when starting a draw operation
444
- if (this.eventRouter.getMode() === 'draw') {
445
- this.activeWheelMode = 'none';
446
- }
447
- if (e.button === 0) {
448
- if (this.eventRouter.getMode() === 'aiAssist') {
449
- this.aiAssistTool.onPointerDown(e);
450
- }
451
- else if (this.eventRouter.getMode() === 'draw') {
452
- this.drawingTool.onPointerDown(e);
453
- }
454
- else if (this.eventRouter.isCrosshairEnabled()) {
455
- this.state.nrrd_states.interaction.cursorPageX =
456
- e.offsetX / this.state.nrrd_states.view.sizeFactor;
457
- this.state.nrrd_states.interaction.cursorPageY =
458
- e.offsetY / this.state.nrrd_states.view.sizeFactor;
459
- this.enableCrosshair();
460
- }
461
- else if (this.state.gui_states.mode.sphereBrush && !this.eventRouter.isCrosshairEnabled()) {
462
- // SphereBrush: direct click (no Shift needed)
463
- this.activeWheelMode = 'sphereBrush';
464
- this.sphereBrushTool.onSphereBrushClick(e);
465
- }
466
- else if (this.state.gui_states.mode.sphereEraser && !this.eventRouter.isCrosshairEnabled()) {
467
- // SphereEraser: direct click (Shift handled at mode level)
468
- this.activeWheelMode = 'sphereBrush';
469
- this.sphereBrushTool.onSphereEraserClick(e);
470
- }
471
- else if (this.state.gui_states.mode.sphere && !this.eventRouter.isCrosshairEnabled()) {
472
- this.handleSphereClick(e);
473
- }
474
- }
475
- else if (e.button === 2) {
476
- this.panTool.onPointerDown(e);
477
- }
478
- else {
479
- return;
480
- }
510
+ this.drawingTool.onPointerMove(e);
481
511
  };
512
+ this.drawingPrameters.handleOnDrawingMouseDown = (e) => this.onCanvasPointerDown(e);
482
513
  // Route pointerdown through EventRouter
483
514
  this.eventRouter.setPointerDownHandler((e) => {
484
515
  // On slice-drag start, temporarily borrow the wheel as Scroll:Slice