From 20f64595ac12be63f77d94b3857d931b3732d4eb Mon Sep 17 00:00:00 2001 From: Denis Drakhnia Date: Thu, 28 Sep 2023 08:22:45 +0300 Subject: [PATCH] pipewire connection: Show banner if disconnected --- Cargo.toml | 2 +- meson.build | 2 +- src/application.rs | 1 + src/graph_manager.rs | 19 +++++++++++++++++-- src/main.rs | 2 ++ src/pipewire_connection/mod.rs | 15 +++++++++++++++ src/ui/window.rs | 3 +++ src/ui/window.ui | 6 ++++++ 8 files changed, 46 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a18731b..5c80f91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ categories = ["gui", "multimedia"] [dependencies] pipewire = "0.7.1" -adw = { version = "0.5", package = "libadwaita", features = ["v1_2"] } +adw = { version = "0.5", package = "libadwaita", features = ["v1_3"] } glib = { version = "0.18", features = ["log"] } log = "0.4.11" diff --git a/meson.build b/meson.build index e9421f5..ed62edc 100644 --- a/meson.build +++ b/meson.build @@ -12,7 +12,7 @@ base_id = 'org.pipewire.Helvum' dependency('glib-2.0', version: '>= 2.66') dependency('gtk4', version: '>= 4.4.0') -dependency('libadwaita-1', version: '>= 1.2') +dependency('libadwaita-1', version: '>= 1.3') dependency('libpipewire-0.3') desktop_file_validate = find_program('desktop-file-validate', required: false) diff --git a/src/application.rs b/src/application.rs index c8acd75..97aa21b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -152,6 +152,7 @@ impl Application { imp.graph_manager .set(GraphManager::new( &imp.window.graph(), + &imp.window.connection_banner(), pw_sender, gtk_receiver, )) diff --git a/src/graph_manager.rs b/src/graph_manager.rs index 119092c..e438552 100644 --- a/src/graph_manager.rs +++ b/src/graph_manager.rs @@ -35,6 +35,9 @@ mod imp { #[property(get, set, construct_only)] pub graph: OnceCell, + #[property(get, set, construct_only)] + pub connection_banner: OnceCell, + pub pw_sender: OnceCell>, pub items: RefCell>, } @@ -66,7 +69,15 @@ mod imp { PipewireMessage::NodeRemoved { id } => imp.remove_node(id), PipewireMessage::PortRemoved { id, node_id } => imp.remove_port(id, node_id), PipewireMessage::LinkRemoved { id } => imp.remove_link(id), - PipewireMessage::Disconnected => imp.clear(), + PipewireMessage::Connecting => { + imp.obj().connection_banner().set_revealed(true); + } + PipewireMessage::Connected => { + imp.obj().connection_banner().set_revealed(false); + }, + PipewireMessage::Disconnected => { + imp.clear(); + }, }; glib::ControlFlow::Continue } @@ -296,10 +307,14 @@ glib::wrapper! { impl GraphManager { pub fn new( graph: &GraphView, + connection_banner: &adw::Banner, sender: PwSender, receiver: glib::Receiver, ) -> Self { - let res: Self = glib::Object::builder().property("graph", graph).build(); + let res: Self = glib::Object::builder() + .property("graph", graph) + .property("connection-banner", connection_banner) + .build(); res.imp().attach_receiver(receiver); assert!( diff --git a/src/main.rs b/src/main.rs index bfa8824..b147c96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,8 @@ pub enum PipewireMessage { LinkRemoved { id: u32, }, + Connecting, + Connected, Disconnected, } diff --git a/src/pipewire_connection/mod.rs b/src/pipewire_connection/mod.rs index 4ca9275..f7ba1a0 100644 --- a/src/pipewire_connection/mod.rs +++ b/src/pipewire_connection/mod.rs @@ -61,12 +61,20 @@ pub(super) fn thread_main( let mainloop = MainLoop::new().expect("Failed to create mainloop"); let context = Rc::new(Context::new(&mainloop).expect("Failed to create context")); let is_stopped = Rc::new(Cell::new(false)); + let mut is_connecting = false; while !is_stopped.get() { // Try to connect let core = match context.connect(None) { Ok(core) => Rc::new(core), Err(_) => { + if !is_connecting { + is_connecting = true; + gtk_sender + .send(PipewireMessage::Connecting) + .expect("Failed to send message"); + } + // If connection is failed, try to connect again in 200ms let interval = Some(Duration::from_millis(200)); @@ -93,6 +101,13 @@ pub(super) fn thread_main( } }; + if is_connecting { + is_connecting = false; + gtk_sender + .send(PipewireMessage::Connected) + .expect("Failed to send message"); + } + let registry = Rc::new(core.get_registry().expect("Failed to get registry")); // Keep proxies and their listeners alive so that we can receive info events. diff --git a/src/ui/window.rs b/src/ui/window.rs index 5756ba1..e1e410b 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -12,6 +12,9 @@ mod imp { #[template_child] pub header_bar: TemplateChild, #[template_child] + #[property(type = adw::Banner, get = |_| self.connection_banner.clone())] + pub connection_banner: TemplateChild, + #[template_child] #[property(type = graph::GraphView, get = |_| self.graph.clone())] pub graph: TemplateChild, } diff --git a/src/ui/window.ui b/src/ui/window.ui index 251c04b..c19955c 100644 --- a/src/ui/window.ui +++ b/src/ui/window.ui @@ -39,6 +39,12 @@ + + + Disconnected + false + +