From ad8f6897e0e7078819beeda7811a246039b16414 Mon Sep 17 00:00:00 2001 From: "Fabian @ Blax Software" Date: Fri, 23 Oct 2026 15:34:36 +0200 Subject: [PATCH] A latch parts onto each other: pins touching, no wires, moving together A part whose pins line up one for one with the pins it is wired to can sit straight on them, like a display on a CPU's outputs or a switch on a component's input. Drag it close (green rings show where) or choose Latch onto in its menu or settings. It then moves with that part; drag it away or Unlatch to let go. Parts whose shared pins already touch latch by themselves, the smaller onto the larger. A pin that would touch another without a wire keeps it from latching. A clamp marks the joint. Co-Authored-By: Claude Opus 5.5 (1M context) --- app/assets/css/main.css | 17 +++ app/components/editor/Canvas.vue | 37 ++++- app/components/editor/Help.vue | 1 + app/components/editor/NodeSettings.vue | 12 ++ app/composables/useEditor.ts | 39 +++++- app/composables/useMenu.ts | 10 ++ app/lib/logic/latch.ts | 178 +++++++++++++++++++++++++ app/lib/logic/ops.ts | 15 ++- app/lib/logic/project.ts | 1 + app/lib/logic/routing.ts | 4 +- app/lib/logic/types.ts | 2 + tests/latch.test.ts | 85 ++++++++++++ 12 files changed, 387 insertions(+), 14 deletions(-) create mode 100644 app/lib/logic/latch.ts create mode 100644 tests/latch.test.ts diff --git a/app/assets/css/main.css b/app/assets/css/main.css index 9c56072..690fa0b 100644 --- a/app/assets/css/main.css +++ b/app/assets/css/main.css @@ -5416,3 +5416,20 @@ button.tile:hover { background: var(--danger); opacity: 1; } + +/* A part latched onto another: a contact strip along the joint, a clamp at each end. */ +.part-latch { + pointer-events: none; +} + +.part-latch .contact { + stroke: #3a4554; + stroke-width: 2.5; + stroke-linecap: round; +} + +.part-latch rect { + fill: #7a8698; + stroke: #10151c; + stroke-width: 1; +} diff --git a/app/components/editor/Canvas.vue b/app/components/editor/Canvas.vue index 1328bc4..c0f3232 100644 --- a/app/components/editor/Canvas.vue +++ b/app/components/editor/Canvas.vue @@ -9,6 +9,7 @@ import { seriesLinks, type Link } from '~/lib/logic/bulk' import { dropLinks, dropPins, fitStart, type DropPin } from '~/lib/logic/fit' import type { CanvasOps, MenuEntry } from '~/composables/useMenu' import { LANE, plugLayout, ribbon, RIBBON_MAX } from '~/lib/logic/cables' +import { latchLayout, latchSeam } from '~/lib/logic/latch' import { wirePath, type HopSpan } from '~/lib/draw' import { downloadSheet, printSheet, sheetSvg } from '~/lib/sheet' import { inputCount } from '~/lib/logic/wide' @@ -916,7 +917,8 @@ function onPointerUp(e: PointerEvent) { if (d.row && !d.moved) ed.pressRow(d.node, d.row, false) if (d.moved && d.plug) { ed.unplugMoved(new Set(d.orig.keys())) - ed.plugEnd(d.node) + if (d.node.kind === 'bundle' || d.node.kind === 'split') ed.plugEnd(d.node) + else ed.latchPart(d.node) } else if (d.moved && d.snap?.links.length) { const [sx, sy] = d.snap.shift for (const id of d.orig.keys()) { @@ -1420,10 +1422,25 @@ function openMenuForSelection() { /** * A free cable end dragged to within a square or so of where it would sit - * on its part's pins: the pins it would plug into. + * on its part's pins: the pins it would plug into. Any other part brought as + * close to where it would latch onto the part it is wired to: the pins it + * would latch onto. */ function plugNear(n: CircuitNode): { part: string; rings: Pt[] } | null { - if ((n.kind !== 'bundle' && n.kind !== 'split') || n.plug) return null + if (n.kind !== 'bundle' && n.kind !== 'split') { + const host = ed.latchTargetOf({ ...n, latch: undefined }) + const lay = host && latchLayout(def.value, n, host, ed.getDef.value) + // Not onto a part that is latched onto this one (it moves along anyway). + if (!host || !lay || ed.withFollowers([n.id]).has(host.id) || Math.hypot(n.x - lay.x, n.y - lay.y) > 1.6) return null + const s = shapes.value.get(n.id)! + const shared = def.value.wires.filter(w => (w.from.node === host.id && w.to.node === n.id) || (w.from.node === n.id && w.to.node === host.id)) + const rings = shared + .map(w => (w.to.node === n.id ? s.inputs.find(p => p.key === w.to.pin) : s.outputs.find(p => p.key === w.from.pin))) + .filter(p => !!p) + .map(p => [lay.x + p.dx, lay.y + p.dy] as Pt) + return { part: host.id, rings } + } + if (n.plug) return null const part = ed.plugTarget(n) const lay = part && plugLayout(def.value, n, part, ed.getDef.value) if (!part || !lay || Math.hypot(n.x - lay.x, n.y - lay.y) > 1.6) return null @@ -1431,6 +1448,14 @@ function plugNear(n: CircuitNode): { part: string; rings: Pt[] } | null { return { part: part.id, rings: lay.offsets.map(o => [x, lay.y + o] as Pt) } } +/** Where latched parts touch their part: a contact strip along the joint, held by a clamp at each end. */ +const latchMarks = computed(() => + def.value.nodes.flatMap(n => { + const seam = n.latch ? latchSeam(def.value, n, ed.getDef.value) : null + return seam ? [{ id: n.id, x: n.x + seam.x, y0: n.y + seam.y0, y1: n.y + seam.y1 }] : [] + }), +) + /** Pins a dragged part will connect to when dropped, shown as green rings. */ const dropRings = computed(() => { const g = ghostLanding.value @@ -1603,6 +1628,12 @@ defineExpose({ fit, zoomAt }) :open="openPins.get(n.id)" /> + + + + + + n.value.kind === 'bundle' || n.value.kind === 'split') +/** The part this one fits straight onto (its pins line up with the ones it is wired to). */ +const latchHost = computed(() => (isCableEnd.value ? undefined : ed.latchTargetOf(n.value))) const plugPart = computed(() => (isCableEnd.value && !n.value.plug ? ed.plugTarget(n.value) : undefined)) const pitch = computed(() => { const o = cableOffsets(n.value) @@ -601,6 +603,16 @@ function setRowHz(row: PanelRow, e: Event) { + +

+ + Latched onto {{ partName(n.latch) }}: it moves with it + +

+ +