musicus/src/search_tag.rs

99 lines
2.5 KiB
Rust
Raw Normal View History

2025-03-01 09:57:01 +01:00
use std::cell::OnceCell;
2023-09-30 00:22:33 +02:00
use adw::{glib, glib::subclass::Signal, prelude::*, subclass::prelude::*};
use once_cell::sync::Lazy;
2025-03-01 20:54:22 +01:00
use crate::db::models::{Ensemble, Instrument, Person, Work};
2024-03-23 18:06:46 +01:00
2023-09-30 00:22:33 +02:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(file = "data/ui/search_tag.blp")]
2025-03-01 09:57:01 +01:00
pub struct SearchTag {
2023-09-30 00:22:33 +02:00
#[template_child]
pub label: TemplateChild<gtk::Label>,
2023-10-08 00:16:41 +02:00
pub tag: OnceCell<Tag>,
2023-09-30 00:22:33 +02:00
}
#[glib::object_subclass]
2025-03-01 09:57:01 +01:00
impl ObjectSubclass for SearchTag {
2023-09-30 00:22:33 +02:00
const NAME: &'static str = "MusicusSearchTag";
2025-03-01 09:57:01 +01:00
type Type = super::SearchTag;
2023-09-30 00:22:33 +02:00
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_instance_callbacks();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
2025-03-01 09:57:01 +01:00
impl ObjectImpl for SearchTag {
2023-09-30 00:22:33 +02:00
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> =
Lazy::new(|| vec![Signal::builder("remove").build()]);
SIGNALS.as_ref()
}
}
2025-03-01 09:57:01 +01:00
impl WidgetImpl for SearchTag {}
impl BoxImpl for SearchTag {}
2023-09-30 00:22:33 +02:00
}
glib::wrapper! {
2025-03-01 09:57:01 +01:00
pub struct SearchTag(ObjectSubclass<imp::SearchTag>)
2023-09-30 00:22:33 +02:00
@extends gtk::Widget;
}
#[gtk::template_callbacks]
2025-03-01 09:57:01 +01:00
impl SearchTag {
2023-10-08 00:16:41 +02:00
pub fn new(tag: Tag) -> Self {
2025-03-01 09:57:01 +01:00
let obj: SearchTag = glib::Object::new();
2023-10-08 00:16:41 +02:00
2025-03-01 20:54:22 +01:00
let label = match &tag {
2024-03-23 18:06:46 +01:00
Tag::Composer(person) => person.name.get(),
Tag::Performer(person) => person.name.get(),
Tag::Ensemble(ensemble) => ensemble.name.get(),
2025-03-01 20:54:22 +01:00
Tag::Instrument(instrument) => instrument.name.get(),
2024-03-23 18:06:46 +01:00
Tag::Work(work) => work.name.get(),
2025-03-01 20:54:22 +01:00
};
2023-10-08 00:16:41 +02:00
2025-03-01 20:54:22 +01:00
obj.imp().label.set_label(label);
obj.set_tooltip_text(Some(label));
2023-10-08 00:16:41 +02:00
obj.imp().tag.set(tag).unwrap();
obj
}
2023-10-08 15:25:00 +02:00
pub fn connect_remove<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_local("remove", true, move |values| {
let obj = values[0].get::<Self>().unwrap();
f(&obj);
None
})
}
2023-10-08 00:16:41 +02:00
pub fn tag(&self) -> &Tag {
self.imp().tag.get().unwrap()
2023-09-30 00:22:33 +02:00
}
#[template_callback]
fn remove(&self) {
2023-09-30 00:22:33 +02:00
self.emit_by_name::<()>("remove", &[]);
}
}
2023-10-08 00:16:41 +02:00
2023-10-08 15:25:00 +02:00
#[derive(Debug, Clone, PartialEq, Eq)]
2023-10-08 00:16:41 +02:00
pub enum Tag {
2023-10-08 15:11:47 +02:00
Composer(Person),
Performer(Person),
2023-10-08 00:16:41 +02:00
Ensemble(Ensemble),
2025-03-01 20:54:22 +01:00
Instrument(Instrument),
2023-10-08 00:16:41 +02:00
Work(Work),
}