Use real icons: per-device/app icon chains, volume icons, app icon
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
92ae5cdbfe
commit
cf8bab0d36
21 files changed
+658
-70
No files matched your search
+200
@@ -0,0 +1,200 @@
|
||||
// Icon selection, kept pure: PipeWire properties -> an ordered chain of
|
||||
// icon-theme names. The GTK layer walks a chain and takes the first name the
|
||||
// theme has (`gtk_icon_theme_has_icon`), so it never shows the broken-image
|
||||
// placeholder unless even the last resort is missing.
|
||||
//
|
||||
// Chain entries starting with `APP_LOOKUP_PREFIX` ("app:<key>") ask the GUI
|
||||
// to look the application up in the installed .desktop files and use that
|
||||
// launcher's icon.
|
||||
|
||||
///|
|
||||
pub fn icon_fallback_device() -> Array[String] {
|
||||
["audio-card-symbolic", "audio-x-generic-symbolic"]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn icon_fallback_stream() -> Array[String] {
|
||||
["application-x-executable-symbolic", "audio-x-generic-symbolic"]
|
||||
}
|
||||
|
||||
///|
|
||||
/// Absolute last resort, only used when the theme lacks everything above.
|
||||
pub const ICON_MISSING : String = "image-missing"
|
||||
|
||||
///|
|
||||
pub const APP_LOOKUP_PREFIX : String = "app:"
|
||||
|
||||
///|
|
||||
/// "Default" toggle: first icon the theme has.
|
||||
pub fn default_button_icons() -> Array[String] {
|
||||
["emblem-ok-symbolic", "object-select-symbolic", "emblem-default-symbolic"]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn symbolic(name : String) -> String {
|
||||
if name.has_suffix("-symbolic") {
|
||||
name
|
||||
} else {
|
||||
name + "-symbolic"
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn push_unique(out : Array[String], name : String) -> Unit {
|
||||
if name != "" && !out.contains(name) {
|
||||
out.push(name)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// `name` as a themed icon, symbolic variant first (it follows the theme's
|
||||
/// foreground colour).
|
||||
fn push_symbolic_first(out : Array[String], name : String) -> Unit {
|
||||
if name == "" {
|
||||
return
|
||||
}
|
||||
push_unique(out, symbolic(name))
|
||||
push_unique(out, name)
|
||||
}
|
||||
|
||||
///|
|
||||
fn has_any(haystack : String, needles : Array[String]) -> Bool {
|
||||
let h = haystack.to_lower()
|
||||
needles.any(fn(n) { h.contains(n) })
|
||||
}
|
||||
|
||||
///|
|
||||
/// Icon (without suffix) for a `device.form-factor` value, "" if unknown.
|
||||
pub fn form_factor_icon(form_factor : String, kind : NodeKind) -> String {
|
||||
match form_factor {
|
||||
"headphone" => "audio-headphones"
|
||||
"headset" => "audio-headset"
|
||||
"speaker" | "hifi" => "audio-speakers"
|
||||
"microphone" => "audio-input-microphone"
|
||||
"webcam" => "camera-web"
|
||||
"handset" | "hands-free" => "phone"
|
||||
"portable" => "multimedia-player"
|
||||
"car" =>
|
||||
if kind is Source {
|
||||
"audio-input-microphone"
|
||||
} else {
|
||||
"audio-speakers"
|
||||
}
|
||||
"computer" => "computer"
|
||||
"tv" | "hdmi" => "video-display"
|
||||
_ => ""
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The bus of a device, from `device.bus` or else guessed from the
|
||||
/// node.name prefix ("alsa_input.usb-...", "bluez_output...").
|
||||
pub fn device_bus(n : Node) -> String {
|
||||
if n.bus != "" {
|
||||
return n.bus
|
||||
}
|
||||
let name = n.name
|
||||
if name.has_prefix("bluez_") {
|
||||
"bluetooth"
|
||||
} else if has_any(name, [".usb-"]) {
|
||||
"usb"
|
||||
} else if has_any(name, [".pci-"]) {
|
||||
"pci"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Icon chain for a sink or source.
|
||||
pub fn device_icon_chain(n : Node) -> Array[String] {
|
||||
let kind = n.kind()
|
||||
let out : Array[String] = []
|
||||
// 1. what the device itself says
|
||||
for h in n.icon_hints {
|
||||
push_symbolic_first(out, h)
|
||||
}
|
||||
// 2. form factor, then HDMI / webcam heuristics, then bus
|
||||
push_symbolic_first(out, form_factor_icon(n.form_factor, kind))
|
||||
let text = n.name + " " + n.description
|
||||
if has_any(text, ["hdmi", "displayport", "display port"]) {
|
||||
push_symbolic_first(out, "video-display")
|
||||
}
|
||||
let bus = device_bus(n)
|
||||
if kind is Source &&
|
||||
(
|
||||
has_any(n.description, ["webcam", "camera"]) ||
|
||||
(bus == "usb" && n.description.to_lower().contains("cam"))
|
||||
) {
|
||||
push_symbolic_first(out, "camera-web")
|
||||
}
|
||||
match (bus, kind) {
|
||||
("bluetooth", Source) => push_symbolic_first(out, "audio-headset")
|
||||
("bluetooth", _) => push_symbolic_first(out, "audio-headphones")
|
||||
_ => ()
|
||||
}
|
||||
// 3. by direction
|
||||
match kind {
|
||||
Source => push_symbolic_first(out, "audio-input-microphone")
|
||||
_ => push_symbolic_first(out, "audio-speakers")
|
||||
}
|
||||
// 4. generic cards, then the placeholder
|
||||
for f in icon_fallback_device() {
|
||||
push_unique(out, f)
|
||||
}
|
||||
push_unique(out, ICON_MISSING)
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
/// Icon chain for an application stream: the icons the app announces, its
|
||||
/// launcher's icon from the installed .desktop files, names derived from the
|
||||
/// binary / app name, then generic ones.
|
||||
pub fn stream_icon_chain(n : Node) -> Array[String] {
|
||||
let out : Array[String] = []
|
||||
for h in n.icon_hints {
|
||||
push_unique(out, h)
|
||||
}
|
||||
for key in [n.app_id, n.app_binary, n.app_name] {
|
||||
if key != "" {
|
||||
push_unique(out, APP_LOOKUP_PREFIX + key)
|
||||
}
|
||||
}
|
||||
for key in [n.app_id, n.app_binary, n.app_name.to_lower()] {
|
||||
// Many apps ship an icon named after their binary or app id.
|
||||
if key != "" && !key.contains(" ") && !key.contains("/") {
|
||||
push_unique(out, key)
|
||||
}
|
||||
}
|
||||
if n.kind() is RecordStream {
|
||||
push_unique(out, "audio-input-microphone-symbolic")
|
||||
}
|
||||
for f in icon_fallback_stream() {
|
||||
push_unique(out, f)
|
||||
}
|
||||
push_unique(out, ICON_MISSING)
|
||||
out
|
||||
}
|
||||
|
||||
///|
|
||||
/// Speaker icon for a volume (linked percent, 100 = 0 dB) and mute state.
|
||||
pub fn volume_icon(percent : Double, mute : Bool) -> String {
|
||||
if mute || percent <= 0.0 {
|
||||
"audio-volume-muted-symbolic"
|
||||
} else if percent < 34.0 {
|
||||
"audio-volume-low-symbolic"
|
||||
} else if percent < 67.0 {
|
||||
"audio-volume-medium-symbolic"
|
||||
} else {
|
||||
"audio-volume-high-symbolic"
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Small symbolic icon next to a tab's label.
|
||||
pub fn Tab::icon(self : Tab) -> String {
|
||||
match self {
|
||||
Playback | OutputDevices => "audio-speakers-symbolic"
|
||||
Recording | InputDevices => "audio-input-microphone-symbolic"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
///|
|
||||
fn dev(
|
||||
media_class : String,
|
||||
name~ : String = "",
|
||||
description~ : String = "",
|
||||
form_factor~ : String = "",
|
||||
bus~ : String = "",
|
||||
hints~ : Array[String] = [],
|
||||
) -> @model.Node {
|
||||
@model.Node::new(
|
||||
1,
|
||||
media_class,
|
||||
name~,
|
||||
description~,
|
||||
form_factor~,
|
||||
bus~,
|
||||
icon_hints=hints,
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "device hint comes first, symbolic variant before plain" {
|
||||
let c = @model.device_icon_chain(
|
||||
dev("Audio/Sink", hints=["audio-card-pci"], form_factor="headphone"),
|
||||
)
|
||||
assert_eq(c[0], "audio-card-pci-symbolic")
|
||||
assert_eq(c[1], "audio-card-pci")
|
||||
assert_eq(c[2], "audio-headphones-symbolic")
|
||||
}
|
||||
|
||||
///|
|
||||
test "device form factors" {
|
||||
let first = fn(ff, cls) {
|
||||
@model.device_icon_chain(dev(cls, form_factor=ff))[0]
|
||||
}
|
||||
assert_eq(first("headset", "Audio/Sink"), "audio-headset-symbolic")
|
||||
assert_eq(first("speaker", "Audio/Sink"), "audio-speakers-symbolic")
|
||||
assert_eq(
|
||||
first("microphone", "Audio/Source"),
|
||||
"audio-input-microphone-symbolic",
|
||||
)
|
||||
assert_eq(first("webcam", "Audio/Source"), "camera-web-symbolic")
|
||||
assert_eq(first("handset", "Audio/Sink"), "phone-symbolic")
|
||||
assert_eq(first("tv", "Audio/Sink"), "video-display-symbolic")
|
||||
assert_eq(first("car", "Audio/Source"), "audio-input-microphone-symbolic")
|
||||
}
|
||||
|
||||
///|
|
||||
test "hdmi, webcam and bluetooth heuristics" {
|
||||
let hdmi = @model.device_icon_chain(
|
||||
dev(
|
||||
"Audio/Sink",
|
||||
name="alsa_output.pci-0000_01_00.1.hdmi-stereo",
|
||||
description="HDMI / DisplayPort",
|
||||
),
|
||||
)
|
||||
assert_eq(hdmi[0], "video-display-symbolic")
|
||||
let cam = @model.device_icon_chain(
|
||||
dev(
|
||||
"Audio/Source",
|
||||
name="alsa_input.usb-EMEET_SmartCam_C960.mono-fallback",
|
||||
description="EMEET SmartCam C960 Mono",
|
||||
),
|
||||
)
|
||||
assert_eq(cam[0], "camera-web-symbolic")
|
||||
let bt = @model.device_icon_chain(
|
||||
dev("Audio/Sink", name="bluez_output.AA_BB.1"),
|
||||
)
|
||||
assert_eq(bt[0], "audio-headphones-symbolic")
|
||||
assert_eq(
|
||||
@model.device_bus(dev("Audio/Sink", name="alsa_output.usb-X.analog-stereo")),
|
||||
"usb",
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
test "device chain always ends in fallbacks" {
|
||||
let c = @model.device_icon_chain(dev("Audio/Sink"))
|
||||
assert_eq(c, [
|
||||
"audio-speakers-symbolic", "audio-speakers", "audio-card-symbolic", "audio-x-generic-symbolic",
|
||||
"image-missing",
|
||||
])
|
||||
let s = @model.device_icon_chain(dev("Audio/Source"))
|
||||
assert_eq(s[0], "audio-input-microphone-symbolic")
|
||||
assert_eq(s[s.length() - 1], "image-missing")
|
||||
}
|
||||
|
||||
///|
|
||||
test "stream chain: hints, desktop lookup, binary, fallbacks" {
|
||||
let n = @model.Node::new(
|
||||
9,
|
||||
"Stream/Output/Audio",
|
||||
app_name="Firefox",
|
||||
app_binary="firefox",
|
||||
app_id="org.mozilla.firefox",
|
||||
icon_hints=["firefox-esr"],
|
||||
)
|
||||
assert_eq(@model.stream_icon_chain(n), [
|
||||
"firefox-esr", "app:org.mozilla.firefox", "app:firefox", "app:Firefox", "org.mozilla.firefox",
|
||||
"firefox", "application-x-executable-symbolic", "audio-x-generic-symbolic", "image-missing",
|
||||
])
|
||||
let bare = @model.Node::new(10, "Stream/Input/Audio")
|
||||
assert_eq(@model.stream_icon_chain(bare), [
|
||||
"audio-input-microphone-symbolic", "application-x-executable-symbolic", "audio-x-generic-symbolic",
|
||||
"image-missing",
|
||||
])
|
||||
}
|
||||
|
||||
///|
|
||||
test "volume icon follows volume and mute" {
|
||||
assert_eq(@model.volume_icon(100.0, false), "audio-volume-high-symbolic")
|
||||
assert_eq(@model.volume_icon(50.0, false), "audio-volume-medium-symbolic")
|
||||
assert_eq(@model.volume_icon(10.0, false), "audio-volume-low-symbolic")
|
||||
assert_eq(@model.volume_icon(0.0, false), "audio-volume-muted-symbolic")
|
||||
assert_eq(@model.volume_icon(100.0, true), "audio-volume-muted-symbolic")
|
||||
}
|
||||
|
||||
///|
|
||||
test "tab icons" {
|
||||
assert_eq(@model.Tab::Playback.icon(), "audio-speakers-symbolic")
|
||||
assert_eq(@model.Tab::InputDevices.icon(), "audio-input-microphone-symbolic")
|
||||
}
|
||||
@@ -6,6 +6,10 @@ import {
|
||||
}
|
||||
|
||||
// Values
|
||||
pub const APP_LOOKUP_PREFIX : String = "app:"
|
||||
|
||||
pub const ICON_MISSING : String = "image-missing"
|
||||
|
||||
pub const MAX_PERCENT : Double = 150.0
|
||||
|
||||
pub const NORM_PERCENT : Double = 100.0
|
||||
@@ -14,16 +18,28 @@ pub fn clamp_percent(Double) -> Double
|
||||
|
||||
pub fn compare_text(String, String) -> Int32
|
||||
|
||||
pub fn default_button_icons() -> Array[String]
|
||||
|
||||
pub fn device_bus(Node) -> String
|
||||
|
||||
pub fn device_icon_chain(Node) -> Array[String]
|
||||
|
||||
pub fn diff_rows(Array[Row], Array[Row]) -> Diff
|
||||
|
||||
pub fn display_title(Node) -> String
|
||||
|
||||
pub fn form_factor_icon(String, NodeKind) -> String
|
||||
|
||||
pub fn format_db(Double) -> String
|
||||
|
||||
pub fn format_percent(Double) -> String
|
||||
|
||||
pub fn format_volume(Array[Double]) -> String
|
||||
|
||||
pub fn icon_fallback_device() -> Array[String]
|
||||
|
||||
pub fn icon_fallback_stream() -> Array[String]
|
||||
|
||||
pub fn linear_to_db(Double) -> Double?
|
||||
|
||||
pub fn linear_to_percent(Double) -> Double
|
||||
@@ -38,6 +54,12 @@ pub fn scale_linked(Array[Double], Double) -> Array[Double]
|
||||
|
||||
pub fn should_apply_remote(Int64, Int64, window_us? : Int64) -> Bool
|
||||
|
||||
pub fn stream_icon_chain(Node) -> Array[String]
|
||||
|
||||
pub fn symbolic(String) -> String
|
||||
|
||||
pub fn volume_icon(Double, Bool) -> String
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
@@ -78,12 +100,16 @@ pub(all) struct Node {
|
||||
media_class : String
|
||||
app_name : String
|
||||
media_name : String
|
||||
icon_name : String
|
||||
icon_hints : Array[String]
|
||||
form_factor : String
|
||||
bus : String
|
||||
app_binary : String
|
||||
app_id : String
|
||||
volumes : Array[Double]
|
||||
mute : Bool
|
||||
}
|
||||
pub fn Node::kind(Self) -> NodeKind
|
||||
pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_name? : String, volumes? : Array[Double], mute? : Bool) -> Self
|
||||
pub fn Node::new(Int32, String, name? : String, description? : String, app_name? : String, media_name? : String, icon_hints? : Array[String], form_factor? : String, bus? : String, app_binary? : String, app_id? : String, volumes? : Array[Double], mute? : Bool) -> Self
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) enum NodeKind {
|
||||
@@ -100,7 +126,8 @@ pub(all) struct Row {
|
||||
id : Int32
|
||||
title : String
|
||||
subtitle : String
|
||||
icon_name : String
|
||||
icons : Array[String]
|
||||
volume_icon : String
|
||||
volumes : Array[Double]
|
||||
percent : Double
|
||||
volume_text : String
|
||||
@@ -131,6 +158,7 @@ pub(all) enum Tab {
|
||||
}
|
||||
pub fn Tab::all() -> Array[Self]
|
||||
pub fn Tab::empty_text(Self) -> String
|
||||
pub fn Tab::icon(Self) -> String
|
||||
pub fn Tab::label(Self) -> String
|
||||
pub fn Tab::node_kind(Self) -> NodeKind
|
||||
|
||||
|
||||
+17
-3
@@ -33,7 +33,13 @@ pub(all) struct Node {
|
||||
media_class : String
|
||||
app_name : String // application.name (streams)
|
||||
media_name : String // media.name (streams)
|
||||
icon_name : String // application.icon-name / device.icon-name
|
||||
/// Icon names PipeWire/the app suggested, best first (application.icon-name,
|
||||
/// media.icon-name for streams; device.icon-name, node.icon-name for devices).
|
||||
icon_hints : Array[String]
|
||||
form_factor : String // device.form-factor: "headset", "webcam", ...
|
||||
bus : String // device.bus: "usb", "bluetooth", "pci", ...
|
||||
app_binary : String // application.process.binary
|
||||
app_id : String // pipewire.access.portal.app_id / application.id
|
||||
volumes : Array[Double] // linear, one per channel; empty = no volume
|
||||
mute : Bool
|
||||
}
|
||||
@@ -51,7 +57,11 @@ pub fn Node::new(
|
||||
description~ : String = "",
|
||||
app_name~ : String = "",
|
||||
media_name~ : String = "",
|
||||
icon_name~ : String = "",
|
||||
icon_hints~ : Array[String] = [],
|
||||
form_factor~ : String = "",
|
||||
bus~ : String = "",
|
||||
app_binary~ : String = "",
|
||||
app_id~ : String = "",
|
||||
volumes~ : Array[Double] = [],
|
||||
mute~ : Bool = false,
|
||||
) -> Node {
|
||||
@@ -62,7 +72,11 @@ pub fn Node::new(
|
||||
media_class,
|
||||
app_name,
|
||||
media_name,
|
||||
icon_name,
|
||||
icon_hints,
|
||||
form_factor,
|
||||
bus,
|
||||
app_binary,
|
||||
app_id,
|
||||
volumes,
|
||||
mute,
|
||||
}
|
||||
|
||||
+7
-15
@@ -60,7 +60,9 @@ pub(all) struct Row {
|
||||
id : Int32
|
||||
title : String
|
||||
subtitle : String
|
||||
icon_name : 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)"
|
||||
@@ -125,11 +127,12 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
||||
id: n.id,
|
||||
title: display_title(n),
|
||||
subtitle,
|
||||
icon_name: if n.icon_name != "" {
|
||||
n.icon_name
|
||||
icons: if is_stream {
|
||||
stream_icon_chain(n)
|
||||
} else {
|
||||
default_icon(kind)
|
||||
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 {
|
||||
@@ -150,17 +153,6 @@ pub fn rows(state : State, tab : Tab) -> Array[Row] {
|
||||
})
|
||||
}
|
||||
|
||||
///|
|
||||
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.
|
||||
|
||||
+2
-1
@@ -33,7 +33,8 @@ test "device rows" {
|
||||
assert_eq(ins.length(), 1)
|
||||
assert_true(ins[0].mute)
|
||||
assert_true(ins[0].is_default)
|
||||
assert_eq(ins[0].icon_name, "audio-input-microphone-symbolic")
|
||||
assert_eq(ins[0].icons[0], "audio-input-microphone-symbolic")
|
||||
assert_eq(ins[0].volume_icon, "audio-volume-muted-symbolic")
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
Reference in new issue
Block a user