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
117 changes: 117 additions & 0 deletions src/layer/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof makeSelectionFixture>;

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]);
});
});
5 changes: 4 additions & 1 deletion src/layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -1261,7 +1264,7 @@ export class LayerSelectedValues extends RefCounted {
get<T extends UserLayer>(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;
}

Expand Down
4 changes: 4 additions & 0 deletions src/ui/skeleton_tab.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading