use anyhow::bail; use egui::Color32; use crate::manifest::song::SongType; use super::{State, Window}; #[derive(Debug, Default)] pub struct GuiSongEditor { song: (String, String), ed_url: String, ed_name: String, ed_type: SongType } 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_name, song_name) = self.song.clone(); if playlist_name.is_empty() { return Ok(()); } let Some(song) = state.manifest.get_song(&playlist_name, &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_name); ui.label(": "); ui.label(&song_name) }); ui.horizontal(|ui| { ui.label("Type: "); ui.label(song.get_type().to_string()); egui::ComboBox::from_id_source("song_edit_window_type") .selected_text(format!("{:?}", self.ed_type)) .show_ui(ui, |ui| { ui.selectable_value(&mut self.ed_type, SongType::Youtube, "Youtube"); ui.selectable_value(&mut self.ed_type, SongType::Spotify, "Spotify"); ui.selectable_value(&mut self.ed_type, SongType::Soundcloud, "Soundcloud"); } ); }); 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 song = { let Some(song) = state.manifest.get_song_mut(&playlist_name, &song_name) else { bail!("Failed to get song (2)"); }; song.get_url_str_mut().clone_from(&self.ed_url); song.get_type_mut().clone_from(&self.ed_type); song.clone() }; let Some(playlist) = state.manifest.get_playlist_mut(&playlist_name) else { bail!("Failed to get playlist"); }; playlist.remove_song(&song_name); playlist.add_song(self.ed_name.clone(), song.clone()); *open = false; let _ = state.manifest.save(None); } Ok(()) } } impl GuiSongEditor { pub fn set_active_song(&mut self, pname: &str, sname: &str, url: &str, typ: &SongType) { self.song.0 = pname.to_string(); self.song.1 = sname.to_string(); self.ed_name = sname.to_string(); self.ed_url = url.to_string(); self.ed_type = typ.clone(); } }