musicus/src/editor/instrument.rs

116 lines
3.4 KiB
Rust
Raw Normal View History

2025-01-15 11:23:04 +01:00
use std::cell::OnceCell;
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::glib::{self, subclass::Signal};
use once_cell::sync::Lazy;
2025-03-01 09:57:01 +01:00
use crate::{db::models::Instrument, editor::translation::TranslationEditor, library::Library};
2025-01-15 11:23:04 +01:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
2025-03-01 09:57:01 +01:00
#[template(file = "data/ui/editor/instrument.blp")]
pub struct InstrumentEditor {
2025-01-15 11:23:04 +01:00
pub navigation: OnceCell<adw::NavigationView>,
2025-03-01 09:57:01 +01:00
pub library: OnceCell<Library>,
2025-01-15 11:23:04 +01:00
pub instrument_id: OnceCell<String>,
#[template_child]
2025-03-01 09:57:01 +01:00
pub name_editor: TemplateChild<TranslationEditor>,
2025-01-15 11:23:04 +01:00
#[template_child]
2025-01-17 18:33:31 +01:00
pub save_row: TemplateChild<adw::ButtonRow>,
2025-01-15 11:23:04 +01:00
}
#[glib::object_subclass]
2025-03-01 09:57:01 +01:00
impl ObjectSubclass for InstrumentEditor {
2025-01-15 11:23:04 +01:00
const NAME: &'static str = "MusicusInstrumentEditor";
2025-03-01 09:57:01 +01:00
type Type = super::InstrumentEditor;
2025-01-15 11:23:04 +01:00
type ParentType = adw::NavigationPage;
fn class_init(klass: &mut Self::Class) {
2025-03-01 09:57:01 +01:00
TranslationEditor::static_type();
2025-01-15 11:23:04 +01:00
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 InstrumentEditor {
2025-01-15 11:23:04 +01:00
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder("created")
.param_types([Instrument::static_type()])
.build()]
});
SIGNALS.as_ref()
}
}
2025-03-01 09:57:01 +01:00
impl WidgetImpl for InstrumentEditor {}
impl NavigationPageImpl for InstrumentEditor {}
2025-01-15 11:23:04 +01:00
}
glib::wrapper! {
2025-03-01 09:57:01 +01:00
pub struct InstrumentEditor(ObjectSubclass<imp::InstrumentEditor>)
2025-01-15 11:23:04 +01:00
@extends gtk::Widget, adw::NavigationPage;
}
#[gtk::template_callbacks]
2025-03-01 09:57:01 +01:00
impl InstrumentEditor {
2025-01-15 11:23:04 +01:00
pub fn new(
navigation: &adw::NavigationView,
2025-03-01 09:57:01 +01:00
library: &Library,
2025-01-15 11:23:04 +01:00
instrument: Option<&Instrument>,
) -> Self {
let obj: Self = glib::Object::new();
obj.imp().navigation.set(navigation.to_owned()).unwrap();
obj.imp().library.set(library.to_owned()).unwrap();
if let Some(instrument) = instrument {
2025-03-01 08:34:53 +01:00
obj.imp().save_row.set_title(&gettext("_Save changes"));
obj.imp()
.instrument_id
.set(instrument.instrument_id.clone())
.unwrap();
2025-01-15 11:23:04 +01:00
obj.imp().name_editor.set_translation(&instrument.name);
}
obj
}
pub fn connect_created<F: Fn(&Self, Instrument) + 'static>(
&self,
f: F,
) -> glib::SignalHandlerId {
2025-01-15 11:23:04 +01:00
self.connect_local("created", true, move |values| {
let obj = values[0].get::<Self>().unwrap();
let instrument = values[1].get::<Instrument>().unwrap();
f(&obj, instrument);
None
})
}
#[template_callback]
fn save(&self) {
2025-01-15 11:23:04 +01:00
let library = self.imp().library.get().unwrap();
let name = self.imp().name_editor.translation();
if let Some(instrument_id) = self.imp().instrument_id.get() {
library.update_instrument(instrument_id, name).unwrap();
} else {
let instrument = library.create_instrument(name).unwrap();
self.emit_by_name::<()>("created", &[&instrument]);
}
self.imp().navigation.get().unwrap().pop();
}
}