Systemlautstärke und Gerätewahl im Mixer

Ausgabe und Eingabe stehen jetzt über den Programmen — mit Lautstärke,
Stummschalter und der Gerätewahl direkt am Namen. Ein eigenes Auswahlfeld
daneben wäre eine zweite Stelle für dieselbe Sache.

Sie stehen **außerhalb** des Mixer-Schalters, und das ist der Punkt: hier
wird kein Ton abgegriffen, sondern es werden die Regler des Systems bedient.
Das braucht keinen Tap, kein Aggregate-Gerät und keine Berechtigung — wer
nur die Lautstärke sucht, soll dafür nichts einschalten müssen.

Zwei Dinge, die beim Bauen nicht offensichtlich waren:

Viele Geräte führen keinen Regler für Element 0, sondern nur je Kanal.
Fragt man bloß den Hauptregler, hält man ein regelbares Gerät für
unregelbar. Also erst der Hauptregler, dann die Kanäle — und beim Setzen
beide Kanäle, sonst wandert das Stereobild.

Manche Geräte bringen gar keinen Regler mit, HDMI etwa. Dort steht „hier
nicht regelbar" statt eines Reglers, der nichts tut.

Warntöne folgen dem Ausgabegerät mit. Zwei getrennte Wahlen sind die
Quelle für „warum kommt der Systemton aus dem Notebook".

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Scarriffle
2026-08-11 19:28:39 +02:00
parent 68e53d3f6f
commit bf799d57d9
5 changed files with 710 additions and 101 deletions

View File

