Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/MIDIControllerKeyTests.swift
T
josh a33d5488c9 Add computer-keyboard control mapping
Bind letter/digit keys (with Shift/Control/Option; Command reserved for
menu shortcuts) to the same rig toggle Actions a MIDI Program-Change
footswitch drives. Keys reuse MIDIController's perform(_:) dispatch via a
new .key learn target and ingestKey(_:modifiers:).

Key capture is platform-specific: macOS installs an AppKit
NSEvent.addLocalMonitorForEvents(.keyDown) monitor (SwiftUI .onKeyPress
only delivers to the focused view); iOS uses .onKeyPress with the root
made focusable/focused, reclaiming focus when a text-field modal closes.
Both gate on the first responder not being a text field so typing is
never hijacked, filter key-repeat, and reserve Command for the menu.

MIDIMap gains keys:[KeyBinding] and a KeyModifiers OptionSet, both with
tolerant Codable decode so old rigs (no keys / no modifiers) still load
with switches/ccs intact. The mapping sheet is unified: each switch row
shows its footswitch (PC) and key bindings with per-source learn/clear,
and the sheet is retitled "MIDI / Control Mapping".

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-08 15:03:12 -07:00

202 lines
8.2 KiB
Swift

// MIDIControllerKeyTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("MIDIController key bindings")
struct MIDIControllerKeyTests {
/// Builds an engine + a one-board rig (a single Volume pedal) + a controller.
private func makeRig() throws -> (MIDIController, ShoeGaze, URL) {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 256)
let sg: ShoeGaze = ShoeGaze(engine: engine)
let board: PedalBoard = try sg.addBoard()
board.label = "Board 1"
try board.add(VolumePedal())
try sg.finalizeBoard(board)
let controller: MIDIController = MIDIController(engine: engine, shoeGaze: sg)
return (controller, sg, url)
}
@Test func learnCapturesKeyAndFiresCallback() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
var changed: Bool = false
controller.onMapChanged = { changed = true }
controller.armKeyLearn(.rigBypass)
let consumed: Bool = controller.ingestKey("b")
#expect(consumed, "the learn press is consumed")
#expect(controller.learnTarget == nil)
#expect(changed)
#expect(controller.keyBinding(for: .rigBypass)?.key == "b")
#expect(sg.midiMap?.keys.count == 1)
#expect(sg.rigBypassed == false, "learn must not toggle")
}
}
@Test func keyLatchesOnEachPress() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b") // learn only
#expect(sg.rigBypassed == false)
#expect(controller.ingestKey("b"))
#expect(sg.rigBypassed == true, "first press engages")
#expect(controller.ingestKey("b"))
#expect(sg.rigBypassed == false, "second press releases")
}
}
@Test func caseInsensitiveMatch() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b")
#expect(controller.ingestKey("B"), "an uppercase press matches a lowercase binding")
#expect(sg.rigBypassed == true)
}
}
@Test func shiftDistinguishesTwoBindingsOnTheSameKey() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b", modifiers: [])
controller.armKeyLearn(.rigMute)
_ = controller.ingestKey("b", modifiers: [.shift])
#expect(sg.midiMap?.keys.count == 2, "b and ⇧b are independent bindings")
#expect(controller.ingestKey("b", modifiers: []))
#expect(sg.rigBypassed == true, "unmodified b drives bypass")
#expect(sg.rigMuted == false, "unmodified b must not fire the ⇧b binding")
#expect(controller.ingestKey("b", modifiers: [.shift]))
#expect(sg.rigMuted == true, "⇧b drives mute")
#expect(sg.rigBypassed == true, "⇧b must not re-fire the plain-b binding")
}
}
@Test func modifierMismatchIsNotConsumed() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b", modifiers: [.shift]) // learn ⇧b
#expect(controller.ingestKey("b", modifiers: []) == false, "plain b does not match ⇧b")
#expect(controller.ingestKey("b", modifiers: [.control]) == false, "⌃b does not match ⇧b")
#expect(sg.rigBypassed == false)
#expect(controller.ingestKey("b", modifiers: [.shift]), "⇧b matches")
#expect(sg.rigBypassed == true)
}
}
@Test func unboundKeyIsNotConsumed() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b")
#expect(controller.ingestKey("z") == false, "an unbound key falls through")
#expect(sg.rigBypassed == false)
}
}
@Test func digitKeyBinds() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("3")
#expect(controller.keyBinding(for: .rigBypass)?.key == "3")
#expect(controller.ingestKey("3"))
#expect(sg.rigBypassed == true)
}
}
@Test func nonAlphanumericKeyRejected() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
// Return, Tab, and Escape are single Characters but not letters/digits — they must not be
// learnable, or binding one would silently swallow it everywhere in the app.
#expect(controller.ingestKey("\r") == false)
#expect(controller.ingestKey("\t") == false)
#expect(controller.ingestKey("\u{1B}") == false)
#expect(controller.ingestKey(" ") == false)
#expect(controller.learnTarget != nil, "a rejected press leaves learn armed")
#expect(sg.midiMap?.keys.isEmpty ?? true)
}
}
@Test func multiCharacterInputRejected() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
#expect(controller.ingestKey("ab") == false, "a multi-character press is ignored")
#expect(controller.learnTarget != nil, "an ignored press leaves learn armed")
#expect(sg.midiMap?.keys.isEmpty ?? true)
}
}
@Test func keyBindingSurvivesSaveLoadAndStillFires() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
let preset: URL = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString + ".json")
defer { try? FileManager.default.removeItem(at: preset) }
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b")
try sg.saveRig(to: preset)
try sg.loadRig(from: preset)
#expect(sg.midiMap?.keys.first?.key == "b", "the key binding survives a file round-trip")
#expect(controller.ingestKey("b"), "the restored binding still fires")
#expect(sg.rigBypassed == true)
withExtendedLifetime(sg) { }
}
@Test func clearRemovesBinding() throws {
let (controller, sg, url): (MIDIController, ShoeGaze, URL) = try makeRig()
defer { try? FileManager.default.removeItem(at: url) }
withExtendedLifetime(sg) {
controller.armKeyLearn(.rigBypass)
_ = controller.ingestKey("b")
#expect(controller.keyBinding(for: .rigBypass) != nil)
controller.clearKeyBinding(for: .rigBypass)
#expect(controller.keyBinding(for: .rigBypass) == nil)
#expect(controller.ingestKey("b") == false, "a cleared key no longer fires")
#expect(sg.rigBypassed == false)
}
}
}