Show file dialog for library selection

This commit is contained in:
Elias Projahn 2023-09-15 10:12:36 +02:00
parent 3df4e8e3f4
commit 8c56e90994
7 changed files with 160 additions and 17 deletions

View file

@ -10,6 +10,8 @@ use gettextrs::{bind_textdomain_codeset, bindtextdomain, textdomain};
use gtk::{gio, glib, prelude::*};
fn main() -> glib::ExitCode {
tracing_subscriber::fmt::init();
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8")
.expect("Unable to set the text domain encoding");

View file

@ -1,15 +1,14 @@
use adw::subclass::{navigation_page::NavigationPageImpl, prelude::*};
use gtk::glib;
use gettextrs::gettext;
use gtk::{gio, glib, glib::subclass::Signal, prelude::*};
use once_cell::sync::Lazy;
mod imp {
use super::*;
#[derive(Debug, Default, gtk::CompositeTemplate)]
#[template(resource = "/de/johrpan/musicus/welcome_page.ui")]
pub struct MusicusWelcomePage {
#[template_child]
pub choose_library_button: TemplateChild<gtk::Button>,
}
pub struct MusicusWelcomePage {}
#[glib::object_subclass]
impl ObjectSubclass for MusicusWelcomePage {
@ -19,6 +18,7 @@ mod imp {
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.bind_template_instance_callbacks();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@ -26,7 +26,18 @@ mod imp {
}
}
impl ObjectImpl for MusicusWelcomePage {}
impl ObjectImpl for MusicusWelcomePage {
fn signals() -> &'static [Signal] {
static SIGNALS: Lazy<Vec<Signal>> = Lazy::new(|| {
vec![Signal::builder("folder-selected")
.param_types([gio::File::static_type()])
.build()]
});
SIGNALS.as_ref()
}
}
impl WidgetImpl for MusicusWelcomePage {}
impl NavigationPageImpl for MusicusWelcomePage {}
}
@ -36,8 +47,35 @@ glib::wrapper! {
@extends gtk::Widget, adw::NavigationPage;
}
#[gtk::template_callbacks]
impl MusicusWelcomePage {
pub fn new() -> Self {
glib::Object::new()
}
#[template_callback]
async fn choose_library_folder(&self, _: &gtk::Button) {
let dialog = gtk::FileDialog::builder()
.title(gettext("Select music library folder"))
.modal(true)
.build();
match dialog
.select_folder_future(
self.root()
.as_ref()
.and_then(|r| r.downcast_ref::<gtk::Window>()),
)
.await
{
Err(err) => {
if !err.matches(gtk::DialogError::Dismissed) {
log::error!("Folder selection failed: {err}");
}
}
Ok(folder) => {
self.emit_by_name::<()>("folder-selected", &[&folder]);
}
}
}
}

View file

@ -1,7 +1,7 @@
use crate::welcome_page::MusicusWelcomePage;
use adw::subclass::prelude::*;
use gtk::{gio, glib};
use gtk::{gio, glib, prelude::*};
mod imp {
use super::*;
@ -20,7 +20,9 @@ mod imp {
type ParentType = adw::ApplicationWindow;
fn class_init(klass: &mut Self::Class) {
MusicusWelcomePage::static_type();
klass.bind_template();
klass.bind_template_instance_callbacks();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
@ -31,8 +33,6 @@ mod imp {
impl ObjectImpl for MusicusWindow {
fn constructed(&self) {
self.parent_constructed();
self.navigation_view.add(&MusicusWelcomePage::new());
}
}
@ -48,10 +48,17 @@ glib::wrapper! {
@implements gio::ActionGroup, gio::ActionMap;
}
#[gtk::template_callbacks]
impl MusicusWindow {
pub fn new<P: glib::IsA<gtk::Application>>(application: &P) -> Self {
glib::Object::builder()
.property("application", application)
.build()
}
#[template_callback]
async fn set_library_folder(&self, folder: &gio::File) {
let path = folder.path();
log::info!("{path:?}");
}
}