Skip to content

Commit 6a5336c

Browse files
committed
fix: preserve camera settings across image switches
1 parent 0d0f13b commit 6a5336c

2 files changed

Lines changed: 80 additions & 4 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2+
import { effectScope, nextTick, ref, type EffectScope } from 'vue';
3+
import { createPinia, setActivePinia } from 'pinia';
4+
import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
5+
import { usePersistCameraConfig } from '@/src/composables/usePersistCameraConfig';
6+
import useViewCameraStore from '@/src/store/view-configs/camera';
7+
8+
describe('usePersistCameraConfig', () => {
9+
let scope: EffectScope;
10+
beforeEach(() => {
11+
setActivePinia(createPinia());
12+
scope = effectScope(true);
13+
});
14+
afterEach(() => scope.stop());
15+
16+
// Camera changes reach the saved config on the next task.
17+
const settle = async () => {
18+
await nextTick();
19+
await new Promise((resolve) => setTimeout(resolve));
20+
};
21+
22+
const setup = () => {
23+
const camera = vtkCamera.newInstance();
24+
const imageId = ref('a');
25+
scope.run(() => usePersistCameraConfig('view', imageId, camera));
26+
// Stands in for the view fitting its camera to a newly shown image.
27+
const show = async (id: string, fittedScale: number) => {
28+
imageId.value = id;
29+
const cameras = useViewCameraStore();
30+
if (!cameras.isCameraInitialized('view', id)) {
31+
camera.setParallelScale(fittedScale);
32+
cameras.markCameraAsInitialized('view', id);
33+
}
34+
await settle();
35+
};
36+
return { camera, imageId, show };
37+
};
38+
39+
it('returns an image to its own zoom when another fits at the same scale', async () => {
40+
const { camera, show } = setup();
41+
await show('a', 16);
42+
await show('b', 16);
43+
44+
await show('a', 16);
45+
camera.setParallelScale(4);
46+
await settle();
47+
await show('b', 16);
48+
49+
expect(camera.getParallelScale()).toBe(16);
50+
});
51+
52+
it('does not save the previous camera for an image that is not shown yet', async () => {
53+
const { camera, imageId, show } = setup();
54+
await show('a', 4);
55+
56+
imageId.value = 'b';
57+
await settle();
58+
await show('a', 4);
59+
await show('b', 16);
60+
61+
expect(camera.getParallelScale()).toBe(16);
62+
});
63+
});

‎src/composables/usePersistCameraConfig.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MaybeRef, Ref, computed, unref } from 'vue';
1+
import { MaybeRef, Ref, computed, unref, watch } from 'vue';
22
import { Maybe } from '@/src/types';
33
import useViewCameraStore from '@/src/store/view-configs/camera';
44
import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
@@ -58,7 +58,20 @@ export function usePersistCameraConfig(
5858
{} as Record<KeyType, Ref<any>>
5959
);
6060

61-
keys.forEach((key) => {
62-
syncRef(configRefs[key], cameraRefs[key]);
63-
});
61+
// Until the view fits an image, the camera still shows the previous one.
62+
const cameraShowsImage = () =>
63+
viewCameraStore.disableCameraAutoReset ||
64+
viewCameraStore.isCameraInitialized(unref(viewID), unref(dataID));
65+
66+
// Rebind so an unchanged camera value is also saved for the new image.
67+
watch(
68+
[() => unref(viewID), () => unref(dataID), cameraShowsImage],
69+
([, , showsImage], _previous, onCleanup) => {
70+
if (!showsImage) return;
71+
const stop = keys.map((key) => syncRef(configRefs[key], cameraRefs[key]));
72+
onCleanup(() => stop.forEach((unsubscribe) => unsubscribe()));
73+
},
74+
// Sync: the saved camera must land before views reset clipping ranges.
75+
{ immediate: true, flush: 'sync' }
76+
);
6477
}

0 commit comments

Comments
 (0)