From 226c8073d476bb1273f199d718199d5dd4e1a88a Mon Sep 17 00:00:00 2001 From: Erica Z Date: Tue, 5 Nov 2024 11:02:02 +0100 Subject: [PATCH] store some glib future join handles --- src/ui/window.rs | 56 +++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/ui/window.rs b/src/ui/window.rs index 7b78e55..c088ff0 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -4,6 +4,7 @@ mod imp { use adw::prelude::*; use adw::subclass::prelude::*; use glib::subclass::InitializingObject; + use glib::JoinHandle; use gtk::{gdk, gio, glib}; use std::cell::{Cell, RefCell}; use std::rc::Rc; @@ -54,6 +55,9 @@ mod imp { _playlist_count: (), pub(super) queued_seek: Cell>, + + mpv_event_loop_handle: RefCell>>, // really !, not () + zbus_executor_loop_handle: RefCell>>, // same } impl Default for Window { @@ -99,6 +103,9 @@ mod imp { _playlist_count: (), queued_seek: Cell::new(None), + + mpv_event_loop_handle: Default::default(), + zbus_executor_loop_handle: Default::default(), } } } @@ -125,8 +132,9 @@ mod imp { self.parent_constructed(); let window = self.obj().downgrade(); - glib::spawn_future_local(async move { - while let Some(window) = window.upgrade() { + let mpv_event_loop_handle = glib::spawn_future_local(async move { + loop { + let window = window.upgrade().unwrap(); let listener = window.imp().mpv.wakeup_listener(); while let Some(event) = window.imp().mpv.wait_event(0.0) { use crate::mpv::Event; @@ -280,10 +288,11 @@ mod imp { listener.await; } }); + self.mpv_event_loop_handle.replace(Some(mpv_event_loop_handle)); // set up mpris let window = self.obj().clone(); - glib::spawn_future_local(async move { + let zbus_executor_loop_handle = glib::spawn_future_local(async move { let conn = zbus::connection::Builder::session() .expect("could not connect to the session bus") .internal_executor(false) @@ -291,33 +300,33 @@ mod imp { .await .expect("could not build connection to the session bus"); - // run this in glib's main loop glib::spawn_future_local(glib::clone!( #[strong] conn, async move { - loop { - conn.executor().tick().await; - } + crate::Mpris::setup(conn.object_server(), &window) + .await + .expect("could not serve mpris"); + /* + crate::mpris::Player::setup(conn.object_server(), &window.imp().playbin) + .await + .expect("could not serve mpris player"); + FIXME */ + + // always set up handlers before requesting service name + conn.request_name("org.mpris.MediaPlayer2.audrey") + .await + .expect("could not register name in session bus"); } )); - crate::Mpris::setup(conn.object_server(), &window) - .await - .expect("could not serve mpris"); - /* - crate::mpris::Player::setup(conn.object_server(), &window.imp().playbin) - .await - .expect("could not serve mpris player"); - FIXME */ - - drop(window); // don't keep this alive - - // always set up handlers before requesting service name - conn.request_name("org.mpris.MediaPlayer2.audrey") - .await - .expect("could not register name in session bus"); + // run this in glib's main loop + loop { + conn.executor().tick().await; + } }); + self.zbus_executor_loop_handle + .replace(Some(zbus_executor_loop_handle)); } } @@ -469,6 +478,9 @@ mod imp { impl Drop for Window { fn drop(&mut self) { event!(Level::DEBUG, "dropping AudreyUiWindow"); + + self.mpv_event_loop_handle.take().unwrap().abort(); + self.zbus_executor_loop_handle.take().unwrap().abort(); } } }