From e0a6a30eaaf85b584858a48f3cf6411ba2059d3b Mon Sep 17 00:00:00 2001 From: Lucy Date: Fri, 11 Mar 2022 11:06:41 -0500 Subject: [PATCH] Now displays proper name and volume --- Cargo.lock | 1 + applets/cosmic-applet-audio/Cargo.toml | 1 + applets/cosmic-applet-audio/src/app.rs | 45 +++++++++++++++++++++----- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 937e5d15..75007e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,6 +240,7 @@ dependencies = [ "futures-util", "gtk4", "libcosmic-widgets", + "libpulse-binding", "once_cell", "pulsectl-rs", "relm4", diff --git a/applets/cosmic-applet-audio/Cargo.toml b/applets/cosmic-applet-audio/Cargo.toml index 3b9af854..7b2a6b1c 100644 --- a/applets/cosmic-applet-audio/Cargo.toml +++ b/applets/cosmic-applet-audio/Cargo.toml @@ -8,6 +8,7 @@ license = "LGPL-3.0-or-later" futures-util = "0.3.21" gtk4 = "0.4.6" libcosmic-widgets = { git = "https://github.com/pop-os/libcosmic", branch = "lucy/widgets" } +libpulse-binding = "2.26.0" once_cell = "1.10.0" pulsectl-rs = "0.3.2" relm4 = { git = "https://github.com/AaronErhardt/relm4", branch = "new-approach", features = ["macros"] } diff --git a/applets/cosmic-applet-audio/src/app.rs b/applets/cosmic-applet-audio/src/app.rs index a9c29304..f7e02a9d 100644 --- a/applets/cosmic-applet-audio/src/app.rs +++ b/applets/cosmic-applet-audio/src/app.rs @@ -3,21 +3,44 @@ use gtk4::{ RevealerTransitionType, Scale, Separator, Stack, Window, }; use libcosmic_widgets::LabeledItem; -use pulsectl::controllers::types::DeviceInfo; +use libpulse_binding::volume::Volume; +use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController}; use relm4::{Component, ComponentParts, Sender}; -#[derive(Default)] pub struct App { + input_controller: SourceController, default_input: Option, inputs: Vec, + output_controller: SinkController, default_output: Option, outputs: Vec, } +impl Default for App { + fn default() -> Self { + let mut input_controller = + SourceController::create().expect("failed to create input controller"); + let default_input = input_controller.get_default_device().ok(); + let inputs = input_controller.list_devices().unwrap_or_default(); + let mut output_controller = + SinkController::create().expect("failed to create output controller"); + let default_output = output_controller.get_default_device().ok(); + let outputs = output_controller.list_devices().unwrap_or_default(); + Self { + input_controller, + default_input, + inputs, + output_controller, + default_output, + outputs, + } + } +} + impl App { pub fn get_default_input_name(&self) -> &str { match &self.default_input { - Some(input) => match &input.name { + Some(input) => match &input.description { Some(name) => name.as_str(), None => "Input Device", }, @@ -27,7 +50,7 @@ impl App { pub fn get_default_output_name(&self) -> &str { match &self.default_output { - Some(output) => match &output.name { + Some(output) => match &output.description { Some(name) => name.as_str(), None => "Output Device", }, @@ -64,13 +87,17 @@ impl Component for App { type Widgets = Widgets; fn init_root() -> Self::Root { - Window::default() + Window::builder() + .title("COSMIC Network Applet") + .default_width(400) + .default_height(300) + .build() } fn init_parts( _args: Self::InitParams, root: &Self::Root, - input: &Sender, + _input: &Sender, _output: &Sender, ) -> ComponentParts { let model = App::default(); @@ -88,8 +115,9 @@ impl Component for App { set_format_value_func: |_, value| { format!("{:.0}%", value) }, + set_value: model.default_output.as_ref().map(|info| dbg!((info.volume.avg().0 as f64 / Volume::NORMAL.0 as f64) * 100.)).unwrap_or(0.), set_value_pos: PositionType::Right, - set_digits: 0 + set_hexpand: true } }, append: input_box = &GtkBox { @@ -102,8 +130,9 @@ impl Component for App { set_format_value_func: |_, value| { format!("{:.0}%", value) }, + set_value: model.default_input.as_ref().map(|info| (info.volume.avg().0 as f64 / Volume::NORMAL.0 as f64) * 100.).unwrap_or(0.), set_value_pos: PositionType::Right, - set_digits: 0 + set_hexpand: true } }, append: _sep = &Separator {