use gtk4::prelude::*;
use gtk4::{FileDialog, Window, Button};
use std::path::PathBuf;
use crate::gettext;
pub type FileProcessorFunc = fn(PathBuf);
fn handle_file_response(
res: Result<gio::File, glib::Error>,
process_func: FileProcessorFunc
) {
match res {
Ok(file) => {
if let Some(path) = file.path() {
process_func(path);
}
}
Err(err) => {
eprintln!("Error opening the file: {}", err);
}
}
}
pub fn show_file_chooser(button: &Button, process_func: FileProcessorFunc) {
let root = button.root();
let parent_window = root.and_then(|r| r.downcast::<Window>().ok());
let dialog = FileDialog::new();
dialog.set_title(&gettext!("Choose File"));
dialog.open(parent_window.as_ref(), gio::Cancellable::NONE, move |res| {
handle_file_response(res, process_func);
});
}