audrey/src/application.rs

93 lines
3 KiB
Rust
Raw Normal View History

2024-10-29 13:02:29 +00:00
mod imp {
use crate::ui;
use adw::prelude::*;
use adw::subclass::prelude::*;
use gtk::glib;
2024-10-29 12:01:42 +00:00
2024-10-29 13:02:29 +00:00
#[derive(Default)]
pub struct Application;
#[glib::object_subclass]
impl ObjectSubclass for Application {
const NAME: &'static str = "AudreyApplication";
type Type = super::Application;
type ParentType = adw::Application;
2024-10-29 12:01:42 +00:00
}
2024-10-29 13:02:29 +00:00
impl ObjectImpl for Application {}
impl ApplicationImpl for Application {
fn activate(&self) {
self.parent_activate();
match self.obj().active_window() {
2024-11-01 11:11:49 +00:00
None => {
let window = ui::Window::new(self.obj().as_ref());
window.present();
2024-11-01 12:50:29 +00:00
glib::spawn_future_local(async move {
2024-11-01 11:11:49 +00:00
let conn = zbus::connection::Builder::session()
2024-11-01 12:50:29 +00:00
.expect("could not connect to the session bus")
2024-11-01 11:11:49 +00:00
.internal_executor(false)
.build()
.await
2024-11-01 12:50:29 +00:00
.expect("could not build connection to the session bus");
2024-11-01 12:58:59 +00:00
// run this in glib's main loop
glib::spawn_future_local(glib::clone!(
#[strong]
conn,
async move {
loop {
conn.executor().tick().await;
}
}
));
2024-11-01 12:50:29 +00:00
crate::Mpris::setup(conn.object_server(), &window)
.await
.expect("could not serve mpris");
2024-11-02 11:24:25 +00:00
crate::mpris::Player::setup(conn.object_server(), &window.playbin())
.await
.expect("could not serve mpris player");
2024-11-01 12:50:29 +00:00
2024-11-01 12:53:42 +00:00
drop(window); // don't keep this alive
2024-11-01 12:50:29 +00:00
// always set up handlers before requesting service name
conn.request_name("org.mpris.MediaPlayer2.audrey")
.await
.expect("could not register name in session bus");
2024-11-01 11:11:49 +00:00
});
}
2024-10-29 13:02:29 +00:00
Some(win) => win.present(),
}
}
2024-10-29 12:01:42 +00:00
}
2024-10-29 13:02:29 +00:00
impl GtkApplicationImpl for Application {}
impl AdwApplicationImpl for Application {}
2024-10-29 12:01:42 +00:00
}
2024-10-29 13:02:29 +00:00
use gtk::{gio, glib};
2024-10-29 12:01:42 +00:00
glib::wrapper! {
2024-10-29 13:02:29 +00:00
pub struct Application(ObjectSubclass<imp::Application>)
2024-10-29 12:01:42 +00:00
@extends adw::Application, gtk::Application, gio::Application,
@implements gio::ActionGroup, gio::ActionMap;
}
2024-10-30 03:26:53 +00:00
impl Default for Application {
fn default() -> Self {
glib::Object::builder::<Application>()
2024-11-01 08:47:03 +00:00
.property("application-id", crate::APP_ID)
2024-10-29 13:02:29 +00:00
.property("flags", gio::ApplicationFlags::default())
.build()
2024-10-29 12:01:42 +00:00
}
}
2024-10-30 03:26:53 +00:00
impl Application {
pub fn new() -> Self {
Self::default()
}
}