Actually usable????????//

This commit is contained in:
2024-11-21 05:02:30 +02:00
parent b1c8417b0f
commit 4ee4ca1add
26 changed files with 2339 additions and 356 deletions

View File

@@ -1,4 +1,4 @@
use egui::RichText;
use egui::{CursorIcon, RichText, Sense};
use xmpd_manifest::store::BaseStore;
use super::{CompGetter, CompUi};
@@ -50,6 +50,7 @@ fn add_playlist_tab(ui: &mut egui::Ui, pid: &Option<uuid::Uuid>, title: &str, au
ui.add(
egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
.fit_to_exact_size(egui::Vec2::new(32.0, 32.0))
);
ui.vertical(|ui| {
@@ -83,9 +84,13 @@ fn add_playlist_tab(ui: &mut egui::Ui, pid: &Option<uuid::Uuid>, title: &str, au
});
}).response.rect;
if ui.interact(wdg_rect, format!("left_nav_playlist_{pid:?}").into(), egui::Sense::click()).clicked() {
let blob = ui.interact(wdg_rect, format!("left_nav_playlist_{pid:?}").into(), egui::Sense::click());
if blob.clicked() {
handle_error_ui!(LeftNav::get()).selected_playlist_id = pid.clone();
}
if blob.hovered() {
ui.output_mut(|o| o.cursor_icon = CursorIcon::PointingHand);
}
ui.separator();
}

View File

