Now displays proper name and volume

This commit is contained in:
Lucy 2022-03-11 11:06:41 -05:00
parent 32375abdef
commit e0a6a30eaa
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
3 changed files with 39 additions and 8 deletions

1
Cargo.lock generated
View file

@ -240,6 +240,7 @@ dependencies = [
"futures-util",
"gtk4",
"libcosmic-widgets",
"libpulse-binding",
"once_cell",
"pulsectl-rs",
"relm4",

View file

@ -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"] }

View file

@ -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<DeviceInfo>,
inputs: Vec<DeviceInfo>,
output_controller: SinkController,
default_output: Option<DeviceInfo>,
outputs: Vec<DeviceInfo>,
}
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<Self::Input>,
_input: &Sender<Self::Input>,
_output: &Sender<Self::Output>,
) -> ComponentParts<Self> {
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 {