A truly modular keyboard
C
app: chained Keybits named by a short UID find their parent, HELLO asked again until answered, the latest ⇄ choice wins
Nathanb62bfab2c923
diff --git a/app/src/renderer/src/components/Board.tsx b/app/src/renderer/src/components/Board.tsxindex 129a30b..be36b1a 100644--- a/app/src/renderer/src/components/Board.tsx+++ b/app/src/renderer/src/components/Board.tsx@@ -34,7 +34,10 @@ export function Board() {const flip = (uid: string, now: Side) => {const to = otherSide(now);if (!to) return;- const next = { ...prefer, [uid]: to };+ // Latest choice last: layout() lets it win a clash with an older one.+ const next = { ...prefer };+ delete next[uid];+ next[uid] = to;setPrefer(next);try { localStorage.setItem(PREFER_KEY, JSON.stringify(next)); } catch { /* only a convenience */ }};diff --git a/app/src/renderer/src/controller.ts b/app/src/renderer/src/controller.tsindex cc0e35e..5b6f151 100644--- a/app/src/renderer/src/controller.ts+++ b/app/src/renderer/src/controller.ts@@ -201,7 +201,17 @@ function addKey(pside: where.pside ?? known?.pside ?? null,backwards: known?.backwards ?? null,};- set((st) => ({ keys: { ...st.keys, [uid]: next } }));+ set((st) => {+ const keys = { ...st.keys, [uid]: next };+ // Chained Keybits listed before this one only had its short UID.+ if (!known && uid.length === 24) {+ const short = uid.slice(-8);+ for (const [id, k] of Object.entries(keys))+ if (k.via === short || k.parent === short)+ keys[id] = { ...k, via: k.via === short ? uid : k.via, parent: k.parent === short ? uid : k.parent };+ }+ return { keys };+ });if (!known) {const where = at ? ` on the ${SIDE_NAMES[at]}` : relay?.via ? ` behind ${keyName(resolveUid(relay.via))}` : '';activity(`${keyName(uid)} connected${where}`, 'add');@@ -461,7 +471,8 @@ async function connect(t: Transport, { auto = false } = {}): Promise<boolean> {syncKeys();timers = [setInterval(() => {- void send(cmd.sides());+ // Ask again until it answers: a HELLO sent as the port opens can be lost.+ void (get().device.fw ? send(cmd.sides()) : send(cmd.hello(), cmd.sides()));if (Date.now() - lastRx > STALE_MS) setStatus(`Connected over ${t.label} · no reply from the Keyword`, 'stale');}, POLL_MS),setInterval(syncKeys, KEYS_POLL_MS),diff --git a/app/src/renderer/src/lib/layout.ts b/app/src/renderer/src/lib/layout.tsindex b11633e..94b92e3 100644--- a/app/src/renderer/src/lib/layout.ts+++ b/app/src/renderer/src/lib/layout.ts@@ -146,14 +146,17 @@ function place(* way it's turned, so it goes exactly there. Older ones are estimated (dashed).*/export function layout(- keys: Record<string, LayoutKey>,+ rawKeys: Record<string, LayoutKey>,sides: Partial<Record<Side, boolean>>,- /** The side the user put an L/T Keybit on, by UID. */+ /** The side the user put an L/T Keybit on, by UID, oldest choice first. */prefer: Record<string, Side> = {},): Placed[] {+ const keys = resolveShort(rawKeys);const direct = Object.entries(keys).filter(([, k]) => k.at).sort(([a], [b]) => a.localeCompare(b));- // The user's choices first, so they win their side.- direct.sort(([a], [b]) => Number(!!prefer[b]) - Number(!!prefer[a]));+ // The user's choices first, the latest first of all, so it wins its side+ // over an older one that wants the same.+ const order = Object.keys(prefer);+ direct.sort(([a], [b]) => order.indexOf(b) - order.indexOf(a));// Every way the Keybits that could be on the left or the top can be// shared out (at most 2^3), the user's way first; keep the first one@@ -194,6 +197,23 @@ export function layout(return out;}+/**+ * A chained Keybit is reported with the low 32 bits (8 hex digits) of the+ * Keybit it came through and hangs off. If that one is known by its full+ * UID, point at it, or the chained one has nowhere to go.+ */+export function resolveShort(keys: Record<string, LayoutKey>): Record<string, LayoutKey> {+ const ids = Object.keys(keys);+ const full = (u: string | null | undefined) => {+ if (!u || keys[u]) return u;+ const m = ids.filter((id) => id.length > u.length && id.endsWith(u));+ return m.length === 1 ? m[0] : u;+ };+ const out: Record<string, LayoutKey> = {};+ for (const [id, k] of Object.entries(keys)) out[id] = { ...k, via: full(k.via) ?? null, parent: full(k.parent) ?? k.parent };+ return out;+}+/** The smallest box around everything placed. */export function bounds(items: Placed[]) {const x = Math.min(...items.map((i) => i.x));diff --git a/app/test/layout.test.ts b/app/test/layout.test.tsindex c7ae9b3..ff0993e 100644--- a/app/test/layout.test.ts+++ b/app/test/layout.test.ts@@ -83,3 +83,32 @@ test('a turned Keybit on top is placed by its chain, not by a guess', () => {}, {});assert.ok(!at(items, 'C')!.estimated);});++test('chained Keybits named by their short UID still hang off the right one', () => {+ // The Keyword lists chained Keybits with the low 32 bits of the one they+ // came through; that one is known by its full UID.+ const T = 'FFFFFFFF7E8BBD5D1561ABCD';+ const items = layout({+ [T]: { at: 'T', via: null, depth: null, face: 'S' },+ '1560ABCD': { at: null, via: '1561ABCD', depth: 2, parent: '1561ABCD', pside: 'N', face: 'S' },+ '156AABCD': { at: null, via: '1561ABCD', depth: 3, parent: '1560ABCD', pside: 'N', face: 'S' },+ }, {});+ assert.deepStrictEqual(cell(items, '1560ABCD'), [0, -2]);+ assert.deepStrictEqual(cell(items, '156AABCD'), [0, -3]);+ assert.ok(!at(items, '1560ABCD')!.estimated && !at(items, '156AABCD')!.estimated);+});++test('a side saved for a Keybit that now reports the right is ignored', () => {+ const items = layout({+ R: { at: 'R', via: null, depth: null, face: 'W' },+ L: { at: 'L', via: null, depth: null, face: 'E' },+ }, {}, { R: 'T', L: 'T' });+ assert.deepStrictEqual(cell(items, 'R'), [1, 0]);+ assert.deepStrictEqual(cell(items, 'L'), [0, -1]);+});++test('two saved sides that clash: the latest choice wins', () => {+ const keys = { A: { at: 'T' as const, via: null, depth: null, face: 'S' as const }, B: { at: 'T' as const, via: null, depth: null, face: 'S' as const } };+ assert.deepStrictEqual(cell(layout(keys, {}, { A: 'T', B: 'T' }), 'B'), [0, -1]);+ assert.deepStrictEqual(cell(layout(keys, {}, { B: 'T', A: 'T' }), 'A'), [0, -1]);+});