Skip to content

Commit 730e6d4

Browse files
authored
Merge pull request #74 from theld21/fix/keymap-crash-optimization
fix keymap: improve stability and defaults, add loading overlay
2 parents aba7eb5 + 201073e commit 730e6d4

File tree

14 files changed

+513
-69
lines changed

14 files changed

+513
-69
lines changed

src/main/saveConfig.ts

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,75 @@ import { codepy } from './pythontemplates/code'
88
import { bootpy } from './pythontemplates/boot'
99
import { pog_serialpy } from './pythontemplates/pog_serial'
1010
import { keymappy } from './pythontemplates/keymap'
11-
import { writePogConfViaSerial } from './index'
11+
import { writePogConfViaSerial, mainWindow } from './index'
1212

13-
export const saveConfiguration = (data: string) => {
13+
export const saveConfiguration = async (data: string) => {
1414
const { pogConfig, serial, writeFirmware } = JSON.parse(data)
1515
if (serial) {
1616
// write by serial to current keyboard
1717
console.log('writing firmware via usb serial')
1818
writePogConfViaSerial(JSON.stringify(pogConfig, null, 0))
19-
} else {
20-
// write pog.json
21-
console.log('writing firmware via usb files', 'overwriting Firmware:', writeFirmware)
22-
fs.writeFile(currentKeyboard.path + '/pog.json', JSON.stringify(pogConfig, null, 4), (e) => {
23-
if (e) {
24-
console.log('error writing pog.json', e)
25-
} else {
26-
console.log('pog.json written successfully')
27-
}
28-
})
29-
30-
const files = [
31-
{ name: 'pog.py', contents: pogpy },
32-
{ name: 'code.py', contents: codepy },
33-
{ name: 'coordmaphelper.py', contents: coordmaphelperpy },
34-
{ name: 'customkeys.py', contents: customkeyspy },
35-
{ name: 'boot.py', contents: bootpy },
36-
{ name: 'pog_serial.py', contents: pog_serialpy },
37-
{ name: 'keymap.py', contents: keymappy },
38-
{ name: 'kb.py', contents: kbpy }
39-
]
40-
for (const file of files) {
41-
if (!fs.existsSync(currentKeyboard.path + '/' + file.name) || writeFirmware) {
42-
fs.writeFile(currentKeyboard.path + '/' + file.name, file.contents, () => {
43-
console.log(file.name + 'File written successfully')
44-
})
45-
}
19+
return
20+
}
21+
22+
// write via mounted USB drive
23+
console.log('writing firmware via usb files', 'overwriting Firmware:', writeFirmware)
24+
25+
type WriteTask = { name: string; path: string; contents: string }
26+
const tasks: WriteTask[] = []
27+
28+
// Always write pog.json
29+
tasks.push({
30+
name: 'pog.json',
31+
path: currentKeyboard.path + '/pog.json',
32+
contents: JSON.stringify(pogConfig, null, 4)
33+
})
34+
35+
const files = [
36+
{ name: 'pog.py', contents: pogpy },
37+
{ name: 'code.py', contents: codepy },
38+
{ name: 'coordmaphelper.py', contents: coordmaphelperpy },
39+
{ name: 'customkeys.py', contents: customkeyspy },
40+
{ name: 'boot.py', contents: bootpy },
41+
{ name: 'pog_serial.py', contents: pog_serialpy },
42+
{ name: 'keymap.py', contents: keymappy },
43+
{ name: 'kb.py', contents: kbpy }
44+
]
45+
46+
for (const file of files) {
47+
const targetPath = currentKeyboard.path + '/' + file.name
48+
if (!fs.existsSync(targetPath) || writeFirmware) {
49+
tasks.push({ name: file.name, path: targetPath, contents: file.contents })
4650
}
4751
}
52+
53+
const total = tasks.length
54+
let completed = 0
55+
56+
for (const task of tasks) {
57+
try {
58+
await fs.promises.writeFile(task.path, task.contents)
59+
completed += 1
60+
mainWindow?.webContents.send('save-configuration-progress', {
61+
state: 'writing',
62+
filename: task.name,
63+
completed,
64+
total
65+
})
66+
} catch (e) {
67+
console.error(`error writing ${task.name}`, e)
68+
mainWindow?.webContents.send('save-configuration-progress', {
69+
state: 'error',
70+
filename: task.name,
71+
completed,
72+
total
73+
})
74+
}
75+
}
76+
77+
mainWindow?.webContents.send('save-configuration-progress', {
78+
state: 'done',
79+
completed,
80+
total
81+
})
4882
}

src/preload/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export interface IElectronAPI {
4343
serialConnect: (port: string) => Promise<void>
4444
serialDisconnect: () => Promise<void>
4545
serialData: (callback: (event: any, data: { message: string }) => void) => void
46+
offSerialData: (callback: (event: any, data: { message: string }) => void) => void
4647
serialConnectionStatus: (
4748
callback: (event: any, data: { connected: boolean; error?: string }) => void
4849
) => void
@@ -60,6 +61,12 @@ export interface IElectronAPI {
6061
onDetectionUpdate: (callback: (data: any, event: any) => void) => void
6162
removeDetectionListeners: () => void
6263
onUpdateFirmwareInstallProgress: (callback: (data: any, event: any) => void) => void
64+
onSaveConfigurationProgress: (
65+
callback: (event: any, data: { state: 'writing' | 'done' | 'error'; filename?: string; completed: number; total: number }) => void
66+
) => void
67+
offSaveConfigurationProgress: (
68+
callback: (event: any, data: { state: 'writing' | 'done' | 'error'; filename?: string; completed: number; total: number }) => void
69+
) => void
6370

6471
// Legacy API (to be migrated)
6572
selectKeyboard: (data: any) => Promise<any>

src/preload/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const api = {
99
selectKeyboard: (data) => ipcRenderer.invoke('selectKeyboard', data),
1010
onUpdateFirmwareInstallProgress: (callback) =>
1111
ipcRenderer.on('onUpdateFirmwareInstallProgress', callback),
12+
onSaveConfigurationProgress: (callback) =>
13+
ipcRenderer.on('save-configuration-progress', callback),
14+
offSaveConfigurationProgress: (callback) =>
15+
ipcRenderer.removeListener('save-configuration-progress', callback),
1216
keyboardScan: (callback) => {
1317
ipcRenderer.on('keyboardScan', callback)
1418
},
@@ -19,6 +23,7 @@ const api = {
1923
checkForUSBKeyboards: (data) => ipcRenderer.invoke('checkForUSBKeyboards', data),
2024
deselectKeyboard: () => ipcRenderer.invoke('deselectKeyboard'),
2125
serialData: (callback) => ipcRenderer.on('serialData', callback),
26+
offSerialData: (callback) => ipcRenderer.removeListener('serialData', callback),
2227
serialConnectionStatus: (callback) => ipcRenderer.on('serialConnectionStatus', callback),
2328
serialPorts: () => ipcRenderer.invoke('serial-ports'),
2429
serialSend: (data) => ipcRenderer.send('serialSend', data),

src/renderer/src/App.vue

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup lang="ts">
2-
import { computed } from 'vue'
2+
import { computed, onMounted, onUnmounted } from 'vue'
33
import { addToHistory, keyboardStore, notifications, serialKeyboards } from './store'
4+
import { addSerialLine } from './store/serial'
45
import { useRouter } from 'vue-router'
56
const router = useRouter()
67
const store = computed(() => {
@@ -29,7 +30,17 @@ window.api.serialKeyboardPogConfig((_event: Event, value: { pogconfig }) => {
2930
if (keyboardStore.pogConfigured) {
3031
addToHistory(keyboardStore)
3132
}
32-
router.push('/configurator')
33+
router.push('/configurator/keymap')
34+
})
35+
36+
let serialHandler: ((event: any, data: { message: string }) => void) | null = null
37+
onMounted(() => {
38+
serialHandler = (_event, data) => addSerialLine(data.message)
39+
window.api.serialData(serialHandler)
40+
})
41+
onUnmounted(() => {
42+
if (serialHandler) window.api.offSerialData(serialHandler)
43+
serialHandler = null
3344
})
3445
</script>
3546

src/renderer/src/components/EncoderLayer.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
v-model="keyboardStore.encoderKeymap[lindex][eindex][0]"
99
type="text"
1010
class="input input-bordered input-sm"
11+
@blur="handleBlur(0)"
1112
/>
1213
<input
1314
v-model="keyboardStore.encoderKeymap[lindex][eindex][1]"
1415
type="text"
1516
class="input input-bordered input-sm"
17+
@blur="handleBlur(1)"
1618
/>
1719
</div>
1820
</template>
@@ -27,6 +29,13 @@ if (!keyboardStore.encoderKeymap[props.lindex]) {
2729
if (!keyboardStore.encoderKeymap[props.lindex][props.eindex]) {
2830
keyboardStore.encoderKeymap[props.lindex][props.eindex] = ['KC.TRNS', 'KC.TRNS']
2931
}
32+
33+
const handleBlur = (index: number) => {
34+
const value = keyboardStore.encoderKeymap[props.lindex][props.eindex][index]
35+
if (!value || value === '') {
36+
keyboardStore.encoderKeymap[props.lindex][props.eindex][index] = 'KC.TRNS'
37+
}
38+
}
3039
</script>
3140

3241
<style lang="scss" scoped></style>

src/renderer/src/components/KeymapEditor.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,13 @@ const currentKeyCode = computed({
143143
},
144144
set(newVal) {
145145
if (newVal === '') return
146-
if (selectedKeys.value.size === 0) return
146+
let setNewVal = newVal
147+
if (!newVal || selectedKeys.value.size === 0) {
148+
setNewVal = 'KC.TRNS'
149+
}
147150
const keys = keyboardStore.keys.filter((_k, index) => selectedKeys.value.has(index))
148151
keys.forEach((key) => {
149-
key.setOnKeymap(newVal)
152+
key.setOnKeymap(setNewVal)
150153
})
151154
}
152155
})
@@ -155,9 +158,9 @@ const switchedKeyCodeType = () => {
155158
const keys = keyboardStore.keys.filter((_k, index) => selectedKeys.value.has(index))
156159
keys.forEach((key) => {
157160
if (keycodeModeForSelection.value === 'macro') {
158-
key.setOnKeymap('KC.MACRO((KC.A, KC.B))')
161+
key.setOnKeymap('KC.MACRO(Press(KC.LCTL),Tap(KC.A),Release(KC.LCTL))')
159162
} else if (keycodeModeForSelection.value === 'string') {
160-
key.setOnKeymap('send_string("")')
163+
key.setOnKeymap('KC.MACRO("Sample string")')
161164
} else if (keycodeModeForSelection.value === 'tapdance') {
162165
key.setOnKeymap('KC.TD(KC.A,KC.B)')
163166
} else if (keycodeModeForSelection.value === 'custom') {

0 commit comments

Comments
 (0)