A browser extension reports audible tabs to moonmix-tabs-host, which matches them to PipeWire streams by time and publishes the names for the mixer; streams of one tab share a row. Ambiguous streams keep the browser's name. Without the extension, a browser's only running stream shows its MPRIS title. The smoke test no longer claims real audio hardware. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
221 lines
5.2 KiB
MoonBit
221 lines
5.2 KiB
MoonBit
// View model: what each pavucontrol-style tab shows, derived from State.
|
|
// The GTK layer only reconciles widgets against these rows.
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub(all) enum Tab {
|
|
Playback
|
|
Recording
|
|
OutputDevices
|
|
InputDevices
|
|
}
|
|
|
|
///|
|
|
pub fn Tab::all() -> Array[Tab] {
|
|
[Playback, Recording, OutputDevices, InputDevices]
|
|
}
|
|
|
|
///|
|
|
pub fn Tab::label(self : Tab) -> String {
|
|
match self {
|
|
Playback => "Playback"
|
|
Recording => "Recording"
|
|
OutputDevices => "Output Devices"
|
|
InputDevices => "Input Devices"
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn Tab::node_kind(self : Tab) -> NodeKind {
|
|
match self {
|
|
Playback => PlaybackStream
|
|
Recording => RecordStream
|
|
OutputDevices => Sink
|
|
InputDevices => Source
|
|
}
|
|
}
|
|
|
|
///|
|
|
pub fn Tab::empty_text(self : Tab) -> String {
|
|
match self {
|
|
Playback => "No application is currently playing audio."
|
|
Recording => "No application is currently recording audio."
|
|
OutputDevices => "No output devices available."
|
|
InputDevices => "No input devices available."
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// A device choice offered in a stream's "output/input device" dropdown.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct DeviceChoice {
|
|
id : Int32
|
|
name : String // node.name, used as the routing target
|
|
label : String
|
|
}
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Row {
|
|
id : Int32
|
|
/// Nodes this row controls: its own id first, then, for a browser tab, the
|
|
/// tab's other streams.
|
|
members : Array[Int32]
|
|
title : String
|
|
subtitle : String
|
|
/// Icon-theme names to try in order (see icons.mbt).
|
|
icons : Array[String]
|
|
volume_icon : String // speaker icon matching volume + mute
|
|
volumes : Array[Double]
|
|
percent : Double // linked slider value (cubic %)
|
|
volume_text : String // "75% (-7.50 dB)"
|
|
mute : Bool
|
|
has_volume : Bool
|
|
/// Devices only: this is the current default sink/source.
|
|
is_default : Bool
|
|
/// Streams only: devices the stream can be moved to, and the index of the
|
|
/// current one in that list (-1 when unknown / not linked yet).
|
|
choices : Array[DeviceChoice]
|
|
current_choice : Int32
|
|
}
|
|
|
|
///|
|
|
pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
|
let kind = tab.node_kind()
|
|
let device_kind = match tab {
|
|
Playback => Some(Sink)
|
|
Recording => Some(Source)
|
|
_ => None
|
|
}
|
|
let choices = match device_kind {
|
|
Some(dk) =>
|
|
state
|
|
.nodes_of(dk)
|
|
.map(fn(d) { { id: d.id, name: d.name, label: display_title(d), } })
|
|
None => []
|
|
}
|
|
// Streams of one browser tab share a row.
|
|
let groups : Map[String, Array[Int32]] = Map([])
|
|
let nodes = state
|
|
.nodes_of(kind)
|
|
.filter(fn(n) {
|
|
match state.tab_of(n) {
|
|
Some(t) =>
|
|
match groups.get(t.key) {
|
|
Some(g) => {
|
|
g.push(n.id)
|
|
false
|
|
}
|
|
None => {
|
|
groups.set(t.key, [n.id])
|
|
true
|
|
}
|
|
}
|
|
None => true
|
|
}
|
|
})
|
|
nodes.map(fn(n) {
|
|
let is_stream = device_kind is Some(_)
|
|
let tab = state.tab_of(n)
|
|
let subtitle = if tab is Some(_) {
|
|
display_title(n)
|
|
} else if is_stream {
|
|
state.media_title(n).unwrap_or(n.media_name)
|
|
} else if n.name != display_title(n) {
|
|
n.name
|
|
} else {
|
|
""
|
|
}
|
|
let current_choice = if is_stream {
|
|
match state.stream_device(n.id) {
|
|
Some(dev) => {
|
|
let mut idx = -1
|
|
for i, c in choices {
|
|
if c.id == dev {
|
|
idx = i
|
|
}
|
|
}
|
|
idx
|
|
}
|
|
None => -1
|
|
}
|
|
} else {
|
|
-1
|
|
}
|
|
let is_default = match kind {
|
|
Sink => n.id == state.default_sink
|
|
Source => n.id == state.default_source
|
|
_ => false
|
|
}
|
|
{
|
|
id: n.id,
|
|
members: match tab {
|
|
Some(t) => groups.get(t.key).unwrap_or([n.id])
|
|
None => [n.id]
|
|
},
|
|
title: match tab {
|
|
Some(t) if t.title != "" => t.title
|
|
_ => display_title(n)
|
|
},
|
|
subtitle,
|
|
icons: if is_stream {
|
|
stream_icon_chain(n)
|
|
} else {
|
|
device_icon_chain(n)
|
|
},
|
|
volume_icon: volume_icon(linked_percent(n.volumes), n.mute),
|
|
volumes: n.volumes,
|
|
percent: linked_percent(n.volumes),
|
|
volume_text: if n.volumes.length() > 0 {
|
|
format_volume(n.volumes)
|
|
} else {
|
|
""
|
|
},
|
|
mute: n.mute,
|
|
has_volume: n.volumes.length() > 0,
|
|
is_default,
|
|
choices: if is_stream {
|
|
choices
|
|
} else {
|
|
[]
|
|
},
|
|
current_choice,
|
|
}
|
|
})
|
|
}
|
|
|
|
///|
|
|
/// Result of reconciling the rows shown on screen against new rows: which
|
|
/// row ids to remove, which to add (with their index) and which to update.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct Diff {
|
|
removed : Array[Int32]
|
|
added : Array[Int32]
|
|
updated : Array[Int32]
|
|
}
|
|
|
|
///|
|
|
pub fn diff_rows(old : Array[Row], new : Array[Row]) -> Diff {
|
|
let old_by_id : Map[Int32, Row] = Map([])
|
|
for r in old {
|
|
old_by_id.set(r.id, r)
|
|
}
|
|
let new_ids : Map[Int32, Bool] = Map([])
|
|
let added = []
|
|
let updated = []
|
|
for r in new {
|
|
new_ids.set(r.id, true)
|
|
match old_by_id.get(r.id) {
|
|
None => added.push(r.id)
|
|
Some(o) => if o != r { updated.push(r.id) }
|
|
}
|
|
}
|
|
let removed = []
|
|
for r in old {
|
|
if !new_ids.contains(r.id) {
|
|
removed.push(r.id)
|
|
}
|
|
}
|
|
{ removed, added, updated, }
|
|
}
|