audio: Fix clippy lints

This commit is contained in:
Ian Douglas Scott 2023-01-05 09:55:44 -08:00
parent 5c16665b7b
commit b9a2655210
2 changed files with 46 additions and 39 deletions

View file

@ -22,7 +22,7 @@ use iced::Color;
mod pulse; mod pulse;
use crate::pulse::DeviceInfo; use crate::pulse::DeviceInfo;
use libpulse_binding::volume::{Volume, VolumeLinear}; use libpulse_binding::volume::VolumeLinear;
pub fn main() -> cosmic::iced::Result { pub fn main() -> cosmic::iced::Result {
let helper = CosmicAppletHelper::default(); let helper = CosmicAppletHelper::default();
@ -134,7 +134,7 @@ impl Application for Audio {
if let Some(device) = &self.current_output { if let Some(device) = &self.current_output {
if let Some(name) = &device.name { if let Some(name) = &device.name {
connection.send(pulse::Message::SetSinkVolumeByName( connection.send(pulse::Message::SetSinkVolumeByName(
name.clone().to_string(), name.clone(),
device.volume, device.volume,
)) ))
} }
@ -151,7 +151,7 @@ impl Application for Audio {
if let Some(name) = &device.name { if let Some(name) = &device.name {
println!("increasing volume of {}", name); println!("increasing volume of {}", name);
connection.send(pulse::Message::SetSourceVolumeByName( connection.send(pulse::Message::SetSourceVolumeByName(
name.clone().to_string(), name.clone(),
device.volume, device.volume,
)) ))
} }
@ -241,28 +241,40 @@ impl Application for Audio {
self.current_output self.current_output
.as_ref() .as_ref()
.map(|o| o.volume.avg()) .map(|o| o.volume.avg())
.unwrap_or(Volume::default()), .unwrap_or_default(),
) )
.0 * 100.0; .0 * 100.0;
let in_f64 = VolumeLinear::from( let in_f64 = VolumeLinear::from(
self.current_input self.current_input
.as_ref() .as_ref()
.map(|o| o.volume.avg()) .map(|o| o.volume.avg())
.unwrap_or(Volume::default()), .unwrap_or_default(),
) )
.0 * 100.0; .0 * 100.0;
let sink = row![ let sink = row![
icon("audio-volume-high-symbolic", 64).width(Length::Units(24)).height(Length::Units(24)).style(Svg::SymbolicActive), icon("audio-volume-high-symbolic", 64)
slider(0.0..=100.0, out_f64, Message::SetOutputVolume).width(Length::FillPortion(5)), .width(Length::Units(24))
text(format!("{}%", out_f64.round())).width(Length::FillPortion(1)).horizontal_alignment(Horizontal::Right) .height(Length::Units(24))
.style(Svg::SymbolicActive),
slider(0.0..=100.0, out_f64, Message::SetOutputVolume)
.width(Length::FillPortion(5)),
text(format!("{}%", out_f64.round()))
.width(Length::FillPortion(1))
.horizontal_alignment(Horizontal::Right)
] ]
.spacing(10) .spacing(10)
.align_items(Alignment::Center); .align_items(Alignment::Center);
let source = row![ let source = row![
icon("audio-input-microphone-symbolic", 64).width(Length::Units(24)).height(Length::Units(24)).style(Svg::SymbolicActive), icon("audio-input-microphone-symbolic", 64)
slider(0.0..=100.0, in_f64, Message::SetInputVolume).width(Length::FillPortion(5)), .width(Length::Units(24))
text(format!("{}%", in_f64.round())).width(Length::FillPortion(1)).horizontal_alignment(Horizontal::Right) .height(Length::Units(24))
.style(Svg::SymbolicActive),
slider(0.0..=100.0, in_f64, Message::SetInputVolume)
.width(Length::FillPortion(5)),
text(format!("{}%", in_f64.round()))
.width(Length::FillPortion(1))
.horizontal_alignment(Horizontal::Right)
] ]
.spacing(10) .spacing(10)
.align_items(Alignment::Center); .align_items(Alignment::Center);
@ -322,14 +334,14 @@ fn spacer() -> iced::widget::Space {
Space::with_width(Length::Fill) Space::with_width(Length::Fill)
} }
fn revealer<'a>( fn revealer(
open: bool, open: bool,
title: &'a str, title: &str,
selected: String, selected: String,
options: Vec<String>, options: Vec<String>,
toggle: Message, toggle: Message,
_change: Message, _change: Message,
) -> widget::Column<'a, Message, Renderer> { ) -> widget::Column<Message, Renderer> {
if open { if open {
options.iter().fold( options.iter().fold(
column![revealer_head(open, title, selected, toggle)].width(Length::Fill), column![revealer_head(open, title, selected, toggle)].width(Length::Fill),
@ -340,9 +352,9 @@ fn revealer<'a>(
} }
} }
fn revealer_head<'a>( fn revealer_head(
_open: bool, _open: bool,
title: &'a str, title: &str,
selected: String, selected: String,
toggle: Message, toggle: Message,
) -> widget::Button<Message, Renderer> { ) -> widget::Button<Message, Renderer> {

View file

@ -8,7 +8,6 @@ use libpulse_binding::{
callbacks::ListResult, callbacks::ListResult,
context::{ context::{
introspect::{Introspector, SinkInfo, SourceInfo}, introspect::{Introspector, SinkInfo, SourceInfo},
subscribe::{Facility, InterestMaskSet, Operation},
Context, Context,
}, },
error::PAErr, error::PAErr,
@ -80,8 +79,7 @@ pub struct Connection(tokio::sync::mpsc::Sender<Message>);
impl Connection { impl Connection {
pub fn send(&mut self, message: Message) { pub fn send(&mut self, message: Message) {
let _ = self self.0
.0
.try_send(message) .try_send(message)
.expect("Send message to PulseAudio server"); .expect("Send message to PulseAudio server");
} }
@ -198,7 +196,7 @@ impl PulseHandle {
} }
fn blocking_send_disconnected(sender: &mut tokio::sync::mpsc::Sender<Message>) { fn blocking_send_disconnected(sender: &mut tokio::sync::mpsc::Sender<Message>) {
sender.blocking_send(Message::Disconnected); sender.blocking_send(Message::Disconnected).unwrap()
} }
fn blocking_send_connected(sender: &mut tokio::sync::mpsc::Sender<Message>) { fn blocking_send_connected(sender: &mut tokio::sync::mpsc::Sender<Message>) {
@ -209,6 +207,7 @@ impl PulseHandle {
sender.send(Message::Disconnected).await.unwrap() sender.send(Message::Disconnected).await.unwrap()
} }
#[allow(dead_code)]
async fn send_connected(sender: &mut tokio::sync::mpsc::Sender<Message>) { async fn send_connected(sender: &mut tokio::sync::mpsc::Sender<Message>) {
sender.send(Message::Connected).await.unwrap() sender.send(Message::Connected).await.unwrap()
} }
@ -258,7 +257,7 @@ impl PulseServer {
context context
.borrow_mut() .borrow_mut()
.connect(None, pulse::context::FlagSet::NOFLAGS, None) .connect(None, pulse::context::FlagSet::NOFLAGS, None)
.map_err(|e| PulseServerError::PAErr(e))?; .map_err(PulseServerError::PAErr)?;
Ok(PulseServer { Ok(PulseServer {
mainloop, mainloop,
@ -308,13 +307,11 @@ impl PulseServer {
} }
}, },
); );
self.wait_for_result(operation) self.wait_for_result(operation).and_then(|_| {
.and_then(|_| { list.borrow_mut().take().ok_or(PulseServerError::Misc(
list.borrow_mut().take().ok_or(PulseServerError::Misc( "get_sinks(): failed to wait for operation",
"get_sinks(): failed to wait for operation", ))
)) })
})
.and_then(|result| Ok(result))
} }
// Get a list of input devices // Get a list of input devices
@ -329,13 +326,11 @@ impl PulseServer {
} }
}, },
); );
self.wait_for_result(operation) self.wait_for_result(operation).and_then(|_| {
.and_then(|_| { list.borrow_mut().take().ok_or(PulseServerError::Misc(
list.borrow_mut().take().ok_or(PulseServerError::Misc( "get_sources(): Failed to wait for operation",
"get_sources(): Failed to wait for operation", ))
)) })
})
.and_then(|result| Ok(result))
} }
pub fn get_server_info(&mut self) -> Result<ServerInfo, PulseServerError> { pub fn get_server_info(&mut self) -> Result<ServerInfo, PulseServerError> {
@ -355,7 +350,7 @@ impl PulseServer {
let server_info = self.get_server_info(); let server_info = self.get_server_info();
match server_info { match server_info {
Ok(info) => { Ok(info) => {
let name = &info.default_sink_name.unwrap_or(String::new()); let name = &info.default_sink_name.unwrap_or_default();
let device = Rc::new(RefCell::new(Some(None))); let device = Rc::new(RefCell::new(Some(None)));
let dev_ref = device.clone(); let dev_ref = device.clone();
let op = self.introspector.get_sink_info_by_name( let op = self.introspector.get_sink_info_by_name(
@ -368,7 +363,7 @@ impl PulseServer {
); );
self.wait_for_result(op)?; self.wait_for_result(op)?;
let mut result = device.borrow_mut(); let mut result = device.borrow_mut();
result.take().unwrap().ok_or_else(|| { result.take().unwrap().ok_or({
PulseServerError::Misc("get_default_sink(): Error getting requested device") PulseServerError::Misc("get_default_sink(): Error getting requested device")
}) })
} }
@ -380,7 +375,7 @@ impl PulseServer {
let server_info = self.get_server_info(); let server_info = self.get_server_info();
match server_info { match server_info {
Ok(info) => { Ok(info) => {
let name = &info.default_source_name.unwrap_or(String::new()); let name = &info.default_source_name.unwrap_or_default();
let device = Rc::new(RefCell::new(Some(None))); let device = Rc::new(RefCell::new(Some(None)));
let dev_ref = device.clone(); let dev_ref = device.clone();
let op = self.introspector.get_source_info_by_name( let op = self.introspector.get_source_info_by_name(
@ -393,7 +388,7 @@ impl PulseServer {
); );
self.wait_for_result(op)?; self.wait_for_result(op)?;
let mut result = device.borrow_mut(); let mut result = device.borrow_mut();
result.take().unwrap().ok_or_else(|| { result.take().unwrap().ok_or({
PulseServerError::Misc("get_default_source(): Error getting requested device") PulseServerError::Misc("get_default_source(): Error getting requested device")
}) })
} }