2023-09-24 11:57:16 +02:00
|
|
|
use crate::{
|
2023-09-30 18:26:11 +02:00
|
|
|
home_page::MusicusHomePage, library::MusicusLibrary, player::MusicusPlayer,
|
|
|
|
|
playlist_page::MusicusPlaylistPage, welcome_page::MusicusWelcomePage,
|
2023-09-24 11:57:16 +02:00
|
|
|
};
|
2023-09-13 14:58:31 +02:00
|
|
|
|
2023-06-18 14:02:21 +02:00
|
|
|
use adw::subclass::prelude::*;
|
2023-09-29 21:18:28 +02:00
|
|
|
use gtk::{gio, glib, glib::clone, prelude::*};
|
2023-06-18 14:02:21 +02:00
|
|
|
|
|
|
|
|
mod imp {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Default, gtk::CompositeTemplate)]
|
2023-09-24 13:58:05 +02:00
|
|
|
#[template(file = "data/ui/window.blp")]
|
2023-06-18 14:02:21 +02:00
|
|
|
pub struct MusicusWindow {
|
2023-09-29 21:18:28 +02:00
|
|
|
pub player: MusicusPlayer,
|
|
|
|
|
|
2023-09-24 11:57:16 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub stack: TemplateChild<gtk::Stack>,
|
2023-06-18 14:02:21 +02:00
|
|
|
#[template_child]
|
2023-09-13 14:58:31 +02:00
|
|
|
pub navigation_view: TemplateChild<adw::NavigationView>,
|
2023-09-21 17:19:31 +02:00
|
|
|
#[template_child]
|
|
|
|
|
pub player_bar_revealer: TemplateChild<gtk::Revealer>,
|
2023-09-24 11:57:16 +02:00
|
|
|
#[template_child]
|
2023-09-29 21:18:28 +02:00
|
|
|
pub play_button: TemplateChild<gtk::Button>,
|
|
|
|
|
#[template_child]
|
2023-09-24 11:57:16 +02:00
|
|
|
pub playlist_button: TemplateChild<gtk::ToggleButton>,
|
2023-06-18 14:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
|
|
|
|
impl ObjectSubclass for MusicusWindow {
|
|
|
|
|
const NAME: &'static str = "MusicusWindow";
|
|
|
|
|
type Type = super::MusicusWindow;
|
|
|
|
|
type ParentType = adw::ApplicationWindow;
|
|
|
|
|
|
|
|
|
|
fn class_init(klass: &mut Self::Class) {
|
2023-09-20 13:49:02 +02:00
|
|
|
MusicusHomePage::static_type();
|
2023-09-24 11:57:16 +02:00
|
|
|
MusicusPlaylistPage::static_type();
|
2023-09-15 10:12:36 +02:00
|
|
|
MusicusWelcomePage::static_type();
|
2023-06-18 14:02:21 +02:00
|
|
|
klass.bind_template();
|
2023-09-15 10:12:36 +02:00
|
|
|
klass.bind_template_instance_callbacks();
|
2023-06-18 14:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
|
|
|
|
|
obj.init_template();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 14:58:31 +02:00
|
|
|
impl ObjectImpl for MusicusWindow {
|
|
|
|
|
fn constructed(&self) {
|
|
|
|
|
self.parent_constructed();
|
2023-09-21 17:49:25 +02:00
|
|
|
self.obj().load_window_state();
|
2023-09-29 21:18:28 +02:00
|
|
|
self.player
|
|
|
|
|
.bind_property("active", &self.player_bar_revealer.get(), "reveal-child")
|
|
|
|
|
.sync_create()
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
let play_button = self.play_button.get();
|
|
|
|
|
|
|
|
|
|
self.player
|
|
|
|
|
.connect_playing_notify(clone!(@weak play_button => move |player| {
|
|
|
|
|
play_button.set_icon_name(if player.playing() {
|
|
|
|
|
"media-playback-pause-symbolic"
|
|
|
|
|
} else {
|
|
|
|
|
"media-playback-start-symbolic"
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
self.play_button
|
|
|
|
|
.connect_clicked(clone!(@weak self.player as player => move |_| {
|
|
|
|
|
if player.playing() {
|
|
|
|
|
player.pause();
|
|
|
|
|
} else {
|
|
|
|
|
player.play();
|
|
|
|
|
}
|
|
|
|
|
}));
|
2023-09-13 14:58:31 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 14:02:21 +02:00
|
|
|
impl WidgetImpl for MusicusWindow {}
|
2023-09-21 17:49:25 +02:00
|
|
|
|
|
|
|
|
impl WindowImpl for MusicusWindow {
|
2023-09-24 14:19:07 +02:00
|
|
|
fn close_request(&self) -> glib::signal::Propagation {
|
2023-09-21 17:49:25 +02:00
|
|
|
if let Err(err) = self.obj().save_window_state() {
|
|
|
|
|
log::warn!("Failed to save window state: {err}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 14:19:07 +02:00
|
|
|
glib::signal::Propagation::Proceed
|
2023-09-21 17:49:25 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-18 14:02:21 +02:00
|
|
|
impl ApplicationWindowImpl for MusicusWindow {}
|
|
|
|
|
impl AdwApplicationWindowImpl for MusicusWindow {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
|
pub struct MusicusWindow(ObjectSubclass<imp::MusicusWindow>)
|
2023-09-13 14:58:31 +02:00
|
|
|
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, adw::ApplicationWindow,
|
|
|
|
|
@implements gio::ActionGroup, gio::ActionMap;
|
2023-06-18 14:02:21 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 10:12:36 +02:00
|
|
|
#[gtk::template_callbacks]
|
2023-06-18 14:02:21 +02:00
|
|
|
impl MusicusWindow {
|
|
|
|
|
pub fn new<P: glib::IsA<gtk::Application>>(application: &P) -> Self {
|
|
|
|
|
glib::Object::builder()
|
|
|
|
|
.property("application", application)
|
|
|
|
|
.build()
|
|
|
|
|
}
|
2023-09-15 10:12:36 +02:00
|
|
|
|
2023-09-21 17:49:25 +02:00
|
|
|
pub fn load_window_state(&self) {
|
|
|
|
|
let settings = gio::Settings::new("de.johrpan.musicus");
|
|
|
|
|
self.set_default_size(settings.int("window-width"), settings.int("window-height"));
|
|
|
|
|
self.set_property("maximized", settings.boolean("is-maximized"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save_window_state(&self) -> Result<(), glib::BoolError> {
|
|
|
|
|
let settings = gio::Settings::new("de.johrpan.musicus");
|
|
|
|
|
|
|
|
|
|
let size = self.default_size();
|
|
|
|
|
settings.set_int("window-width", size.0)?;
|
|
|
|
|
settings.set_int("window-height", size.1)?;
|
|
|
|
|
settings.set_boolean("is-maximized", self.is_maximized())?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 10:12:36 +02:00
|
|
|
#[template_callback]
|
2023-09-21 17:49:25 +02:00
|
|
|
fn set_library_folder(&self, folder: &gio::File) {
|
2023-09-30 18:26:11 +02:00
|
|
|
let path = folder.path().unwrap();
|
|
|
|
|
let library = MusicusLibrary::new(path);
|
2023-09-29 21:18:28 +02:00
|
|
|
self.imp()
|
|
|
|
|
.navigation_view
|
2023-09-30 18:26:11 +02:00
|
|
|
.replace(&[MusicusHomePage::new(&library, &self.imp().player).into()]);
|
2023-09-15 10:12:36 +02:00
|
|
|
}
|
2023-09-24 11:57:16 +02:00
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
fn show_playlist(&self, button: >k::ToggleButton) {
|
|
|
|
|
self.imp()
|
|
|
|
|
.stack
|
|
|
|
|
.set_visible_child_name(if button.is_active() {
|
|
|
|
|
"playlist"
|
|
|
|
|
} else {
|
|
|
|
|
"navigation"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[template_callback]
|
|
|
|
|
fn hide_playlist(&self, _: &MusicusPlaylistPage) {
|
|
|
|
|
self.imp().playlist_button.set_active(false);
|
|
|
|
|
}
|
2023-06-18 14:02:21 +02:00
|
|
|
}
|