diff --git a/src/api/focus.js b/src/api/focus.js index 7e4f3a98c..726b5c056 100644 --- a/src/api/focus.js +++ b/src/api/focus.js @@ -26,6 +26,7 @@ import Colormap from "./focus/colormap"; import Macros from "./focus/macros"; import Keymap, { OnlyCustom } from "./focus/keymap"; import LayerNames from "./focus/layernames"; +import CustomKeyLabels from "./focus/custom-key-labels"; global.chrysalis_focus_instance = null; @@ -564,6 +565,7 @@ const focus = new Focus(); focus.addCommands({ colormap: new Colormap() }); focus.addMethod("setLayerSize", "colormap"); focus.addCommands({ layernames: new LayerNames() }); +focus.addCommands({ "keymap.labels": new CustomKeyLabels() }); focus.addCommands({ macros: new Macros() }); focus.addCommands({ keymap: new Keymap(), diff --git a/src/api/focus/custom-key-labels.js b/src/api/focus/custom-key-labels.js new file mode 100644 index 000000000..452f5fba6 --- /dev/null +++ b/src/api/focus/custom-key-labels.js @@ -0,0 +1,69 @@ +/* Chrysalis -- Kaleidoscope Command Center + * Copyright (C) 2022 Keyboardio, Inc. + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +global.chrysalis_custom_key_labels_instance = null; + +export default class CustomKeyLabels { + constructor(opts) { + if (!global.chrysalis_custom_key_labels_instance) { + global.chrysalis_custom_key_labels_instance = this; + } + + return global.chrysalis_custom_key_labels_instance; + } + + async _pull(s) { + const data = await s.request("keymap.labels"); + if (!data) return { storageSize: 0, labels: [] }; + + const labelMap = data + .split(/\n/) + .map((x) => x.split(/^(\d+) (\d+) /).slice(1)); + const storageSize = labelMap.pop()[2].split(/=/).pop(); + + return { + storageSize: storageSize, + labels: labelMap.map(([_, code, label]) => { + let l; + try { + l = JSON.parse(label); + } catch (e) { + console.log("error parsing:", label); + } + return { + code: code, + label: l, + }; + }), + }; + } + + async _push(s, data) { + /* + const serialized = + data.names.flatMap((n) => [n.length, n]).join(" ") + " 0"; + await s.request("keymap.layerNames", serialized); + */ + } + + async focus(s, data) { + if (!data) { + return this._pull(s); + } + + return this._push(s, data); + } +} diff --git a/src/api/focus/keymap/db.js b/src/api/focus/keymap/db.js index 31aedd9a5..58e0831be 100644 --- a/src/api/focus/keymap/db.js +++ b/src/api/focus/keymap/db.js @@ -111,6 +111,17 @@ class KeymapDB { } } + setCustomLabels(customLabels) { + for (const key of customLabels) { + if (this._codetable[key.code]) { + const base = this._codetable[key.code]; + this._codetable[key.code].label = Object.assign({}, key.label); + } else { + this._codetable[key.code] = Object.assign({}, key); + } + } + } + setLayout(layout) { this.resetLayout(); @@ -299,7 +310,11 @@ class KeymapDB { let label = key.label.base; const shifted = key.label.shifted; if (typeof label != "string") { - label = key.label.base[keycapSize] || key.label.base.full; + try { + label = key.label.base[keycapSize] || key.label.base.full; + } catch (_) { + console.log("ZING", key); + } } if (label.length == 1 && autoCase) { label = label.toUpperCase(); diff --git a/src/renderer/screens/Editor/Editor.js b/src/renderer/screens/Editor/Editor.js index 3baf77dce..4818d487e 100644 --- a/src/renderer/screens/Editor/Editor.js +++ b/src/renderer/screens/Editor/Editor.js @@ -272,6 +272,9 @@ const Editor = (props) => { setKeymap(deviceKeymap); setColormap(deviceColormap); + const customLabels = await focus.command("keymap.labels"); + k.db.setCustomLabels(customLabels.labels); + const deviceMacros = await focus.command("macros"); setMacros(deviceMacros);