audrey/src/ui/play_queue.rs

95 lines
2.7 KiB
Rust

pub mod song;
pub use song::Song;
mod imp {
use adw::{glib, prelude::*, subclass::prelude::*};
use glib::{subclass::InitializingObject, WeakRef};
#[derive(gtk::CompositeTemplate, glib::Properties, Default)]
#[template(resource = "/eu/callcc/audrey/play_queue.ui")]
#[properties(wrapper_type = super::PlayQueue)]
pub struct PlayQueue {
#[property(get, set)]
playbin: WeakRef<crate::Playbin>,
}
#[glib::object_subclass]
impl ObjectSubclass for PlayQueue {
const NAME: &'static str = "AudreyUiPlayQueue";
type Type = super::PlayQueue;
type ParentType = adw::Bin;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_callbacks();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for PlayQueue {}
impl WidgetImpl for PlayQueue {}
impl BinImpl for PlayQueue {}
#[gtk::template_callbacks]
impl PlayQueue {
#[template_callback]
fn visible_child_name(&self, n_items: u32) -> &'static str {
if n_items > 0 {
"not-empty"
} else {
"empty"
}
}
#[template_callback]
fn on_song_list_setup(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child = super::Song::new(&self.playbin.upgrade().unwrap());
child.set_draggable(true);
child.set_show_position(true);
child.set_show_artist(true);
child.set_show_cover(true);
item.set_child(Some(&child));
}
#[template_callback]
fn on_song_list_bind(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child = item.child().and_downcast::<super::Song>().unwrap();
child.bind(
item.position(),
item.item()
.unwrap()
.downcast_ref::<crate::playbin::Song>()
.unwrap(),
);
}
#[template_callback]
fn on_song_list_unbind(&self, item: &gtk::ListItem, _factory: &gtk::SignalListItemFactory) {
let child = item.child().and_downcast::<super::Song>().unwrap();
child.unbind();
}
#[template_callback]
fn on_row_activated(&self, position: u32) {
self.obj().playbin().unwrap().select_track(position);
}
}
}
use gtk::glib;
glib::wrapper! {
pub struct PlayQueue(ObjectSubclass<imp::PlayQueue>)
@extends adw::Bin, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}