audio: Use volume subscription to detect changes

Fixes https://github.com/pop-os/cosmic-applets/issues/301.

Should be combined with the subscription code here to avoid duplication,
and two things using pulse.
This commit is contained in:
Ian Douglas Scott 2024-06-17 17:49:53 -07:00 committed by Ian Douglas Scott
parent 35491b80a2
commit 78b734afc2
5 changed files with 33 additions and 24 deletions

4
Cargo.lock generated
View file

@ -929,6 +929,7 @@ dependencies = [
name = "cosmic-applet-audio"
version = "0.1.1"
dependencies = [
"cosmic-settings-subscriptions",
"cosmic-time",
"i18n-embed 0.14.1",
"i18n-embed-fl 0.8.0",
@ -964,7 +965,6 @@ dependencies = [
"tracing-log",
"tracing-subscriber",
"udev",
"upower_dbus",
"zbus 4.2.2",
]
@ -5308,7 +5308,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smithay-client-toolkit"
version = "0.18.0"
source = "git+https://github.com/smithay/client-toolkit?rev=3bed072#3bed072b966022f5f929d12f3aff089b1ace980b"
source = "git+https://github.com/smithay/client-toolkit//?rev=3bed072#3bed072b966022f5f929d12f3aff089b1ace980b"
dependencies = [
"bitflags 2.5.0",
"bytemuck",

View file

@ -26,6 +26,7 @@ cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-c
cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols", default-features = false, features = [
"client",
], rev = "c8d3a1c" }
cosmic-settings-subscriptions = { git = "https://github.com/pop-os/cosmic-settings-subscriptions" }
cosmic-time = { git = "https://github.com/pop-os/cosmic-time", default-features = false, features = [
"libcosmic",
"once_cell",

View file

@ -5,6 +5,7 @@ edition = "2021"
license = "GPL-3.0"
[dependencies]
cosmic-settings-subscriptions.workspace = true
cosmic-time.workspace = true
i18n-embed-fl.workspace = true
i18n-embed.workspace = true

View file

@ -32,6 +32,7 @@ use cosmic::widget::Row;
use cosmic::widget::{divider, icon};
use cosmic::Renderer;
use cosmic::{Element, Theme};
use cosmic_settings_subscriptions::pulse as sub_pulse;
use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline};
use iced::wayland::popup::{destroy_popup, get_popup};
use iced::widget::container;
@ -64,6 +65,10 @@ pub struct Audio {
current_input: Option<DeviceInfo>,
outputs: Vec<DeviceInfo>,
inputs: Vec<DeviceInfo>,
sink_mute: bool,
sink_volume: u32,
source_mute: bool,
source_volume: u32,
pulse_state: PulseState,
popup: Option<window::Id>,
timeline: Timeline,
@ -78,19 +83,13 @@ impl Audio {
}
fn output_icon_name(&self) -> &'static str {
let Some(output) = self.current_output.as_ref() else {
return "audio-volume-muted-symbolic";
};
let volume = output.volume.avg();
let output_volume = volume_to_percent(volume);
if volume.is_muted() {
if self.sink_mute || self.sink_volume == 0 {
"audio-volume-muted-symbolic"
} else if output_volume < 33.0 {
} else if self.sink_volume < 33 {
"audio-volume-low-symbolic"
} else if output_volume < 66.0 {
} else if self.sink_volume < 66 {
"audio-volume-medium-symbolic"
} else if output_volume <= 100.0 {
} else if self.sink_volume <= 100 {
"audio-volume-high-symbolic"
} else {
"audio-volume-overamplified-symbolic"
@ -102,17 +101,11 @@ impl Audio {
}
fn input_icon_name(&self) -> &'static str {
let Some(input) = self.current_input.as_ref() else {
return "microphone-sensitivity-muted-symbolic";
};
let volume = input.volume.avg();
let input_volume = volume_to_percent(volume);
if volume.is_muted() || input_volume == 0.0 {
if self.source_mute || self.source_volume == 0 {
"microphone-sensitivity-muted-symbolic"
} else if input_volume < 33.0 {
} else if self.source_volume < 33 {
"microphone-sensitivity-low-symbolic"
} else if input_volume < 66.0 {
} else if self.source_volume < 66 {
"microphone-sensitivity-medium-symbolic"
} else {
"microphone-sensitivity-high-symbolic"
@ -145,6 +138,7 @@ pub enum Message {
MprisRequest(MprisRequest),
Token(TokenUpdate),
OpenSettings,
PulseSub(sub_pulse::Event),
}
impl Audio {
@ -540,6 +534,20 @@ impl cosmic::Application for Audio {
cosmic::process::spawn(cmd);
}
},
Message::PulseSub(event) => match event {
sub_pulse::Event::SinkVolume(value) => {
self.sink_volume = value;
}
sub_pulse::Event::SinkMute(value) => {
self.sink_mute = value;
}
sub_pulse::Event::SourceVolume(value) => {
self.source_volume = value;
}
sub_pulse::Event::SourceMute(value) => {
self.source_mute = value;
}
},
};
Command::none()
@ -559,6 +567,7 @@ impl cosmic::Application for Audio {
}),
mpris_subscription::mpris_subscription(0).map(Message::Mpris),
activation_token_subscription(0).map(Message::Token),
sub_pulse::subscription().map(Message::PulseSub),
])
}

View file

@ -5,6 +5,7 @@ edition = "2021"
license = "GPL-3.0"
[dependencies]
cosmic-settings-subscriptions.workspace = true
cosmic-time.workspace = true
drm = "0.11.1"
futures.workspace = true
@ -19,6 +20,3 @@ tracing-subscriber.workspace = true
tracing.workspace = true
udev = "0.8"
zbus.workspace = true
# TODO branch
upower_dbus = { git = "https://github.com/pop-os/dbus-settings-bindings", branch = "upower" }
cosmic-settings-subscriptions = { git = "https://github.com/pop-os/cosmic-settings-subscriptions" }