Files
ReactorcoremeltdownandClaude Opus 5.5 4d9e2328cb 0.38.0 (72): Bluetooth LE MIDI controllers in Settings (Android and iOS)
- Android: BleMidi scans for the BLE MIDI service, connects through
  MidiManager.openBluetoothDevice, remembers and reconnects controllers;
  Settings › MIDI inputs › Bluetooth MIDI (Scan / Connect / Disconnect / Forget)
- iOS: the system Bluetooth MIDI panel from Setup › MIDI Sync, connected
  controllers remembered and reconnected at launch via CoreBluetooth

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:57:53 +01:00

175 lines
7.1 KiB
Swift

// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
import SwiftUI
// The Setup tab's UIKit-backed pieces: the share sheet, the device lab and the
// plugin check. The Mac snapshot tool has no UIKit (and doesn't compile
// DeviceLabView / PluginCheck), so there they are no-ops.
#if canImport(UIKit)
import AVKit
import CoreAudioKit
import UIKit
enum ShareSheet {
/// Presents the system share sheet for [url] over the frontmost view controller
/// (a centred popover on iPad); [done] runs when it closes.
@MainActor static func present(_ url: URL, done: @escaping () -> Void = {}) {
let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first
guard var top = (scene?.windows.first { $0.isKeyWindow } ?? scene?.windows.first)?.rootViewController else { return }
while let next = top.presentedViewController { top = next }
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
vc.completionWithItemsHandler = { _, _, _, _ in done() }
if let pop = vc.popoverPresentationController {
pop.sourceView = top.view
pop.sourceRect = CGRect(x: top.view.bounds.midX, y: top.view.bounds.midY, width: 0, height: 0)
pop.permittedArrowDirections = []
}
top.present(vc, animated: true)
}
}
extension View {
/// The old device test screen (instruments, effects, benchmark), full screen.
func deviceLab(isPresented: Binding<Bool>) -> some View {
fullScreenCover(isPresented: isPresented) {
VStack(spacing: 0) {
HStack {
Text("Device lab").font(.headline)
Spacer()
Button("Done") { isPresented.wrappedValue = false }.font(.body.weight(.semibold))
}
.padding(.horizontal).padding(.top, 12)
DeviceLabView()
}
.background(Theme.background.ignoresSafeArea())
.preferredColorScheme(.dark)
}
}
/// Loads every registered Sizzle plugin out of process and renders through it.
func pluginCheck(isPresented: Binding<Bool>) -> some View {
sheet(isPresented: isPresented) { PluginCheckSheet { isPresented.wrappedValue = false } }
}
}
private struct PluginCheckSheet: View {
let close: () -> Void
@StateObject private var check = PluginCheck()
var body: some View {
VStack(alignment: .leading, spacing: 10) {
HStack {
Text("Plugin check").font(.headline)
if check.running { ProgressView().padding(.leading, 6) }
Spacer()
Button("Run again") { Task { await check.run() } }.disabled(check.running)
Button("Done", action: close).font(.body.weight(.semibold)).padding(.leading, 12)
}
ScrollView {
Text(check.lines.joined(separator: "\n"))
.font(.system(.footnote, design: .monospaced))
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
}
}
.padding()
.preferredColorScheme(.dark)
.task { await check.run() }
}
}
/// The system's audio route picker (AirPlay, Bluetooth, an interface) and the
/// current route's names (Android's audio device rows; iOS chooses the route).
struct AudioRouteView: View {
@State private var output = AudioRouteView.names(\.outputs)
@State private var input = AudioRouteView.names(\.inputs)
var body: some View {
VStack(alignment: .leading, spacing: 8) {
HStack(spacing: 10) {
VStack(alignment: .leading, spacing: 3) {
Text("Output: " + output).font(.system(size: 14)).foregroundColor(Theme.text)
Text("Input: " + input).font(.system(size: 14)).foregroundColor(Theme.text)
}
Spacer()
RoutePicker().frame(width: 44, height: 44)
}
}
.onReceive(NotificationCenter.default.publisher(for: AVAudioSession.routeChangeNotification).receive(on: RunLoop.main)) { _ in
output = Self.names(\.outputs)
input = Self.names(\.inputs)
}
}
static func names(_ kp: KeyPath<AVAudioSessionRouteDescription, [AVAudioSessionPortDescription]>) -> String {
let ports = AVAudioSession.sharedInstance().currentRoute[keyPath: kp].map(\.portName)
return ports.isEmpty ? "—" : ports.joined(separator: ", ")
}
private struct RoutePicker: UIViewRepresentable {
func makeUIView(context: Context) -> AVRoutePickerView {
let v = AVRoutePickerView()
v.tintColor = UIColor(Theme.accent)
v.activeTintColor = UIColor(Theme.accent)
return v
}
func updateUIView(_ v: AVRoutePickerView, context: Context) {}
}
}
/// The system's Bluetooth MIDI panel (find, connect, disconnect BLE MIDI
/// controllers) in a sheet with Done; [done] runs however it closes.
enum BluetoothMidiPanel {
@MainActor static func present(done: @escaping () -> Void) {
let scenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
let scene = scenes.first { $0.activationState == .foregroundActive } ?? scenes.first
var top = scene?.windows.first { $0.isKeyWindow }?.rootViewController
while let p = top?.presentedViewController { top = p }
guard let host = top else { return }
let panel = CABTMIDICentralViewController()
let nav = UINavigationController(rootViewController: panel)
let closer = Closer(done)
panel.navigationItem.rightBarButtonItem = UIBarButtonItem(systemItem: .done, primaryAction: UIAction { [weak nav] _ in
nav?.dismiss(animated: true) { closer.fire() }
})
nav.modalPresentationStyle = .formSheet
nav.presentationController?.delegate = closer
objc_setAssociatedObject(nav, &Closer.key, closer, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
host.present(nav, animated: true)
}
private final class Closer: NSObject, UIAdaptivePresentationControllerDelegate {
static var key = 0
private var done: (() -> Void)?
init(_ done: @escaping () -> Void) { self.done = done }
func fire() { done?(); done = nil }
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { fire() }
}
}
#else
enum BluetoothMidiPanel {
@MainActor static func present(done: @escaping () -> Void) { done() }
}
struct AudioRouteView: View {
var body: some View {
Text("Output: Speaker\nInput: Microphone").font(.system(size: 14)).foregroundColor(Theme.text)
}
}
enum ShareSheet {
@MainActor static func present(_ url: URL, done: @escaping () -> Void = {}) { done() }
}
extension View {
func deviceLab(isPresented: Binding<Bool>) -> some View { self }
func pluginCheck(isPresented: Binding<Bool>) -> some View { self }
}
#endif