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>
This commit is contained in:
@@ -22,6 +22,7 @@ public struct MIDIMappingSheet: View {
|
||||
private static let sheetMinWidth: CGFloat = 460
|
||||
private static let sheetMinHeight: CGFloat = 520
|
||||
private static let rowVerticalPadding: CGFloat = 2
|
||||
private static let switchRowControlSpacing: CGFloat = 8
|
||||
|
||||
// MARK: - Inputs
|
||||
|
||||
@@ -49,7 +50,7 @@ public struct MIDIMappingSheet: View {
|
||||
switchesSection
|
||||
expressionSection
|
||||
}
|
||||
.navigationTitle("MIDI Mapping")
|
||||
.navigationTitle("MIDI / Control Mapping")
|
||||
.toolbar {
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button("Done") {
|
||||
@@ -174,30 +175,37 @@ public struct MIDIMappingSheet: View {
|
||||
// MARK: - Switches (Program Change)
|
||||
|
||||
private var switchesSection: some View {
|
||||
Section("Switches (Program Change)") {
|
||||
Section("Switches (footswitch / key)") {
|
||||
ForEach(switchRowModels) { row in
|
||||
switchRow(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A switch row carries two independent trigger sources for one toggle action: a MIDI Program
|
||||
/// Change (footswitch) and a computer key. Each has its own learn / clear; the detail line
|
||||
/// summarises whichever are bound, or the "Listening…" prompt for the armed source.
|
||||
private func switchRow(_ row: SwitchRowModel) -> some View {
|
||||
let isArmed: Bool = controller.learnTarget == .action(row.action)
|
||||
let binding: MIDIMap.SwitchBinding? = controller.switchBinding(for: row.action)
|
||||
return HStack {
|
||||
let isArmedPC: Bool = controller.learnTarget == .action(row.action)
|
||||
let isArmedKey: Bool = controller.learnTarget == .key(row.action)
|
||||
let pcBinding: MIDIMap.SwitchBinding? = controller.switchBinding(for: row.action)
|
||||
let keyBinding: MIDIMap.KeyBinding? = controller.keyBinding(for: row.action)
|
||||
let isArmed: Bool = isArmedPC || isArmedKey
|
||||
return HStack(spacing: MIDIMappingSheet.switchRowControlSpacing) {
|
||||
VStack(alignment: .leading, spacing: 1) {
|
||||
Text(row.label)
|
||||
Text(switchBindingLabel(isArmed: isArmed, binding: binding))
|
||||
Text(switchBindingLabel(isArmedPC: isArmedPC, isArmedKey: isArmedKey,
|
||||
pc: pcBinding, key: keyBinding))
|
||||
.font(.caption)
|
||||
.foregroundStyle(isArmed ? Color.orange : .secondary)
|
||||
}
|
||||
Spacer()
|
||||
if binding != nil {
|
||||
Button("Clear") { controller.clearSwitchBinding(for: row.action) }
|
||||
if pcBinding != nil {
|
||||
Button("Clear PC") { controller.clearSwitchBinding(for: row.action) }
|
||||
.buttonStyle(.borderless)
|
||||
}
|
||||
Button(isArmed ? "Cancel" : "Learn") {
|
||||
if isArmed {
|
||||
Button(isArmedPC ? "Cancel" : "PC") {
|
||||
if isArmedPC {
|
||||
controller.cancelLearn()
|
||||
} else {
|
||||
controller.armSwitchLearn(row.action)
|
||||
@@ -205,15 +213,50 @@ public struct MIDIMappingSheet: View {
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.tint(.orange)
|
||||
if keyBinding != nil {
|
||||
Button("Clear Key") { controller.clearKeyBinding(for: row.action) }
|
||||
.buttonStyle(.borderless)
|
||||
}
|
||||
Button(isArmedKey ? "Cancel" : "Key") {
|
||||
if isArmedKey {
|
||||
controller.cancelLearn()
|
||||
} else {
|
||||
controller.armKeyLearn(row.action)
|
||||
}
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
.tint(.orange)
|
||||
}
|
||||
.padding(.vertical, MIDIMappingSheet.rowVerticalPadding)
|
||||
}
|
||||
|
||||
private func switchBindingLabel(isArmed: Bool, binding: MIDIMap.SwitchBinding?) -> String {
|
||||
if isArmed { return "Listening — press a footswitch…" }
|
||||
guard let binding: MIDIMap.SwitchBinding = binding else { return "Unassigned" }
|
||||
let channelText: String = binding.channel.map { " · ch \($0 + 1)" } ?? ""
|
||||
return "PC \(binding.program)\(channelText)"
|
||||
private func switchBindingLabel(
|
||||
isArmedPC: Bool,
|
||||
isArmedKey: Bool,
|
||||
pc: MIDIMap.SwitchBinding?,
|
||||
key: MIDIMap.KeyBinding?
|
||||
) -> String {
|
||||
if isArmedPC { return "Listening — press a footswitch…" }
|
||||
if isArmedKey { return "Listening — press a key…" }
|
||||
var parts: [String] = []
|
||||
if let pc: MIDIMap.SwitchBinding = pc {
|
||||
let channelText: String = pc.channel.map { " · ch \($0 + 1)" } ?? ""
|
||||
parts.append("PC \(pc.program)\(channelText)")
|
||||
}
|
||||
if let key: MIDIMap.KeyBinding = key {
|
||||
parts.append("Key \(keyLabel(key))")
|
||||
}
|
||||
return parts.isEmpty ? "Unassigned" : parts.joined(separator: " · ")
|
||||
}
|
||||
|
||||
/// A key binding formatted with its modifier glyphs, e.g. `A`, `⇧A`, `⌃A` (macOS modifier order:
|
||||
/// control, option, shift).
|
||||
private func keyLabel(_ binding: MIDIMap.KeyBinding) -> String {
|
||||
var prefix: String = ""
|
||||
if binding.modifiers.contains(.control) { prefix += "⌃" }
|
||||
if binding.modifiers.contains(.option) { prefix += "⌥" }
|
||||
if binding.modifiers.contains(.shift) { prefix += "⇧" }
|
||||
return prefix + binding.key.uppercased()
|
||||
}
|
||||
|
||||
// MARK: - Expression (CC)
|
||||
|
||||
Reference in New Issue
Block a user