use eframe::Frame; use egui::{Align, Align2, Area, Key, Layout, Order, Style}; use uuid::Uuid; use xmpd_cache::DlStatus; use xmpd_manifest::{song::Song, store::BaseStore}; use crate::{components::{CompGetter, CompUi, left_nav::LeftNav, toast::ToastType}, utils::SearchType, windows::WindowId}; use super::SongList; #[derive(Debug, Default)] pub struct Header { pub search_text: String, } component_register!(Header); impl CompUi for Header { fn draw(ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> { let theme = xmpd_settings::Settings::get()?.theme.clone(); let pid = {LeftNav::get()?.selected_playlist_id.clone()}; ui.horizontal(|ui| { let search_icon = egui::Image::new(crate::data::SEARCH_ICON) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) .tint(theme.accent_color); ui.add(search_icon); { let resp = ui.text_edit_singleline(&mut handle_error_ui!(Header::get()).search_text); let popup_id = resp.id.with("mode_popup"); if resp.clicked() { ui.memory_mut(|m| m.open_popup(popup_id)); } // NOTE: Genuinely cancerous code incoming let search_text = handle_error_ui!(Header::get()).search_text.clone(); if !search_text.is_empty() { if ui.memory(|mem: &egui::Memory| mem.is_popup_open(popup_id)) { Area::new(popup_id) .order(Order::Foreground) .constrain(true) .fixed_pos(resp.rect.left_bottom()) .pivot(Align2::LEFT_TOP) .show(ui.ctx(), |ui| { let tf = crate::main_window::get_themed_frame(&theme) .stroke(egui::Stroke::new( 1.5, theme.secondary_bg_color, )); let frame_margin = tf.total_margin(); tf.show(ui, |ui| { ui.style_mut().visuals.override_text_color = Some(theme.text_color); ui.with_layout(Layout::top_down_justified(Align::LEFT), |ui| { ui.set_width(resp.rect.width() - frame_margin.sum().x); let st = SearchType::from_str(&search_text); let style = ui.style_mut(); style.visuals.selection.bg_fill = theme.secondary_bg_color; style.visuals.selection.stroke.color = theme.accent_color; for item in &[SearchType::Author, SearchType::Source, SearchType::Genre] { if ui.selectable_label(st.0 == *item, item.new_query(&st.1)).clicked() { { handle_error_ui!(Header::get()).search_text = item.new_query(&st.1); } ui.memory_mut(|m| m.close_popup()); } } }) }) }); if ui.input(|i| i.key_pressed(Key::Escape)) || resp.clicked_elsewhere() { ui.memory_mut(|mem| mem.close_popup()); } } } } ui.with_layout(egui::Layout::right_to_left(egui::Align::RIGHT), |ui| { let download_all = ui.add( egui::Image::new(crate::data::DL_ICON) .tint(theme.accent_color) .sense(egui::Sense::click()) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) ); let add_song = ui.add( egui::Image::new(crate::data::PLUS_ICON) .tint(theme.accent_color) .sense(egui::Sense::click()) .fit_to_exact_size(egui::Vec2::new(16.0, 16.0)) ); if download_all.clicked() { let songs: Vec<_>; match pid { Some(pid) => { songs = state.manifest.store().get_playlist(&pid).unwrap().songs().to_vec(); } None => { songs = state.manifest.store().get_songs().keys().cloned().collect(); } } for sid in handle_error_ui!(Self::get_songs_to_download(&songs)) { if let Some(song) = state.manifest.store().get_song(&sid) { handle_error_ui!(xmpd_cache::Cache::get()).download_song_to_cache(sid.clone(), song.clone()) } } let mut toast = handle_error_ui!(crate::components::toast::Toast::get()); toast.show_toast( "Downloading Songs", &format!("Started downloading {} songs", songs.len()), ToastType::Info ); } if add_song.clicked() { state.windows.toggle(&WindowId::AddSongToPl, true); } }); }); Ok(()) } } impl Header { fn get_songs_to_download(songs: &Vec) -> crate::Result> { let mut songs2 = Vec::new(); for sid in songs { if let None = xmpd_cache::Cache::get()?.get_cached_song_status(&sid) { songs2.push(sid.clone()); } } Ok(songs2) } }