musicus/src/editor/role.rs

109 lines
3.4 KiB
Rust
Raw Normal View History

2024-07-14 13:03:44 +02: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::Role, editor::translation::TranslationEditor, library::Library};
2024-07-14 13:03:44 +02:00
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
2025-03-01 09:57:01 +01:00
#[template(file = "data/ui/editor/role.blp")]
pub struct RoleEditor {
2024-07-14 13:03:44 +02:00
pub navigation: OnceCell<adw::NavigationView>,
2025-03-01 09:57:01 +01:00
pub library: OnceCell<Library>,
2024-07-14 13:03:44 +02:00
pub role_id: OnceCell<String>,
#[template_child]
2025-03-01 09:57:01 +01:00
pub name_editor: TemplateChild<TranslationEditor>,
2024-07-14 13:03:44 +02:00
#[template_child]
2025-04-27 15:22:04 +02:00
pub enable_updates_row: TemplateChild<adw::SwitchRow>,
#[template_child]
2025-01-17 18:33:31 +01:00
pub save_row: TemplateChild<adw::ButtonRow>,
2024-07-14 13:03:44 +02:00
}
#[glib::object_subclass]
2025-03-01 09:57:01 +01:00
impl ObjectSubclass for RoleEditor {
2024-07-14 13:03:44 +02:00
const NAME: &'static str = "MusicusRoleEditor";
2025-03-01 09:57:01 +01:00
type Type = super::RoleEditor;
2024-07-14 13:03:44 +02:00
type ParentType = adw::NavigationPage;
fn class_init(klass: &mut Self::Class) {
2025-03-01 09:57:01 +01:00
TranslationEditor::static_type();
2024-07-14 13:03:44 +02: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 RoleEditor {
2024-07-14 13:03:44 +02:00
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder("created")
.param_types([Role::static_type()])
.build()]
});
SIGNALS.as_ref()
}
}
2025-03-01 09:57:01 +01:00
impl WidgetImpl for RoleEditor {}
impl NavigationPageImpl for RoleEditor {}
2024-07-14 13:03:44 +02:00
}
glib::wrapper! {
2025-03-01 09:57:01 +01:00
pub struct RoleEditor(ObjectSubclass<imp::RoleEditor>)
2024-07-14 13:03:44 +02:00
@extends gtk::Widget, adw::NavigationPage;
}
#[gtk::template_callbacks]
2025-03-01 09:57:01 +01:00
impl RoleEditor {
pub fn new(navigation: &adw::NavigationView, library: &Library, role: Option<&Role>) -> Self {
2024-07-14 13:03:44 +02:00
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(role) = role {
2025-03-01 08:34:53 +01:00
obj.imp().save_row.set_title(&gettext("_Save changes"));
2024-07-14 13:03:44 +02:00
obj.imp().role_id.set(role.role_id.clone()).unwrap();
obj.imp().name_editor.set_translation(&role.name);
2025-04-27 15:22:04 +02:00
obj.imp().enable_updates_row.set_active(role.enable_updates);
2024-07-14 13:03:44 +02:00
}
obj
}
pub fn connect_created<F: Fn(&Self, Role) + 'static>(&self, f: F) -> glib::SignalHandlerId {
self.connect_local("created", true, move |values| {
let obj = values[0].get::<Self>().unwrap();
let role = values[1].get::<Role>().unwrap();
f(&obj, role);
None
})
}
#[template_callback]
fn save(&self) {
2024-07-14 13:03:44 +02:00
let library = self.imp().library.get().unwrap();
let name = self.imp().name_editor.translation();
2025-04-27 15:22:04 +02:00
let enable_updates = self.imp().enable_updates_row.is_active();
2024-07-14 13:03:44 +02:00
if let Some(role_id) = self.imp().role_id.get() {
2025-04-27 15:22:04 +02:00
library.update_role(role_id, name, enable_updates).unwrap();
2024-07-14 13:03:44 +02:00
} else {
2025-04-27 15:22:04 +02:00
let role = library.create_role(name, enable_updates).unwrap();
2024-07-14 13:03:44 +02:00
self.emit_by_name::<()>("created", &[&role]);
}
self.imp().navigation.get().unwrap().pop();
}
}