musicus/src/home_page.rs

103 lines
3.1 KiB
Rust
Raw Normal View History

2023-09-30 18:26:11 +02:00
use crate::{
library::MusicusLibrary, player::MusicusPlayer, search_entry::MusicusSearchEntry,
tile::MusicusTile,
};
2023-09-20 13:49:02 +02:00
use adw::subclass::{navigation_page::NavigationPageImpl, prelude::*};
2023-09-29 21:18:28 +02:00
use gtk::{glib, glib::Properties, prelude::*};
2023-09-30 18:26:11 +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-09-29 21:18:28 +02:00
#[property(get, set)]
pub player: RefCell<MusicusPlayer>,
2023-09-30 18:26:11 +02:00
pub library: OnceCell<MusicusLibrary>,
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 persons_flow_box: TemplateChild<gtk::FlowBox>,
#[template_child]
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-09-30 19:10:32 +02:00
self.search_entry.connect_query_changed(|entry| {
log::info!("{}", entry.query());
});
2023-09-29 21:18:28 +02:00
self.player
.borrow()
.bind_property("active", &self.play_button.get(), "visible")
.invert_boolean()
.sync_create()
.build();
2023-09-20 13:49:02 +02:00
for _ in 0..9 {
self.persons_flow_box.append(&MusicusTile::new());
self.works_flow_box.append(&MusicusTile::new());
self.recordings_flow_box.append(&MusicusTile::new());
}
}
}
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 {
let obj: MusicusHomePage = glib::Object::builder().property("player", player).build();
obj.imp().library.set(library.to_owned()).unwrap();
obj
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-09-29 21:18:28 +02:00
self.imp().player.borrow().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-09-20 13:49:02 +02:00
}