musicus/src/home_page.rs

162 lines
4.9 KiB
Rust
Raw Normal View History

2023-09-30 18:26:11 +02:00
use crate::{
2023-10-07 22:49:20 +02:00
library::{LibraryQuery, MusicusLibrary},
player::MusicusPlayer,
search_entry::MusicusSearchEntry,
2023-09-30 18:26:11 +02:00
tile::MusicusTile,
};
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::*,
};
use std::cell::OnceCell;
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-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]
pub stack: TemplateChild<gtk::Stack>,
#[template_child]
2023-09-20 13:49:02 +02:00
pub persons_flow_box: TemplateChild<gtk::FlowBox>,
#[template_child]
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, _: &gtk::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) {
search_entry.add_tag("Tag");
2023-09-21 17:19:31 +02:00
}
2023-10-07 22:49:20 +02:00
fn query(&self, query: &LibraryQuery) {
let imp = self.imp();
2023-10-07 22:49:20 +02:00
let results = self.library().query(query);
for flowbox in [
&imp.persons_flow_box,
&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
}
if results.is_empty() {
imp.stack.set_visible_child_name("empty");
} else {
imp.stack.set_visible_child_name("results");
imp.persons_flow_box
.set_visible(!results.persons.is_empty());
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());
for person in results.persons {
imp.persons_flow_box
.append(&MusicusTile::with_title(&person.name_fl()));
}
for ensemble in results.ensembles {
imp.ensembles_flow_box
.append(&MusicusTile::with_title(&ensemble.name));
}
for work in results.works {
imp.works_flow_box
.append(&MusicusTile::with_subtitle(&work.title, &work.composer.name_fl()));
}
for _recording in results.recordings {
imp.recordings_flow_box
.append(&MusicusTile::with_title("TODO"));
}
}
2023-10-07 22:49:20 +02:00
}
2023-09-20 13:49:02 +02:00
}