379 lines
11 KiB
MoonBit
379 lines
11 KiB
MoonBit
// 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. Entries starting with `FILE_ICON_PREFIX` ("file:<path>")
|
|
// point to an icon file directly.
|
|
|
|
///|
|
|
pub fn icon_fallback_device() -> Array[String] {
|
|
[
|
|
"moonmix-device-audio-symbolic", "audio-card-symbolic", "audio-x-generic-symbolic",
|
|
]
|
|
}
|
|
|
|
///|
|
|
pub fn icon_fallback_stream() -> Array[String] {
|
|
[
|
|
"moonmix-app-fallback-symbolic", "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:"
|
|
|
|
///|
|
|
pub const FILE_ICON_PREFIX : String = "file:"
|
|
|
|
///|
|
|
/// True if a resolved icon name or path is a symbolic monochrome asset.
|
|
pub fn is_symbolic_icon(name : String) -> Bool {
|
|
name == ICON_MISSING ||
|
|
name.has_suffix("-symbolic") ||
|
|
name.has_suffix("-symbolic.svg") ||
|
|
name.has_suffix("-symbolic.png")
|
|
}
|
|
|
|
///|
|
|
/// "Default" toggle: first icon the theme has.
|
|
pub fn default_button_icons() -> Array[String] {
|
|
["moonmix-default-symbolic", "emblem-ok-symbolic", "object-select-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) })
|
|
}
|
|
|
|
///|
|
|
/// Bundled Lucide symbolic icon name for a `device.form-factor` value, "" if unknown.
|
|
pub fn bundled_form_factor_icon(
|
|
form_factor : String,
|
|
kind : NodeKind,
|
|
) -> String {
|
|
match form_factor {
|
|
"headphone" => "moonmix-device-headphones-symbolic"
|
|
"headset" => "moonmix-device-headset-symbolic"
|
|
"speaker" | "hifi" => "moonmix-device-speaker-symbolic"
|
|
"microphone" => "moonmix-device-mic-symbolic"
|
|
"webcam" => "moonmix-device-webcam-symbolic"
|
|
"handset" | "hands-free" | "portable" => "moonmix-device-phone-symbolic"
|
|
"tv" | "hdmi" | "computer" => "moonmix-device-display-symbolic"
|
|
"car" =>
|
|
if kind is Source {
|
|
"moonmix-device-mic-symbolic"
|
|
} else {
|
|
"moonmix-device-speaker-symbolic"
|
|
}
|
|
_ => ""
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Map a freedesktop/PipeWire device icon hint to one of our bundled Lucide icons.
|
|
fn bundled_hint_icon(hint : String) -> String {
|
|
let h = hint.to_lower()
|
|
if h.contains("headset") {
|
|
"moonmix-device-headset-symbolic"
|
|
} else if h.contains("headphone") {
|
|
"moonmix-device-headphones-symbolic"
|
|
} else if h.contains("webcam") || h.contains("camera") {
|
|
"moonmix-device-webcam-symbolic"
|
|
} else if h.contains("display") ||
|
|
h.contains("monitor") ||
|
|
h.contains("hdmi") ||
|
|
h.contains("tv") {
|
|
"moonmix-device-display-symbolic"
|
|
} else if h.contains("phone") {
|
|
"moonmix-device-phone-symbolic"
|
|
} else if h.contains("microphone") {
|
|
"moonmix-device-mic-symbolic"
|
|
} else if h.contains("speaker") {
|
|
"moonmix-device-speaker-symbolic"
|
|
} else if h.contains("bluetooth") {
|
|
"moonmix-device-bluetooth-symbolic"
|
|
} else if h.contains("usb") {
|
|
"moonmix-device-usb-symbolic"
|
|
} else {
|
|
""
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Standard freedesktop 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: bundled crisp Lucide symbolic icons first,
|
|
/// keeping standard freedesktop names as secondary fallbacks.
|
|
pub fn device_icon_chain(n : Node) -> Array[String] {
|
|
let kind = n.kind()
|
|
let out : Array[String] = []
|
|
let text = n.name + " " + n.description
|
|
let bus = device_bus(n)
|
|
let is_hdmi = has_any(text, ["hdmi", "displayport", "display port"])
|
|
let is_webcam = kind is Source &&
|
|
(
|
|
has_any(n.description, ["webcam", "camera"]) ||
|
|
(bus == "usb" && n.description.to_lower().contains("cam"))
|
|
)
|
|
|
|
// Phase 1: bundled Lucide symbolic icons
|
|
push_unique(out, bundled_form_factor_icon(n.form_factor, kind))
|
|
if is_hdmi {
|
|
push_unique(out, "moonmix-device-display-symbolic")
|
|
}
|
|
if is_webcam {
|
|
push_unique(out, "moonmix-device-webcam-symbolic")
|
|
}
|
|
for h in n.icon_hints {
|
|
push_unique(out, bundled_hint_icon(h))
|
|
}
|
|
if has_any(text, ["headset"]) {
|
|
push_unique(out, "moonmix-device-headset-symbolic")
|
|
}
|
|
if has_any(text, ["headphone", "earbud"]) {
|
|
push_unique(out, "moonmix-device-headphones-symbolic")
|
|
}
|
|
if bus == "bluetooth" {
|
|
push_unique(out, "moonmix-device-bluetooth-symbolic")
|
|
} else if bus == "usb" {
|
|
push_unique(out, "moonmix-device-usb-symbolic")
|
|
}
|
|
match kind {
|
|
Source => push_unique(out, "moonmix-device-mic-symbolic")
|
|
Sink => push_unique(out, "moonmix-device-speaker-symbolic")
|
|
_ => push_unique(out, "moonmix-device-audio-symbolic")
|
|
}
|
|
push_unique(out, "moonmix-device-audio-symbolic")
|
|
|
|
// Phase 2: freedesktop secondary fallbacks
|
|
for h in n.icon_hints {
|
|
push_symbolic_first(out, h)
|
|
}
|
|
push_symbolic_first(out, form_factor_icon(n.form_factor, kind))
|
|
if is_hdmi {
|
|
push_symbolic_first(out, "video-display")
|
|
}
|
|
if is_webcam {
|
|
push_symbolic_first(out, "camera-web")
|
|
}
|
|
match (bus, kind) {
|
|
("bluetooth", Source) => push_symbolic_first(out, "audio-headset")
|
|
("bluetooth", _) => push_symbolic_first(out, "audio-headphones")
|
|
_ => ()
|
|
}
|
|
match kind {
|
|
Source => push_symbolic_first(out, "audio-input-microphone")
|
|
_ => push_symbolic_first(out, "audio-speakers")
|
|
}
|
|
for f in icon_fallback_device() {
|
|
push_unique(out, f)
|
|
}
|
|
push_unique(out, ICON_MISSING)
|
|
out
|
|
}
|
|
|
|
///|
|
|
/// Icon chain for an application stream: prefer real full-colour host app
|
|
/// icons first (non-symbolic hints, .desktop lookup, direct binary/app_id),
|
|
/// then any symbolic hints, then crisp bundled fallbacks.
|
|
pub fn stream_icon_chain(n : Node) -> Array[String] {
|
|
let out : Array[String] = []
|
|
// 1. Non-symbolic icon hints (including absolute file paths).
|
|
for h in n.icon_hints {
|
|
if h.has_prefix("/") {
|
|
if !is_symbolic_icon(h) {
|
|
push_unique(out, FILE_ICON_PREFIX + h)
|
|
}
|
|
} else if !h.has_suffix("-symbolic") {
|
|
push_unique(out, h)
|
|
}
|
|
}
|
|
// 2. Installed .desktop lookup (prefers full-colour icons on the GTK side).
|
|
for key in [n.app_id, n.app_binary, n.app_name] {
|
|
if key != "" {
|
|
push_unique(out, APP_LOOKUP_PREFIX + key)
|
|
}
|
|
}
|
|
// 3. Direct non-symbolic icon-theme lookup by app id / binary / lower name.
|
|
for key in [n.app_id, n.app_binary, n.app_name.to_lower()] {
|
|
if key != "" &&
|
|
!key.contains(" ") &&
|
|
!key.contains("/") &&
|
|
!key.has_suffix("-symbolic") {
|
|
push_unique(out, key)
|
|
}
|
|
}
|
|
// 4. Symbolic hints only if no full-colour app icon was found.
|
|
for h in n.icon_hints {
|
|
if h.has_prefix("/") {
|
|
if is_symbolic_icon(h) {
|
|
push_unique(out, FILE_ICON_PREFIX + h)
|
|
}
|
|
} else if h.has_suffix("-symbolic") {
|
|
push_unique(out, h)
|
|
}
|
|
}
|
|
// 5-8. Record-stream mic, bundled fallback, freedesktop fallback, placeholder.
|
|
if n.kind() is RecordStream {
|
|
push_unique(out, "moonmix-device-mic-symbolic")
|
|
}
|
|
for f in icon_fallback_stream() {
|
|
push_unique(out, f)
|
|
}
|
|
if n.kind() is RecordStream {
|
|
push_unique(out, "audio-input-microphone-symbolic")
|
|
}
|
|
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 {
|
|
"moonmix-volume-muted-symbolic"
|
|
} else if percent < 34.0 {
|
|
"moonmix-volume-low-symbolic"
|
|
} else if percent < 67.0 {
|
|
"moonmix-volume-medium-symbolic"
|
|
} else {
|
|
"moonmix-volume-high-symbolic"
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Volume icon resolution chain: bundled Lucide symbolic first, then
|
|
/// standard freedesktop fallbacks.
|
|
pub fn volume_icon_chain(percent : Double, mute : Bool) -> Array[String] {
|
|
if mute || percent <= 0.0 {
|
|
[
|
|
"moonmix-volume-muted-symbolic", "audio-volume-muted-symbolic", "audio-volume-muted",
|
|
]
|
|
} else if percent < 34.0 {
|
|
[
|
|
"moonmix-volume-low-symbolic", "audio-volume-low-symbolic", "audio-volume-low",
|
|
]
|
|
} else if percent < 67.0 {
|
|
[
|
|
"moonmix-volume-medium-symbolic", "audio-volume-medium-symbolic", "audio-volume-medium",
|
|
]
|
|
} else {
|
|
[
|
|
"moonmix-volume-high-symbolic", "audio-volume-high-symbolic", "audio-volume-high",
|
|
]
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Small symbolic icon next to a tab's label.
|
|
pub fn Tab::icon(self : Tab) -> String {
|
|
match self {
|
|
Playback => "moonmix-tab-playback-symbolic"
|
|
Recording => "moonmix-tab-recording-symbolic"
|
|
OutputDevices => "moonmix-tab-output-symbolic"
|
|
InputDevices => "moonmix-tab-input-symbolic"
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Tab icon resolution chain: bundled Lucide symbolic first, then
|
|
/// standard freedesktop fallbacks.
|
|
pub fn Tab::icon_chain(self : Tab) -> Array[String] {
|
|
match self {
|
|
Playback =>
|
|
[
|
|
"moonmix-tab-playback-symbolic", "multimedia-volume-control-symbolic", "audio-speakers-symbolic",
|
|
"audio-x-generic",
|
|
]
|
|
Recording =>
|
|
[
|
|
"moonmix-tab-recording-symbolic", "audio-input-microphone-symbolic", "audio-x-generic",
|
|
]
|
|
OutputDevices =>
|
|
[
|
|
"moonmix-tab-output-symbolic", "audio-speakers-symbolic", "audio-card-symbolic",
|
|
"audio-x-generic",
|
|
]
|
|
InputDevices =>
|
|
[
|
|
"moonmix-tab-input-symbolic", "audio-input-microphone-symbolic", "audio-card-symbolic",
|
|
"audio-x-generic",
|
|
]
|
|
}
|
|
}
|