Major update

too lazy to describe
This commit is contained in:
Gvidas Juknevičius 2025-12-07 19:46:47 +02:00
parent 67a948bb66
commit a21295ecc8
24 changed files with 2143 additions and 1486 deletions

11
Cargo.lock generated
View File

@ -1387,6 +1387,12 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "downcast-rs"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117240f60069e65410b3ae1bb213295bd828f707b5bec6596a1afc8793ce0cbc"
[[package]] [[package]]
name = "ecolor" name = "ecolor"
version = "0.27.2" version = "0.27.2"
@ -4933,7 +4939,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6" checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6"
dependencies = [ dependencies = [
"cc", "cc",
"downcast-rs", "downcast-rs 1.2.1",
"rustix 0.38.39", "rustix 0.38.39",
"scoped-tls", "scoped-tls",
"smallvec", "smallvec",
@ -5792,12 +5798,14 @@ dependencies = [
"anyhow", "anyhow",
"camino", "camino",
"dirs", "dirs",
"downcast-rs 2.0.2",
"eframe", "eframe",
"egui", "egui",
"egui_extras", "egui_extras",
"lazy_static", "lazy_static",
"log", "log",
"rfd", "rfd",
"url",
"uuid", "uuid",
"xmpd-cache", "xmpd-cache",
"xmpd-cliargs", "xmpd-cliargs",
@ -5840,6 +5848,7 @@ dependencies = [
"lazy_static", "lazy_static",
"serde", "serde",
"toml 0.8.19", "toml 0.8.19",
"xmpd-cliargs",
] ]
[[package]] [[package]]

View File

@ -48,3 +48,4 @@ toml = "0.8.19"
rfd = "0.15.1" rfd = "0.15.1"
rodio = { version = "0.20.1", features = ["symphonia-all"] } rodio = { version = "0.20.1", features = ["symphonia-all"] }
image = "0.25.5" image = "0.25.5"
downcast-rs = "2.0.2"

File diff suppressed because it is too large Load Diff

View File

@ -30,11 +30,12 @@ impl IconCacheDl {
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> { pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
match song.icon_type().clone() { match song.icon_type().clone() {
IconType::FromSource => { IconType::FromSource => {
let tooling = xmpd_settings::Settings::get()?.tooling.clone(); let settings = xmpd_settings::Settings::get()?.clone();
let tooling = settings.tooling.clone();
match song.source_type() { match song.source_type() {
SourceType::Youtube => { SourceType::Youtube => {
self.jobs.insert(sid.clone(), DlStatus::Downloading); self.jobs.insert(sid.clone(), DlStatus::Downloading);
let mut path = xmpd_cliargs::CLIARGS.cache_path(); let mut path = settings.cache_settings.cache_path.clone();
path.push("icons"); path.push("icons");
path.push(sid.to_string()); path.push(sid.to_string());
@ -91,7 +92,7 @@ impl IconCacheDl {
anyhow::bail!("Url without extension, cant continue"); anyhow::bail!("Url without extension, cant continue");
}; };
let ext = ext.to_string_lossy().to_string(); let ext = ext.to_string_lossy().to_string();
let mut path = xmpd_cliargs::CLIARGS.cache_path(); let mut path = xmpd_settings::Settings::get()?.clone().cache_settings.cache_path;
path.push("icons"); path.push("icons");
path.push(sid.to_string()); path.push(sid.to_string());
path.set_extension(ext); path.set_extension(ext);

View File

@ -1,4 +1,5 @@
use std::{collections::HashMap, ffi::OsStr, process::{Command, Stdio}, sync::{Arc, Mutex, MutexGuard}}; use std::{collections::HashMap, ffi::OsStr, process::{Command, Stdio}, sync::{Arc, Mutex, MutexGuard}};
use camino::{Utf8Path, Utf8PathBuf};
use xmpd_manifest::song::{Song, SourceType}; use xmpd_manifest::song::{Song, SourceType};
@ -14,6 +15,12 @@ pub enum SongStatus {
Done Done
} }
#[cfg(target_family = "windows")]
const PATH_SEP: char = ';';
#[cfg(target_family = "unix")]
const PATH_SEP: char = ':';
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct SongCacheDl { pub struct SongCacheDl {
pub jobs: HashMap<uuid::Uuid, SongStatus>, pub jobs: HashMap<uuid::Uuid, SongStatus>,
@ -30,12 +37,15 @@ impl SongCacheDl {
pub fn is_job_list_full(&self) -> bool { pub fn is_job_list_full(&self) -> bool {
self.current_jobs >= 5 self.current_jobs >= 5
} }
pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> { pub fn download(&mut self, sid: uuid::Uuid, song: Song) -> crate::Result<()> {
self.current_jobs += 1; self.current_jobs += 1;
let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone(); let song_format = xmpd_settings::Settings::get().unwrap().tooling.song_format.clone();
let tooling = xmpd_settings::Settings::get()?.tooling.clone(); let settings = xmpd_settings::Settings::get()?.clone();
let mut song_cache_d = xmpd_cliargs::CLIARGS.cache_path(); let tooling = settings.tooling.clone();
let mut song_cache_d = settings.cache_settings.cache_path.clone();
song_cache_d.push("songs"); song_cache_d.push("songs");
match song.source_type() { match song.source_type() {
SourceType::Youtube | SourceType::Youtube |
@ -49,7 +59,7 @@ impl SongCacheDl {
dl_cmd.args(["-x", "--audio-format", &song_format]); dl_cmd.args(["-x", "--audio-format", &song_format]);
dl_cmd.arg("-o"); dl_cmd.arg("-o");
dl_cmd.arg(&song_p); dl_cmd.arg(&song_p);
if xmpd_cliargs::CLIARGS.debug { if xmpd_cliargs::CLIARGS.debug {
dl_cmd.stdout(Stdio::piped()); dl_cmd.stdout(Stdio::piped());
dl_cmd.stderr(Stdio::piped()); dl_cmd.stderr(Stdio::piped());

View File

@ -1,4 +1,6 @@
use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration}; use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::{mpsc::{self, Receiver, Sender}, Arc, Mutex, MutexGuard}, time::Duration};
use anyhow::anyhow;
use camino::{Utf8Path, Utf8PathBuf};
use downloader::song::SongStatus; use downloader::song::SongStatus;
use xmpd_manifest::song::Song; use xmpd_manifest::song::Song;
@ -41,18 +43,40 @@ impl Cache {
Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))), Err(e) => Err(anyhow::anyhow!(format!("{e:?}"))),
} }
} }
fn check_if_tool_exists(&self, tool_path: &Utf8Path) -> crate::Result<()> {
if std::fs::metadata(tool_path).is_ok() {
return Ok(());
}
if let Ok(path) = std::env::var("PATH") {
for p in path.split(":") {
let p_str = Utf8PathBuf::from(p).join(tool_path);
if std::fs::metadata(p_str).is_ok() {
return Ok(());
}
}
}
anyhow::bail!("Tool {} was not found", tool_path)
}
pub fn init(&mut self) -> Result<Receiver<Message>> { pub fn init(&mut self) -> Result<Receiver<Message>> {
// Check for missing tooling
let tooling = xmpd_settings::Settings::get()?.tooling.clone();
self.check_if_tool_exists(&tooling.ytdlp_path)?;
self.check_if_tool_exists(&tooling.spotdl_path)?;
self.check_if_tool_exists(&tooling.ffmpeg_path)?;
let (internal_tx, cache_rx) = mpsc::channel::<Message>(); let (internal_tx, cache_rx) = mpsc::channel::<Message>();
// let (internal_rx, cache_tx) = mpsc::channel::<Message>(); // let (internal_rx, cache_tx) = mpsc::channel::<Message>();
start_cache_mv_thread(internal_tx); start_cache_mv_thread(internal_tx);
self.cache_dir = xmpd_cliargs::CLIARGS.cache_path(); self.cache_dir = xmpd_settings::Settings::get()?.cache_settings.cache_path.clone();
std::fs::create_dir_all(&self.cache_dir)?;
{ // Get cached songs { // Get cached songs
let mut song_cache_dir = self.cache_dir.clone(); let mut song_cache_dir = self.cache_dir.clone();
std::fs::create_dir_all(&song_cache_dir)?;
song_cache_dir.push("songs"); song_cache_dir.push("songs");
for file in song_cache_dir.read_dir_utf8()? { std::fs::create_dir_all(&song_cache_dir)?;
for file in song_cache_dir.read_dir_utf8().map_err(|e| anyhow!("failed to read cache dir: {e}"))? {
if let Ok(file) = file { if let Ok(file) = file {
if !file.file_type()?.is_file() { if !file.file_type()?.is_file() {
log::warn!("Non song file in: {}", file.path()); log::warn!("Non song file in: {}", file.path());
@ -126,7 +150,7 @@ fn start_cache_mv_thread(tx: Sender<Message>) {
for (sid, status) in &dlc.jobs { for (sid, status) in &dlc.jobs {
if *status == SongStatus::Done { if *status == SongStatus::Done {
let mut cache = he!(tx, CACHE.lock()); let mut cache = he!(tx, CACHE.lock());
let mut song_p = xmpd_cliargs::CLIARGS.cache_path().clone(); let mut song_p = he!(tx, xmpd_settings::Settings::get()).cache_settings.cache_path.clone();
song_p.push("songs"); song_p.push("songs");
song_p.push(sid.clone().to_string()); song_p.push(sid.clone().to_string());
let song_p = song_p.with_extension(&song_format); let song_p = song_p.with_extension(&song_format);

View File

@ -10,72 +10,28 @@ lazy_static::lazy_static!(
#[derive(Debug, clap::Parser)] #[derive(Debug, clap::Parser)]
pub struct CliArgs { pub struct CliArgs {
/// Manifest path /// Manifest path
#[arg(long, short, default_value_t=get_default_manifest_path())] #[arg(long, short)]
manifest: camino::Utf8PathBuf, manifest: Option<camino::Utf8PathBuf>,
/// settings file path /// settings file path
#[arg(long, short, default_value_t=get_default_settings_path())] #[arg(long, short, default_value="./settings.toml")]
settings: camino::Utf8PathBuf, settings: camino::Utf8PathBuf,
/// Cache dir path /// Cache dir path
#[arg(long, short, default_value_t=get_default_cache_path())] #[arg(long, short)]
cache: camino::Utf8PathBuf, cache: Option<camino::Utf8PathBuf>,
/// Debug mode /// Debug mode
#[arg(long, short)] #[arg(long, short)]
pub debug: bool, pub debug: bool,
} }
impl CliArgs { impl CliArgs {
pub fn manifest_path(&self) -> PathBuf { pub fn manifest_path(&self) -> Option<Utf8PathBuf> {
self.manifest.clone().into_std_path_buf() self.manifest.clone()
} }
pub fn settings_path(&self) -> PathBuf { pub fn settings_path(&self) -> Utf8PathBuf {
self.settings.clone().into_std_path_buf() self.settings.clone()
} }
pub fn cache_path(&self) -> Utf8PathBuf { pub fn cache_path(&self) -> Option<Utf8PathBuf> {
self.cache.clone() self.cache.clone()
} }
} }
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_settings_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_SETTINGS_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("config.toml");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_manifest_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_MANIFEST_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::config_dir() {
p.push("xmpd");
p.push("manifest.json");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}
#[allow(irrefutable_let_patterns)] // Broken?
fn get_default_cache_path() -> camino::Utf8PathBuf {
if let Ok(p) = std::env::var("XMPD_CACHE_PATH") {
if let Ok(p) = camino::Utf8PathBuf::from_str(&p) {
return p;
}
}
if let Some(mut p) = dirs::cache_dir() {
p.push("xmpd");
return camino::Utf8PathBuf::from_path_buf(p).expect("Invalid os path");
}
unreachable!()
}

View File

@ -11,7 +11,10 @@ fn main() -> Result<()> {
let cliargs = &xmpd_cliargs::CLIARGS; let cliargs = &xmpd_cliargs::CLIARGS;
logger::init(&cliargs); logger::init(&cliargs);
log::debug!("Initialising settings"); log::debug!("Initialising settings");
xmpd_settings::Settings::get()?.load(Some(cliargs.settings_path()))?; {
xmpd_settings::Settings::get()?.load(Some(cliargs.settings_path().into_std_path_buf()))?;
xmpd_settings::Settings::get()?.load_cli_args(cliargs);
}
log::debug!("Starting gui"); log::debug!("Starting gui");
xmpd_gui::start()?; xmpd_gui::start()?;
Ok(()) Ok(())

View File

@ -33,3 +33,5 @@ uuid.workspace = true
camino.workspace = true camino.workspace = true
rfd.workspace = true rfd.workspace = true
dirs.workspace = true dirs.workspace = true
downcast-rs.workspace = true
url.workspace = true

View File

@ -1,9 +1,11 @@
use anyhow::anyhow;
use egui::{Color32, CursorIcon, ImageSource, RichText, Sense, Vec2}; use egui::{Color32, CursorIcon, ImageSource, RichText, Sense, Vec2};
use xmpd_cache::DlStatus; use xmpd_cache::DlStatus;
use xmpd_manifest::{query, song::Song, store::{BaseStore, StoreExtras}}; use xmpd_manifest::{query, song::Song, store::{BaseStore, StoreExtras}};
use crate::utils::SearchType; use crate::{components::toast::{Toast, ToastType}, utils::SearchType, windows::WindowId};
use std::any::Any;
use super::{CompGetter, CompUi}; use super::{CompGetter, CompUi};
use downcast_rs::Downcast;
pub mod header; pub mod header;
@ -93,7 +95,7 @@ impl SongList {
fn display_song_tab(ui: &mut egui::Ui, state: &mut crate::GuiState, sid: &uuid::Uuid) -> crate::Result<()> { fn display_song_tab(ui: &mut egui::Ui, state: &mut crate::GuiState, sid: &uuid::Uuid) -> crate::Result<()> {
let mut clicked = false; let mut clicked = false;
ui.horizontal(|ui| { ui.horizontal(|ui| {
let song = handle_option!("(internal)", state.manifest.store().get_song(sid)); let song = handle_option!("(internal)", state.manifest.store().get_song(sid)).clone();
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone(); let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
// let icon_status = handle_error_ui!(xmpd_cache::Cache::get()).get_cached_icon_status(&sid).clone(); // let icon_status = handle_error_ui!(xmpd_cache::Cache::get()).get_cached_icon_status(&sid).clone();
let img = ui.add( let img = ui.add(
@ -151,7 +153,7 @@ impl SongList {
} }
} }
// label.context_menu(|ui| handle_error_ui!(Self::show_context_menu(ui, sid, song))); label.context_menu(|ui| handle_error_ui!(Self::show_context_menu(state, ui, sid, &song)));
ui.monospace( ui.monospace(
RichText::new(format!("By {}", song.author())) RichText::new(format!("By {}", song.author()))
.color(theme.dim_text_color) .color(theme.dim_text_color)
@ -165,14 +167,14 @@ impl SongList {
match status { match status {
Some(DlStatus::Done(_)) => { Some(DlStatus::Done(_)) => {
//let img = egui::Image::new(crate::data::CHECK_ICON) let img = egui::Image::new(crate::data::CHECK_ICON)
// .tint(Color32::LIGHT_GREEN) .tint(Color32::LIGHT_GREEN)
// .sense(Sense::hover()) .sense(Sense::hover())
// .fit_to_exact_size(Vec2::new(16.0, 16.0)); .fit_to_exact_size(Vec2::new(16.0, 16.0));
//ui.add(img).on_hover_ui(|ui| { ui.add(img).on_hover_ui(|ui| {
// ui.label(format!("Path: {p}")); ui.label(format!("Id: {sid}"));
//}); });
} }
Some(DlStatus::Downloading) => { Some(DlStatus::Downloading) => {
let spinner = egui::Spinner::new() let spinner = egui::Spinner::new()
@ -264,10 +266,36 @@ impl SongList {
} }
Ok(playable_songs) Ok(playable_songs)
} }
//fn show_context_menu(ui: &mut egui::Ui, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> { fn show_context_menu(state: &mut crate::GuiState, ui: &mut egui::Ui, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> {
// if ui.button("Download icon").clicked() { if ui.button("Edit").clicked() {
// xmpd_cache::Cache::get()?.download_icon_to_cache(sid.clone(), song.clone()); // TODO: Implement song editing
// } Toast::get().unwrap().show_toast("Not Implemented", "Song editing is not implemented", ToastType::Error);
// Ok(()) //state.windows.toggle(&crate::windows::WindowId::NewSong, status);
//} ui.close_menu();
}
if ui.button("Add to playlist").clicked() {
// TODO: Implement song editing
Toast::get().unwrap().show_toast("Not Implemented", "Adding songs to another playlist is not implemented, go to that playlist and press add song", ToastType::Error);
//state.windows.toggle(&WindowId::AddSongToPl, true);
//let mut windows = crate::windows::WINDOWS.lock().map_err(|e| anyhow!("{e}"))?;
//let mut w = windows.get_mut(&WindowId::AddSongToPl);
//let w = w.as_any_mut();
ui.close_menu();
}
if ui.button("Songs by artist").clicked() {
crate::components::song_list::header::Header::get()?.search_text = format!("author:{}", song.author());
ui.close_menu();
}
if ui.button(RichText::new("Remove from playlist").color(Color32::RED)).clicked() {
Toast::get().unwrap().show_toast("Not Implemented", "Removing songs from playlists is not implemented", ToastType::Error);
ui.close_menu();
}
if ui.button(RichText::new("Remove song globally").color(Color32::RED)).clicked() {
Toast::get().unwrap().show_toast("Not Implemented", "Removing songs globally is not implemented", ToastType::Error);
ui.close_menu();
}
Ok(())
}
} }

View File

@ -28,6 +28,9 @@ impl CompUi for TopNav {
} }
}); });
ui.menu_button("Manifest", |ui| { ui.menu_button("Manifest", |ui| {
if ui.button("Add New Song").clicked() {
state.windows.toggle(&WindowId::NewSong, true);
}
if ui.button("Save").clicked() { if ui.button("Save").clicked() {
handle_error_ui!(state.manifest.save()); handle_error_ui!(state.manifest.save());
ui.close_menu(); ui.close_menu();

View File

@ -1,4 +1,6 @@
use std::path::Path;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use anyhow::anyhow;
use xmpd_manifest::{store::JsonStore, Manifest}; use xmpd_manifest::{store::JsonStore, Manifest};
#[macro_use] #[macro_use]
@ -14,15 +16,20 @@ const W_NAME: &str = "xmpd v2.0.0a";
type Result<T> = anyhow::Result<T>; type Result<T> = anyhow::Result<T>;
pub fn start() -> Result<()> { pub fn start() -> Result<()> {
let cache_rx = xmpd_cache::Cache::get()?.init()?; let manifest_p = xmpd_settings::Settings::get()?.cache_settings.manifest_path.clone().into_std_path_buf();
let cache_rx = xmpd_cache::Cache::get()
.map_err(|e| anyhow!("Failed to get cache: {e}"))?
.init()
.map_err(|e| anyhow!("Failed to init cache: {e}"))?;
let options = eframe::NativeOptions::default(); let options = eframe::NativeOptions::default();
let mut state = GuiState::new()?; let mut state = GuiState::new(&manifest_p)?;
let res = eframe::run_simple_native(W_NAME, options, move |ctx, _frame| { let res = eframe::run_simple_native(W_NAME, options, move |ctx, _frame| {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let f_start = Instant::now(); let f_start = Instant::now();
egui_extras::install_image_loaders(ctx); egui_extras::install_image_loaders(ctx);
windows::Windows::draw_all(ctx, &mut state); windows::Windows::draw_all(ctx, &mut state);
handle_error_ui!(main_window::draw(ctx, &mut state, &cache_rx)); handle_error_ui!(main_window::draw(ctx, &mut state, &cache_rx));
ctx.request_repaint_after(Duration::from_millis(500)); ctx.request_repaint_after(Duration::from_millis(500));
@ -54,22 +61,22 @@ pub struct GuiState {
impl GuiState { impl GuiState {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub fn new() -> Result<Self> { pub fn new(manifest_p: &Path) -> Result<Self> {
Ok(Self { Ok(Self {
debug_info: DebugInfo { debug_info: DebugInfo {
last_frame_time: Default::default() last_frame_time: Default::default()
}, },
player: xmpd_player::Player::new(), player: xmpd_player::Player::new(),
manifest: Manifest::new(&xmpd_cliargs::CLIARGS.manifest_path())?, manifest: Manifest::new(manifest_p)?,
windows: windows::Windows::new(), windows: windows::Windows::new(),
}) })
} }
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
pub fn new() -> Result<Self> { pub fn new(manifest_p: &Path) -> Result<Self> {
Ok(Self { Ok(Self {
player: xmpd_player::Player::new(), player: xmpd_player::Player::new(),
manifest: Manifest::new(&xmpd_cliargs::CLIARGS.manifest_path())?, manifest: Manifest::new(manifest_p)?,
windows: windows::Windows::new(), windows: windows::Windows::new(),
}) })
} }

View File

@ -1,6 +1,8 @@
use egui::{RichText, TextEdit}; use egui::{RichText, Sense, TextEdit, TopBottomPanel};
use xmpd_cache::DlStatus; use xmpd_cache::DlStatus;
use xmpd_manifest::store::{BaseStore, StoreExtras}; use xmpd_manifest::{song::Song, store::{BaseStore, StoreExtras}};
use crate::{components::{CompGetter, toast::{Toast, ToastType}}, windows::WindowId};
use super::Window; use super::Window;
@ -8,6 +10,7 @@ use super::Window;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct AddSongW { pub struct AddSongW {
sid: uuid::Uuid, sid: uuid::Uuid,
pid: Option<uuid::Uuid>,
} }
impl Window for AddSongW { impl Window for AddSongW {
@ -18,6 +21,8 @@ impl Window for AddSongW {
"Add Song to Playlist" "Add Song to Playlist"
} }
fn draw(&mut self, ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> { fn draw(&mut self, ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
let mut save = false;
self.pid = crate::components::left_nav::LeftNav::get()?.selected_playlist_id.clone();
let theme = xmpd_settings::Settings::get()?.theme.clone(); let theme = xmpd_settings::Settings::get()?.theme.clone();
let songs: Vec<_> = state.manifest.store().get_songs_sorted(); let songs: Vec<_> = state.manifest.store().get_songs_sorted();
@ -61,7 +66,7 @@ impl Window for AddSongW {
.show(ui, |ui| { .show(ui, |ui| {
ui.vertical(|ui| { ui.vertical(|ui| {
for (sid, song) in songs { for (sid, song) in songs {
ui.group(|ui| { let resp = ui.group(|ui| {
let avail = ui.available_size(); let avail = ui.available_size();
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.set_width(avail.x); ui.set_width(avail.x);
@ -87,13 +92,62 @@ impl Window for AddSongW {
}); });
}); });
}); });
if resp.response.interact(Sense::click()).clicked() {
self.sid = sid.clone();
}
} }
}); });
} }
); );
}); });
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
TopBottomPanel::bottom("bottom_bar")
.frame(
egui::Frame::none()
.fill(theme.primary_bg_color)
.stroke(egui::Stroke::new(
1.0,
theme.secondary_bg_color,
)),
)
.show(ui.ctx(), |ui| {
ui.style_mut().visuals.override_text_color = Some(theme.text_color);
ui.add_space(3.0);
ui.horizontal(|ui| {
// ui.add_space(3.0);
if ui.button("Add").clicked() {
save = true;
}
if ui.button("Cancel").clicked() {
state.windows.toggle(&WindowId::AddSongToPl, false);
}
if ui.button("Close").clicked() {
state.windows.toggle(&WindowId::AddSongToPl, false);
}
});
});
}) })
}); });
if save {
match &self.pid {
Some(pid) => {
let pl = state.manifest.store_mut().get_playlist_mut(pid);
match pl {
Some(pl) => pl.add_song(&self.sid),
None => Toast::get().unwrap().show_toast("Not Allowed", "You cant add a song to the 'All Songs' playlist", ToastType::Error)
};
}
None => (),
}
}
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@ -62,6 +62,9 @@ impl Window for DebugW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }
impl DebugW { impl DebugW {

View File

@ -17,4 +17,7 @@ impl Window for ErrorW {
ui.label("Hello from other window!"); ui.label("Hello from other window!");
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@ -3,15 +3,15 @@ use egui::{ViewportBuilder, ViewportId};
use crate::GuiState; use crate::GuiState;
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
mod debug; pub mod debug;
mod error; pub mod error;
mod settings; pub mod settings;
mod add_song; pub mod add_song;
mod new_song; pub mod new_song;
mod new_playlist; pub mod new_playlist;
lazy_static::lazy_static!( lazy_static::lazy_static!(
static ref WINDOWS: Arc<Mutex<HashMap<WindowId, Box<dyn Window>>>> = Arc::new(Mutex::new(HashMap::new())); pub static ref WINDOWS: Arc<Mutex<HashMap<WindowId, Box<dyn Window>>>> = Arc::new(Mutex::new(HashMap::new()));
static ref OPEN_WINDOWS: Arc<Mutex<HashSet<WindowId>>> = Arc::new(Mutex::new(HashSet::new())); static ref OPEN_WINDOWS: Arc<Mutex<HashSet<WindowId>>> = Arc::new(Mutex::new(HashSet::new()));
); );
@ -22,6 +22,7 @@ pub trait Window: std::fmt::Debug + Send {
fn close(&self) where Self: Sized{ fn close(&self) where Self: Sized{
OPEN_WINDOWS.lock().unwrap().remove(&Self::id()); OPEN_WINDOWS.lock().unwrap().remove(&Self::id());
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized;
} }
#[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)] #[derive(Debug, Clone, Hash, PartialEq, PartialOrd, Ord, Eq)]
@ -104,4 +105,10 @@ impl Windows {
pub fn is_open(&self, id: &WindowId) -> bool { pub fn is_open(&self, id: &WindowId) -> bool {
OPEN_WINDOWS.lock().unwrap().contains(&id) OPEN_WINDOWS.lock().unwrap().contains(&id)
} }
pub fn set_value(&self, id: &WindowId, k: impl ToString, v: impl ToString) -> crate::Result<()> {
// WINDOWS.lock().unwrap().get_mut(&win_id).unwrap().set_value();
//
Ok(())
}
} }

View File

@ -99,4 +99,7 @@ impl Window for NewPlaylistW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }

View File

@ -1,9 +1,20 @@
use std::str::FromStr;
use egui::{Sense, Vec2};
use xmpd_manifest::{song::{Song, SourceType}, store::BaseStore};
use crate::{components::{CompGetter, toast::{Toast, ToastType}}, windows::WindowId};
use super::Window; use super::Window;
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct NewSongW { pub struct NewSongW {
name: String,
author: String,
source_t: SourceType,
source_url: String,
source_url_old: String,
} }
impl Window for NewSongW { impl Window for NewSongW {
@ -13,8 +24,113 @@ impl Window for NewSongW {
fn default_title() -> &'static str where Self: Sized { fn default_title() -> &'static str where Self: Sized {
"New Song" "New Song"
} }
fn draw(&mut self, ui: &mut egui::Ui, _: &mut crate::GuiState) -> crate::Result<()> { fn draw(&mut self, ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
ui.label("Hello from other window!"); let theme = xmpd_settings::Settings::get()?.theme.clone();
let img_size = 64.0;
let img_spacing = 10.0;
ui.vertical(|ui| {
let mut rect = egui::Rect::ZERO;
rect.set_width(img_size);
rect.set_height(img_size);
rect.set_top(img_spacing);
rect.set_left(img_spacing);
let rect_int = ui.interact(rect, "new_playlist_w".into(), Sense::click());
if rect_int.hovered() {
ui.allocate_ui_at_rect(rect, |ui| {
ui.group(|ui| {
let img = egui::Image::new(crate::data::PLUS_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(Vec2::new(img_size, img_size));
//.paint_at(ui, rect);
ui.add(img);
});
});
} else {
ui.allocate_ui_at_rect(rect, |ui| {
ui.group(|ui| {
let img = egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.fit_to_exact_size(Vec2::new(img_size, img_size));
//.paint_at(ui, rect);
ui.add(img);
});
});
}
if rect_int.clicked() {
// TODO: Add a way to add custom icons
Toast::get().unwrap().show_toast("Not Implemented", "Adding icons to songs is not implemented", ToastType::Error);
}
ui.add_space(img_spacing);
ui.horizontal(|ui| {
ui.label("Name: ");
ui.text_edit_singleline(&mut self.name);
});
ui.horizontal(|ui| {
ui.label("Author: ");
ui.text_edit_singleline(&mut self.author);
});
ui.horizontal(|ui| {
ui.label("Source Type: ");
egui::ComboBox::new("new_song_song_t_sel", "")
.selected_text(self.source_t.to_string())
.show_ui(ui, |ui| {
ui.selectable_value(&mut self.source_t, SourceType::Youtube, SourceType::Youtube.to_string());
ui.selectable_value(&mut self.source_t, SourceType::Spotify, SourceType::Spotify.to_string());
ui.selectable_value(&mut self.source_t, SourceType::Soundcloud, SourceType::Soundcloud.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::HttpBare, SourceType::HttpBare.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::HttpZip, SourceType::HttpZip.to_string());
//ui.selectable_value(&mut self.source_t, SourceType::Http7z, SourceType::Http7z.to_string());
}
);
});
ui.horizontal(|ui| {
ui.label("Source URL: ");
ui.text_edit_singleline(&mut self.source_url);
if self.source_url != self.source_url_old {
if let Some(t) = SourceType::from_url(&handle_error_ui!(url::Url::from_str(&self.source_url))) {
self.source_t = t;
}
}
});
ui.with_layout(egui::Layout::bottom_up(egui::Align::Max), |ui| {
ui.add_space(3.0);
ui.horizontal(|ui| {
ui.add_space(3.0);
if ui.button("Close").clicked() {
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
state.windows.toggle(&WindowId::NewSong, false);
}
if ui.button("Add").clicked() {
let mut s = handle_error_ui!(Song::new_from_str(&self.source_url, self.source_t));
s.set_name(&self.name);
s.set_author(&self.author);
state.manifest.store_mut().get_songs_mut().insert(uuid::Uuid::new_v4(), s);
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
}
if ui.button("Cancel").clicked() {
self.author = String::from("New Song");
self.name = String::from("Unknown");
self.source_t = SourceType::Youtube;
self.source_url = String::default();
state.windows.toggle(&WindowId::NewSong, false);
}
});
});
});
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, _: String, _: Box<V>) where Self: Sized {
}
} }

View File

@ -85,6 +85,9 @@ impl Window for SettingsW {
}); });
Ok(()) Ok(())
} }
fn set_value<V>(&mut self, k: String, v: Box<V>) where Self: Sized {
}
} }
impl SettingsW { impl SettingsW {

View File

@ -1,5 +1,7 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::anyhow;
#[cfg(test)] #[cfg(test)]
pub mod tests; pub mod tests;
@ -59,12 +61,13 @@ impl<ST: store::BaseStore + Clone> Manifest<ST> {
} }
pub fn get_song_as_path(&self, sid: uuid::Uuid) -> Result<PathBuf> { pub fn get_song_as_path(&self, sid: uuid::Uuid) -> Result<PathBuf> {
let ext = &xmpd_settings::Settings::get()?.tooling.song_format; let settings = &xmpd_settings::Settings::get()?;
let mut p = xmpd_cliargs::CLIARGS.cache_path().into_std_path_buf(); let ext = &settings.tooling.song_format;
let mut p = settings.cache_settings.cache_path.clone();
p.push("songs"); p.push("songs");
p.push(sid.to_string()); p.push(sid.to_string());
p.set_extension(ext); p.set_extension(ext);
Ok(p) Ok(p.into_std_path_buf())
} }
} }

View File

@ -83,8 +83,9 @@ impl Song {
} }
} }
#[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd)] #[derive(Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, PartialOrd, Default)]
pub enum SourceType { pub enum SourceType {
#[default]
Youtube, Youtube,
Spotify, Spotify,
Soundcloud, Soundcloud,
@ -94,7 +95,7 @@ pub enum SourceType {
} }
impl SourceType { impl SourceType {
fn from_url(url: &url::Url) -> Option<Self> { pub fn from_url(url: &url::Url) -> Option<Self> {
match url.host_str() { match url.host_str() {
Some("youtube.com") | Some("youtu.be") => Some("youtube.com") | Some("youtu.be") =>
Some(Self::Youtube), Some(Self::Youtube),

View File

@ -7,6 +7,7 @@ license.workspace = true
authors.workspace = true authors.workspace = true
[dependencies] [dependencies]
xmpd-cliargs.path = "../xmpd-cliargs"
anyhow.workspace = true anyhow.workspace = true
camino.workspace = true camino.workspace = true
egui.workspace = true egui.workspace = true

View File

@ -0,0 +1,29 @@
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Cache {
#[serde(default="Cache::default_cache_path")]
pub cache_path: Utf8PathBuf,
#[serde(default="Cache::default_manifest_path")]
pub manifest_path: Utf8PathBuf,
}
impl Default for Cache {
fn default() -> Self {
Self {
cache_path: Self::default_cache_path(),
manifest_path: Self::default_manifest_path(),
}
}
}
impl Cache {
fn default_cache_path() -> Utf8PathBuf {
Utf8PathBuf::from("./cache")
}
fn default_manifest_path() -> Utf8PathBuf {
Utf8PathBuf::from("./manifest.json")
}
}

View File

@ -5,6 +5,7 @@ use tooling::Tooling;
pub mod theme; pub mod theme;
pub mod tooling; pub mod tooling;
pub mod cache;
lazy_static::lazy_static!( lazy_static::lazy_static!(
static ref SETTINGS: Arc<Mutex<Settings>> = Arc::new(Mutex::new(Settings::default())); static ref SETTINGS: Arc<Mutex<Settings>> = Arc::new(Mutex::new(Settings::default()));
@ -20,6 +21,8 @@ pub struct Settings {
pub theme: Theme, pub theme: Theme,
#[serde(default)] #[serde(default)]
pub tooling: Tooling, pub tooling: Tooling,
#[serde(default)]
pub cache_settings: cache::Cache
} }
impl Settings { impl Settings {
@ -54,6 +57,16 @@ impl Settings {
self.settings_path = path; self.settings_path = path;
Ok(()) Ok(())
} }
pub fn load_cli_args(&mut self, cli_args: &xmpd_cliargs::CliArgs) {
if let Some(mp) = cli_args.manifest_path() {
self.cache_settings.manifest_path = mp;
}
if let Some(cp) = cli_args.cache_path() {
self.cache_settings.cache_path = cp;
}
}
} }