From 660c19624bb89505ac705ab7378adeeebf19e608 Mon Sep 17 00:00:00 2001 From: Lucy Date: Wed, 23 Mar 2022 13:36:09 -0400 Subject: [PATCH] Changing audio from an external source works now! --- Cargo.lock | 12 +++--- applets/cosmic-applet-audio/Cargo.toml | 4 +- applets/cosmic-applet-audio/src/app.rs | 51 +++++++++++++++--------- applets/cosmic-applet-network/Cargo.toml | 2 +- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4655c1f..f19c6fe6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -237,8 +237,8 @@ dependencies = [ name = "cosmic-applet-audio" version = "0.1.0" dependencies = [ + "async-io", "futures-util", - "gtk4", "libcosmic-widgets", "libpulse-binding", "pulsectl-rs", @@ -251,7 +251,7 @@ version = "0.1.0" dependencies = [ "gtk4", "once_cell", - "relm4-macros 0.4.2", + "relm4-macros 0.4.3", "tokio", "zbus", ] @@ -266,7 +266,7 @@ dependencies = [ "itertools", "libcosmic-widgets", "once_cell", - "relm4-macros 0.4.2", + "relm4-macros 0.4.3", "slotmap", "tokio", "zbus", @@ -281,7 +281,7 @@ dependencies = [ "logind-zbus", "nix 0.23.1", "once_cell", - "relm4-macros 0.4.2", + "relm4-macros 0.4.3", "tokio", "zbus", ] @@ -1519,9 +1519,9 @@ dependencies = [ [[package]] name = "relm4-macros" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d70f60fbeae94001a45d0a2f7ff66b4c18535cc00ebd746d2ced0be1916cf83f" +checksum = "b0df3a77e22dee74ce668daa437252e02a19252ae0eda49ebebb20b365afc52a" dependencies = [ "proc-macro2", "quote", diff --git a/applets/cosmic-applet-audio/Cargo.toml b/applets/cosmic-applet-audio/Cargo.toml index c5dc3700..7d6266c2 100644 --- a/applets/cosmic-applet-audio/Cargo.toml +++ b/applets/cosmic-applet-audio/Cargo.toml @@ -5,9 +5,11 @@ edition = "2021" license = "LGPL-3.0-or-later" [dependencies] +async-io = "1.6.0" 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" pulsectl-rs = "0.3.2" relm4 = { git = "https://github.com/AaronErhardt/relm4", branch = "new-approach", features = ["macros"] } + +[features] diff --git a/applets/cosmic-applet-audio/src/app.rs b/applets/cosmic-applet-audio/src/app.rs index cfb2acb2..5bfb5bdd 100644 --- a/applets/cosmic-applet-audio/src/app.rs +++ b/applets/cosmic-applet-audio/src/app.rs @@ -1,7 +1,4 @@ -use gtk4::{ - prelude::*, Box as GtkBox, Button, Image, Label, ListBox, Orientation, PositionType, Revealer, - RevealerTransitionType, Scale, Separator, Window, -}; +use futures_util::StreamExt; use libcosmic_widgets::LabeledItem; use libpulse_binding::{ context::subscribe::{Facility, InterestMaskSet, Operation}, @@ -11,7 +8,17 @@ use pulsectl::{ controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController}, Handler, }; -use relm4::{component, view, ComponentParts, RelmContainerExt, Sender, SimpleComponent}; +use relm4::{ + component, + gtk::{ + self, + glib::{self, clone}, + prelude::*, + Box as GtkBox, Button, Image, Label, ListBox, Orientation, PositionType, Revealer, + RevealerTransitionType, Scale, Separator, Window, + }, + view, ComponentParts, RelmContainerExt, Sender, SimpleComponent, +}; use std::rc::Rc; pub enum AppInput { @@ -41,6 +48,13 @@ impl Default for App { let outputs = output_controller.list_devices().unwrap_or_default(); let handler = Handler::connect("com.system76.cosmic.applets.audio") .expect("failed to connect to pulse"); + relm4::spawn_local(clone!(@weak handler.mainloop as main_loop => async move { + let mut timer = async_io::Timer::interval(std::time::Duration::from_millis(100)); + loop { + main_loop.borrow_mut().iterate(false); + timer.next().await; + } + })); Self { default_input, inputs, @@ -100,11 +114,10 @@ impl App { let mut context = self.handler.context.borrow_mut(); let input = input.clone(); context.set_subscribe_callback(Some(Box::new(move |facility, operation, _idx| { - match dbg!(operation) { - Some(Operation::Changed) => {} - _ => return, + if !matches!(operation, Some(Operation::Changed)) { + return; } - match dbg!(facility) { + match facility { Some(Facility::Sink | Facility::SinkInput) => { send!(input, AppInput::OutputVolume); } @@ -114,9 +127,7 @@ impl App { _ => {} } }))); - context.subscribe(InterestMaskSet::all(), |success| { - println!("success: {}", success); - }); + context.subscribe(InterestMaskSet::all(), |_| {}); } fn update_inputs(&self, widgets: &AppWidgets) { @@ -213,7 +224,7 @@ impl SimpleComponent for App { set_format_value_func: |_, value| { format!("{:.0}%", value) }, - set_value: model.default_output.as_ref().map(|info| (info.volume.avg().0 as f64 / Volume::NORMAL.0 as f64) * 100.).unwrap_or(0.), + set_value: watch! { model.default_output.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_hexpand: true } @@ -228,10 +239,12 @@ impl SimpleComponent 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: watch! { + 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_hexpand: true } @@ -253,7 +266,7 @@ impl SimpleComponent for App { append: outputs_revealer = &Revealer { set_transition_type: RevealerTransitionType::SlideDown, set_child: outputs = Some(&ListBox) { - set_selection_mode: gtk4::SelectionMode::None, + set_selection_mode: gtk::SelectionMode::None, set_activate_on_single_click: true } } @@ -275,7 +288,7 @@ impl SimpleComponent for App { append: inputs_revealer = &Revealer { set_transition_type: RevealerTransitionType::SlideDown, set_child: inputs = Some(&ListBox) { - set_selection_mode: gtk4::SelectionMode::None, + set_selection_mode: gtk::SelectionMode::None, set_activate_on_single_click: true } } diff --git a/applets/cosmic-applet-network/Cargo.toml b/applets/cosmic-applet-network/Cargo.toml index d6dddb16..c5b63666 100644 --- a/applets/cosmic-applet-network/Cargo.toml +++ b/applets/cosmic-applet-network/Cargo.toml @@ -10,7 +10,7 @@ futures-util = "0.3.21" gtk4 = "0.4.6" itertools = "0.10.3" once_cell = "1.9.0" -relm4-macros = "0.4.1" +relm4-macros = "0.4.3" slotmap = "1.0.6" tokio = { version = "1.15.0", features = ["full"] } zbus = "2.0.1"