Move desktop app to subdirectory

This commit is contained in:
Elias Projahn 2020-11-14 23:01:51 +01:00
parent ea3bd35ffd
commit 775f3ffe90
109 changed files with 64 additions and 53 deletions

View file

@ -1,22 +0,0 @@
use crate::config;
use gettextrs::gettext;
use gtk::prelude::*;
pub fn show_about_dialog<W: IsA<gtk::Window>>(parent: &W) {
let dialog = gtk::AboutDialogBuilder::new()
.transient_for(parent)
.modal(true)
.logo_icon_name("de.johrpan.musicus")
.program_name(&gettext("Musicus"))
.version(config::VERSION)
.comments(&gettext("The classical music player and organizer."))
.website("https://github.com/johrpan/musicus")
.website_label(&gettext("Further information and source code"))
.copyright("© 2020 Elias Projahn")
.license_type(gtk::License::Agpl30)
.authors(vec![String::from("Elias Projahn <johrpan@gmail.com>")])
.build();
dialog.connect_response(|dialog, _| dialog.close());
dialog.show();
}

View file

@ -1,80 +0,0 @@
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::rc::Rc;
pub struct EnsembleEditor<F>
where
F: Fn(Ensemble) -> () + 'static,
{
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
id: i64,
name_entry: gtk::Entry,
}
impl<F> EnsembleEditor<F>
where
F: Fn(Ensemble) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
ensemble: Option<Ensemble>,
callback: F,
) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/ensemble_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, name_entry);
let id = match ensemble {
Some(ensemble) => {
name_entry.set_text(&ensemble.name);
ensemble.id
}
None => rand::random::<u32>().into(),
};
let result = Rc::new(EnsembleEditor {
backend: backend,
window: window,
callback: callback,
id: id,
name_entry: name_entry,
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
let ensemble = Ensemble {
id: result.id,
name: result.name_entry.get_text().to_string(),
};
let clone = result.clone();
let c = glib::MainContext::default();
c.spawn_local(async move {
clone.backend.update_ensemble(ensemble.clone()).await.unwrap();
clone.window.close();
(clone.callback)(ensemble.clone());
});
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,112 +0,0 @@
use super::EnsembleEditor;
use crate::backend::Backend;
use crate::database::*;
use crate::widgets::*;
use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::convert::TryInto;
use std::rc::Rc;
pub struct EnsembleSelector<F>
where
F: Fn(Ensemble) -> () + 'static,
{
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
list: gtk::ListBox,
search_entry: gtk::SearchEntry,
}
impl<F> EnsembleSelector<F>
where
F: Fn(Ensemble) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P, callback: F) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/ensemble_selector.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, add_button);
get_widget!(builder, gtk::SearchEntry, search_entry);
get_widget!(builder, gtk::ListBox, list);
let result = Rc::new(EnsembleSelector {
backend: backend,
window: window,
callback: callback,
search_entry: search_entry,
list: list,
});
let c = glib::MainContext::default();
let clone = result.clone();
c.spawn_local(async move {
let ensembles = clone.backend.get_ensembles().await.unwrap();
for (index, ensemble) in ensembles.iter().enumerate() {
let label = gtk::Label::new(Some(&ensemble.name));
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
clone.list.insert(&row, -1);
}
clone.list.connect_row_activated(
clone!(@strong clone, @strong ensembles => move |_, row| {
clone.window.close();
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
let index: usize = row.get_index().try_into().unwrap();
(clone.callback)(ensembles[index].clone());
}),
);
clone
.list
.set_filter_func(Some(Box::new(clone!(@strong clone => move |row| {
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
let index: usize = row.get_index().try_into().unwrap();
let search = clone.search_entry.get_text().to_string().to_lowercase();
search.is_empty() || ensembles[index]
.name
.to_lowercase()
.contains(&search)
}))));
});
result
.search_entry
.connect_search_changed(clone!(@strong result => move |_| {
result.list.invalidate_filter();
}));
add_button.connect_clicked(clone!(@strong result => move |_| {
let editor = EnsembleEditor::new(
result.backend.clone(),
&result.window,
None,
clone!(@strong result => move |ensemble| {
result.window.close();
(result.callback)(ensemble);
}),
);
editor.show();
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,80 +0,0 @@
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::rc::Rc;
pub struct InstrumentEditor<F>
where
F: Fn(Instrument) -> () + 'static,
{
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
id: i64,
name_entry: gtk::Entry,
}
impl<F> InstrumentEditor<F>
where
F: Fn(Instrument) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
instrument: Option<Instrument>,
callback: F,
) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/instrument_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, name_entry);
let id = match instrument {
Some(instrument) => {
name_entry.set_text(&instrument.name);
instrument.id
}
None => rand::random::<u32>().into(),
};
let result = Rc::new(InstrumentEditor {
backend: backend,
window: window,
callback: callback,
id: id,
name_entry: name_entry,
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
let instrument = Instrument {
id: result.id,
name: result.name_entry.get_text().to_string(),
};
let c = glib::MainContext::default();
let clone = result.clone();
c.spawn_local(async move {
clone.backend.update_instrument(instrument.clone()).await.unwrap();
clone.window.close();
(clone.callback)(instrument.clone());
});
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,113 +0,0 @@
use super::InstrumentEditor;
use crate::backend::Backend;
use crate::database::*;
use crate::widgets::*;
use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::convert::TryInto;
use std::rc::Rc;
pub struct InstrumentSelector<F>
where
F: Fn(Instrument) -> () + 'static,
{
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
list: gtk::ListBox,
search_entry: gtk::SearchEntry,
}
impl<F> InstrumentSelector<F>
where
F: Fn(Instrument) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P, callback: F) -> Rc<Self> {
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/instrument_selector.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, add_button);
get_widget!(builder, gtk::SearchEntry, search_entry);
get_widget!(builder, gtk::ListBox, list);
let result = Rc::new(InstrumentSelector {
backend: backend,
window: window,
callback: callback,
search_entry: search_entry,
list: list,
});
let c = glib::MainContext::default();
let clone = result.clone();
c.spawn_local(async move {
let instruments = clone.backend.get_instruments().await.unwrap();
for (index, instrument) in instruments.iter().enumerate() {
let label = gtk::Label::new(Some(&instrument.name));
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
let row = SelectorRow::new(index.try_into().unwrap(), &label);
row.show_all();
clone.list.insert(&row, -1);
}
clone.list.connect_row_activated(
clone!(@strong clone, @strong instruments => move |_, row| {
clone.window.close();
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
let index: usize = row.get_index().try_into().unwrap();
(clone.callback)(instruments[index].clone());
}),
);
clone
.list
.set_filter_func(Some(Box::new(clone!(@strong clone => move |row| {
let row = row.get_child().unwrap().downcast::<SelectorRow>().unwrap();
let index: usize = row.get_index().try_into().unwrap();
let search = clone.search_entry.get_text().to_string();
search.is_empty() || instruments[index]
.name
.to_lowercase()
.contains(&clone.search_entry.get_text().to_string().to_lowercase())
}))));
});
result
.search_entry
.connect_search_changed(clone!(@strong result => move |_| {
result.list.invalidate_filter();
}));
add_button.connect_clicked(clone!(@strong result => move |_| {
let editor = InstrumentEditor::new(
result.backend.clone(),
&result.window,
None,
clone!(@strong result => move |instrument| {
result.window.close();
(result.callback)(instrument);
}),
);
editor.show();
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,88 +0,0 @@
use crate::backend::{Backend, LoginData};
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for entering login credentials.
pub struct LoginDialog {
backend: Rc<Backend>,
window: libhandy::Window,
stack: gtk::Stack,
info_bar: gtk::InfoBar,
username_entry: gtk::Entry,
password_entry: gtk::Entry,
selected_cb: RefCell<Option<Box<dyn Fn(LoginData) -> ()>>>,
}
impl LoginDialog {
/// Create a new login dialog.
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/login_dialog.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Stack, stack);
get_widget!(builder, gtk::InfoBar, info_bar);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, login_button);
get_widget!(builder, gtk::Entry, username_entry);
get_widget!(builder, gtk::Entry, password_entry);
window.set_transient_for(Some(parent));
let this = Rc::new(Self {
backend,
window,
stack,
info_bar,
username_entry,
password_entry,
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
login_button.connect_clicked(clone!(@strong this => move |_| {
this.stack.set_visible_child_name("loading");
let data = LoginData {
username: this.username_entry.get_text().to_string(),
password: this.password_entry.get_text().to_string(),
};
let c = glib::MainContext::default();
let clone = this.clone();
c.spawn_local(async move {
clone.backend.set_login_data(data.clone()).await.unwrap();
if clone.backend.login().await.unwrap() {
if let Some(cb) = &*clone.selected_cb.borrow() {
cb(data);
}
clone.window.close();
} else {
clone.stack.set_visible_child_name("content");
clone.info_bar.set_revealed(true);
}
});
}));
this
}
/// The closure to call when the login succeded.
pub fn set_selected_cb<F: Fn(LoginData) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the login dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,41 +0,0 @@
pub mod about;
pub use about::*;
pub mod ensemble_editor;
pub use ensemble_editor::*;
pub mod ensemble_selector;
pub use ensemble_selector::*;
pub mod instrument_editor;
pub use instrument_editor::*;
pub mod instrument_selector;
pub use instrument_selector::*;
pub mod login_dialog;
pub use login_dialog::*;
pub mod person_editor;
pub use person_editor::*;
pub mod person_selector;
pub use person_selector::*;
pub mod preferences;
pub use preferences::*;
pub mod server_dialog;
pub use server_dialog::*;
pub mod recording;
pub use recording::*;
pub mod track_editor;
pub use track_editor::*;
pub mod tracks_editor;
pub use tracks_editor::*;
pub mod work;
pub use work::*;

View file

@ -1,84 +0,0 @@
use crate::backend::Backend;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::rc::Rc;
pub struct PersonEditor<F>
where
F: Fn(Person) -> () + 'static,
{
backend: Rc<Backend>,
window: libhandy::Window,
callback: F,
id: i64,
first_name_entry: gtk::Entry,
last_name_entry: gtk::Entry,
}
impl<F> PersonEditor<F>
where
F: Fn(Person) -> () + 'static,
{
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
person: Option<Person>,
callback: F,
) -> Rc<Self> {
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/person_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, first_name_entry);
get_widget!(builder, gtk::Entry, last_name_entry);
let id = match person {
Some(person) => {
first_name_entry.set_text(&person.first_name);
last_name_entry.set_text(&person.last_name);
person.id
}
None => rand::random::<u32>().into(),
};
let result = Rc::new(PersonEditor {
backend: backend,
window: window,
callback: callback,
id: id,
first_name_entry: first_name_entry,
last_name_entry: last_name_entry,
});
cancel_button.connect_clicked(clone!(@strong result => move |_| {
result.window.close();
}));
save_button.connect_clicked(clone!(@strong result => move |_| {
let person = Person {
id: result.id,
first_name: result.first_name_entry.get_text().to_string(),
last_name: result.last_name_entry.get_text().to_string(),
};
let c = glib::MainContext::default();
let clone = result.clone();
c.spawn_local(async move {
clone.backend.update_person(person.clone()).await.unwrap();
clone.window.close();
(clone.callback)(person.clone());
});
}));
result.window.set_transient_for(Some(parent));
result
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,61 +0,0 @@
use super::PersonEditor;
use crate::backend::Backend;
use crate::database::*;
use crate::widgets::*;
use gio::prelude::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::rc::Rc;
pub struct PersonSelector {
window: libhandy::Window,
}
impl PersonSelector {
pub fn new<P, F>(backend: Rc<Backend>, parent: &P, callback: F) -> Self
where
P: IsA<gtk::Window>,
F: Fn(Person) -> () + 'static,
{
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/person_selector.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Box, vbox);
get_widget!(builder, gtk::Button, add_button);
let callback = Rc::new(callback);
let list = PersonList::new(backend.clone());
list.set_selected(clone!(@strong window, @strong callback => move |person| {
window.close();
callback(person.clone());
}));
vbox.pack_start(&list.widget, true, true, 0);
window.set_transient_for(Some(parent));
add_button.connect_clicked(
clone!(@strong backend, @strong window, @strong callback => move |_| {
let editor = PersonEditor::new(
backend.clone(),
&window,
None,
clone!(@strong window, @strong callback => move |person| {
window.close();
callback(person);
}),
);
editor.show();
}),
);
Self { window }
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,105 +0,0 @@
use super::{LoginDialog, ServerDialog};
use crate::backend::Backend;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::prelude::*;
use std::rc::Rc;
/// A dialog for configuring the app.
pub struct Preferences {
backend: Rc<Backend>,
window: libhandy::Window,
music_library_path_row: libhandy::ActionRow,
url_row: libhandy::ActionRow,
login_row: libhandy::ActionRow,
}
impl Preferences {
/// Create a new preferences dialog.
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/preferences.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, libhandy::ActionRow, music_library_path_row);
get_widget!(builder, gtk::Button, select_music_library_path_button);
get_widget!(builder, libhandy::ActionRow, url_row);
get_widget!(builder, gtk::Button, url_button);
get_widget!(builder, libhandy::ActionRow, login_row);
get_widget!(builder, gtk::Button, login_button);
window.set_transient_for(Some(parent));
let this = Rc::new(Self {
backend,
window,
music_library_path_row,
url_row,
login_row,
});
// Connect signals and callbacks
select_music_library_path_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = gtk::FileChooserNative::new(
Some(&gettext("Select music library folder")),
Some(&this.window), gtk::FileChooserAction::SelectFolder,None, None);
if let gtk::ResponseType::Accept = dialog.run() {
if let Some(path) = dialog.get_filename() {
this.music_library_path_row.set_subtitle(Some(path.to_str().unwrap()));
let context = glib::MainContext::default();
let backend = this.backend.clone();
context.spawn_local(async move {
backend.set_music_library_path(path).await.unwrap();
});
}
}
}));
url_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = ServerDialog::new(this.backend.clone(), &this.window);
dialog.set_selected_cb(clone!(@strong this => move |url| {
this.url_row.set_subtitle(Some(&url));
}));
dialog.show();
}));
login_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = LoginDialog::new(this.backend.clone(), &this.window);
dialog.set_selected_cb(clone!(@strong this => move |data| {
this.login_row.set_subtitle(Some(&data.username));
}));
dialog.show();
}));
// Initialize
if let Some(path) = this.backend.get_music_library_path() {
this.music_library_path_row
.set_subtitle(Some(path.to_str().unwrap()));
}
if let Some(url) = this.backend.get_server_url() {
this.url_row.set_subtitle(Some(&url));
}
if let Some(data) = this.backend.get_login_data() {
this.login_row.set_subtitle(Some(&data.username));
}
this
}
/// Show the preferences dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,11 +0,0 @@
pub mod recording_dialog;
pub use recording_dialog::*;
pub mod recording_editor_dialog;
pub use recording_editor_dialog::*;
mod performance_editor;
mod recording_editor;
mod recording_selector;
mod recording_selector_person_screen;
mod recording_selector_work_screen;

View file

@ -1,174 +0,0 @@
use crate::backend::*;
use crate::database::*;
use crate::dialogs::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for editing a performance within a recording.
pub struct PerformanceEditor {
backend: Rc<Backend>,
window: libhandy::Window,
save_button: gtk::Button,
person_label: gtk::Label,
ensemble_label: gtk::Label,
role_label: gtk::Label,
reset_role_button: gtk::Button,
person: RefCell<Option<Person>>,
ensemble: RefCell<Option<Ensemble>>,
role: RefCell<Option<Instrument>>,
selected_cb: RefCell<Option<Box<dyn Fn(PerformanceDescription) -> ()>>>,
}
impl PerformanceEditor {
/// Create a new performance editor.
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
performance: Option<PerformanceDescription>,
) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/performance_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Button, person_button);
get_widget!(builder, gtk::Button, ensemble_button);
get_widget!(builder, gtk::Button, role_button);
get_widget!(builder, gtk::Button, reset_role_button);
get_widget!(builder, gtk::Label, person_label);
get_widget!(builder, gtk::Label, ensemble_label);
get_widget!(builder, gtk::Label, role_label);
window.set_transient_for(Some(parent));
let this = Rc::new(PerformanceEditor {
backend,
window,
save_button,
person_label,
ensemble_label,
role_label,
reset_role_button,
person: RefCell::new(None),
ensemble: RefCell::new(None),
role: RefCell::new(None),
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
this.save_button
.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(PerformanceDescription {
person: this.person.borrow().clone(),
ensemble: this.ensemble.borrow().clone(),
role: this.role.borrow().clone(),
});
this.window.close();
}
}));
person_button.connect_clicked(clone!(@strong this => move |_| {
PersonSelector::new(this.backend.clone(), &this.window, clone!(@strong this => move |person| {
this.show_person(Some(&person));
this.person.replace(Some(person));
this.show_ensemble(None);
this.ensemble.replace(None);
})).show();
}));
ensemble_button.connect_clicked(clone!(@strong this => move |_| {
EnsembleSelector::new(this.backend.clone(), &this.window, clone!(@strong this => move |ensemble| {
this.show_person(None);
this.person.replace(None);
this.show_ensemble(Some(&ensemble));
this.ensemble.replace(Some(ensemble));
})).show();
}));
role_button.connect_clicked(clone!(@strong this => move |_| {
InstrumentSelector::new(this.backend.clone(), &this.window, clone!(@strong this => move |role| {
this.show_role(Some(&role));
this.role.replace(Some(role));
})).show();
}));
this.reset_role_button
.connect_clicked(clone!(@strong this => move |_| {
this.show_role(None);
this.role.replace(None);
}));
// Initialize
if let Some(performance) = performance {
if let Some(person) = performance.person {
this.show_person(Some(&person));
this.person.replace(Some(person));
} else if let Some(ensemble) = performance.ensemble {
this.show_ensemble(Some(&ensemble));
this.ensemble.replace(Some(ensemble));
}
if let Some(role) = performance.role {
this.show_role(Some(&role));
this.role.replace(Some(role));
}
}
this
}
/// Set a closure to be called when the user has chosen to save the performance.
pub fn set_selected_cb<F: Fn(PerformanceDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the performance editor.
pub fn show(&self) {
self.window.show();
}
/// Update the UI according to person.
fn show_person(&self, person: Option<&Person>) {
if let Some(person) = person {
self.person_label.set_text(&person.name_fl());
self.save_button.set_sensitive(true);
} else {
self.person_label.set_text(&gettext("Select …"));
}
}
/// Update the UI according to ensemble.
fn show_ensemble(&self, ensemble: Option<&Ensemble>) {
if let Some(ensemble) = ensemble {
self.ensemble_label.set_text(&ensemble.name);
self.save_button.set_sensitive(true);
} else {
self.ensemble_label.set_text(&gettext("Select …"));
}
}
/// Update the UI according to role.
fn show_role(&self, role: Option<&Instrument>) {
if let Some(role) = role {
self.role_label.set_text(&role.name);
self.reset_role_button.show();
} else {
self.role_label.set_text(&gettext("Select …"));
self.reset_role_button.hide();
}
}
}

View file

@ -1,86 +0,0 @@
use super::recording_editor::*;
use super::recording_selector::*;
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for selecting and creating a recording.
pub struct RecordingDialog {
pub window: libhandy::Window,
stack: gtk::Stack,
selector: Rc<RecordingSelector>,
editor: Rc<RecordingEditor>,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
}
impl RecordingDialog {
/// Create a new recording dialog.
pub fn new<W: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &W) -> Rc<Self> {
// Create UI
let window = libhandy::Window::new();
window.set_type_hint(gdk::WindowTypeHint::Dialog);
window.set_modal(true);
window.set_transient_for(Some(parent));
window.set_default_size(600, 424);
let selector = RecordingSelector::new(backend.clone());
let editor = RecordingEditor::new(backend.clone(), &window, None);
let stack = gtk::Stack::new();
stack.set_transition_type(gtk::StackTransitionType::Crossfade);
stack.add(&selector.widget);
stack.add(&editor.widget);
window.add(&stack);
window.show_all();
let this = Rc::new(Self {
window,
stack,
selector,
editor,
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
this.selector.set_add_cb(clone!(@strong this => move || {
this.stack.set_visible_child(&this.editor.widget);
}));
this.selector
.set_selected_cb(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording);
this.window.close();
}
}));
this.editor.set_back_cb(clone!(@strong this => move || {
this.stack.set_visible_child(&this.selector.widget);
}));
this.editor
.set_selected_cb(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording);
this.window.close();
}
}));
this
}
/// Set the closure to be called when the user has selected or created a recording.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the recording dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,204 +0,0 @@
use super::performance_editor::*;
use crate::backend::*;
use crate::database::*;
use crate::dialogs::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A widget for creating or editing a recording.
// TODO: Disable buttons if no performance is selected.
pub struct RecordingEditor {
pub widget: gtk::Box,
backend: Rc<Backend>,
parent: gtk::Window,
save_button: gtk::Button,
work_label: gtk::Label,
comment_entry: gtk::Entry,
performance_list: Rc<List<PerformanceDescription>>,
id: i64,
work: RefCell<Option<WorkDescription>>,
performances: RefCell<Vec<PerformanceDescription>>,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
back_cb: RefCell<Option<Box<dyn Fn() -> ()>>>,
}
impl RecordingEditor {
/// Create a new recording editor widget and optionally initialize it. The parent window is
/// used as the parent for newly created dialogs.
pub fn new<W: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &W,
recording: Option<RecordingDescription>,
) -> Rc<Self> {
// Create UI
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/recording_editor.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Button, work_button);
get_widget!(builder, gtk::Label, work_label);
get_widget!(builder, gtk::Entry, comment_entry);
get_widget!(builder, gtk::ScrolledWindow, scroll);
get_widget!(builder, gtk::Button, add_performer_button);
get_widget!(builder, gtk::Button, edit_performer_button);
get_widget!(builder, gtk::Button, remove_performer_button);
let performance_list = List::new(&gettext("No performers added."));
scroll.add(&performance_list.widget);
let (id, work, performances) = match recording {
Some(recording) => {
comment_entry.set_text(&recording.comment);
(recording.id, Some(recording.work), recording.performances)
}
None => (rand::random::<u32>().into(), None, Vec::new()),
};
let this = Rc::new(RecordingEditor {
widget,
backend,
parent: parent.clone().upcast(),
save_button,
work_label,
comment_entry,
performance_list,
id,
work: RefCell::new(work),
performances: RefCell::new(performances),
selected_cb: RefCell::new(None),
back_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.back_cb.borrow() {
cb();
}
}));
this.save_button
.connect_clicked(clone!(@strong this => move |_| {
let recording = RecordingDescription {
id: this.id,
work: this.work.borrow().clone().expect("Tried to create recording without work!"),
comment: this.comment_entry.get_text().to_string(),
performances: this.performances.borrow().clone(),
};
let c = glib::MainContext::default();
let clone = this.clone();
c.spawn_local(async move {
clone.backend.update_recording(recording.clone().into()).await.unwrap();
if let Some(cb) = &*clone.selected_cb.borrow() {
cb(recording.clone());
}
});
}));
work_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = WorkDialog::new(this.backend.clone(), &this.parent);
dialog.set_selected_cb(clone!(@strong this => move |work| {
this.work_selected(&work);
this.work.replace(Some(work));
}));
dialog.show();
}));
this.performance_list.set_make_widget(|performance| {
let label = gtk::Label::new(Some(&performance.get_title()));
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
label.upcast()
});
add_performer_button.connect_clicked(clone!(@strong this => move |_| {
let editor = PerformanceEditor::new(this.backend.clone(), &this.parent, None);
editor.set_selected_cb(clone!(@strong this => move |performance| {
let mut performances = this.performances.borrow_mut();
let index = match this.performance_list.get_selected_index() {
Some(index) => index + 1,
None => performances.len(),
};
performances.insert(index, performance);
this.performance_list.show_items(performances.clone());
this.performance_list.select_index(index);
}));
editor.show();
}));
edit_performer_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.performance_list.get_selected_index() {
let performance = &this.performances.borrow()[index];
let editor = PerformanceEditor::new(
this.backend.clone(),
&this.parent,
Some(performance.clone()),
);
editor.set_selected_cb(clone!(@strong this => move |performance| {
let mut performances = this.performances.borrow_mut();
performances[index] = performance;
this.performance_list.show_items(performances.clone());
this.performance_list.select_index(index);
}));
editor.show();
}
}));
remove_performer_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.performance_list.get_selected_index() {
let mut performances = this.performances.borrow_mut();
performances.remove(index);
this.performance_list.show_items(performances.clone());
this.performance_list.select_index(index);
}
}));
// Initialize
if let Some(work) = &*this.work.borrow() {
this.work_selected(work);
}
this.performance_list.show_items(this.performances.borrow().clone());
this
}
/// Set the closure to be called if the editor is canceled.
pub fn set_back_cb<F: Fn() -> () + 'static>(&self, cb: F) {
self.back_cb.replace(Some(Box::new(cb)));
}
/// Set the closure to be called if the recording was created.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Update the UI according to work.
fn work_selected(&self, work: &WorkDescription) {
self.work_label.set_text(&format!("{}: {}", work.composer.name_fl(), work.title));
self.save_button.set_sensitive(true);
}
}

View file

@ -1,63 +0,0 @@
use super::recording_editor::*;
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for creating or editing a recording.
pub struct RecordingEditorDialog {
pub window: libhandy::Window,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
}
impl RecordingEditorDialog {
/// Create a new recording editor dialog and optionally initialize it.
pub fn new<W: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &W,
recording: Option<RecordingDescription>,
) -> Rc<Self> {
// Create UI
let window = libhandy::Window::new();
window.set_type_hint(gdk::WindowTypeHint::Dialog);
window.set_modal(true);
window.set_transient_for(Some(parent));
let editor = RecordingEditor::new(backend.clone(), &window, recording);
window.add(&editor.widget);
window.show_all();
let this = Rc::new(Self {
window,
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
editor.set_back_cb(clone!(@strong this => move || {
this.window.close();
}));
editor.set_selected_cb(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording);
this.window.close();
}
}));
this
}
/// Set the closure to be called when the user edited or created a recording.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the recording editor dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,89 +0,0 @@
use super::recording_selector_person_screen::*;
use crate::backend::Backend;
use crate::database::*;
use crate::widgets::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A widget for selecting a recording from a list of existing ones.
pub struct RecordingSelector {
pub widget: libhandy::Leaflet,
backend: Rc<Backend>,
sidebar_box: gtk::Box,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
add_cb: RefCell<Option<Box<dyn Fn() -> ()>>>,
navigator: Rc<Navigator>,
}
impl RecordingSelector {
/// Create a new recording selector.
pub fn new(backend: Rc<Backend>) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/recording_selector.ui");
get_widget!(builder, libhandy::Leaflet, widget);
get_widget!(builder, gtk::Button, add_button);
get_widget!(builder, gtk::Box, sidebar_box);
get_widget!(builder, gtk::Box, empty_screen);
let person_list = PersonList::new(backend.clone());
sidebar_box.pack_start(&person_list.widget, true, true, 0);
let navigator = Navigator::new(&empty_screen);
widget.add(&navigator.widget);
let this = Rc::new(Self {
widget,
backend,
sidebar_box,
selected_cb: RefCell::new(None),
add_cb: RefCell::new(None),
navigator,
});
// Connect signals and callbacks
add_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.add_cb.borrow() {
cb();
}
}));
person_list.set_selected(clone!(@strong this => move |person| {
let person_screen = RecordingSelectorPersonScreen::new(
this.backend.clone(),
person.clone(),
);
person_screen.set_selected_cb(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording);
}
}));
this.navigator.clone().push(person_screen);
this.widget.set_visible_child(&this.navigator.widget);
}));
this.navigator.set_back_cb(clone!(@strong this => move || {
this.widget.set_visible_child(&this.sidebar_box);
}));
this
}
/// Set the closure to be called if the user wants to add a new recording.
pub fn set_add_cb<F: Fn() -> () + 'static>(&self, cb: F) {
self.add_cb.replace(Some(Box::new(cb)));
}
/// Set the closure to be called when the user has selected a recording.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
}

View file

@ -1,126 +0,0 @@
use super::recording_selector_work_screen::*;
use crate::backend::*;
use crate::database::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::HeaderBarExt;
use std::cell::RefCell;
use std::rc::Rc;
/// A screen within the recording selector that presents a list of works and switches to a work
/// screen on selection.
pub struct RecordingSelectorPersonScreen {
backend: Rc<Backend>,
widget: gtk::Box,
stack: gtk::Stack,
work_list: Rc<List<WorkDescription>>,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
navigator: RefCell<Option<Rc<Navigator>>>,
}
impl RecordingSelectorPersonScreen {
/// Create a new recording selector person screen.
pub fn new(backend: Rc<Backend>, person: Person) -> Rc<Self> {
// Create UI
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/recording_selector_screen.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, libhandy::HeaderBar, header);
get_widget!(builder, gtk::Button, back_button);
get_widget!(builder, gtk::Stack, stack);
header.set_title(Some(&person.name_fl()));
let work_list = List::new(&gettext("No works found."));
stack.add_named(&work_list.widget, "content");
let this = Rc::new(Self {
backend,
widget,
stack,
work_list,
selected_cb: RefCell::new(None),
navigator: RefCell::new(None),
});
// Connect signals and callbacks
back_button.connect_clicked(clone!(@strong this => move |_| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
navigator.pop();
}
}));
this.work_list.set_make_widget(|work: &WorkDescription| {
let label = gtk::Label::new(Some(&work.title));
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
label.upcast()
});
this.work_list
.set_selected(clone!(@strong this => move |work| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
let work_screen = RecordingSelectorWorkScreen::new(
this.backend.clone(),
work.clone(),
);
work_screen.set_selected_cb(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording);
}
}));
navigator.push(work_screen);
}
}));
// Initialize
let context = glib::MainContext::default();
let clone = this.clone();
context.spawn_local(async move {
let works = clone
.backend
.get_work_descriptions(person.id)
.await
.unwrap();
clone.work_list.show_items(works);
clone.stack.set_visible_child_name("content");
});
this
}
/// Sets a closure to be called when the user has selected a recording.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
}
impl NavigatorScreen for RecordingSelectorPersonScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget {
self.widget.clone().upcast()
}
fn detach_navigator(&self) {
self.navigator.replace(None);
}
}

View file

@ -1,120 +0,0 @@
use crate::backend::*;
use crate::database::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::HeaderBarExt;
use std::cell::RefCell;
use std::rc::Rc;
/// A screen within the recording selector presenting a list of recordings for a work.
pub struct RecordingSelectorWorkScreen {
backend: Rc<Backend>,
widget: gtk::Box,
stack: gtk::Stack,
recording_list: Rc<List<RecordingDescription>>,
selected_cb: RefCell<Option<Box<dyn Fn(RecordingDescription) -> ()>>>,
navigator: RefCell<Option<Rc<Navigator>>>,
}
impl RecordingSelectorWorkScreen {
/// Create a new recording selector work screen.
pub fn new(backend: Rc<Backend>, work: WorkDescription) -> Rc<Self> {
// Create UI
let builder =
gtk::Builder::from_resource("/de/johrpan/musicus/ui/recording_selector_screen.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, libhandy::HeaderBar, header);
get_widget!(builder, gtk::Button, back_button);
get_widget!(builder, gtk::Stack, stack);
header.set_title(Some(&work.title));
header.set_subtitle(Some(&work.composer.name_fl()));
let recording_list = List::new(&gettext("No recordings found."));
stack.add_named(&recording_list.widget, "content");
let this = Rc::new(Self {
backend,
widget,
stack,
recording_list,
selected_cb: RefCell::new(None),
navigator: RefCell::new(None),
});
// Connect signals and callbacks
back_button.connect_clicked(clone!(@strong this => move |_| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
navigator.pop();
}
}));
this.recording_list.set_make_widget(|recording: &RecordingDescription| {
let work_label = gtk::Label::new(Some(&recording.work.get_title()));
work_label.set_ellipsize(pango::EllipsizeMode::End);
work_label.set_halign(gtk::Align::Start);
let performers_label = gtk::Label::new(Some(&recording.get_performers()));
performers_label.set_ellipsize(pango::EllipsizeMode::End);
performers_label.set_opacity(0.5);
performers_label.set_halign(gtk::Align::Start);
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
vbox.set_border_width(6);
vbox.add(&work_label);
vbox.add(&performers_label);
vbox.upcast()
});
this.recording_list
.set_selected(clone!(@strong this => move |recording| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(recording.clone());
}
}));
// Initialize
let context = glib::MainContext::default();
let clone = this.clone();
context.spawn_local(async move {
let recordings = clone
.backend
.get_recordings_for_work(work.id)
.await
.unwrap();
clone.recording_list.show_items(recordings);
clone.stack.set_visible_child_name("content");
});
this
}
/// Sets a closure to be called when the user has selected a recording.
pub fn set_selected_cb<F: Fn(RecordingDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
}
impl NavigatorScreen for RecordingSelectorWorkScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget {
self.widget.clone().upcast()
}
fn detach_navigator(&self) {
self.navigator.replace(None);
}
}

View file

@ -1,65 +0,0 @@
use crate::backend::Backend;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for setting up the server.
pub struct ServerDialog {
backend: Rc<Backend>,
window: libhandy::Window,
url_entry: gtk::Entry,
selected_cb: RefCell<Option<Box<dyn Fn(String) -> ()>>>,
}
impl ServerDialog {
/// Create a new server dialog.
pub fn new<P: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &P) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/server_dialog.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, set_button);
get_widget!(builder, gtk::Entry, url_entry);
window.set_transient_for(Some(parent));
let this = Rc::new(Self {
backend,
window,
url_entry,
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
set_button.connect_clicked(clone!(@strong this => move |_| {
let url = this.url_entry.get_text().to_string();
this.backend.set_server_url(&url).unwrap();
if let Some(cb) = &*this.selected_cb.borrow() {
cb(url);
}
this.window.close();
}));
this
}
/// The closure to call when the server was set.
pub fn set_selected_cb<F: Fn(String) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the server dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,117 +0,0 @@
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::convert::TryInto;
use std::rc::Rc;
pub struct TrackEditor {
window: libhandy::Window,
}
impl TrackEditor {
pub fn new<W, F>(parent: &W, track: TrackDescription, work: WorkDescription, callback: F) -> Self
where
W: IsA<gtk::Window>,
F: Fn(TrackDescription) -> () + 'static,
{
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/track_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::ListBox, list);
window.set_transient_for(Some(parent));
cancel_button.connect_clicked(clone!(@strong window => move |_| {
window.close();
}));
let work = Rc::new(work);
let work_parts = Rc::new(RefCell::new(track.work_parts));
let file_name = track.file_name;
save_button.connect_clicked(clone!(@strong work_parts, @strong window => move |_| {
let mut work_parts = work_parts.borrow_mut();
work_parts.sort();
callback(TrackDescription {
work_parts: work_parts.clone(),
file_name: file_name.clone(),
});
window.close();
}));
for (index, part) in work.parts.iter().enumerate() {
let check = gtk::CheckButton::new();
check.set_active(work_parts.borrow().contains(&index));
check.connect_toggled(clone!(@strong check, @strong work_parts => move |_| {
if check.get_active() {
let mut work_parts = work_parts.borrow_mut();
work_parts.push(index);
} else {
let mut work_parts = work_parts.borrow_mut();
if let Some(pos) = work_parts.iter().position(|part| *part == index) {
work_parts.remove(pos);
}
}
}));
let label = gtk::Label::new(Some(&part.title));
label.set_halign(gtk::Align::Start);
label.set_ellipsize(pango::EllipsizeMode::End);
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 6);
hbox.set_border_width(6);
hbox.add(&check);
hbox.add(&label);
let row = gtk::ListBoxRow::new();
row.add(&hbox);
row.show_all();
list.add(&row);
list.connect_row_activated(
clone!(@strong row, @strong check => move |_, activated_row| {
if *activated_row == row {
check.activate();
}
}),
);
}
let mut section_count = 0;
for section in &work.sections {
let attributes = pango::AttrList::new();
attributes.insert(pango::Attribute::new_weight(pango::Weight::Bold).unwrap());
let label = gtk::Label::new(Some(&section.title));
label.set_halign(gtk::Align::Start);
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_attributes(Some(&attributes));
let wrap = gtk::Box::new(gtk::Orientation::Vertical, 0);
wrap.set_border_width(6);
wrap.add(&label);
let row = gtk::ListBoxRow::new();
row.set_activatable(false);
row.add(&wrap);
row.show_all();
list.insert(
&row,
(section.before_index + section_count).try_into().unwrap(),
);
section_count += 1;
}
Self { window }
}
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,284 +0,0 @@
use super::*;
use crate::backend::*;
use crate::database::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for editing a set of tracks.
// TODO: Disable buttons if no track is selected.
pub struct TracksEditor {
backend: Rc<Backend>,
window: libhandy::Window,
save_button: gtk::Button,
recording_stack: gtk::Stack,
work_label: gtk::Label,
performers_label: gtk::Label,
track_list: Rc<List<TrackDescription>>,
recording: RefCell<Option<RecordingDescription>>,
tracks: RefCell<Vec<TrackDescription>>,
callback: RefCell<Option<Box<dyn Fn() -> ()>>>,
}
impl TracksEditor {
/// Create a new track editor an optionally initialize it with a recording and a list of
/// tracks.
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
recording: Option<RecordingDescription>,
tracks: Vec<TrackDescription>,
) -> Rc<Self> {
// UI setup
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/tracks_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Button, recording_button);
get_widget!(builder, gtk::Stack, recording_stack);
get_widget!(builder, gtk::Label, work_label);
get_widget!(builder, gtk::Label, performers_label);
get_widget!(builder, gtk::ScrolledWindow, scroll);
get_widget!(builder, gtk::Button, add_track_button);
get_widget!(builder, gtk::Button, edit_track_button);
get_widget!(builder, gtk::Button, remove_track_button);
get_widget!(builder, gtk::Button, move_track_up_button);
get_widget!(builder, gtk::Button, move_track_down_button);
window.set_transient_for(Some(parent));
cancel_button.connect_clicked(clone!(@strong window => move |_| {
window.close();
}));
let track_list = List::new(&gettext("Add some tracks."));
scroll.add(&track_list.widget);
let this = Rc::new(Self {
backend,
window,
save_button,
recording_stack,
work_label,
performers_label,
track_list,
recording: RefCell::new(recording),
tracks: RefCell::new(tracks),
callback: RefCell::new(None),
});
// Signals and callbacks
this.save_button
.connect_clicked(clone!(@strong this => move |_| {
let context = glib::MainContext::default();
let this = this.clone();
context.spawn_local(async move {
this.backend.update_tracks(
this.recording.borrow().as_ref().unwrap().id,
this.tracks.borrow().clone(),
).await.unwrap();
if let Some(callback) = &*this.callback.borrow() {
callback();
}
this.window.close();
});
}));
recording_button.connect_clicked(clone!(@strong this => move |_| {
let dialog = RecordingDialog::new(this.backend.clone(), &this.window);
dialog.set_selected_cb(clone!(@strong this => move |recording| {
this.recording_selected(&recording);
this.recording.replace(Some(recording));
}));
dialog.show();
}
));
this.track_list
.set_make_widget(clone!(@strong this => move |track| {
this.build_track_row(track)
}));
add_track_button.connect_clicked(clone!(@strong this => move |_| {
let music_library_path = this.backend.get_music_library_path().unwrap();
let dialog = gtk::FileChooserNative::new(
Some(&gettext("Select audio files")),
Some(&this.window),
gtk::FileChooserAction::Open,
None,
None,
);
dialog.set_select_multiple(true);
dialog.set_current_folder(&music_library_path);
if let gtk::ResponseType::Accept = dialog.run() {
let mut index = match this.track_list.get_selected_index() {
Some(index) => index + 1,
None => this.tracks.borrow().len(),
};
{
let mut tracks = this.tracks.borrow_mut();
for file_name in dialog.get_filenames() {
let file_name = file_name.strip_prefix(&music_library_path).unwrap();
tracks.insert(index, TrackDescription {
work_parts: Vec::new(),
file_name: String::from(file_name.to_str().unwrap()),
});
index += 1;
}
}
this.track_list.show_items(this.tracks.borrow().clone());
this.autofill_parts();
this.track_list.select_index(index);
}
}));
remove_track_button.connect_clicked(clone!(@strong this => move |_| {
match this.track_list.get_selected_index() {
Some(index) => {
let mut tracks = this.tracks.borrow_mut();
tracks.remove(index);
this.track_list.show_items(tracks.clone());
this.track_list.select_index(index);
}
None => (),
}
}));
move_track_up_button.connect_clicked(clone!(@strong this => move |_| {
match this.track_list.get_selected_index() {
Some(index) => {
if index > 0 {
let mut tracks = this.tracks.borrow_mut();
tracks.swap(index - 1, index);
this.track_list.show_items(tracks.clone());
this.track_list.select_index(index - 1);
}
}
None => (),
}
}));
move_track_down_button.connect_clicked(clone!(@strong this => move |_| {
match this.track_list.get_selected_index() {
Some(index) => {
let mut tracks = this.tracks.borrow_mut();
if index < tracks.len() - 1 {
tracks.swap(index, index + 1);
this.track_list.show_items(tracks.clone());
this.track_list.select_index(index + 1);
}
}
None => (),
}
}));
edit_track_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.track_list.get_selected_index() {
if let Some(recording) = &*this.recording.borrow() {
TrackEditor::new(&this.window, this.tracks.borrow()[index].clone(), recording.work.clone(), clone!(@strong this => move |track| {
let mut tracks = this.tracks.borrow_mut();
tracks[index] = track;
this.track_list.show_items(tracks.clone());
this.track_list.select_index(index);
})).show();
}
}
}));
// Initialization
if let Some(recording) = &*this.recording.borrow() {
this.recording_selected(recording);
}
this.track_list.show_items(this.tracks.borrow().clone());
this
}
/// Set a callback to be called when the tracks are saved.
pub fn set_callback<F: Fn() -> () + 'static>(&self, cb: F) {
self.callback.replace(Some(Box::new(cb)));
}
/// Open the track editor.
pub fn show(&self) {
self.window.show();
}
/// Create a widget representing a track.
fn build_track_row(&self, track: &TrackDescription) -> gtk::Widget {
let mut title_parts = Vec::<String>::new();
for part in &track.work_parts {
if let Some(recording) = &*self.recording.borrow() {
title_parts.push(recording.work.parts[*part].title.clone());
}
}
let title = if title_parts.is_empty() {
gettext("Unknown")
} else {
title_parts.join(", ")
};
let title_label = gtk::Label::new(Some(&title));
title_label.set_ellipsize(pango::EllipsizeMode::End);
title_label.set_halign(gtk::Align::Start);
let file_name_label = gtk::Label::new(Some(&track.file_name));
file_name_label.set_ellipsize(pango::EllipsizeMode::End);
file_name_label.set_opacity(0.5);
file_name_label.set_halign(gtk::Align::Start);
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
vbox.set_border_width(6);
vbox.add(&title_label);
vbox.add(&file_name_label);
vbox.upcast()
}
/// Set everything up after selecting a recording.
fn recording_selected(&self, recording: &RecordingDescription) {
self.work_label.set_text(&recording.work.get_title());
self.performers_label.set_text(&recording.get_performers());
self.recording_stack.set_visible_child_name("selected");
self.save_button.set_sensitive(true);
self.autofill_parts();
}
/// Automatically try to put work part information from the selected recording into the
/// selected tracks.
fn autofill_parts(&self) {
if let Some(recording) = &*self.recording.borrow() {
let mut tracks = self.tracks.borrow_mut();
for (index, _) in recording.work.parts.iter().enumerate() {
if let Some(mut track) = tracks.get_mut(index) {
track.work_parts = vec![index];
} else {
break;
}
}
self.track_list.show_items(tracks.clone());
}
}
}

View file

@ -1,11 +0,0 @@
pub mod work_dialog;
pub use work_dialog::*;
pub mod work_editor_dialog;
pub use work_editor_dialog::*;
mod part_editor;
mod section_editor;
mod work_editor;
mod work_selector;
mod work_selector_person_screen;

View file

@ -1,170 +0,0 @@
use crate::backend::*;
use crate::database::*;
use crate::dialogs::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for creating or editing a work part.
pub struct PartEditor {
backend: Rc<Backend>,
window: libhandy::Window,
title_entry: gtk::Entry,
composer_label: gtk::Label,
reset_composer_button: gtk::Button,
instrument_list: Rc<List<Instrument>>,
composer: RefCell<Option<Person>>,
instruments: RefCell<Vec<Instrument>>,
ready_cb: RefCell<Option<Box<dyn Fn(WorkPartDescription) -> ()>>>,
}
impl PartEditor {
/// Create a new part editor and optionally initialize it.
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
part: Option<WorkPartDescription>,
) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/part_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, title_entry);
get_widget!(builder, gtk::Button, composer_button);
get_widget!(builder, gtk::Label, composer_label);
get_widget!(builder, gtk::Button, reset_composer_button);
get_widget!(builder, gtk::ScrolledWindow, scroll);
get_widget!(builder, gtk::Button, add_instrument_button);
get_widget!(builder, gtk::Button, remove_instrument_button);
window.set_transient_for(Some(parent));
let instrument_list = List::new(&gettext("No instruments added."));
scroll.add(&instrument_list.widget);
let (composer, instruments) = match part {
Some(part) => {
title_entry.set_text(&part.title);
(part.composer, part.instruments)
}
None => (None, Vec::new()),
};
let this = Rc::new(Self {
backend,
window,
title_entry,
composer_label,
reset_composer_button,
instrument_list,
composer: RefCell::new(composer),
instruments: RefCell::new(instruments),
ready_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
save_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.ready_cb.borrow() {
cb(WorkPartDescription {
title: this.title_entry.get_text().to_string(),
composer: this.composer.borrow().clone(),
instruments: this.instruments.borrow().clone(),
});
}
this.window.close();
}));
composer_button.connect_clicked(clone!(@strong this => move |_| {
PersonSelector::new(this.backend.clone(), &this.window, clone!(@strong this => move |person| {
this.show_composer(Some(&person));
this.composer.replace(Some(person));
})).show();
}));
this.reset_composer_button
.connect_clicked(clone!(@strong this => move |_| {
this.composer.replace(None);
this.show_composer(None);
}));
this.instrument_list.set_make_widget(|instrument| {
let label = gtk::Label::new(Some(&instrument.name));
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
label.upcast()
});
add_instrument_button.connect_clicked(clone!(@strong this => move |_| {
InstrumentSelector::new(this.backend.clone(), &this.window, clone!(@strong this => move |instrument| {
let mut instruments = this.instruments.borrow_mut();
let index = match this.instrument_list.get_selected_index() {
Some(index) => index + 1,
None => instruments.len(),
};
instruments.insert(index, instrument);
this.instrument_list.show_items(instruments.clone());
this.instrument_list.select_index(index);
})).show();
}));
remove_instrument_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.instrument_list.get_selected_index() {
let mut instruments = this.instruments.borrow_mut();
instruments.remove(index);
this.instrument_list.show_items(instruments.clone());
this.instrument_list.select_index(index);
}
}));
// Initialize
if let Some(composer) = &*this.composer.borrow() {
this.show_composer(Some(composer));
}
this.instrument_list
.show_items(this.instruments.borrow().clone());
this
}
/// Set the closure to be called when the user wants to save the part.
pub fn set_ready_cb<F: Fn(WorkPartDescription) -> () + 'static>(&self, cb: F) {
self.ready_cb.replace(Some(Box::new(cb)));
}
/// Show the part editor.
pub fn show(&self) {
self.window.show();
}
/// Update the UI according to person.
fn show_composer(&self, person: Option<&Person>) {
if let Some(person) = person {
self.composer_label.set_text(&person.name_fl());
self.reset_composer_button.show();
} else {
self.composer_label.set_text(&gettext("Select …"));
self.reset_composer_button.hide();
}
}
}

View file

