Skip to content

Commit a08aabf

Browse files
committed
refactor(views): derive the active named layout from the panes
1 parent 14cd798 commit a08aabf

5 files changed

Lines changed: 45 additions & 26 deletions

File tree

‎src/enface/__tests__/layout.spec.ts‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,26 @@ function configureComparison() {
3737
}
3838

3939
describe('En face view configuration', () => {
40-
it.each(['custom', 'named', 'legacy named'])(
41-
'restores the %s layout selection without inferring its identity from panes',
42-
async (selection) => {
40+
it.each([
41+
['named', true],
42+
['regridded', false],
43+
['retyped', false],
44+
] as const)(
45+
'highlights a %s layout after restore only when its panes match',
46+
async (selection, highlighted) => {
4347
await loadVolume('oct');
4448
const views = useViewStore();
4549
views.setDataForAllViews('oct');
4650
configureComparison();
4751
views.switchToNamedLayout('Comparison');
48-
if (selection === 'custom') views.setLayoutFromGrid([1, 2]);
52+
if (selection === 'regridded') views.setLayoutFromGrid([2, 1]);
53+
if (selection === 'retyped')
54+
views.replaceView(views.layoutViews[1].id, {
55+
name: 'Oblique',
56+
type: 'Oblique',
57+
dataID: 'oct',
58+
options: {},
59+
});
4960
const hidden = views
5061
.getAllViews()
5162
.filter((view) => !views.layoutViews.includes(view));
@@ -63,7 +74,6 @@ describe('En face view configuration', () => {
6374
});
6475
const zip = await JSZip.loadAsync(saved);
6576
const raw = JSON.parse(await zip.file(MANIFEST)!.async('string'));
66-
if (selection === 'legacy named') delete raw.currentLayoutName;
6777
const manifest = ManifestSchema.parse(raw);
6878
await cleanupCachedImages([]);
6979
setActivePinia(createPinia());
@@ -77,7 +87,10 @@ describe('En face view configuration', () => {
7787
restored.layoutViews.map(({ type, dataID }) => ({ type, dataID }))
7888
).toEqual([
7989
{ type: '2D', dataID: 'restored-oct' },
80-
{ type: 'EnFace', dataID: 'restored-oct' },
90+
{
91+
type: selection === 'retyped' ? 'Oblique' : 'EnFace',
92+
dataID: 'restored-oct',
93+
},
8194
]);
8295
hidden.forEach(({ id, type }) =>
8396
expect(restored.getView(id)?.type).toBe(type)
@@ -87,9 +100,7 @@ describe('En face view configuration', () => {
87100
});
88101
const named = wrapper.get('.v-list-item');
89102
expect(named.text()).toBe('Comparison');
90-
expect(named.classes().includes('v-list-item--active')).toBe(
91-
selection === 'named'
92-
);
103+
expect(named.classes().includes('v-list-item--active')).toBe(highlighted);
93104
expect(Object.keys(restored.namedLayouts)).toEqual(['Comparison']);
94105
}
95106
);

‎src/io/state-file/__tests__/serializeResilience.spec.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ describe('state-file serialization resilience', () => {
168168
...manifestWithSelection('dataset-1'),
169169
activeView: 'view-1',
170170
isActiveViewMaximized: true,
171-
currentLayoutName: 'Four Up',
172171
layout: {
173172
direction: 'column',
174173
items: [{ type: 'slot', slotIndex: 0 }],
@@ -191,7 +190,6 @@ describe('state-file serialization resilience', () => {
191190
expect(normalized.manifest).not.toHaveProperty('activeView');
192191
expect(normalized.manifest).not.toHaveProperty('isActiveViewMaximized');
193192
expect(normalized.manifest).not.toHaveProperty('layout');
194-
expect(normalized.manifest).not.toHaveProperty('currentLayoutName');
195193
expect(normalized.manifest).not.toHaveProperty('layoutSlots');
196194
expect(normalized.omitted).toContain('view/layout: invalid viewByID state');
197195
});

‎src/io/state-file/schema.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,6 @@ export const ManifestSchema = z.object({
539539
viewByID: z.record(z.string(), View).optional(),
540540
primarySelection: z.string().optional(),
541541
layout: Layout.optional(),
542-
currentLayoutName: z.string().nullable().optional(),
543542
layoutSlots: z.array(z.string()).optional(),
544543
parentToLayers: ParentToLayers.optional(),
545544
layerDisplay: z.record(z.string(), LayerDisplayConfig).optional(),

‎src/io/state-file/serialize.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ export function normalizeManifest(manifest: Manifest, zip: JSZip) {
232232
'isActiveViewMaximized',
233233
'viewByID',
234234
'layout',
235-
'currentLayoutName',
236235
'layoutSlots',
237236
]);
238237
const invalidViewByID =
@@ -257,7 +256,6 @@ export function normalizeManifest(manifest: Manifest, zip: JSZip) {
257256
'viewByID',
258257
'primarySelection',
259258
'layout',
260-
'currentLayoutName',
261259
'layoutSlots',
262260
] as const;
263261
const optionalEntries = optionalRoots.flatMap((key) => {

‎src/store/views.ts‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ function generateLayoutFromGrid(size: [number, number]): Layout {
9797
};
9898
}
9999

100+
function sameLayout(a: Layout, b: Layout): boolean {
101+
return (
102+
a.direction === b.direction &&
103+
a.items.length === b.items.length &&
104+
a.items.every((item, index) => {
105+
const other = b.items[index];
106+
if (item.type === 'slot')
107+
return other.type === 'slot' && item.slotIndex === other.slotIndex;
108+
return other.type === 'layout' && sameLayout(item, other);
109+
})
110+
);
111+
}
112+
100113
function needsViewReplacement(existingView: ViewInfo, nextView: ViewInfoInit) {
101114
if (existingView.type !== nextView.type) return true;
102115

@@ -121,7 +134,6 @@ export const useViewStore = defineStore('view', () => {
121134

122135
const defaultNamedLayoutEntries = Object.entries(parsedDefaultLayouts);
123136
const firstLayout = defaultNamedLayoutEntries[0][1];
124-
const firstLayoutName = defaultNamedLayoutEntries[0][0];
125137

126138
const layout = ref<Layout>(firstLayout.layout);
127139
// which assigns view IDs to layout slots
@@ -133,7 +145,18 @@ export const useViewStore = defineStore('view', () => {
133145
ref<Record<string, { layout: Layout; views: ViewInfoInit[] }>>(
134146
parsedDefaultLayouts
135147
);
136-
const currentLayoutName = ref<Maybe<string>>(firstLayoutName);
148+
// A named layout is current when switching to it would only rename views.
149+
const currentLayoutName = computed(
150+
() =>
151+
Object.entries(namedLayouts.value).find(
152+
([, named]) =>
153+
sameLayout(named.layout, layout.value) &&
154+
named.views.every((viewInit, index) => {
155+
const view = viewByID[layoutSlots.value[index]];
156+
return view && !needsViewReplacement(view, viewInit);
157+
})
158+
)?.[0]
159+
);
137160

138161
const isActiveViewMaximized = ref(false);
139162
const maximizedView = computed(() => {
@@ -242,14 +265,9 @@ export const useViewStore = defineStore('view', () => {
242265
function applyLayoutChange(
243266
newLayout: Layout,
244267
options?: {
245-
layoutName?: Maybe<string>;
246268
updateSlots?: (viewCount: number) => void;
247269
}
248270
) {
249-
if (options?.layoutName !== undefined) {
250-
currentLayoutName.value = options.layoutName;
251-
}
252-
253271
isActiveViewMaximized.value = false;
254272

255273
if (options?.updateSlots) {
@@ -273,7 +291,6 @@ export const useViewStore = defineStore('view', () => {
273291
}
274292

275293
function setLayoutFromGrid(gridSize: [number, number]) {
276-
currentLayoutName.value = null;
277294
setLayout(generateLayoutFromGrid(gridSize));
278295
}
279296

@@ -287,7 +304,6 @@ export const useViewStore = defineStore('view', () => {
287304
throw new Error(`Named layout "${name}" not found`);
288305
}
289306
applyLayoutChange(namedLayout.layout, {
290-
layoutName: name,
291307
updateSlots: () => {
292308
namedLayout.views.forEach((viewInit, index) => {
293309
if (index < layoutSlots.value.length) {
@@ -359,7 +375,6 @@ export const useViewStore = defineStore('view', () => {
359375
function serialize(stateFile: StateFile) {
360376
const { manifest } = stateFile;
361377
manifest.layout = layout.value;
362-
manifest.currentLayoutName = currentLayoutName.value;
363378
manifest.activeView = activeView.value;
364379
manifest.isActiveViewMaximized = isActiveViewMaximized.value;
365380
manifest.layoutSlots = layoutSlots.value;
@@ -368,8 +383,6 @@ export const useViewStore = defineStore('view', () => {
368383

369384
function deserializeLayout(manifest: Manifest) {
370385
if (manifest.layout) {
371-
// Older scenes have unknown selection identity, even for matching panes.
372-
currentLayoutName.value = manifest.currentLayoutName;
373386
setLayout(manifest.layout);
374387
}
375388
setActiveView(manifest.activeView);

0 commit comments

Comments
 (0)