musicus/src/tag_tile.rs

81 lines
2.1 KiB
Rust
Raw Normal View History

2023-10-08 16:40:59 +02:00
use std::cell::OnceCell;
2025-03-01 09:57:01 +01:00
use gtk::{glib, prelude::*, subclass::prelude::*};
use crate::search_tag::Tag;
2023-10-08 16:40:59 +02:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "data/ui/tag_tile.blp")]
2025-03-01 09:57:01 +01:00
pub struct TagTile {
2023-10-08 16:40:59 +02:00
#[template_child]
pub title_label: TemplateChild<gtk::Label>,
#[template_child]
pub subtitle_label: TemplateChild<gtk::Label>,
pub tag: OnceCell<Tag>,
}
#[glib::object_subclass]
2025-03-01 09:57:01 +01:00
impl ObjectSubclass for TagTile {
2023-10-08 16:40:59 +02:00
const NAME: &'static str = "MusicusTagTile";
2025-03-01 09:57:01 +01:00
type Type = super::TagTile;
2023-10-08 16:40:59 +02:00
type ParentType = gtk::FlowBoxChild;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
2025-03-01 09:57:01 +01:00
impl ObjectImpl for TagTile {}
impl WidgetImpl for TagTile {}
impl FlowBoxChildImpl for TagTile {}
2023-10-08 16:40:59 +02:00
}
glib::wrapper! {
2025-03-01 09:57:01 +01:00
pub struct TagTile(ObjectSubclass<imp::TagTile>)
2023-10-08 16:40:59 +02:00
@extends gtk::Widget, gtk::FlowBoxChild;
}
2025-03-01 09:57:01 +01:00
impl TagTile {
2023-10-08 16:40:59 +02:00
pub fn new(tag: Tag) -> Self {
let obj: Self = glib::Object::new();
let imp = obj.imp();
match &tag {
Tag::Composer(person) | Tag::Performer(person) => {
2024-03-23 18:06:46 +01:00
imp.title_label.set_label(person.name.get());
2023-10-08 16:40:59 +02:00
}
Tag::Ensemble(ensemble) => {
2024-03-23 18:06:46 +01:00
imp.title_label.set_label(ensemble.name.get());
2023-10-08 16:40:59 +02:00
}
2025-03-01 20:54:22 +01:00
Tag::Instrument(instrument) => {
imp.title_label.set_label(instrument.name.get());
}
2023-10-08 16:40:59 +02:00
Tag::Work(work) => {
2024-03-23 18:06:46 +01:00
imp.title_label.set_label(work.name.get());
if let Some(composers) = work.composers_string() {
imp.subtitle_label.set_label(&composers);
imp.subtitle_label.set_visible(true);
} else {
imp.subtitle_label.set_visible(false);
}
2023-10-08 16:40:59 +02:00
}
}
imp.tag.set(tag).unwrap();
obj
}
2023-10-11 12:04:49 +02:00
pub fn tag(&self) -> &Tag {
self.imp().tag.get().unwrap()
}
2023-10-08 16:40:59 +02:00
}