Inital library manager UI

This commit is contained in:
Elias Projahn 2025-01-17 09:38:00 +01:00
parent 38638d6fcd
commit f0135cd415
8 changed files with 1528 additions and 486 deletions

View file

@ -1,14 +1,20 @@
use adw::subclass::prelude::*;
use gtk::glib;
use std::cell::OnceCell;
use crate::{
editor::{
ensemble_editor::MusicusEnsembleEditor, instrument_editor::MusicusInstrumentEditor,
person_editor::MusicusPersonEditor, recording_editor::MusicusRecordingEditor,
role_editor::MusicusRoleEditor, work_editor::MusicusWorkEditor,
db::{
models::{Album, Ensemble, Instrument, Person, Recording, Role, Track, Work},
tables::Medium,
},
library::MusicusLibrary,
window::MusicusWindow,
};
use adw::{prelude::*, subclass::prelude::*};
use gettextrs::gettext;
use gtk::glib;
use std::{
cell::{OnceCell, RefCell},
ffi::OsStr,
path::Path,
};
mod imp {
@ -19,6 +25,37 @@ mod imp {
pub struct LibraryManager {
pub navigation: OnceCell<adw::NavigationView>,
pub library: OnceCell<MusicusLibrary>,
pub persons: RefCell<Vec<Person>>,
pub roles: RefCell<Vec<Role>>,
pub instruments: RefCell<Vec<Instrument>>,
pub works: RefCell<Vec<Work>>,
pub ensembles: RefCell<Vec<Ensemble>>,
pub recordings: RefCell<Vec<Recording>>,
pub tracks: RefCell<Vec<Track>>,
pub mediums: RefCell<Vec<Medium>>,
pub albums: RefCell<Vec<Album>>,
#[template_child]
pub library_path_row: TemplateChild<adw::ActionRow>,
#[template_child]
pub n_persons_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_roles_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_instruments_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_works_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_ensembles_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_recordings_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_tracks_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_mediums_label: TemplateChild<gtk::Label>,
#[template_child]
pub n_albums_label: TemplateChild<gtk::Label>,
}
#[glib::object_subclass]
@ -39,7 +76,13 @@ mod imp {
impl ObjectImpl for LibraryManager {}
impl WidgetImpl for LibraryManager {}
impl NavigationPageImpl for LibraryManager {}
impl NavigationPageImpl for LibraryManager {
fn showing(&self) {
self.parent_showing();
self.obj().update();
}
}
}
glib::wrapper! {
@ -60,99 +103,215 @@ impl LibraryManager {
}
#[template_callback]
fn add_person(&self, _: &gtk::Button) {
async fn open_library(&self, _: &adw::ActionRow) {
let dialog = gtk::FileDialog::builder()
.title(gettext("Select music library folder"))
.modal(true)
.build();
let root = self.root();
let window = root
.as_ref()
.and_then(|r| r.downcast_ref::<gtk::Window>())
.and_then(|w| w.downcast_ref::<MusicusWindow>())
.unwrap();
match dialog.select_folder_future(Some(window)).await {
Err(err) => {
if !err.matches(gtk::DialogError::Dismissed) {
log::error!("Folder selection failed: {err}");
}
}
Ok(folder) => window.set_library_folder(&folder),
}
}
#[template_callback]
fn import_archive(&self, _: &adw::ButtonRow) {}
#[template_callback]
fn export_archive(&self, _: &adw::ButtonRow) {}
#[template_callback]
fn show_persons(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_roles(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_instruments(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_works(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_ensembles(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_recordings(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_tracks(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_mediums(&self, _: &adw::ActionRow) {}
#[template_callback]
fn show_albums(&self, _: &adw::ActionRow) {}
// TODO: Make this async.
fn update(&self) {
let library = self.imp().library.get().unwrap();
if let Some(Some(filename)) = Path::new(&library.folder()).file_name().map(OsStr::to_str) {
self.imp().library_path_row.set_subtitle(filename);
}
let persons = library.all_persons().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusPersonEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
}
.n_persons_label
.set_label(&persons.len().to_string());
self.imp().persons.replace(persons);
#[template_callback]
fn add_role(&self, _: &gtk::Button) {
let roles = library.all_roles().unwrap();
self.imp().n_roles_label.set_label(&roles.len().to_string());
self.imp().roles.replace(roles);
let instruments = library.all_instruments().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusRoleEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
}
.n_instruments_label
.set_label(&instruments.len().to_string());
self.imp().instruments.replace(instruments);
#[template_callback]
fn add_instrument(&self, _: &gtk::Button) {
let works = library.all_works().unwrap();
self.imp().n_works_label.set_label(&works.len().to_string());
self.imp().works.replace(works);
let ensembles = library.all_ensembles().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusInstrumentEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
}
.n_ensembles_label
.set_label(&ensembles.len().to_string());
self.imp().ensembles.replace(ensembles);
#[template_callback]
fn add_work(&self, _: &gtk::Button) {
let recordings = library.all_recordings().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusWorkEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
}
.n_recordings_label
.set_label(&recordings.len().to_string());
self.imp().recordings.replace(recordings);
#[template_callback]
fn add_ensemble(&self, _: &gtk::Button) {
let tracks = library.all_tracks().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusEnsembleEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
}
.n_tracks_label
.set_label(&tracks.len().to_string());
self.imp().tracks.replace(tracks);
#[template_callback]
fn add_recording(&self, _: &gtk::Button) {
let mediums = library.all_mediums().unwrap();
self.imp()
.navigation
.get()
.unwrap()
.push(&MusicusRecordingEditor::new(
&self.imp().navigation.get().unwrap(),
&self.imp().library.get().unwrap(),
None,
));
.n_mediums_label
.set_label(&mediums.len().to_string());
self.imp().mediums.replace(mediums);
let albums = library.all_albums().unwrap();
self.imp()
.n_albums_label
.set_label(&albums.len().to_string());
self.imp().albums.replace(albums);
}
#[template_callback]
fn add_medium(&self, _: &gtk::Button) {
todo!("Medium import");
}
// #[template_callback]
// fn add_person(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusPersonEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
#[template_callback]
fn add_album(&self, _: &gtk::Button) {
todo!("Album editor");
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusAlbumEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
}
// #[template_callback]
// fn add_role(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusRoleEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
// #[template_callback]
// fn add_instrument(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusInstrumentEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
// #[template_callback]
// fn add_work(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusWorkEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
// #[template_callback]
// fn add_ensemble(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusEnsembleEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
// #[template_callback]
// fn add_recording(&self, _: &gtk::Button) {
// self.imp()
// .navigation
// .get()
// .unwrap()
// .push(&MusicusRecordingEditor::new(
// &self.imp().navigation.get().unwrap(),
// &self.imp().library.get().unwrap(),
// None,
// ));
// }
// #[template_callback]
// fn add_medium(&self, _: &gtk::Button) {
// todo!("Medium import");
// }
// #[template_callback]
// fn add_album(&self, _: &gtk::Button) {
// todo!("Album editor");
// // self.imp()
// // .navigation
// // .get()
// // .unwrap()
// // .push(&MusicusAlbumEditor::new(
// // &self.imp().navigation.get().unwrap(),
// // &self.imp().library.get().unwrap(),
// // None,
// // ));
// }
}