// 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 title : String subtitle : String icon_name : String 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 => [] } state .nodes_of(kind) .map(fn(n) { let is_stream = device_kind is Some(_) let subtitle = if is_stream { 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, title: display_title(n), subtitle, icon_name: if n.icon_name != "" { n.icon_name } else { default_icon(kind) }, 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, } }) } ///| fn default_icon(kind : NodeKind) -> String { match kind { Sink => "audio-speakers-symbolic" Source => "audio-input-microphone-symbolic" PlaybackStream => "audio-x-generic-symbolic" RecordStream => "audio-input-microphone-symbolic" Other => "audio-x-generic-symbolic" } } ///| /// 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, } }