@@ -1,19 +1,30 @@
use egui::{Sense, Stroke, Vec2};
use super::{CompGetter, CompUi};
use super::{song_list::SongList, CompGetter, CompUi};
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct Player {
slider_progress: usize,
is_playing: bool,
old_slider_progress: usize,
volume_slider: f64,
}
impl Default for Player {
fn default() -> Self {
Self {
volume_slider: 1.0,
old_slider_progress: 0,
slider_progress: 0
}
}
}
component_register!(Player);
impl CompUi for Player {
fn draw(ui: &mut egui::Ui, _: &mut crate::GuiState) -> crate::Result<()> {
fn draw(ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
let theme = xmpd_settings::Settings::get()?.theme.clone();
let avail = ui.available_size();
ui.vertical_centered_justified(|ui| {
@@ -21,10 +32,7 @@ impl CompUi for Player {
ui.horizontal(|ui| {
{
ui.add_space(avail.x * 0.05 / 2.0);
let mut slf = handle_error_ui!(Player::get());
let slider = egui::Slider::new(&mut slf.slider_progress, 0..=100)
.show_value(false);
ui.style_mut().spacing.slider_width = avail.x * 0.90;
ui.style_mut().spacing.slider_width = avail.x * 0.87;
let s = Stroke {
color: theme.accent_color,
width: 2.0
@@ -32,19 +40,35 @@ impl CompUi for Player {
ui.style_mut().visuals.widgets.inactive.fg_stroke = s;
ui.style_mut().visuals.widgets.active.fg_stroke = s;
ui.style_mut().visuals.widgets.hovered.fg_stroke = s;
ui.add(slider);
ui.label("00:00");
let mut slf = handle_error_ui!(Player::get());
ui.add(
egui::Slider::new(&mut slf.slider_progress, 0..=100)
.show_value(false)
);
if slf.slider_progress == slf.old_slider_progress {
slf.slider_progress = (state.player.get_played_f() * 100.0) as usize;
slf.old_slider_progress = slf.slider_progress;
} else {
handle_error_ui!(state.player.seek_to_f(slf.slider_progress as f64 / 100.0 ));
slf.old_slider_progress = slf.slider_progress;
}
let secs_left = state.player.get_ms_left() as f64 / 1000.0;
let h = (secs_left/60.0/60.0).floor();
let m = ((secs_left - h * 60.0)/60.0).floor();
let s = (secs_left - m * 60.0).floor();
ui.label(format!("{h:02}:{m:02}:{s:02}"));
}
});
ui.horizontal(|ui| {
ui.add_space((avail.x / 2.0) - 16.0 - 8.0 - ui.spacing().item_spacing.x);
let pp = if handle_error_ui!(Player::get()).is_playing {
crate::data::PAUSE_ICON
} else {
let pp = if state.player.is_paused() {
crate::data::PLAY_ICON
} else {
crate::data::PAUSE_ICON
};
let prev = egui::Image::new(crate::data::PREV_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
@@ -57,12 +81,43 @@ impl CompUi for Player {
.tint(theme.accent_color)
.sense(Sense::click())
.max_size(Vec2::new(16.0, 16.0));
if ui.add(prev).clicked() {}
if ui.add(pp).clicked() {
let mut slf = handle_error_ui!(Player::get());
slf.is_playing = !slf.is_playing;
if ui.add(prev).clicked() {
handle_error_ui!(handle_error_ui!(SongList::get()).play_prev(state));
}
if ui.add(next).clicked() {}
if ui.add(pp).clicked() {
if state.player.is_paused() {
state.player.play();
} else {
state.player.pause();
}
}
if ui.add(next).clicked() || state.player.just_stopped() {
handle_error_ui!(handle_error_ui!(SongList::get()).play_next(state));
}
ui.with_layout(egui::Layout::right_to_left(egui::Align::RIGHT), |ui| {
ui.add_space(15.0);
ui.style_mut().spacing.slider_width = avail.x * 0.15;
let s = Stroke {
color: theme.accent_color,
width: 1.0
};
ui.style_mut().visuals.widgets.inactive.fg_stroke = s;
ui.style_mut().visuals.widgets.active.fg_stroke = s;
ui.style_mut().visuals.widgets.hovered.fg_stroke = s;
let mut slf = handle_error_ui!(Player::get());
let slider =ui.add(
egui::Slider::new(&mut slf.volume_slider, 0.0..=1.0)
.show_value(false)
);
if slider.changed() {
state.player.set_volume(slf.volume_slider);
}
});
});
ui.add_space(3.0);
});

View File

@@ -1,6 +1,4 @@
use std::fmt::write;
use egui::{Color32, RichText, Sense, Vec2};
use egui::{Color32, CursorIcon, RichText, Sense, Vec2};
use song_list_nav::SearchType;
use xmpd_cache::DlStatus;
use xmpd_manifest::{song::Song, store::BaseStore};
@@ -10,171 +8,264 @@ pub mod song_list_nav;
#[derive(Debug, Default)]
pub struct SongList {
selected_song_id: uuid::Uuid
selected_sid: uuid::Uuid,
playable_songs: Vec<uuid::Uuid>,
}
component_register!(SongList);
impl CompUi for SongList {
fn draw(ui: &mut egui::Ui, state: &mut crate::GuiState) -> crate::Result<()> {
let songs = Self::get_and_sort_songs(state)?;
let disp_songs = Self::get_songs_to_display(&songs)?;
{
let mut sl = SongList::get()?;
sl.playable_songs = Self::get_playable_songs(&songs)?;
if let Some((sid, _)) = songs.first() {
if sl.selected_sid == Default::default() {
sl.selected_sid = sid.clone();
}
}
}
egui::ScrollArea::vertical()
.id_source("song_list")
.drag_to_scroll(false)
.show(ui, |ui| {
ui.vertical(|ui| {
ui.add_space(3.0);
let pid = {handle_error_ui!(super::left_nav::LeftNav::get()).selected_playlist_id.clone()};
match pid {
None => {
let mut songs: Vec<_> = state.manifest.store().get_songs().iter().collect();
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
for (sid, song) in songs {
let query = {handle_error_ui!(song_list_nav::SongListNav::get()).parse_search()}.clone();
let should_display = match query {
SearchType::Source(s) if !s.is_empty() => format!("{:?}", &song.source_type()).to_lowercase().contains(&s),
SearchType::Author(s) if !s.is_empty() => song.author().to_lowercase().contains(&s),
SearchType::Name(s) if !s.is_empty() => song.name().to_owned().contains(&s),
_ => true
};
if should_display {
display_song_tab(ui, sid, song);
}
}
}
Some(pid) => {
if let Some(playlist) = state.manifest.store().get_playlist(&pid) {
let mut songs = Vec::new();
for sid in playlist.songs() {
if let Some(song) = state.manifest.store().get_song(&sid) {
songs.push((sid, song));
}
}
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
let query = {handle_error_ui!(song_list_nav::SongListNav::get()).parse_search()}.clone();
for (sid, song) in songs {
let should_display = match query {
SearchType::Source(ref s) if !s.is_empty() => format!("{:?}", &song.source_type()).to_lowercase().contains(s),
SearchType::Author(ref s) if !s.is_empty() => song.author().to_lowercase().contains(s),
SearchType::Name(ref s) if !s.is_empty() => song.name().to_owned().contains(s),
_ => true
};
if should_display {
display_song_tab(ui, sid, song);
}
}
}
}
for (sid, song) in disp_songs {
handle_error_ui!(Self::display_song_tab(ui, state, &sid, &song));
}
});
});
Ok(())
}
}
fn display_song_tab(ui: &mut egui::Ui, sid: &uuid::Uuid, song: &Song) {
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
ui.horizontal(|ui| {
let mut clicked = ui.add(
egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
.fit_to_exact_size(Vec2::new(32.0, 32.0))
).clicked();
impl SongList {
fn get_and_sort_songs(state: &mut crate::GuiState) -> crate::Result<Vec<(uuid::Uuid, Song)>> {
let pid = super::left_nav::LeftNav::get()?.selected_playlist_id.clone();
match pid {
None => {
let songs = state.manifest.store().get_songs().clone().into_iter();
let mut songs: Vec<_> = songs.collect();
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
Ok(songs)
}
Some(pid) => {
let Some(playlist) = state.manifest.store().get_playlist(&pid) else {
anyhow::bail!("Couldnt find playlist (corruption?)");
};
let mut songs = Vec::new();
for sid in playlist.songs() {
if let Some(song) = state.manifest.store().get_song(&sid) {
songs.push((sid.clone(), song.clone()));
}
}
songs.sort_by(|a, b| {
let a = a.1.name().to_lowercase();
let b = b.1.name().to_lowercase();
a.cmp(&b)
});
Ok(songs)
}
}
}
ui.vertical(|ui| {
let selected_song_id = {handle_error_ui!(SongList::get()).selected_song_id};
let label = if selected_song_id == *sid {
ui.label(
RichText::new(song.name())
.color(theme.accent_color)
)
} else {
ui.label(
RichText::new(song.name())
.color(theme.text_color)
)
fn get_songs_to_display(songs: &Vec<(uuid::Uuid, Song)>) -> crate::Result<Vec<(uuid::Uuid, Song)>>{
let mut to_display = Vec::new();
let query = {song_list_nav::SongListNav::get()?.parse_search()}.clone();
for (sid, song) in songs {
let should_display = match &query {
SearchType::Name(s) |
SearchType::Author(s) |
SearchType::Source(s) if s.is_empty() => true,
SearchType::Source(s) => {
song.source_type().to_string()
.to_lowercase()
.contains(s)
},
SearchType::Author(s) => {
song.author()
.to_lowercase()
.contains(s)
},
SearchType::Name(s) => {
song.name()
.to_lowercase()
.contains(s)
},
};
if label.clicked() {
clicked = true;
if should_display {
to_display.push((sid.clone(), song.clone()));
}
ui.monospace(
RichText::new(format!("By {}", song.author()))
.color(theme.dim_text_color)
.size(10.0)
);
}
Ok(to_display)
}
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::RIGHT), |ui| {
ui.add_space(3.0);
fn display_song_tab(ui: &mut egui::Ui, state: &mut crate::GuiState, sid: &uuid::Uuid, song: &Song) -> crate::Result<()> {
let mut clicked = false;
ui.horizontal(|ui| {
let theme = handle_error_ui!(xmpd_settings::Settings::get()).theme.clone();
let img = ui.add(
egui::Image::new(crate::data::NOTE_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
.fit_to_exact_size(Vec2::new(32.0, 32.0))
);
let status = {
handle_error_ui!(xmpd_cache::Cache::get()).get_cached_song_status(&sid).clone()
};
match status {
Some(DlStatus::Done(p)) => {
let img = ui.add(
egui::Image::new(crate::data::CHECK_ICON)
.tint(Color32::LIGHT_GREEN)
.sense(Sense::hover())
.fit_to_exact_size(Vec2::new(16.0, 16.0))
);
img.on_hover_ui(|ui| {
ui.label(format!("Path: {p}"));
});
}
Some(DlStatus::Downloading) => {
ui.add(
egui::Spinner::new()
.color(theme.accent_color)
.size(16.0)
);
}
Some(DlStatus::Error(e)) => {
let img = ui.add(
egui::Image::new(crate::data::WARN_ICON)
.tint(Color32::LIGHT_YELLOW)
.sense(Sense::hover())
.fit_to_exact_size(Vec2::new(16.0, 16.0))
);
img.on_hover_ui(|ui| {
ui.label(e);
});
}
None => {
let img = ui.add(
egui::Image::new(crate::data::DL_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
.fit_to_exact_size(Vec2::new(16.0, 16.0))
);
if img.clicked() {
handle_error_ui!(xmpd_cache::Cache::get()).download_to_cache(sid.clone(), song.clone());
let mut toast = handle_error_ui!(crate::components::toast::Toast::get());
toast.show_toast(
"Downloading Song",
&format!("Started downloading {} by {}", song.name(), song.author()),
super::toast::ToastType::Info
);
}
if img.clicked() {
clicked = true;
}
if img.hovered() {
if matches!(status, Some(DlStatus::Done(_))) {
ui.output_mut(|o| o.cursor_icon = CursorIcon::PointingHand);
} else {
ui.output_mut(|o| o.cursor_icon = CursorIcon::Default);
}
}
ui.vertical(|ui| {
let slf = handle_error_ui!(SongList::get());
let label = if slf.selected_sid == *sid {
RichText::new(song.name())
.color(theme.accent_color)
} else if matches!(status, Some(DlStatus::Done(_))) {
RichText::new(song.name())
.color(theme.text_color)
} else {
RichText::new(song.name())
.color(theme.dim_text_color)
};
let label = ui.label(label);
if label.clicked() {
clicked = true;
}
if label.hovered() {
if matches!(status, Some(DlStatus::Done(_))) {
ui.output_mut(|o| o.cursor_icon = CursorIcon::PointingHand);
} else {
ui.output_mut(|o| o.cursor_icon = CursorIcon::Default);
}
}
ui.monospace(
RichText::new(format!("By {}", song.author()))
.color(theme.dim_text_color)
.size(10.0)
);
});
ui.with_layout(egui::Layout::right_to_left(egui::Align::RIGHT), |ui| {
ui.add_space(3.0);
match status {
Some(DlStatus::Done(_p)) => {
//let img = egui::Image::new(crate::data::CHECK_ICON)
// .tint(Color32::LIGHT_GREEN)
// .sense(Sense::hover())
// .fit_to_exact_size(Vec2::new(16.0, 16.0));
//ui.add(img).on_hover_ui(|ui| {
// ui.label(format!("Path: {p}"));
//});
}
Some(DlStatus::Downloading) => {
let spinner = egui::Spinner::new()
.color(theme.accent_color)
.size(16.0);
ui.add(spinner);
}
Some(DlStatus::Error(e)) => {
let img = egui::Image::new(crate::data::WARN_ICON)
.tint(Color32::LIGHT_YELLOW)
.sense(Sense::hover())
.fit_to_exact_size(Vec2::new(16.0, 16.0));
ui.add(img).on_hover_ui(|ui| {
ui.label(e);
});
}
None => {
let img = egui::Image::new(crate::data::DL_ICON)
.tint(theme.accent_color)
.sense(Sense::click())
.fit_to_exact_size(Vec2::new(16.0, 16.0));
if ui.add(img).clicked() {
handle_error_ui!(xmpd_cache::Cache::get()).download_to_cache(sid.clone(), song.clone());
let mut toast = handle_error_ui!(crate::components::toast::Toast::get());
toast.show_toast(
"Downloading Song",
&format!("Started downloading {} by {}", song.name(), song.author()),
super::toast::ToastType::Info
);
}
}
}
});
});
ui.separator();
if clicked {
handle_error_ui!(SongList::get()).selected_song_id = sid.clone();
let mut sl = SongList::get()?;
sl.play_song(sid.clone(), state)?;
}
});
ui.separator();
Ok(())
}
pub fn play_song(&mut self, sid: uuid::Uuid, state: &mut crate::GuiState) -> crate::Result<()> {
if self.playable_songs.contains(&sid) {
self.selected_sid = sid.clone();
let path = state.manifest.get_song_as_path(sid)?;
state.player.play_song(&path)?;
}
Ok(())
}
pub fn play_prev(&mut self, state: &mut crate::GuiState) -> crate::Result<()> {
let Some(mut prev) = self.playable_songs.last().cloned() else {
anyhow::bail!("Trying to play a song in an empty playlist (impossible)")
};
for sid in self.playable_songs.clone() {
if sid == self.selected_sid {
self.play_song(prev, state)?;
}
prev = sid;
}
Ok(())
}
pub fn play_next(&mut self, state: &mut crate::GuiState) -> crate::Result<()> {
let Some(mut next) = self.playable_songs.first().cloned() else {
anyhow::bail!("Trying to play a song in an empty playlist (impossible)")
};
let mut found = false;
for sid in self.playable_songs.clone() {
if sid == self.selected_sid {
found = true;
} else if found {
next = sid;
break;
}
}
self.play_song(next, state)?;
Ok(())
}
fn get_playable_songs(songs: &Vec<(uuid::Uuid, Song)>) -> crate::Result<Vec<uuid::Uuid>> {
let mut playable_songs = Vec::new();
for (sid, _) in songs {
if let Some(DlStatus::Done(_)) = xmpd_cache::Cache::get()?.get_cached_song_status(&sid) {
playable_songs.push(sid.clone());
}
}
Ok(playable_songs)
}
}

View File

@@ -1,8 +1,11 @@
use uuid::Uuid;
use xmpd_manifest::store::BaseStore;
use xmpd_cache::DlStatus;
use xmpd_manifest::{song::Song, store::BaseStore};
use crate::components::{left_nav::LeftNav, toast::ToastType, CompGetter, CompUi};
use super::SongList;
#[derive(Debug, Clone)]
pub enum SearchType {
Name(String),
@@ -53,8 +56,9 @@ impl CompUi for SongListNav {
None => {
songs = state.manifest.store().get_songs().keys().cloned().collect();
}
}
for sid in &songs {
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_to_cache(sid.clone(), song.clone())
}
@@ -86,6 +90,16 @@ impl SongListNav {
i @ _ => SearchType::Name(i.to_string().to_lowercase())
}
}
fn get_songs_to_download(songs: &Vec<uuid::Uuid>) -> crate::Result<Vec<uuid::Uuid>> {
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)
}
}

View File

@@ -1,7 +1,7 @@
use std::{collections::VecDeque, time::SystemTime};
use egui::{epaint::Shadow, load::TexturePoll, Align2, Color32, Frame, Image, Margin, Pos2, Rect, RichText, Rounding, Stroke, Style, Vec2};
use egui::{epaint::Shadow, load::TexturePoll, Align2, Color32, Frame, Image, ImageSource, Margin, Pos2, Rect, RichText, Rounding, Stroke, Style, TextureFilter, TextureOptions, TextureWrapMode, Vec2};
use super::{CompGetter, CompUi};
@@ -43,19 +43,24 @@ impl CompUi for Toast {
ToastType::Info => {
color = theme.accent_color;
img = Image::new(crate::data::INFO_ICON)
.max_size(Vec2::new(16.0, 16.0))
.fit_to_exact_size(Vec2::new(16.0, 16.0))
.tint(color);
}
ToastType::Warn => {
color = crate::data::C_WARN;
img = Image::new(crate::data::WARN_ICON)
.max_size(Vec2::new(16.0, 16.0))
.fit_to_exact_size(Vec2::new(16.0, 16.0))
.texture_options(TextureOptions {
magnification: TextureFilter::Linear,
minification: TextureFilter::Linear,
wrap_mode: TextureWrapMode::ClampToEdge,
})
.tint(color);
}
ToastType::Error => {
color = Color32::LIGHT_RED;
img = Image::new(crate::data::ERROR_ICON)
.max_size(Vec2::new(16.0, 16.0))
.fit_to_exact_size(Vec2::new(16.0, 16.0))
.tint(color);
}
}

View File

@@ -1,9 +1,19 @@
use std::path::PathBuf;
use egui::TextBuffer;
use xmpd_manifest::store::{JsonStore, TomlStore};
use crate::windows::WindowId;
use super::CompUi;
use super::{CompGetter, CompUi};
#[derive(Debug, Default)]
pub struct TopNav;
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<()> {
@@ -22,6 +32,25 @@ impl CompUi for TopNav {
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() {
@@ -39,7 +68,20 @@ impl CompUi for TopNav {
});
});
Ok(())
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(())
}
}

View File

@@ -36,11 +36,13 @@ pub fn start() -> Result<()> {
pub struct GuiState {
pub manifest: Manifest<JsonStore>,
pub windows: windows::Windows,
pub player: xmpd_player::Player,
}
impl GuiState {
pub fn new() -> Result<Self> {
Ok(Self {
player: xmpd_player::Player::new(),
manifest: Manifest::new(&xmpd_cliargs::CLIARGS.manifest_path())?,
windows: windows::Windows::new(),
})

View File

@@ -35,3 +35,14 @@ macro_rules! handle_error_ui {
}
};
}
macro_rules! handle_option {
($reason:expr, $val:expr) => {
if let Some(v) = $val {
v
} else {
handle_error_ui!(Err(anyhow::anyhow!($reason)));
return;
}
};
}