Rewrite the structure of the gui and cli interfaces

Added a more modular way to add windows
This commit is contained in:
2024-09-22 00:42:50 +03:00
parent 33ca4502e4
commit 2cde24e7a8
15 changed files with 398 additions and 251 deletions

View File

@@ -0,0 +1,95 @@
use anyhow::{Result, bail};
use egui::Color32;
use crate::ui::gui::Gui;
use super::{State, Window};
#[derive(Debug, Default)]
pub struct GuiSongEditor {
song: (String, String),
ed_url: String,
ed_name: String,
}
impl Window for GuiSongEditor {
fn ui(&mut self, state: &mut State, ctx: &egui::Context, open: &mut bool) -> anyhow::Result<()> {
let mut save = false;
let (playlist, song_name) = self.song.clone();
if playlist.is_empty() {
return Ok(());
}
let Some(song) = state.manifest.get_song(&playlist, &song_name) else {
bail!("Failed to get song (1)");
};
let song = song.clone();
egui::Window::new("Song editor")
.open(open)
.show(ctx,
|ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("[");
ui.hyperlink_to("link", song.get_url().unwrap());
ui.label("] ");
ui.colored_label(Color32::LIGHT_BLUE, &playlist);
ui.label(": ");
ui.label(&song_name)
});
ui.horizontal(|ui| {
ui.label("Type: ");
ui.label(&song.get_type().to_string());
});
ui.horizontal(|ui| {
ui.label("Name: ");
ui.text_edit_singleline(&mut self.ed_name);
});
ui.horizontal(|ui| {
ui.label("Url: ");
ui.text_edit_singleline(&mut self.ed_url);
});
if ui.button("Save").clicked() {
save = true;
}
});
if save {
{
let Some(song) = state.manifest.get_song_mut(&playlist, &song_name) else {
bail!("Failed to get song (2)");
};
*song.get_url_str_mut() = self.ed_url.clone();
}
let Some(playlist) = state.manifest.get_playlist_mut(&playlist) else {
bail!("Failed to get playlist");
};
playlist.remove_song(&song_name);
playlist.add_song(self.ed_name.clone(), song);
*open = false;
let _ = state.manifest.save(None);
}
Ok(())
}
}
impl GuiSongEditor {
pub fn set_active_song(&mut self, pname: &String, sname: &String, url: &String) {
self.song.0 = pname.clone();
self.song.1 = sname.clone();
self.ed_name = sname.clone();
self.ed_url = url.clone();
}
}