Skip to content

Commit 3b4d6b8

Browse files
committed
fix(sound): wrong profile position when profiles are filtered
1 parent 278f24d commit 3b4d6b8

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

cosmic-settings/src/pages/sound/device_profiles.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,34 @@ pub fn view() -> Section<crate::pages::Message> {
8484
.and_then(|profile| {
8585
profiles
8686
.iter()
87+
.filter(|p| {
88+
matches!(
89+
p.available,
90+
pipewire::Availability::Yes
91+
| pipewire::Availability::Unknown
92+
)
93+
})
8794
.enumerate()
8895
.find(|(_, p)| p.index == profile.index)
8996
.map(|(pos, _)| pos)
9097
});
9198

9299
// TODO: cache
93-
let profiles = profiles
100+
let (indexes, profiles): (Vec<_>, Vec<_>) = profiles
94101
.iter()
95102
.filter(|p| {
96103
matches!(
97104
p.available,
98105
pipewire::Availability::Yes | pipewire::Availability::Unknown
99106
)
100107
})
101-
.map(|p| p.description.clone());
108+
.map(|p| (p.index as u32, p.description.clone()))
109+
.collect();
102110

103111
let dropdown = widget::dropdown::popup_dropdown(
104112
Vec::from_iter(profiles),
105113
active_profile,
106-
move |id| super::Message::SetProfile(device_id, id),
114+
move |id| super::Message::SetProfile(device_id, indexes[id]),
107115
cosmic::iced::window::Id::RESERVED,
108116
super::Message::Surface,
109117
crate::Message::from,

cosmic-settings/src/pages/sound/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const AMPLIFICATION_SOURCE: &str = "amplification_source";
2222
#[derive(Clone, Debug)]
2323
pub enum Message {
2424
/// Set the profile of a sound device.
25-
SetProfile(u32, usize),
25+
SetProfile(u32, u32),
2626
/// Change the balance of the active sink.
2727
SinkBalanceChanged(u32),
2828
/// Change the default output.

subscriptions/sound/src/lib.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ pub struct Model {
134134
active_sink_device: Option<u32>,
135135
/// Index of active sink device.
136136
active_sink: Option<usize>,
137-
/// Card profile index of active sink device.
138-
active_sink_profile: Option<usize>,
139137

140138
/** Source devices */
141139

@@ -147,8 +145,6 @@ pub struct Model {
147145
active_source_device: Option<u32>,
148146
/// Index of active source device.
149147
active_source: Option<usize>,
150-
/// Card profile index of active source device.
151-
active_source_profile: Option<usize>,
152148

153149
/// Device identifier of the default sink.
154150
default_sink: String,
@@ -176,18 +172,10 @@ impl Model {
176172
self.active_sink
177173
}
178174

179-
pub fn active_sink_profile(&self) -> Option<usize> {
180-
self.active_sink_profile
181-
}
182-
183175
pub fn active_source(&self) -> Option<usize> {
184176
self.active_source
185177
}
186178

187-
pub fn active_source_profile(&self) -> Option<usize> {
188-
self.active_source_profile
189-
}
190-
191179
pub fn sinks(&self) -> &[String] {
192180
&self.sinks
193181
}
@@ -210,20 +198,16 @@ impl Model {
210198
/// Sets and applies a profile to a device with wpctl.
211199
///
212200
/// Requires using the device ID rather than a node ID.
213-
pub fn set_profile(&mut self, device_id: DeviceId, pos: usize) {
201+
pub fn set_profile(&mut self, device_id: DeviceId, index: u32) {
202+
eprintln!("set profile {device_id} {index}");
214203
if let Some(profiles) = self.device_profiles.get(device_id) {
215-
if let Some(profile) = profiles.get(pos) {
216-
let index = profile.index as u32;
217-
tokio::spawn(async move {
218-
wpctl::set_profile(device_id, index).await;
219-
});
220-
221-
self.active_profiles.insert(device_id, profile.clone());
204+
for profile in profiles {
205+
if profile.index as u32 == index {
206+
tokio::spawn(async move {
207+
wpctl::set_profile(device_id, index).await;
208+
});
222209

223-
if self.active_sink_device == Some(device_id) {
224-
self.active_sink_profile = Some(pos)
225-
} else if self.active_source_device == Some(device_id) {
226-
self.active_source_profile = Some(pos);
210+
self.active_profiles.insert(device_id, profile.clone());
227211
}
228212
}
229213
}

0 commit comments

Comments
 (0)