mirror of
https://github.com/johrpan/musicus.git
synced 2025-10-27 12:17:24 +01:00
Refactor module layout
This commit is contained in:
parent
e59052a362
commit
5956b7ff15
70 changed files with 757 additions and 841 deletions
144
src/editor/work/composer_row.rs
Normal file
144
src/editor/work/composer_row.rs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
use std::cell::{OnceCell, RefCell};
|
||||
|
||||
use adw::{prelude::*, subclass::prelude::*};
|
||||
use gtk::glib::{self, clone, subclass::Signal, Properties};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{
|
||||
db::models::Composer, editor::role::RoleEditor, library::Library,
|
||||
selector::role::RoleSelectorPopover,
|
||||
};
|
||||
|
||||
mod imp {
|
||||
use super::*;
|
||||
|
||||
#[derive(Properties, Debug, Default, gtk::CompositeTemplate)]
|
||||
#[properties(wrapper_type = super::WorkEditorComposerRow)]
|
||||
#[template(file = "data/ui/editor/work/composer_row.blp")]
|
||||
pub struct WorkEditorComposerRow {
|
||||
#[property(get, construct_only)]
|
||||
pub navigation: OnceCell<adw::NavigationView>,
|
||||
|
||||
#[property(get, construct_only)]
|
||||
pub library: OnceCell<Library>,
|
||||
|
||||
pub composer: RefCell<Option<Composer>>,
|
||||
pub role_popover: OnceCell<RoleSelectorPopover>,
|
||||
|
||||
#[template_child]
|
||||
pub role_label: TemplateChild<gtk::Label>,
|
||||
#[template_child]
|
||||
pub role_box: TemplateChild<gtk::Box>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for WorkEditorComposerRow {
|
||||
const NAME: &'static str = "MusicusWorkEditorComposerRow";
|
||||
type Type = super::WorkEditorComposerRow;
|
||||
type ParentType = adw::ActionRow;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
#[glib::derived_properties]
|
||||
impl ObjectImpl for WorkEditorComposerRow {
|
||||
fn signals() -> &'static [Signal] {
|
||||
static SIGNALS: Lazy<Vec<Signal>> =
|
||||
Lazy::new(|| vec![Signal::builder("remove").build()]);
|
||||
|
||||
SIGNALS.as_ref()
|
||||
}
|
||||
|
||||
fn constructed(&self) {
|
||||
self.parent_constructed();
|
||||
|
||||
let role_popover = RoleSelectorPopover::new(self.library.get().unwrap());
|
||||
|
||||
let obj = self.obj().to_owned();
|
||||
role_popover.connect_role_selected(move |_, role| {
|
||||
if let Some(composer) = &mut *obj.imp().composer.borrow_mut() {
|
||||
obj.imp().role_label.set_label(&role.to_string());
|
||||
composer.role = role;
|
||||
}
|
||||
});
|
||||
|
||||
let obj = self.obj().to_owned();
|
||||
role_popover.connect_create(move |_| {
|
||||
let editor = RoleEditor::new(&obj.navigation(), &obj.library(), None);
|
||||
|
||||
editor.connect_created(clone!(
|
||||
#[weak]
|
||||
obj,
|
||||
move |_, role| {
|
||||
if let Some(composer) = &mut *obj.imp().composer.borrow_mut() {
|
||||
obj.imp().role_label.set_label(&role.to_string());
|
||||
composer.role = role;
|
||||
};
|
||||
}
|
||||
));
|
||||
|
||||
obj.navigation().push(&editor);
|
||||
});
|
||||
|
||||
self.role_box.append(&role_popover);
|
||||
self.role_popover.set(role_popover).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for WorkEditorComposerRow {}
|
||||
impl ListBoxRowImpl for WorkEditorComposerRow {}
|
||||
impl PreferencesRowImpl for WorkEditorComposerRow {}
|
||||
impl ActionRowImpl for WorkEditorComposerRow {}
|
||||
}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct WorkEditorComposerRow(ObjectSubclass<imp::WorkEditorComposerRow>)
|
||||
@extends gtk::Widget, gtk::ListBoxRow, adw::PreferencesRow, adw::ActionRow;
|
||||
}
|
||||
|
||||
#[gtk::template_callbacks]
|
||||
impl WorkEditorComposerRow {
|
||||
pub fn new(navigation: &adw::NavigationView, library: &Library, composer: Composer) -> Self {
|
||||
let obj: Self = glib::Object::builder()
|
||||
.property("navigation", navigation)
|
||||
.property("library", library)
|
||||
.build();
|
||||
obj.set_composer(composer);
|
||||
obj
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
pub fn composer(&self) -> Composer {
|
||||
self.imp().composer.borrow().to_owned().unwrap()
|
||||
}
|
||||
|
||||
fn set_composer(&self, composer: Composer) {
|
||||
self.set_title(&composer.person.to_string());
|
||||
self.imp().role_label.set_label(&composer.role.to_string());
|
||||
self.imp().composer.replace(Some(composer));
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn open_role_popover(&self) {
|
||||
self.imp().role_popover.get().unwrap().popup();
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn remove(&self) {
|
||||
self.emit_by_name::<()>("remove", &[]);
|
||||
}
|
||||
}
|
||||
129
src/editor/work/part_row.rs
Normal file
129
src/editor/work/part_row.rs
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
use std::cell::{OnceCell, RefCell};
|
||||
|
||||
use adw::{prelude::*, subclass::prelude::*};
|
||||
use gtk::glib::{self, clone, subclass::Signal, Properties};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
use crate::{db::models::Work, editor::work::WorkEditor, library::Library};
|
||||
|
||||
mod imp {
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Properties, Debug, Default, gtk::CompositeTemplate)]
|
||||
#[properties(wrapper_type = super::WorkEditorPartRow)]
|
||||
#[template(file = "data/ui/editor/work/part_row.blp")]
|
||||
pub struct WorkEditorPartRow {
|
||||
#[property(get, construct_only)]
|
||||
pub navigation: OnceCell<adw::NavigationView>,
|
||||
|
||||
#[property(get, construct_only)]
|
||||
pub library: OnceCell<Library>,
|
||||
|
||||
pub part: RefCell<Option<Work>>,
|
||||
}
|
||||
|
||||
#[glib::object_subclass]
|
||||
impl ObjectSubclass for WorkEditorPartRow {
|
||||
const NAME: &'static str = "MusicusWorkEditorPartRow";
|
||||
type Type = super::WorkEditorPartRow;
|
||||
type ParentType = adw::ActionRow;
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
#[glib::derived_properties]
|
||||
impl ObjectImpl for WorkEditorPartRow {
|
||||
fn signals() -> &'static [Signal] {
|
||||
static SIGNALS: Lazy<Vec<Signal>> =
|
||||
Lazy::new(|| vec![Signal::builder("remove").build()]);
|
||||
|
||||
SIGNALS.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetImpl for WorkEditorPartRow {}
|
||||
impl ListBoxRowImpl for WorkEditorPartRow {}
|
||||
impl PreferencesRowImpl for WorkEditorPartRow {}
|
||||
impl ActionRowImpl for WorkEditorPartRow {}
|
||||
}
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct WorkEditorPartRow(ObjectSubclass<imp::WorkEditorPartRow>)
|
||||
@extends gtk::Widget, gtk::ListBoxRow, adw::PreferencesRow, adw::ActionRow;
|
||||
}
|
||||
|
||||
#[gtk::template_callbacks]
|
||||
impl WorkEditorPartRow {
|
||||
pub fn new(navigation: &adw::NavigationView, library: &Library, part: Work) -> Self {
|
||||
let obj: Self = glib::Object::builder()
|
||||
.property("navigation", navigation)
|
||||
.property("library", library)
|
||||
.build();
|
||||
obj.set_part(part);
|
||||
obj
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
|
||||
pub fn part(&self) -> Work {
|
||||
self.imp().part.borrow().to_owned().unwrap()
|
||||
}
|
||||
|
||||
fn set_part(&self, part: Work) {
|
||||
self.set_title(&part.name.get());
|
||||
|
||||
if !part.parts.is_empty() {
|
||||
self.set_subtitle(
|
||||
&part
|
||||
.parts
|
||||
.iter()
|
||||
.map(|p| p.name.get())
|
||||
.collect::<Vec<&str>>()
|
||||
.join("\n"),
|
||||
);
|
||||
} else {
|
||||
self.set_subtitle("");
|
||||
}
|
||||
|
||||
self.imp().part.replace(Some(part));
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn edit(&self) {
|
||||
let editor = WorkEditor::new(
|
||||
&self.navigation(),
|
||||
&self.library(),
|
||||
self.imp().part.borrow().as_ref(),
|
||||
true,
|
||||
);
|
||||
|
||||
editor.connect_created(clone!(
|
||||
#[weak(rename_to = this)]
|
||||
self,
|
||||
move |_, part| {
|
||||
this.set_part(part);
|
||||
}
|
||||
));
|
||||
|
||||
self.navigation().push(&editor);
|
||||
}
|
||||
|
||||
#[template_callback]
|
||||
fn remove(&self) {
|
||||
self.emit_by_name::<()>("remove", &[]);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue