2023-09-30 18:26:11 +02:00
|
|
|
use crate::{
|
2023-10-25 17:45:32 +02:00
|
|
|
library::{Ensemble, LibraryQuery, MusicusLibrary, Person, Recording, Track, Work},
|
2023-10-07 22:49:20 +02:00
|
|
|
player::MusicusPlayer,
|
2023-10-25 17:45:32 +02:00
|
|
|
playlist_item::PlaylistItem,
|
2023-10-08 16:40:59 +02:00
|
|
|
recording_tile::MusicusRecordingTile,
|
2023-10-07 22:49:20 +02:00
|
|
|
search_entry::MusicusSearchEntry,
|
2023-10-08 00:16:41 +02:00
|
|
|
search_tag::Tag,
|
2023-10-08 16:40:59 +02:00
|
|
|
tag_tile::MusicusTagTile,
|
2023-09-30 18:26:11 +02:00
|
|
|
};
|
2023-09-20 13:49:02 +02:00
|
|
|
use adw::subclass::{navigation_page::NavigationPageImpl, prelude::*};
|
2023-10-07 22:49:20 +02:00
|
|
|
use gtk::{
|
|
|
|
|
glib::{self, clone, Properties},
|
|
|
|
|
prelude::*,
|
|
|
|
|
};
|
2023-10-08 00:16:41 +02:00
|
|
|
use std::cell::{OnceCell, RefCell};
|
2023-09-20 13:49:02 +02:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2023-09-29 21:18:28 +02:00
|
|
|
#[derive(Properties, Debug, Default, gtk::CompositeTemplate)]
|
|
|
|
|
#[properties(wrapper_type = super::MusicusHomePage)]
|
2023-09-24 13:58:05 +02:00
|
|
|
#[template(file = "data/ui/home_page.blp")]
|
2023-09-20 13:49:02 +02:00
|
|
|
pub struct MusicusHomePage {
|
2023-10-07 22:49:20 +02:00
|
|
|
#[property(get, construct_only)]
|
2023-09-30 18:26:11 +02:00
|
|
|
pub library: OnceCell<MusicusLibrary>,
|
|
|
|
|
|
2023-10-07 22:49:20 +02:00
|
|
|
#[property(get, construct_only)]
|
|
|
|
|
pub player: OnceCell<MusicusPlayer>,
|
|
|
|
|
|
2023-10-08 15:11:47 +02:00
|
|
|
pub composers: RefCell<Vec<Person>>,
|
|
|
|
|
pub performers: RefCell<Vec<Person>>,
|
2023-10-08 00:16:41 +02:00
|
|
|
pub ensembles: RefCell<Vec<Ensemble>>,
|
|
|
|
|
pub works: RefCell<Vec<Work>>,
|
|
|
|
|
pub recordings: RefCell<Vec<Recording>>,
|
|
|
|
|
|
2023-09-20 13:49:02 +02:00
|
|
|
#[template_child]
|
2023-09-30 00:22:33 +02:00
|
|
|
pub search_entry: TemplateChild<MusicusSearchEntry>,
|
2023-09-20 13:49:02 +02:00
|
|
|
#[template_child]
|
2023-10-07 23:13:32 +02:00
|
|
|
pub stack: TemplateChild<gtk::Stack>,
|
|
|
|
|
#[template_child]
|
2023-10-08 15:11:47 +02:00
|
|
|
pub composers_flow_box: TemplateChild<gtk::FlowBox>,
|
|
|
|
|
#[template_child]
|
|
|
|
|
pub performers_flow_box: TemplateChild<gtk::FlowBox>,
|
2023-09-20 13:49:02 +02:00
|
|
|
#[template_child]
|
2023-10-07 23:13:32 +02:00
|
|
|
pub ensembles_flow_box: TemplateChild<gtk::FlowBox>,
|
|
|
|
|
#[template_child]
|
2023-09-20 13:49:02 +02:00
|
|
|
pub works_flow_box: TemplateChild<gtk::FlowBox>,
|
|
|
|
|
#[template_child]
|
|
|
|
|
pub recordings_flow_box: TemplateChild<gtk::FlowBox>,
|
2023-09-21 17:19:31 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub play_button: TemplateChild<gtk::Button>,
|
2023-09-20 13:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
|
impl ObjectSubclass for MusicusHomePage {
|
|
|
|
|
const NAME: &'static str = "MusicusHomePage";
|
|
|
|
|
type Type = super::MusicusHomePage;
|
|
|
|
|
type ParentType = adw::NavigationPage;
|
|
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
|
|
|
|
klass.bind_template();
|
2023-09-21 17:19:31 +02:00
|
|
|
klass.bind_template_instance_callbacks();
|
2023-09-20 13:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
|
|
|
|
obj.init_template();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-29 21:18:28 +02:00
|
|
|
#[glib::derived_properties]
|
2023-09-20 13:49:02 +02:00
|
|
|
impl ObjectImpl for MusicusHomePage {
|
|
|
|
|
fn constructed(&self) {
|
|
|
|
|
self.parent_constructed();
|
2023-09-30 00:22:33 +02:00
|
|
|
|
|
|
|
|
self.search_entry.set_key_capture_widget(&*self.obj());
|
2023-09-20 13:49:02 +02:00
|
|
|
|
2023-10-07 22:49:20 +02:00
|
|
|
self.search_entry
|
|
|
|
|
.connect_query_changed(clone!(@weak self as _self => move |entry| {
|
|
|
|
|
_self.obj().query(&entry.query());
|
|
|
|
|
}));
|
2023-09-30 19:10:32 +02:00
|
|
|
|
2023-09-29 21:18:28 +02:00
|
|
|
self.player
|
2023-10-07 22:49:20 +02:00
|
|
|
.get()
|
|
|
|
|
.unwrap()
|
2023-09-29 21:18:28 +02:00
|
|
|
.bind_property("active", &self.play_button.get(), "visible")
|
|
|
|
|
.invert_boolean()
|
|
|
|
|
.sync_create()
|
|
|
|
|
.build();
|
|
|
|
|
|
2023-10-07 22:49:20 +02:00
|
|
|
self.obj().query(&LibraryQuery::default());
|
2023-09-20 13:49:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WidgetImpl for MusicusHomePage {}
|
|
|
|
|
impl NavigationPageImpl for MusicusHomePage {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
|
pub struct MusicusHomePage(ObjectSubclass<imp::MusicusHomePage>)
|
|
|
|
|
@extends gtk::Widget, adw::NavigationPage;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 17:19:31 +02:00
|
|
|
#[gtk::template_callbacks]
|
2023-09-20 13:49:02 +02:00
|
|
|
impl MusicusHomePage {
|
2023-09-30 18:26:11 +02:00
|
|
|
pub fn new(library: &MusicusLibrary, player: &MusicusPlayer) -> Self {
|
2023-10-07 22:49:20 +02:00
|
|
|
glib::Object::builder()
|
|
|
|
|
.property("library", library)
|
|
|
|
|
.property("player", player)
|
|
|
|
|
.build()
|
2023-09-20 13:49:02 +02:00
|
|
|
}
|
2023-09-21 17:19:31 +02:00
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
fn play(&self, _: >k::Button) {
|
|
|
|
|
log::info!("Play button clicked");
|
2023-10-07 22:49:20 +02:00
|
|
|
self.player().play();
|
2023-09-21 17:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
2023-09-30 00:22:33 +02:00
|
|
|
fn select(&self, search_entry: &MusicusSearchEntry) {
|
2023-10-08 00:16:41 +02:00
|
|
|
let imp = self.imp();
|
|
|
|
|
|
2023-10-11 12:04:49 +02:00
|
|
|
let (composer, performer, ensemble, work, recording) = {
|
2023-10-08 00:30:30 +02:00
|
|
|
(
|
2023-10-08 15:11:47 +02:00
|
|
|
imp.composers.borrow().first().cloned(),
|
|
|
|
|
imp.performers.borrow().first().cloned(),
|
2023-10-08 00:30:30 +02:00
|
|
|
imp.ensembles.borrow().first().cloned(),
|
|
|
|
|
imp.works.borrow().first().cloned(),
|
2023-10-11 12:04:49 +02:00
|
|
|
imp.recordings.borrow().first().cloned(),
|
2023-10-08 00:30:30 +02:00
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-08 15:11:47 +02:00
|
|
|
if let Some(person) = composer {
|
|
|
|
|
search_entry.add_tag(Tag::Composer(person));
|
|
|
|
|
} else if let Some(person) = performer {
|
|
|
|
|
search_entry.add_tag(Tag::Performer(person));
|
2023-10-08 00:30:30 +02:00
|
|
|
} else if let Some(ensemble) = ensemble {
|
|
|
|
|
search_entry.add_tag(Tag::Ensemble(ensemble));
|
|
|
|
|
} else if let Some(work) = work {
|
|
|
|
|
search_entry.add_tag(Tag::Work(work));
|
2023-10-11 12:04:49 +02:00
|
|
|
} else if let Some(recording) = recording {
|
|
|
|
|
self.play_recording(&recording);
|
2023-10-08 00:16:41 +02:00
|
|
|
}
|
2023-09-21 17:19:31 +02:00
|
|
|
}
|
2023-10-07 22:49:20 +02:00
|
|
|
|
2023-10-11 12:04:49 +02:00
|
|
|
#[template_callback]
|
|
|
|
|
fn tile_selected(&self, tile: >k::FlowBoxChild, _: >k::FlowBox) {
|
|
|
|
|
self.imp()
|
|
|
|
|
.search_entry
|
|
|
|
|
.add_tag(tile.downcast_ref::<MusicusTagTile>().unwrap().tag().clone())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
fn recording_selected(&self, tile: >k::FlowBoxChild, _: >k::FlowBox) {
|
|
|
|
|
self.play_recording(
|
|
|
|
|
tile.downcast_ref::<MusicusRecordingTile>()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.recording(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn play_recording(&self, recording: &Recording) {
|
2023-10-25 17:45:32 +02:00
|
|
|
let tracks = self.library().tracks(recording);
|
|
|
|
|
|
|
|
|
|
if tracks.is_empty() {
|
|
|
|
|
log::warn!("Ignoring recording without tracks being added to the playlist.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let title = format!(
|
|
|
|
|
"{}: {}",
|
|
|
|
|
recording.work.composer.name_fl(),
|
|
|
|
|
recording.work.title
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let performances = self.library().performances(recording);
|
|
|
|
|
let performances = if performances.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(performances.join(", "))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut items = Vec::new();
|
|
|
|
|
|
|
|
|
|
if tracks.len() == 1 {
|
|
|
|
|
items.push(PlaylistItem::new(
|
2023-10-27 14:15:05 +02:00
|
|
|
true,
|
2023-10-25 17:45:32 +02:00
|
|
|
&title,
|
|
|
|
|
performances.as_ref().map(|x| x.as_str()),
|
|
|
|
|
None,
|
|
|
|
|
&tracks[0].path,
|
2023-10-27 14:15:05 +02:00
|
|
|
));
|
2023-10-25 17:45:32 +02:00
|
|
|
} else {
|
|
|
|
|
let work_parts = self.library().work_parts(&recording.work);
|
|
|
|
|
let mut tracks = tracks.into_iter();
|
|
|
|
|
let first_track = tracks.next().unwrap();
|
|
|
|
|
|
|
|
|
|
let track_title = |track: &Track| -> String {
|
|
|
|
|
track
|
|
|
|
|
.work_parts
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|w| work_parts[*w].clone())
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
.join(", ")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
items.push(PlaylistItem::new(
|
2023-10-27 14:15:05 +02:00
|
|
|
true,
|
2023-10-25 17:45:32 +02:00
|
|
|
&title,
|
|
|
|
|
performances.as_ref().map(|x| x.as_str()),
|
|
|
|
|
Some(&track_title(&first_track)),
|
|
|
|
|
&first_track.path,
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
while let Some(track) = tracks.next() {
|
2023-10-27 14:15:05 +02:00
|
|
|
items.push(PlaylistItem::new(
|
|
|
|
|
false,
|
|
|
|
|
&title,
|
|
|
|
|
performances.as_ref().map(|x| x.as_str()),
|
|
|
|
|
Some(&track_title(&track)),
|
|
|
|
|
&track.path,
|
|
|
|
|
));
|
2023-10-25 17:45:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.player().append(items);
|
2023-10-11 12:04:49 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-07 22:49:20 +02:00
|
|
|
fn query(&self, query: &LibraryQuery) {
|
2023-10-07 23:13:32 +02:00
|
|
|
let imp = self.imp();
|
2023-10-07 22:49:20 +02:00
|
|
|
let results = self.library().query(query);
|
|
|
|
|
|
2023-10-07 23:13:32 +02:00
|
|
|
for flowbox in [
|
2023-10-08 15:11:47 +02:00
|
|
|
&imp.composers_flow_box,
|
|
|
|
|
&imp.performers_flow_box,
|
2023-10-07 23:13:32 +02:00
|
|
|
&imp.ensembles_flow_box,
|
|
|
|
|
&imp.works_flow_box,
|
|
|
|
|
&imp.recordings_flow_box,
|
|
|
|
|
] {
|
|
|
|
|
while let Some(widget) = flowbox.first_child() {
|
|
|
|
|
flowbox.remove(&widget);
|
|
|
|
|
}
|
2023-10-07 22:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-07 23:13:32 +02:00
|
|
|
if results.is_empty() {
|
|
|
|
|
imp.stack.set_visible_child_name("empty");
|
|
|
|
|
} else {
|
|
|
|
|
imp.stack.set_visible_child_name("results");
|
|
|
|
|
|
2023-10-08 15:11:47 +02:00
|
|
|
imp.composers_flow_box
|
|
|
|
|
.set_visible(!results.composers.is_empty());
|
|
|
|
|
imp.performers_flow_box
|
|
|
|
|
.set_visible(!results.performers.is_empty());
|
2023-10-07 23:13:32 +02:00
|
|
|
imp.ensembles_flow_box
|
|
|
|
|
.set_visible(!results.ensembles.is_empty());
|
|
|
|
|
imp.works_flow_box.set_visible(!results.works.is_empty());
|
|
|
|
|
imp.recordings_flow_box
|
|
|
|
|
.set_visible(!results.recordings.is_empty());
|
|
|
|
|
|
2023-10-08 15:11:47 +02:00
|
|
|
for composer in &results.composers {
|
|
|
|
|
imp.composers_flow_box
|
2023-10-08 16:40:59 +02:00
|
|
|
.append(&MusicusTagTile::new(Tag::Composer(composer.clone())));
|
2023-10-08 15:11:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for performer in &results.performers {
|
|
|
|
|
imp.performers_flow_box
|
2023-10-08 16:40:59 +02:00
|
|
|
.append(&MusicusTagTile::new(Tag::Performer(performer.clone())));
|
2023-10-07 23:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 00:16:41 +02:00
|
|
|
for ensemble in &results.ensembles {
|
2023-10-07 23:13:32 +02:00
|
|
|
imp.ensembles_flow_box
|
2023-10-08 16:40:59 +02:00
|
|
|
.append(&MusicusTagTile::new(Tag::Ensemble(ensemble.clone())));
|
2023-10-07 23:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 00:16:41 +02:00
|
|
|
for work in &results.works {
|
2023-10-08 16:40:59 +02:00
|
|
|
imp.works_flow_box
|
|
|
|
|
.append(&MusicusTagTile::new(Tag::Work(work.clone())));
|
2023-10-07 23:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
2023-10-08 00:16:41 +02:00
|
|
|
for recording in &results.recordings {
|
2023-10-08 16:40:59 +02:00
|
|
|
let performances = self.library().performances(recording);
|
|
|
|
|
imp.recordings_flow_box
|
|
|
|
|
.append(&MusicusRecordingTile::new(recording, performances));
|
2023-10-07 23:13:32 +02:00
|
|
|
}
|
2023-10-08 00:16:41 +02:00
|
|
|
|
2023-10-08 15:11:47 +02:00
|
|
|
imp.composers.replace(results.composers);
|
|
|
|
|
imp.performers.replace(results.performers);
|
2023-10-08 00:16:41 +02:00
|
|
|
imp.ensembles.replace(results.ensembles);
|
|
|
|
|
imp.works.replace(results.works);
|
|
|
|
|
imp.recordings.replace(results.recordings);
|
2023-10-07 23:13:32 +02:00
|
|
|
}
|
2023-10-07 22:49:20 +02:00
|
|
|
}
|
2023-09-20 13:49:02 +02:00
|
|
|
}
|