@ -1,73 +0,0 @@
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for creating or editing a work section.
pub struct SectionEditor {
window: libhandy::Window,
title_entry: gtk::Entry,
ready_cb: RefCell<Option<Box<dyn Fn(WorkSectionDescription) -> ()>>>,
}
impl SectionEditor {
/// Create a new section editor and optionally initialize it.
pub fn new<P: IsA<gtk::Window>>(
parent: &P,
section: Option<WorkSectionDescription>,
) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/section_editor.ui");
get_widget!(builder, libhandy::Window, window);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, title_entry);
window.set_transient_for(Some(parent));
if let Some(section) = section {
title_entry.set_text(&section.title);
}
let this = Rc::new(Self {
window,
title_entry,
ready_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
this.window.close();
}));
save_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.ready_cb.borrow() {
cb(WorkSectionDescription {
before_index: 0,
title: this.title_entry.get_text().to_string(),
});
}
this.window.close();
}));
this
}
/// Set the closure to be called when the user wants to save the section. Note that the
/// resulting object will always have `before_index` set to 0. The caller is expected to
/// change that later before adding the section to the database.
pub fn set_ready_cb<F: Fn(WorkSectionDescription) -> () + 'static>(&self, cb: F) {
self.ready_cb.replace(Some(Box::new(cb)));
}
/// Show the section editor.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,86 +0,0 @@
use super::work_editor::*;
use super::work_selector::*;
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for selecting and creating a work.
pub struct WorkDialog {
pub window: libhandy::Window,
stack: gtk::Stack,
selector: Rc<WorkSelector>,
editor: Rc<WorkEditor>,
selected_cb: RefCell<Option<Box<dyn Fn(WorkDescription) -> ()>>>,
}
impl WorkDialog {
/// Create a new work dialog.
pub fn new<W: IsA<gtk::Window>>(backend: Rc<Backend>, parent: &W) -> Rc<Self> {
// Create UI
let window = libhandy::Window::new();
window.set_type_hint(gdk::WindowTypeHint::Dialog);
window.set_modal(true);
window.set_transient_for(Some(parent));
window.set_default_size(600, 424);
let selector = WorkSelector::new(backend.clone());
let editor = WorkEditor::new(backend.clone(), &window, None);
let stack = gtk::Stack::new();
stack.set_transition_type(gtk::StackTransitionType::Crossfade);
stack.add(&selector.widget);
stack.add(&editor.widget);
window.add(&stack);
window.show_all();
let this = Rc::new(Self {
window,
stack,
selector,
editor,
selected_cb: RefCell::new(None),
});
// Connect signals and callbacks
this.selector.set_add_cb(clone!(@strong this => move || {
this.stack.set_visible_child(&this.editor.widget);
}));
this.selector
.set_selected_cb(clone!(@strong this => move |work| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(work);
this.window.close();
}
}));
this.editor.set_cancel_cb(clone!(@strong this => move || {
this.stack.set_visible_child(&this.selector.widget);
}));
this.editor
.set_saved_cb(clone!(@strong this => move |work| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(work);
this.window.close();
}
}));
this
}
/// Set the closure to be called when the user has selected or created a work.
pub fn set_selected_cb<F: Fn(WorkDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
/// Show the work dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,357 +0,0 @@
use super::part_editor::*;
use super::section_editor::*;
use crate::backend::*;
use crate::database::*;
use crate::dialogs::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use std::cell::RefCell;
use std::convert::TryInto;
use std::rc::Rc;
/// Either a work part or a work section.
#[derive(Clone)]
enum PartOrSection {
Part(WorkPartDescription),
Section(WorkSectionDescription),
}
/// A widget for editing and creating works.
pub struct WorkEditor {
pub widget: gtk::Box,
backend: Rc<Backend>,
parent: gtk::Window,
save_button: gtk::Button,
title_entry: gtk::Entry,
composer_label: gtk::Label,
instrument_list: Rc<List<Instrument>>,
part_list: Rc<List<PartOrSection>>,
id: i64,
composer: RefCell<Option<Person>>,
instruments: RefCell<Vec<Instrument>>,
structure: RefCell<Vec<PartOrSection>>,
cancel_cb: RefCell<Option<Box<dyn Fn() -> ()>>>,
saved_cb: RefCell<Option<Box<dyn Fn(WorkDescription) -> ()>>>,
}
impl WorkEditor {
/// Create a new work editor widget and optionally initialize it. The parent window is used
/// as the parent for newly created dialogs.
pub fn new<P: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &P,
work: Option<WorkDescription>,
) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_editor.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, gtk::Button, cancel_button);
get_widget!(builder, gtk::Button, save_button);
get_widget!(builder, gtk::Entry, title_entry);
get_widget!(builder, gtk::Button, composer_button);
get_widget!(builder, gtk::Label, composer_label);
get_widget!(builder, gtk::ScrolledWindow, instruments_scroll);
get_widget!(builder, gtk::Button, add_instrument_button);
get_widget!(builder, gtk::Button, remove_instrument_button);
get_widget!(builder, gtk::ScrolledWindow, structure_scroll);
get_widget!(builder, gtk::Button, add_part_button);
get_widget!(builder, gtk::Button, remove_part_button);
get_widget!(builder, gtk::Button, add_section_button);
get_widget!(builder, gtk::Button, edit_part_button);
get_widget!(builder, gtk::Button, move_part_up_button);
get_widget!(builder, gtk::Button, move_part_down_button);
let instrument_list = List::new(&gettext("No instruments added."));
instruments_scroll.add(&instrument_list.widget);
let part_list = List::new(&gettext("No work parts added."));
structure_scroll.add(&part_list.widget);
let (id, composer, instruments, structure) = match work {
Some(work) => {
title_entry.set_text(&work.title);
let mut structure = Vec::new();
for part in work.parts {
structure.push(PartOrSection::Part(part));
}
for section in work.sections {
structure.insert(
section.before_index.try_into().unwrap(),
PartOrSection::Section(section),
);
}
(work.id, Some(work.composer), work.instruments, structure)
}
None => (rand::random::<u32>().into(), None, Vec::new(), Vec::new()),
};
let this = Rc::new(Self {
widget,
backend,
parent: parent.clone().upcast(),
save_button,
id,
title_entry,
composer_label,
instrument_list,
part_list,
composer: RefCell::new(composer),
instruments: RefCell::new(instruments),
structure: RefCell::new(structure),
cancel_cb: RefCell::new(None),
saved_cb: RefCell::new(None),
});
// Connect signals and callbacks
cancel_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.cancel_cb.borrow() {
cb();
}
}));
this.save_button.connect_clicked(clone!(@strong this => move |_| {
let mut section_count = 0;
let mut parts = Vec::new();
let mut sections = Vec::new();
for (index, pos) in this.structure.borrow().iter().enumerate() {
match pos {
PartOrSection::Part(part) => parts.push(part.clone()),
PartOrSection::Section(section) => {
let mut section = section.clone();
let index: i64 = index.try_into().unwrap();
section.before_index = index - section_count;
sections.push(section);
section_count += 1;
}
}
}
let work = WorkDescription {
id: this.id,
title: this.title_entry.get_text().to_string(),
composer: this.composer.borrow().clone().expect("Tried to create work without composer!"),
instruments: this.instruments.borrow().clone(),
parts: parts,
sections: sections,
};
let c = glib::MainContext::default();
let clone = this.clone();
c.spawn_local(async move {
clone.backend.update_work(work.clone().into()).await.unwrap();
if let Some(cb) = &*clone.saved_cb.borrow() {
cb(work);
}
});
}));
composer_button.connect_clicked(clone!(@strong this => move |_| {
PersonSelector::new(this.backend.clone(), &this.parent, clone!(@strong this => move |person| {
this.show_composer(&person);
this.composer.replace(Some(person));
})).show();
}));
this.instrument_list.set_make_widget(|instrument| {
let label = gtk::Label::new(Some(&instrument.name));
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
label.upcast()
});
add_instrument_button.connect_clicked(clone!(@strong this => move |_| {
InstrumentSelector::new(this.backend.clone(), &this.parent, clone!(@strong this => move |instrument| {
let mut instruments = this.instruments.borrow_mut();
let index = match this.instrument_list.get_selected_index() {
Some(index) => index + 1,
None => instruments.len(),
};
instruments.insert(index, instrument);
this.instrument_list.show_items(instruments.clone());
this.instrument_list.select_index(index);
})).show();
}));
remove_instrument_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.instrument_list.get_selected_index() {
let mut instruments = this.instruments.borrow_mut();
instruments.remove(index);
this.instrument_list.show_items(instruments.clone());
this.instrument_list.select_index(index);
}
}));
this.part_list.set_make_widget(|pos| {
let label = gtk::Label::new(None);
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
match pos {
PartOrSection::Part(part) => {
label.set_text(&part.title);
label.set_margin_start(12);
}
PartOrSection::Section(section) => {
let attrs = pango::AttrList::new();
attrs.insert(pango::Attribute::new_weight(pango::Weight::Bold).unwrap());
label.set_attributes(Some(&attrs));
label.set_text(&section.title);
label.set_margin_start(6);
}
}
label.upcast()
});
add_part_button.connect_clicked(clone!(@strong this => move |_| {
let editor = PartEditor::new(this.backend.clone(), &this.parent, None);
editor.set_ready_cb(clone!(@strong this => move |part| {
let mut structure = this.structure.borrow_mut();
let index = match this.part_list.get_selected_index() {
Some(index) => index + 1,
None => structure.len(),
};
structure.insert(index, PartOrSection::Part(part));
this.part_list.show_items(structure.clone());
this.part_list.select_index(index);
}));
editor.show();
}));
add_section_button.connect_clicked(clone!(@strong this => move |_| {
let editor = SectionEditor::new(&this.parent, None);
editor.set_ready_cb(clone!(@strong this => move |section| {
let mut structure = this.structure.borrow_mut();
let index = match this.part_list.get_selected_index() {
Some(index) => index + 1,
None => structure.len(),
};
structure.insert(index, PartOrSection::Section(section));
this.part_list.show_items(structure.clone());
this.part_list.select_index(index);
}));
editor.show();
}));
edit_part_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.part_list.get_selected_index() {
match this.structure.borrow()[index].clone() {
PartOrSection::Part(part) => {
let editor = PartEditor::new(
this.backend.clone(),
&this.parent,
Some(part),
);
editor.set_ready_cb(clone!(@strong this => move |part| {
let mut structure = this.structure.borrow_mut();
structure[index] = PartOrSection::Part(part);
this.part_list.show_items(structure.clone());
this.part_list.select_index(index);
}));
editor.show();
}
PartOrSection::Section(section) => {
let editor = SectionEditor::new(&this.parent, Some(section));
editor.set_ready_cb(clone!(@strong this => move |section| {
let mut structure = this.structure.borrow_mut();
structure[index] = PartOrSection::Section(section);
this.part_list.show_items(structure.clone());
this.part_list.select_index(index);
}));
editor.show();
}
}
}
}));
remove_part_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.part_list.get_selected_index() {
let mut structure = this.structure.borrow_mut();
structure.remove(index);
this.part_list.show_items(structure.clone());
this.part_list.select_index(index);
}
}));
move_part_up_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.part_list.get_selected_index() {
if index > 0 {
let mut structure = this.structure.borrow_mut();
structure.swap(index - 1, index);
this.part_list.show_items(structure.clone());
this.part_list.select_index(index - 1);
}
}
}));
move_part_down_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(index) = this.part_list.get_selected_index() {
let mut structure = this.structure.borrow_mut();
if index < structure.len() - 1 {
structure.swap(index, index + 1);
this.part_list.show_items(structure.clone());
this.part_list.select_index(index + 1);
}
}
}));
// Initialization
if let Some(composer) = &*this.composer.borrow() {
this.show_composer(composer);
}
this.instrument_list.show_items(this.instruments.borrow().clone());
this.part_list.show_items(this.structure.borrow().clone());
this
}
/// The closure to call when the editor is canceled.
pub fn set_cancel_cb<F: Fn() -> () + 'static>(&self, cb: F) {
self.cancel_cb.replace(Some(Box::new(cb)));
}
/// The closure to call when a work was created.
pub fn set_saved_cb<F: Fn(WorkDescription) -> () + 'static>(&self, cb: F) {
self.saved_cb.replace(Some(Box::new(cb)));
}
/// Update the UI according to person.
fn show_composer(&self, person: &Person) {
self.composer_label.set_text(&person.name_fl());
self.save_button.set_sensitive(true);
}
}

