musicus/src/playlist_tile.rs

95 lines
2.8 KiB
Rust
Raw Normal View History

2023-10-25 17:45:32 +02:00
use crate::playlist_item::PlaylistItem;
use gtk::{glib, prelude::*, subclass::prelude::*};
2023-10-26 11:48:42 +02:00
use std::cell::RefCell;
2023-10-25 17:45:32 +02:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "data/ui/playlist_tile.blp")]
pub struct PlaylistTile {
#[template_child]
pub playing_icon: TemplateChild<gtk::Image>,
#[template_child]
2024-07-13 12:20:39 +02:00
pub composers_label: TemplateChild<gtk::Label>,
#[template_child]
pub work_label: TemplateChild<gtk::Label>,
2023-10-25 17:45:32 +02:00
#[template_child]
pub performances_label: TemplateChild<gtk::Label>,
#[template_child]
pub part_title_label: TemplateChild<gtk::Label>,
2023-10-26 11:48:42 +02:00
pub binding: RefCell<Option<glib::Binding>>,
2023-10-25 17:45:32 +02:00
}
#[glib::object_subclass]
impl ObjectSubclass for PlaylistTile {
const NAME: &'static str = "MusicusPlaylistTile";
type Type = super::PlaylistTile;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for PlaylistTile {}
impl WidgetImpl for PlaylistTile {}
impl BoxImpl for PlaylistTile {}
}
glib::wrapper! {
pub struct PlaylistTile(ObjectSubclass<imp::PlaylistTile>)
2023-10-27 12:32:40 +02:00
@extends gtk::Widget, gtk::Box;
2023-10-25 17:45:32 +02:00
}
impl PlaylistTile {
pub fn new() -> Self {
glib::Object::new()
}
2023-10-26 11:48:42 +02:00
pub fn set_item(&self, item: Option<&PlaylistItem>) {
2023-10-25 17:45:32 +02:00
let imp = self.imp();
2023-10-26 11:48:42 +02:00
if let Some(binding) = &*imp.binding.borrow() {
binding.unbind();
2023-10-25 17:45:32 +02:00
}
2023-10-26 11:48:42 +02:00
if let Some(item) = item {
2023-10-27 14:15:05 +02:00
if item.is_title() {
2024-07-13 12:20:39 +02:00
if let Some(composers) = item.composers() {
imp.composers_label.set_label(&composers);
imp.composers_label.set_visible(true);
imp.work_label.set_margin_top(3);
}
imp.work_label.set_label(&item.work());
imp.work_label.set_visible(true);
2023-10-25 17:45:32 +02:00
2023-10-27 14:15:05 +02:00
if let Some(performances) = item.performers() {
imp.performances_label.set_label(&performances);
imp.performances_label.set_visible(true);
2024-07-13 12:20:39 +02:00
imp.work_label.set_margin_bottom(6);
2023-10-27 14:15:05 +02:00
}
2023-10-26 11:48:42 +02:00
}
if let Some(part_title) = item.part_title() {
imp.part_title_label.set_label(&part_title);
imp.part_title_label.set_visible(true);
} else {
imp.obj().set_margin_bottom(24);
}
2023-10-25 17:45:32 +02:00
2023-10-26 11:48:42 +02:00
imp.binding.replace(Some(
item.bind_property("is-playing", &imp.playing_icon.get(), "visible")
.sync_create()
.build(),
));
}
2023-10-25 17:45:32 +02:00
}
}