// MIDIIndicatorView.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import SwiftUI import LyrebirdShoeGaze /// A compact transport-row indicator showing whether a MIDI device is connected. /// /// Green when at least one input source is present, gray otherwise; a badge shows the count when /// more than one device is connected. Tapping invokes `onTap` (the host presents the MIDI mapping /// sheet). Observes the `@Observable` ``MIDIController`` so it refreshes automatically as devices /// connect and disconnect. public struct MIDIIndicatorView: View { // MARK: - Constants private static let iconSize: CGFloat = 13 private static let spacing: CGFloat = 3 private static let badgeFontSize: CGFloat = 10 private static let disconnectedOpacity: Double = 0.5 // MARK: - Inputs private let controller: MIDIController private let onTap: () -> Void /// Creates a MIDI connection indicator. /// /// - Parameters: /// - controller: The MIDI controller whose connection state is displayed. /// - onTap: Invoked when the indicator is tapped (present the MIDI mapping sheet). Defaults to /// a no-op. public init(controller: MIDIController, onTap: @escaping () -> Void = {}) { self.controller = controller self.onTap = onTap } // MARK: - Body public var body: some View { let connected: Bool = controller.isConnected return Button(action: onTap) { HStack(spacing: MIDIIndicatorView.spacing) { Image(systemName: connected ? "pianokeys" : "pianokeys.inverse") .font(.system(size: MIDIIndicatorView.iconSize)) .foregroundStyle(connected ? Color.green : Color.white.opacity(MIDIIndicatorView.disconnectedOpacity)) if controller.connectedDeviceNames.count > 1 { Text("\(controller.connectedDeviceNames.count)") .font(.system(size: MIDIIndicatorView.badgeFontSize, weight: .semibold).monospacedDigit()) .foregroundStyle(Color.green) } } } .buttonStyle(.plain) .help(connected ? "MIDI connected: \(controller.connectedDeviceNames.joined(separator: ", "))" : "No MIDI device detected") } }