Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(ui): keep neighbouring architecture labels off each other's text

Bernhard Jackiewiczb504f0a0e85c
diff --git a/graph-ui/src/architecture/ArchitectureScene.tsx b/graph-ui/src/architecture/ArchitectureScene.tsx
index cfaf50bb8..f2823823e 100644
--- a/graph-ui/src/architecture/ArchitectureScene.tsx
+++ b/graph-ui/src/architecture/ArchitectureScene.tsx
@@ -6,6 +6,7 @@ import type { OrbitControls as OrbitControlsImpl } from 'three-stdlib';
import type { SemanticEdge, SemanticGraph, SemanticNode, SemanticPlatform } from './semantic-graph';
import { languageColor, measureSourceNode, sourceBrickHeight, sourceLanguage, sourceNodeSizePercent, type SourceCatalog, type SourceMeasure } from './source-metrics';
import { gravityPercent, gravityStrength, hotspotsForNode, type HotspotCatalog, type HotspotGroup } from './hotspot-map';
+import { labelEdges, labelInset, labelRect, labelsCollide, type LabelBox, type LabelRect } from './label-space';
import { edgeColor, isDirectedEdge } from '../graph/edge-style';
import { EdgePulseLayer } from '../graph/EdgePulseLayer';
import './architecture-scene.css';
@@ -191,16 +192,20 @@ function HotspotLabels({ model, priorityId, onVisible }: { model: RenderGraph; p
// Html labels mount where drei puts them: the event source, else the canvas parent.
const labelHost = (events.connected as HTMLElement | undefined) ?? gl.domElement.parentElement;
const previous = useRef('');
- const boxes = useRef(new Map<string, [number, number]>());
+ const boxes = useRef(new Map<string, LabelBox>());
const attempts = useRef(0);
useEffect(() => { boxes.current.clear(); attempts.current = 0; }, [model]);
useFrame(() => {
const unmeasured = () => model.nodes.some(node => !boxes.current.has(node.id));
if (unmeasured()) labelHost?.querySelectorAll<HTMLElement>('.architecture-node-label[data-node-id]').forEach(label => {
// A selected label carries extra rows; it is the priority label and keeps its reserve below.
- if (label.offsetWidth && !label.classList.contains('is-selected')) boxes.current.set(label.dataset.nodeId!, [label.offsetWidth, label.offsetHeight]);
+ if (!label.offsetWidth || label.classList.contains('is-selected')) return;
+ const [edgeX, edgeY] = labelEdges(getComputedStyle(label));
+ boxes.current.set(label.dataset.nodeId!, { width: label.offsetWidth, height: label.offsetHeight, edgeX, edgeY });
});
- const occupied: { left: number; right: number; top: number; bottom: number }[] = [];
+ // A measured label may overlap a neighbour by its padding, never by its text.
+ const inset = labelInset(boxes.current.values());
+ const occupied: LabelRect[] = [];
const ids = new Set<string>();
// Hotspot wells, then larger parts, claim their label space first.
const ordered = [...model.nodes].sort((a, b) => Number(b.id === priorityId) - Number(a.id === priorityId)
@@ -209,13 +214,11 @@ function HotspotLabels({ model, priorityId, onVisible }: { model: RenderGraph; p
const point = new Vector3(...node.position).add(new Vector3(0, dimensions(node)[1] + 7, 0)).project(camera);
const x = (point.x + 1) * size.width / 2; const y = (1 - point.y) * size.height / 2;
const box = boxes.current.get(node.id);
- const width = box?.[0] ?? Math.min(178, Math.max(78, node.label.length * 7 + (node.gravityPercent === undefined ? 26 : 68)));
- const halfHeight = node.id === priorityId ? 48 : (box?.[1] ?? 38) / 2;
- // A measured label may overlap a neighbour by its padding, never by its text.
- const [insetX, insetY] = box ? [6, 4] : [-5, 0];
- const rect = { left: x - width / 2 + insetX, right: x + width / 2 - insetX, top: y - halfHeight + insetY, bottom: y + halfHeight - insetY };
+ const width = box?.width ?? Math.min(178, Math.max(78, node.label.length * 7 + (node.gravityPercent === undefined ? 26 : 68)));
+ const height = node.id === priorityId ? 96 : box?.height ?? 38;
+ const rect = labelRect(x, y, width, height, box ? inset : [-5, 0]);
if (node.id !== priorityId && (rect.right < 0 || rect.left > size.width || rect.bottom < 0 || rect.top > size.height
- || occupied.some(other => rect.left < other.right && rect.right > other.left && rect.top < other.bottom && rect.bottom > other.top))) continue;
+ || occupied.some(other => labelsCollide(rect, other)))) continue;
ids.add(node.id); occupied.push(rect);
}
const key = [...ids].sort().join('|');
diff --git a/graph-ui/src/architecture/label-space.test.ts b/graph-ui/src/architecture/label-space.test.ts
new file mode 100644
index 000000000..517149b86
--- /dev/null
+++ b/graph-ui/src/architecture/label-space.test.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from 'vitest';
+import { labelEdges, labelInset, labelRect, labelsCollide, type LabelBox } from './label-space';
+
+// The compact Overview and Endpoints label: padding 5px 7px and a 1px border (spatial-architecture.css).
+const compact: LabelBox = { width: 100, height: 24, edgeX: 8, edgeY: 6 };
+const style = (values: Record<string, string>) => ({ getPropertyValue: (name: string) => values[name] ?? '' });
+
+describe('label space', () => {
+ it('reads padding plus border on the thinner side of each axis', () => {
+ expect(labelEdges(style({ 'padding-left': '7px', 'padding-right': '9px', 'border-left-width': '1px', 'border-right-width': '1px',
+ 'padding-top': '5px', 'padding-bottom': '5px', 'border-top-width': '1px', 'border-bottom-width': '0px' }))).toEqual([8, 5]);
+ expect(labelEdges(style({}))).toEqual([0, 0]);
+ });
+ it('lets two neighbours share their padding but never reach into either text', () => {
+ const inset = labelInset([compact, compact]);
+ expect(inset).toEqual([4, 3]);
+ const left = labelRect(100, 50, compact.width, compact.height, inset);
+ // The boxes overlap by exactly one label's padding plus border: the neighbour's edge stops where the text begins.
+ const touching = labelRect(100 + compact.width - compact.edgeX, 50, compact.width, compact.height, inset);
+ expect(labelsCollide(left, touching)).toBe(false);
+ expect(labelsCollide(left, labelRect(100 + compact.width - compact.edgeX - 1, 50, compact.width, compact.height, inset))).toBe(true);
+ const below = labelRect(100, 50 + compact.height - compact.edgeY, compact.width, compact.height, inset);
+ expect(labelsCollide(left, below)).toBe(false);
+ expect(labelsCollide(left, labelRect(100, 50 + compact.height - compact.edgeY - 1, compact.width, compact.height, inset))).toBe(true);
+ });
+ it('takes the thinnest padding when labels differ and keeps a margin around an estimate', () => {
+ expect(labelInset([compact, { width: 145, height: 30, edgeX: 13, edgeY: 8 }])).toEqual([4, 3]);
+ expect(labelInset([])).toEqual([0, 0]);
+ expect(labelRect(100, 50, 80, 38, [-5, 0])).toEqual({ left: 55, right: 145, top: 31, bottom: 69 });
+ });
+});
diff --git a/graph-ui/src/architecture/label-space.ts b/graph-ui/src/architecture/label-space.ts
new file mode 100644
index 000000000..c55213909
--- /dev/null
+++ b/graph-ui/src/architecture/label-space.ts
@@ -0,0 +1,30 @@
+/** Screen space in canvas pixels. */
+export interface LabelRect { left: number; right: number; top: number; bottom: number }
+/** A label laid out in the DOM: its size, and the padding plus border between its edge and its text. */
+export interface LabelBox { width: number; height: number; edgeX: number; edgeY: number }
+
+/** The padding plus border of a laid-out label on its thinner side, per axis. */
+export function labelEdges(style: Pick<CSSStyleDeclaration, 'getPropertyValue'>): [number, number] {
+ const side = (name: string) => (parseFloat(style.getPropertyValue(`padding-${name}`)) || 0)
+ + (parseFloat(style.getPropertyValue(`border-${name}-width`)) || 0);
+ return [Math.min(side('left'), side('right')), Math.min(side('top'), side('bottom'))];
+}
+
+/**
+ * How far a measured label may reach under a neighbour: half of the thinnest
+ * padding plus border among the labels, so two neighbours together never
+ * reach the text of either.
+ */
+export function labelInset(boxes: Iterable<LabelBox>): [number, number] {
+ let x = Infinity, y = Infinity;
+ for (const box of boxes) { x = Math.min(x, box.edgeX); y = Math.min(y, box.edgeY); }
+ return [Number.isFinite(x) ? x / 2 : 0, Number.isFinite(y) ? y / 2 : 0];
+}
+
+/** The space a label centred at (x, y) claims; a negative inset keeps a margin around an estimate. */
+export function labelRect(x: number, y: number, width: number, height: number, [insetX, insetY]: [number, number]): LabelRect {
+ return { left: x - width / 2 + insetX, right: x + width / 2 - insetX, top: y - height / 2 + insetY, bottom: y + height / 2 - insetY };
+}
+
+export const labelsCollide = (a: LabelRect, b: LabelRect): boolean =>
+ a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;