From 696ab0a0890ab2dea53b10c6ddea3482fbc845b1 Mon Sep 17 00:00:00 2001 From: 409 Date: Sun, 6 Oct 2024 04:16:19 +0200 Subject: [PATCH] fix(instructions): `RemoveSinkInput` switches selection sometimes --- src/mixer.rs | 62 +++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/mixer.rs b/src/mixer.rs index dc42e69..39abd3c 100644 --- a/src/mixer.rs +++ b/src/mixer.rs @@ -243,57 +243,31 @@ impl Mixer { } } } - PulseInstruction::RemoveSinkInput(sink_index) => { + PulseInstruction::RemoveSinkInput(sink_index) => 'remove_sink: { let selected_index_lock = self.selected_index.lock().unwrap(); - match *selected_index_lock { - Some(current_index) => { - drop(selected_index_lock); + let Some(selected_index) = *selected_index_lock else { + break 'remove_sink; + }; - if let Some(removed_sink_input_index) = - self.sink_inputs.keys().position(|k| *k == sink_index) - { - let current_key = - *self.sink_inputs.keys().nth(current_index).unwrap(); + drop(selected_index_lock); - if self.sink_inputs.remove(&sink_index).is_some() { - if sink_index == current_key - || removed_sink_input_index > current_index - { - self.select_previous(); - } - } - } + let Some(removed_sink_input_index) = + self.sink_inputs.keys().position(|k| *k == sink_index) + else { + break 'remove_sink; + }; + + let current_key = *self.sink_inputs.keys().nth(selected_index).unwrap(); + + if self.sink_inputs.remove(&sink_index).is_some() { + if sink_index == current_key + || removed_sink_input_index <= selected_index + { + self.select_previous(); } - - None => (), } } - PulseInstruction::UpdateSinkInput(sink_index) => { - match self.sink_inputs.get_mut(&sink_index) { - Some(sink_input_mixer_data) => { - let new_sink_input: Arc>> = - Arc::new(Mutex::new(None)); - let callback_new_sink_input = new_sink_input.clone(); - - let operation = self - .context - .borrow_mut() - .introspect() - .borrow_mut() - .get_sink_input_info(sink_index, move |r| { - let ListResult::Item(sink_input) = r else { - return; - }; - - *callback_new_sink_input.lock().unwrap() = - Some(SinkInputMixerData { - name: get_sink_input_name(&sink_input).unwrap(), - volume: sink_input.volume.avg().0, - channels: sink_input.volume.len(), - muted: sink_input.mute, - }); - }); PulseInstruction::UpdateSinkInput(sink_index) => 'update_sink: { let Some(sink_input_mixer_data) = self.sink_inputs.get_mut(&sink_index) else {