@@ -2,6 +2,7 @@ import SwiftUI
import AppKit
import OnyxDesign
import OnyxWidgetKit
import CoreAudio
/// Die Mischpult-Ansicht. Dieselbe im Panel und im Menüleisten-Popover.
///
@@ -14,16 +15,27 @@ import OnyxWidgetKit
/// gewöhnliche Lautstärke nicht.
public struct MixerView: View {
let mixer: AudioMixer
let system: SystemAudio
/// Im Panel ist es eng, im Popover nicht.
let compact: Bool
public init(mixer: AudioMixer, compact: Bool = false) {
public init(mixer: AudioMixer, system: SystemAudio, compact: Bool = false) {
self.mixer = mixer
self.system = system
self.compact = compact
}
public var body: some View {
VStack(alignment: .leading, spacing: compact ? 10 : 14) {
VStack(alignment: .leading, spacing: compact ? 10 : 12) {
// Die Systemregler stehen **vor** den Programmen und außerhalb des
// Mixer-Schalters: sie brauchen keinen Tap, keine Berechtigung und
// keinen laufenden Mixer. Wer nur die Lautstärke sucht, soll dafür
// nichts einschalten müssen.
SectionTitle("mixer.section.system", compact: compact)
SystemSection(system: system, compact: compact)
SectionTitle("mixer.section.apps", compact: compact)
if mixer.isEnabled {
if mixer.processes.isEmpty {
Text("mixer.noProcesses", bundle: .module)
@@ -314,15 +326,216 @@ public struct AudioMixerWidget: OnyxWidget {
public let symbolName = "slider.horizontal.3"
private let mixer: AudioMixer
private let system: SystemAudio
public init(mixer: AudioMixer) { self.mixer = mixer }
public init(mixer: AudioMixer, system: SystemAudio) {
self.mixer = mixer
self.system = system
}
public func makeView() -> AnyView {
AnyView(
ScrollView {
MixerView(mixer: mixer, compact: true)
MixerView(mixer: mixer, system: system, compact: true)
}
.scrollIndicators(.never)
)
}
}
// MARK: - Systemzeilen
private struct SectionTitle: View {
let key: LocalizedStringKey
let compact: Bool
init(_ key: LocalizedStringKey, compact: Bool) {
self.key = key
self.compact = compact
}
var body: some View {
Text(key, bundle: .module)
.font(Onyx.Font.metricSmall)
.foregroundStyle(Onyx.Color.textTertiary)
.textCase(.uppercase)
.padding(.top, compact ? 0 : 2)
}
}
/// Ausgabe, Eingabe und Gerätewahl.
///
/// Bewusst dieselbe Zeilenform wie bei den Programmen: Symbol, Stummschalter,
/// Regler, Wert. Zwei verschiedene Regler in einem Fenster wären eine
/// Behauptung, dass es zwei verschiedene Dinge sind es ist aber beides
/// Lautstärke.
private struct SystemSection: View {
let system: SystemAudio
let compact: Bool
var body: some View {
VStack(spacing: 0) {
SystemRow(
symbol: system.outputMuted ? "speaker.slash.fill" : "speaker.wave.2.fill",
title: outputName,
devices: system.outputDevices,
selected: system.outputDeviceID,
volume: system.outputVolume,
muted: system.outputMuted,
adjustable: system.outputIsAdjustable,
compact: compact,
onVolume: { system.setOutputVolume($0) },
onMute: { system.setOutputMuted(!system.outputMuted) },
onSelect: { system.selectOutput($0) })
Divider().overlay(Onyx.Color.hairline)
SystemRow(
symbol: system.inputMuted ? "mic.slash.fill" : "mic.fill",
title: inputName,
devices: system.inputDevices,
selected: system.inputDeviceID,
volume: system.inputVolume,
muted: system.inputMuted,
adjustable: system.inputIsAdjustable,
compact: compact,
onVolume: { system.setInputVolume($0) },
onMute: { system.setInputMuted(!system.inputMuted) },
onSelect: { system.selectInput($0) })
}
.background(Onyx.Color.elevated.opacity(0.55),
in: .rect(cornerRadius: 10, style: .continuous))
.overlay {
RoundedRectangle(cornerRadius: 10, style: .continuous)
.strokeBorder(Onyx.Color.hairline, lineWidth: 1)
}
}
private var outputName: String {
system.outputDevices.first { $0.id == system.outputDeviceID }?.name
?? String(localized: "mixer.output", bundle: .module)
}
private var inputName: String {
system.inputDevices.first { $0.id == system.inputDeviceID }?.name
?? String(localized: "mixer.input", bundle: .module)
}
}
private struct SystemRow: View {
let symbol: String
let title: String
let devices: [AudioDevice]
let selected: AudioObjectID
let volume: Double
let muted: Bool
let adjustable: Bool
let compact: Bool
let onVolume: (Double) -> Void
let onMute: () -> Void
let onSelect: (AudioDevice) -> Void
var body: some View {
VStack(alignment: .leading, spacing: compact ? 5 : 6) {
// Der Gerätename **ist** die Auswahl: ein eigenes Auswahlfeld
// daneben wäre eine zweite Stelle für dieselbe Sache.
Menu {
ForEach(devices) { device in
Button { onSelect(device) } label: {
if device.id == selected {
Label(device.name, systemImage: "checkmark")
} else {
Text(device.name)
}
}
}
} label: {
HStack(spacing: 4) {
Text(title)
.font(Onyx.Font.caption)
.foregroundStyle(Onyx.Color.textPrimary)
.lineLimit(1)
.truncationMode(.middle)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 7))
.foregroundStyle(Onyx.Color.textTertiary)
Spacer(minLength: 0)
}
}
.menuStyle(.borderlessButton)
.menuIndicator(.hidden)
.fixedSize()
HStack(spacing: 9) {
Button(action: onMute) {
Image(systemName: symbol)
.font(.system(size: 11))
.frame(width: 15, alignment: .leading)
}
.buttonStyle(.plain)
.foregroundStyle(muted ? Onyx.Color.critical : Onyx.Color.textSecondary)
if adjustable {
SystemVolumeBar(volume: volume, isActive: !muted, onChange: onVolume)
Text("\(Int(volume * 100)) %")
.font(Onyx.Font.metricSmall).monospacedDigit()
.foregroundStyle(Onyx.Color.textTertiary)
.frame(width: 36, alignment: .trailing)
} else {
// Manche Geräte bringen keinen eigenen Regler mit HDMI,
// viele USB-Interfaces. Ein Regler, der nichts tut, wäre
// eine Pseudobedienung.
Text("mixer.notAdjustable", bundle: .module)
.font(Onyx.Font.metricSmall)
.foregroundStyle(Onyx.Color.textTertiary)
Spacer(minLength: 0)
}
}
}
.padding(.horizontal, 10)
.padding(.vertical, compact ? 7 : 9)
}
}
/// Wie der Programmregler, nur ohne Verstärkung und ohne Pegel: die
/// Systemlautstärke geht von null bis hundert, mehr gibt Core Audio nicht her.
private struct SystemVolumeBar: View {
let volume: Double
let isActive: Bool
let onChange: (Double) -> Void
@State private var isDragging = false
var body: some View {
GeometryReader { geometry in
let knob: CGFloat = isDragging ? 13 : 11
let travel = max(geometry.size.width - knob, 1)
let fraction = min(max(volume, 0), 1)
ZStack(alignment: .leading) {
Capsule().fill(Onyx.Color.surface).frame(height: 6)
Capsule()
.fill(Onyx.Color.accent)
.frame(width: travel * fraction + knob / 2, height: 6)
.opacity(isActive ? 1 : 0.3)
Circle()
.fill(.white)
.frame(width: knob, height: knob)
.shadow(color: .black.opacity(0.45), radius: 1.5, y: 0.5)
.offset(x: travel * fraction)
.opacity(isActive ? 1 : 0.55)
}
.frame(height: 16)
.contentShape(.rect)
.gesture(
DragGesture(minimumDistance: 0)
.onChanged { value in
isDragging = true
onChange(min(max((value.location.x - knob / 2) / travel, 0), 1))
}
.onEnded { _ in isDragging = false })
}
.frame(height: 16)
.animation(Onyx.Motion.value, value: isDragging)
}
}