Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/composables/__tests__/usePersistCameraConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { effectScope, nextTick, ref, type EffectScope } from 'vue';
import { createPinia, setActivePinia } from 'pinia';
import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
import { usePersistCameraConfig } from '@/src/composables/usePersistCameraConfig';
import useViewCameraStore from '@/src/store/view-configs/camera';

describe('usePersistCameraConfig', () => {
let scope: EffectScope;
beforeEach(() => {
setActivePinia(createPinia());
scope = effectScope(true);
});
afterEach(() => scope.stop());

// Camera changes reach the saved config on the next task.
const settle = async () => {
await nextTick();
await new Promise((resolve) => setTimeout(resolve));
};

const setup = () => {
const camera = vtkCamera.newInstance();
const imageId = ref('a');
scope.run(() => usePersistCameraConfig('view', imageId, camera));
// Stands in for the view fitting its camera to a newly shown image.
const show = async (id: string, fittedScale: number) => {
imageId.value = id;
const cameras = useViewCameraStore();
if (!cameras.isCameraInitialized('view', id)) {
camera.setParallelScale(fittedScale);
cameras.markCameraAsInitialized('view', id);
}
await settle();
};
return { camera, imageId, show };
};

it('returns an image to its own zoom when another fits at the same scale', async () => {
const { camera, show } = setup();
await show('a', 16);
await show('b', 16);

await show('a', 16);
camera.setParallelScale(4);
await settle();
await show('b', 16);

expect(camera.getParallelScale()).toBe(16);
});

it('does not save the previous camera for an image that is not shown yet', async () => {
const { camera, imageId, show } = setup();
await show('a', 4);

imageId.value = 'b';
await settle();
await show('a', 4);
await show('b', 16);

expect(camera.getParallelScale()).toBe(16);
});
});
21 changes: 17 additions & 4 deletions src/composables/usePersistCameraConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MaybeRef, Ref, computed, unref } from 'vue';
import { MaybeRef, Ref, computed, unref, watch } from 'vue';
import { Maybe } from '@/src/types';
import useViewCameraStore from '@/src/store/view-configs/camera';
import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera';
Expand Down Expand Up @@ -58,7 +58,20 @@ export function usePersistCameraConfig(
{} as Record<KeyType, Ref<any>>
);

keys.forEach((key) => {
syncRef(configRefs[key], cameraRefs[key]);
});
// Until the view fits an image, the camera still shows the previous one.
const cameraShowsImage = () =>
viewCameraStore.disableCameraAutoReset ||
viewCameraStore.isCameraInitialized(unref(viewID), unref(dataID));

// Rebind so an unchanged camera value is also saved for the new image.
watch(
[() => unref(viewID), () => unref(dataID), cameraShowsImage],
([, , showsImage], _previous, onCleanup) => {
if (!showsImage) return;
const stop = keys.map((key) => syncRef(configRefs[key], cameraRefs[key]));
onCleanup(() => stop.forEach((unsubscribe) => unsubscribe()));
},
// Sync: the saved camera must land before views reset clipping ranges.
{ immediate: true, flush: 'sync' }
);
}
Loading