diff --git a/src/layer/index.spec.ts b/src/layer/index.spec.ts new file mode 100644 index 0000000000..b71cb5d85b --- /dev/null +++ b/src/layer/index.spec.ts @@ -0,0 +1,117 @@ +/** + * @license + * Copyright 2026 Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { + CoordinateSpaceCombiner, + TrackableCoordinateSpace, +} from "#src/coordinate_transform.js"; +import type { LayerListSpecification } from "#src/layer/index.js"; +import { + LayerManager, + LayerSelectedValues, + ManagedUserLayer, + MouseSelectionState, + TrackableDataSelectionState, + UserLayer, +} from "#src/layer/index.js"; +import { GlobalToolBinder } from "#src/ui/tool.js"; +import type { MultiToolPaletteState } from "#src/ui/tool_palette.js"; +import { RefCounted } from "#src/util/disposable.js"; +import { NullarySignal } from "#src/util/signal.js"; + +function makeSelectionFixture() { + const scope = new RefCounted(); + const toolBinder = scope.registerDisposer( + new GlobalToolBinder(() => {}, {} as MultiToolPaletteState), + ); + const manager = scope.registerDisposer(new LayerManager()); + scope.registerDisposer(manager.useDirectly()); + const coordinateSpace = new TrackableCoordinateSpace(); + const specification = { + rootLayers: manager, + root: { + coordinateSpaceCombiner: new CoordinateSpaceCombiner( + coordinateSpace, + () => true, + ), + display: { updateStarted: new NullarySignal(), scheduleRedraw: vi.fn() }, + toolBinder, + }, + } as unknown as LayerListSpecification; + const mouse = new MouseSelectionState(); + mouse.setActive(true); + const selected = scope.registerDisposer( + new LayerSelectedValues(manager, mouse), + ); + const persistent = scope.registerDisposer( + new TrackableDataSelectionState(coordinateSpace, selected), + ); + + function createLayer(name: string) { + const managed = new ManagedUserLayer(name, specification); + const layer = new UserLayer(managed); + managed.layer = layer; + manager.addManagedLayer(managed); + return layer; + } + + return { scope, selected, persistent, createLayer }; +} + +describe("selection of layers added before initialization", () => { + let fixture: ReturnType; + + beforeEach(() => { + vi.useFakeTimers(); + fixture = makeSelectionFixture(); + }); + + afterEach(() => { + fixture.scope.dispose(); + vi.clearAllTimers(); + vi.useRealTimers(); + }); + + it("omits an uninitialized layer from selection reads and serialization", () => { + const layer = fixture.createLayer("layer"); + + expect(fixture.selected.get(layer)).toBeUndefined(); + expect(fixture.selected.toJSON()).toEqual({}); + + layer.initializationDone(); + + expect(fixture.selected.get(layer)?.localPositionValid).toBe(true); + expect(fixture.selected.toJSON()).toEqual({ layer: {} }); + }); + + it("resamples live selection after initialization without mouse movement", () => { + fixture.persistent.unpin(); + vi.runAllTimers(); + + const layer = fixture.createLayer("layer"); + vi.runAllTimers(); + + expect(fixture.persistent.value?.layers).toEqual([]); + layer.initializationDone(); + vi.runAllTimers(); + + expect( + fixture.persistent.value?.layers.map((entry) => entry.layer), + ).toEqual([layer]); + }); +}); diff --git a/src/layer/index.ts b/src/layer/index.ts index 50ab94260d..bc2ce37db8 100644 --- a/src/layer/index.ts +++ b/src/layer/index.ts @@ -481,6 +481,8 @@ export class UserLayer extends RefCounted { const selectionState = (this.selectionState = {} as any); this.initializeSelectionState(selectionState); this.decrementLoadingCounter(); + // Layers can be published before initialization, including while sources load. + this.layersChanged.dispatch(); } getLegacyDataSourceSpecifications( @@ -1250,6 +1252,7 @@ export class LayerSelectedValues extends RefCounted { const userLayer = layer.layer; if (layer.visible && userLayer !== null) { const { selectionState } = userLayer; + if (selectionState === undefined) continue; userLayer.resetSelectionState(selectionState); selectionState.generation = generation; userLayer.captureSelectionState(selectionState, mouseState); @@ -1261,7 +1264,7 @@ export class LayerSelectedValues extends RefCounted { get(userLayer: T): T["selectionState"] | undefined { this.update(); const { selectionState } = userLayer; - if (selectionState.generation !== this.changed.count) return undefined; + if (selectionState?.generation !== this.changed.count) return undefined; return selectionState; } diff --git a/src/ui/skeleton_tab.css b/src/ui/skeleton_tab.css index cbb01eb9d3..ee42e7b2e3 100644 --- a/src/ui/skeleton_tab.css +++ b/src/ui/skeleton_tab.css @@ -80,6 +80,10 @@ overflow-y: auto; } +.neuroglancer-skeleton-queue-tab { + overflow-y: auto; +} + .neuroglancer-skeleton-section { --neuroglancer-skeleton-type-width: 21px; border: 1px solid #2f2f2f;