xmpd/xmpd-gui/src/components/top_nav.rs

88 lines
3.1 KiB
Rust

use std::path::PathBuf;
use egui::TextBuffer;
use xmpd_manifest::store::{JsonStore, TomlStore};
use crate::windows::WindowId;
use super::{CompGetter, CompUi};
#[derive(Debug, Default)]
pub struct TopNav {
// for dialog
manifest_path: Option<(PathBuf, String)>
}
component_register!(TopNav);
impl CompUi for TopNav {
fn draw(ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
ui.add_space(3.0);
ui.horizontal(|ui| {
ui.add_space(3.0);
egui::menu::bar(ui, |ui| {
ui.menu_button("File", |ui| {
if ui.button("Settings").clicked() {
state.windows.toggle(&WindowId::Settings, true);
ui.close_menu();
}
});
ui.menu_button("Manifest", |ui| {
if ui.button("Save").clicked() {
handle_error_ui!(state.manifest.save());
ui.close_menu();
}
if ui.button("Save As").clicked() {
std::thread::spawn(|| -> crate::Result<()> {
let mut dialog = rfd::FileDialog::new()
.add_filter("Json", &["json"])
.add_filter("Toml", &["toml"])
.set_title("Save Manifest As")
.set_can_create_directories(true)
.set_file_name("manifest");
if let Some(home_dir) = dirs::home_dir() {
dialog = dialog.set_directory(home_dir);
}
if let Some(path) = dialog.save_file() {
if let Some(ext) = path.extension() {
TopNav::get()?.manifest_path = Some((path.clone(), ext.to_string_lossy().to_string()))
}
}
Ok(())
});
}
});
ui.menu_button("Help", |ui| {
if ui.button("Source").clicked() {
ui.ctx().open_url(egui::OpenUrl::new_tab("https://git.mcorangehq.xyz/XOR64/music"));
ui.close_menu();
}
#[cfg(debug_assertions)]
if ui.button("Debug").clicked() {
state.windows.toggle(&WindowId::Debug, true);
ui.close_menu();
}
});
});
});
let mut used = false;
if let Some((path, ext)) = &TopNav::get()?.manifest_path {
match ext.as_str() {
"json" => state.manifest.convert_and_save_to::<JsonStore>(&path)?,
"toml" => state.manifest.convert_and_save_to::<TomlStore>(&path)?,
_ => ()
}
used = true;
}
if used {
TopNav::get()?.manifest_path = None;
}
Ok(())
}
}