move things to window class

This commit is contained in:
Erica Z 2024-11-03 10:37:37 +01:00
parent c2d9eb61e2
commit 3f6909128d
3 changed files with 85 additions and 66 deletions

View file

@ -1,14 +1,10 @@
mod imp { mod imp {
use crate::{mpv, ui}; use crate::ui;
use adw::{prelude::*, subclass::prelude::*}; use adw::{prelude::*, subclass::prelude::*};
use gtk::glib; use gtk::glib;
use std::rc::Rc;
#[derive(Default)] #[derive(Default)]
pub struct Application { pub struct Application {}
// FIXME: move somewhere else
mpv: Rc<mpv::Handle>,
}
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for Application { impl ObjectSubclass for Application {
@ -26,64 +22,6 @@ mod imp {
None => { None => {
let window = ui::Window::new(self.obj().as_ref()); let window = ui::Window::new(self.obj().as_ref());
window.present(); window.present();
self.mpv
.set_property("audio-client-name", "audrey")
.unwrap();
self.mpv
.set_property("user-agent", crate::USER_AGENT)
.unwrap();
self.mpv.set_property("video", false).unwrap();
self.mpv.set_property("prefetch-playlist", true).unwrap();
self.mpv.set_property("gapless-audio", true).unwrap();
// TODO: observe properties
let mpv_weak = Rc::downgrade(&self.mpv);
glib::spawn_future_local(async move {
while let Some(mpv) = mpv_weak.upgrade() {
match mpv.tick() {
None => break,
Some(listener) => {
drop(mpv); // don't
listener.await;
}
}
}
});
glib::spawn_future_local(async move {
let conn = zbus::connection::Builder::session()
.expect("could not connect to the session bus")
.internal_executor(false)
.build()
.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.playbin())
.await
.expect("could not serve mpris player");
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");
});
} }
Some(win) => win.present(), Some(win) => win.present(),
} }
@ -99,7 +37,6 @@ mod imp {
impl Drop for Application { impl Drop for Application {
fn drop(&mut self) { fn drop(&mut self) {
println!("dropping AudreyApplication"); println!("dropping AudreyApplication");
self.mpv.command(["quit"]).unwrap();
} }
} }
} }

7
src/mpv/node.rs Normal file
View file

@ -0,0 +1,7 @@
use super::ffi;
use std::marker::PhantomData;
pub struct Node<'a> {
pub(crate) ffi::mpv_node,
_ref: PhantomData<&'a ()>,
}

View file

@ -1,9 +1,11 @@
mod imp { mod imp {
use crate::mpv;
use adw::prelude::*; use adw::prelude::*;
use adw::subclass::prelude::*; use adw::subclass::prelude::*;
use glib::subclass::InitializingObject; use glib::subclass::InitializingObject;
use gtk::{gdk, glib}; use gtk::{gdk, glib};
use std::cell::{Cell, RefCell}; use std::cell::{Cell, RefCell};
use std::rc::Rc;
#[derive(gtk::CompositeTemplate, glib::Properties, Default)] #[derive(gtk::CompositeTemplate, glib::Properties, Default)]
#[template(resource = "/eu/callcc/audrey/window.ui")] #[template(resource = "/eu/callcc/audrey/window.ui")]
@ -26,6 +28,7 @@ mod imp {
pub(super) setup: crate::ui::Setup, pub(super) setup: crate::ui::Setup,
pub(super) api: RefCell<Option<crate::subsonic_vala::Client>>, pub(super) api: RefCell<Option<crate::subsonic_vala::Client>>,
pub(super) mpv: Rc<mpv::Handle>,
} }
#[glib::object_subclass] #[glib::object_subclass]
@ -45,7 +48,72 @@ mod imp {
} }
#[glib::derived_properties] #[glib::derived_properties]
impl ObjectImpl for Window {} impl ObjectImpl for Window {
fn constructed(&self) {
self.parent_constructed();
// set up mpv
self.mpv
.set_property("audio-client-name", "audrey")
.unwrap();
self.mpv
.set_property("user-agent", crate::USER_AGENT)
.unwrap();
self.mpv.set_property("video", false).unwrap();
self.mpv.set_property("prefetch-playlist", true).unwrap();
self.mpv.set_property("gapless-audio", true).unwrap();
// TODO: observe properties
let mpv_weak = Rc::downgrade(&self.mpv);
glib::spawn_future_local(async move {
while let Some(mpv) = mpv_weak.upgrade() {
match mpv.tick() {
None => break,
Some(listener) => {
drop(mpv); // don't
listener.await;
}
}
}
});
// set up mpris
let window = self.obj().clone();
glib::spawn_future_local(async move {
let conn = zbus::connection::Builder::session()
.expect("could not connect to the session bus")
.internal_executor(false)
.build()
.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.playbin())
.await
.expect("could not serve mpris player");
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");
});
}
}
impl WidgetImpl for Window {} impl WidgetImpl for Window {}
@ -116,6 +184,13 @@ mod imp {
todo!() todo!()
} }
} }
impl Drop for Window {
fn drop(&mut self) {
println!("dropping AudreyUiWindow");
self.mpv.command(["quit"]).unwrap();
}
}
} }
use adw::prelude::*; use adw::prelude::*;