View file

@ -1,63 +0,0 @@
use super::work_editor::*;
use crate::backend::*;
use crate::database::*;
use glib::clone;
use gtk::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A dialog for creating or editing a work.
pub struct WorkEditorDialog {
pub window: libhandy::Window,
saved_cb: RefCell<Option<Box<dyn Fn(WorkDescription) -> ()>>>,
}
impl WorkEditorDialog {
/// Create a new work editor dialog and optionally initialize it.
pub fn new<W: IsA<gtk::Window>>(
backend: Rc<Backend>,
parent: &W,
work: Option<WorkDescription>,
) -> Rc<Self> {
// Create UI
let window = libhandy::Window::new();
window.set_type_hint(gdk::WindowTypeHint::Dialog);
window.set_modal(true);
window.set_transient_for(Some(parent));
let editor = WorkEditor::new(backend.clone(), &window, work);
window.add(&editor.widget);
window.show_all();
let this = Rc::new(Self {
window,
saved_cb: RefCell::new(None),
});
// Connect signals and callbacks
editor.set_cancel_cb(clone!(@strong this => move || {
this.window.close();
}));
editor.set_saved_cb(clone!(@strong this => move |work| {
if let Some(cb) = &*this.saved_cb.borrow() {
cb(work);
this.window.close();
}
}));
this
}
/// Set the closure to be called when the user edited or created a work.
pub fn set_saved_cb<F: Fn(WorkDescription) -> () + 'static>(&self, cb: F) {
self.saved_cb.replace(Some(Box::new(cb)));
}
/// Show the work editor dialog.
pub fn show(&self) {
self.window.show();
}
}

View file

@ -1,89 +0,0 @@
use super::work_selector_person_screen::*;
use crate::backend::Backend;
use crate::database::*;
use crate::widgets::*;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
/// A widget for selecting a work from a list of existing ones.
pub struct WorkSelector {
pub widget: libhandy::Leaflet,
backend: Rc<Backend>,
sidebar_box: gtk::Box,
selected_cb: RefCell<Option<Box<dyn Fn(WorkDescription) -> ()>>>,
add_cb: RefCell<Option<Box<dyn Fn() -> ()>>>,
navigator: Rc<Navigator>,
}
impl WorkSelector {
/// Create a new work selector.
pub fn new(backend: Rc<Backend>) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_selector.ui");
get_widget!(builder, libhandy::Leaflet, widget);
get_widget!(builder, gtk::Button, add_button);
get_widget!(builder, gtk::Box, sidebar_box);
get_widget!(builder, gtk::Box, empty_screen);
let person_list = PersonList::new(backend.clone());
sidebar_box.pack_start(&person_list.widget, true, true, 0);
let navigator = Navigator::new(&empty_screen);
widget.add(&navigator.widget);
let this = Rc::new(Self {
widget,
backend,
sidebar_box,
selected_cb: RefCell::new(None),
add_cb: RefCell::new(None),
navigator,
});
// Connect signals and callbacks
add_button.connect_clicked(clone!(@strong this => move |_| {
if let Some(cb) = &*this.add_cb.borrow() {
cb();
}
}));
person_list.set_selected(clone!(@strong this => move |person| {
let person_screen = WorkSelectorPersonScreen::new(
this.backend.clone(),
person.clone(),
);
person_screen.set_selected_cb(clone!(@strong this => move |work| {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(work);
}
}));
this.navigator.clone().push(person_screen);
this.widget.set_visible_child(&this.navigator.widget);
}));
this.navigator.set_back_cb(clone!(@strong this => move || {
this.widget.set_visible_child(&this.sidebar_box);
}));
this
}
/// Set the closure to be called if the user wants to add a new work.
pub fn set_add_cb<F: Fn() -> () + 'static>(&self, cb: F) {
self.add_cb.replace(Some(Box::new(cb)));
}
/// Set the closure to be called when the user has selected a work.
pub fn set_selected_cb<F: Fn(WorkDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
}

View file

@ -1,114 +0,0 @@
use crate::backend::*;
use crate::database::*;
use crate::widgets::*;
use gettextrs::gettext;
use glib::clone;
use gtk::prelude::*;
use gtk_macros::get_widget;
use libhandy::HeaderBarExt;
use std::cell::RefCell;
use std::rc::Rc;
/// A screen within the work selector that presents a list of works by a person.
pub struct WorkSelectorPersonScreen {
backend: Rc<Backend>,
widget: gtk::Box,
stack: gtk::Stack,
work_list: Rc<List<WorkDescription>>,
selected_cb: RefCell<Option<Box<dyn Fn(WorkDescription) -> ()>>>,
navigator: RefCell<Option<Rc<Navigator>>>,
}
impl WorkSelectorPersonScreen {
/// Create a new work selector person screen.
pub fn new(backend: Rc<Backend>, person: Person) -> Rc<Self> {
// Create UI
let builder = gtk::Builder::from_resource("/de/johrpan/musicus/ui/work_selector_screen.ui");
get_widget!(builder, gtk::Box, widget);
get_widget!(builder, libhandy::HeaderBar, header);
get_widget!(builder, gtk::Button, back_button);
get_widget!(builder, gtk::Stack, stack);
header.set_title(Some(&person.name_fl()));
let work_list = List::new(&gettext("No works found."));
stack.add_named(&work_list.widget, "content");
let this = Rc::new(Self {
backend,
widget,
stack,
work_list,
selected_cb: RefCell::new(None),
navigator: RefCell::new(None),
});
// Connect signals and callbacks
back_button.connect_clicked(clone!(@strong this => move |_| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
navigator.pop();
}
}));
this.work_list.set_make_widget(|work: &WorkDescription| {
let label = gtk::Label::new(Some(&work.title));
label.set_ellipsize(pango::EllipsizeMode::End);
label.set_halign(gtk::Align::Start);
label.set_margin_start(6);
label.set_margin_end(6);
label.set_margin_top(6);
label.set_margin_bottom(6);
label.upcast()
});
this.work_list
.set_selected(clone!(@strong this => move |work| {
let navigator = this.navigator.borrow().clone();
if let Some(navigator) = navigator {
if let Some(cb) = &*this.selected_cb.borrow() {
cb(work.clone());
}
}
}));
// Initialize
let context = glib::MainContext::default();
let clone = this.clone();
context.spawn_local(async move {
let works = clone
.backend
.get_work_descriptions(person.id)
.await
.unwrap();
clone.work_list.show_items(works);
clone.stack.set_visible_child_name("content");
});
this
}
/// Sets a closure to be called when the user has selected a work.
pub fn set_selected_cb<F: Fn(WorkDescription) -> () + 'static>(&self, cb: F) {
self.selected_cb.replace(Some(Box::new(cb)));
}
}
impl NavigatorScreen for WorkSelectorPersonScreen {
fn attach_navigator(&self, navigator: Rc<Navigator>) {
self.navigator.replace(Some(navigator));
}
fn get_widget(&self) -> gtk::Widget {
self.widget.clone().upcast()
}
fn detach_navigator(&self) {
self.navigator.replace(None);
}
}