Skip to content

Commit

Permalink
fix use count to stop setting hotkeys, dont reimport layout with isCu…
Browse files Browse the repository at this point in the history
…stom
  • Loading branch information
TeemuKoivisto committed Mar 26, 2024
1 parent 06534cd commit 6a600ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import { keyboard, keyboardOptions, rows, keyboardActions } from '$stores/keyboard'
onMount(() => {
keyboardActions.setLayout(navigator.languages)
if (!$keyboard.opts.isCustom) {
keyboardActions.setLayout(navigator.languages)
}
})
$: settableRows = $rows.map((_, idx) =>
Expand Down
30 changes: 18 additions & 12 deletions packages/client/src/stores/keyboard/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface Captured {
rowIndex: number
nextIndex: number
count: number
scaleNotes: ScaleNote[]
}

const ENGLISH_LAYOUT: Layout = {
Expand Down Expand Up @@ -86,11 +87,15 @@ export const kbdNotes = derived(keyMap, kmap =>
export const keyboardActions = {
async setLayout(languages: readonly string[]) {
const layout = await importLayout(languages)
keyboardOptions.update(v => ({
...v,
layout
}))
languageLayout.set(layout)
const opts = get(keyboardOptions)
if (layout.code !== opts.layout.code) {
keyboardOptions.update(v => ({
...v,
isCustom: false,
layout
}))
languageLayout.set(layout)
}
},
setCustomLayout(val: boolean) {
if (val) {
Expand Down Expand Up @@ -122,11 +127,13 @@ export const keyboardActions = {
},
captureHotkeyRow(rowIndex: number) {
const kbd = get(keyboard)
const scale = get(scaleData)
const { first, count } = kbd.startSetCustomRow(rowIndex)
capturingHotkeys.set({
nextIndex: first,
rowIndex,
count: count
count,
scaleNotes: Array.from(scale.notesMap.values())
})
},
findNote(note: string): ScaleNote | undefined {
Expand Down Expand Up @@ -158,20 +165,19 @@ export const keyboardActions = {
const evt = captureHotkey(captured, code, key)
const kbd = get(keyboard)
// console.log(`input: ${code} ${key} ${evt.e}`)
let next: { key: KeyboardKey; done: boolean; index: number } | undefined
let next: { key: KeyboardKey; index: number } | undefined
const index = kbd.setCustomRow.nextKeyIdx
if (evt.e === 'hotkeys-cancel') {
capturingHotkeys.set(undefined)
captured.clear()
} else if (evt.e === 'hotkeys-skip-key') {
next = kbd.skipNextCustomNote()
} else if (evt.e === 'hotkeys-captured-key') {
const scale = get(scaleData)
const notes = Array.from(scale.notesMap.values())
next = kbd.setNextCustomNote(evt.data.key, evt.data.code, notes)
next = kbd.setNextCustomNote(evt.data.key, evt.data.code, cpt.scaleNotes)
}
const newRows = get(rows)
if (next) {
newRows[cpt.rowIndex][next.index - 1] = next.key
newRows[cpt.rowIndex][index] = next.key
rows.set(newRows)
capturingHotkeys.update(v =>
v
Expand All @@ -182,7 +188,7 @@ export const keyboardActions = {
: undefined
)
}
if (next?.done) {
if (cpt.count === index) {
const layout = layoutFromRows(newRows)
keyboardOptions.update(v => ({
...v,
Expand Down
6 changes: 2 additions & 4 deletions packages/keyboard/src/Keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ export class Keyboard {
this.setCustomRow.nextKeyIdx += 1
return {
index: this.setCustomRow.nextKeyIdx,
key: rowKey,
done: this.setCustomRow.nextKeyIdx >= this.setCustomRow.row.length
key: rowKey
}
}

Expand All @@ -122,8 +121,7 @@ export class Keyboard {
this.setCustomRow.nextNoteOffset += 1
return {
index: this.setCustomRow.nextKeyIdx,
key: { code: 'EMPTY', key: '{empty}' },
done: this.setCustomRow.nextKeyIdx >= this.setCustomRow.row.length
key: { code: 'EMPTY', key: '{empty}' }
}
}

Expand Down

0 comments on commit 6a600ff

Please sign in to comment.