update client + remake web-ui (server, incomplete)

This commit is contained in:
Mark 2024-05-15 22:58:30 +02:00
parent 55e0e02622
commit 6eb884e7a5
29 changed files with 584 additions and 843 deletions

View File

@ -33,6 +33,7 @@ use crate::{
gui_edit_song::EditorForSongs, gui_edit_song::EditorForSongs,
gui_notif::{NotifInfo, NotifOverlay}, gui_notif::{NotifInfo, NotifOverlay},
gui_screen::GuiScreen, gui_screen::GuiScreen,
gui_song_adder::SongAdder,
gui_text::Label, gui_text::Label,
textcfg, textcfg,
}; };
@ -1127,6 +1128,8 @@ pub enum GuiAction {
EditSongs(Vec<Song>), EditSongs(Vec<Song>),
// EditAlbums(Vec<Album>), // EditAlbums(Vec<Album>),
// EditArtists(Vec<Artist>), // EditArtists(Vec<Artist>),
OpenAddSongsMenu,
CloseAddSongsMenu,
} }
pub enum Dragging { pub enum Dragging {
Artist(ArtistId), Artist(ArtistId),
@ -1307,6 +1310,19 @@ impl Gui {
GuiAction::EditSongs(songs) => { GuiAction::EditSongs(songs) => {
self.gui.c_editing_songs = Some(EditorForSongs::new(songs)); self.gui.c_editing_songs = Some(EditorForSongs::new(songs));
} }
GuiAction::OpenAddSongsMenu => {
if self.gui.c_song_adder.is_none() {
self.gui.c_song_adder = Some(SongAdder::new(
GuiElemCfg::default(),
self.high_performance,
self.line_height,
self.scroll_pixels_multiplier,
self.scroll_lines_multiplier,
self.scroll_pages_multiplier,
));
}
}
GuiAction::CloseAddSongsMenu => self.gui.c_song_adder = None,
} }
} }
} }

View File

@ -19,6 +19,7 @@ use crate::{
gui_notif::NotifOverlay, gui_notif::NotifOverlay,
gui_queue::QueueViewer, gui_queue::QueueViewer,
gui_settings::Settings, gui_settings::Settings,
gui_song_adder::SongAdder,
gui_statusbar::StatusBar, gui_statusbar::StatusBar,
gui_text::Label, gui_text::Label,
gui_wrappers::Hotkey, gui_wrappers::Hotkey,
@ -49,6 +50,7 @@ pub struct GuiScreen {
pub c_editing_songs: Option<EditorForSongs>, pub c_editing_songs: Option<EditorForSongs>,
pub c_status_bar: StatusBar, pub c_status_bar: StatusBar,
pub c_settings: Settings, pub c_settings: Settings,
pub c_song_adder: Option<SongAdder>,
pub c_main_view: Panel<MainView>, pub c_main_view: Panel<MainView>,
pub c_context_menu: Option<Box<dyn GuiElem>>, pub c_context_menu: Option<Box<dyn GuiElem>>,
pub idle: AnimationController<f32>, pub idle: AnimationController<f32>,
@ -110,6 +112,7 @@ impl GuiScreen {
scroll_sensitivity_lines, scroll_sensitivity_lines,
scroll_sensitivity_pages, scroll_sensitivity_pages,
), ),
c_song_adder: None,
c_main_view: Panel::new( c_main_view: Panel::new(
GuiElemCfg::at(Rectangle::from_tuples((0.0, 0.0), (1.0, 0.9))), GuiElemCfg::at(Rectangle::from_tuples((0.0, 0.0), (1.0, 0.9))),
MainView { MainView {
@ -248,6 +251,7 @@ impl GuiElem for GuiScreen {
] ]
.into_iter() .into_iter()
.chain(self.c_editing_songs.as_mut().map(|v| v.elem_mut())) .chain(self.c_editing_songs.as_mut().map(|v| v.elem_mut()))
.chain(self.c_song_adder.as_mut().map(|v| v.elem_mut()).into_iter())
.chain([ .chain([
self.c_status_bar.elem_mut(), self.c_status_bar.elem_mut(),
self.c_settings.elem_mut(), self.c_settings.elem_mut(),

View File

@ -68,6 +68,7 @@ pub struct SettingsContent {
pub scroll_sensitivity: Panel<(Label, Slider)>, pub scroll_sensitivity: Panel<(Label, Slider)>,
pub idle_time: Panel<(Label, Slider)>, pub idle_time: Panel<(Label, Slider)>,
pub save_button: Button<[Label; 1]>, pub save_button: Button<[Label; 1]>,
pub add_new_songs_button: Button<[Label; 1]>,
pub keybinds: Vec<Panel<(AdvancedLabel, KeybindInput)>>, pub keybinds: Vec<Panel<(AdvancedLabel, KeybindInput)>>,
pub keybinds_should_be_updated: Arc<AtomicBool>, pub keybinds_should_be_updated: Arc<AtomicBool>,
pub keybinds_updated: bool, pub keybinds_updated: bool,
@ -84,13 +85,14 @@ impl GuiElemChildren for SettingsContent {
self.scroll_sensitivity.elem_mut(), self.scroll_sensitivity.elem_mut(),
self.idle_time.elem_mut(), self.idle_time.elem_mut(),
self.save_button.elem_mut(), self.save_button.elem_mut(),
self.add_new_songs_button.elem_mut(),
] ]
.into_iter() .into_iter()
.chain(self.keybinds.iter_mut().map(|v| v.elem_mut())), .chain(self.keybinds.iter_mut().map(|v| v.elem_mut())),
) )
} }
fn len(&self) -> usize { fn len(&self) -> usize {
7 + self.keybinds.len() 8 + self.keybinds.len()
} }
} }
pub struct KeybindInput { pub struct KeybindInput {
@ -455,6 +457,17 @@ impl SettingsContent {
Vec2::new(0.5, 0.5), Vec2::new(0.5, 0.5),
)], )],
), ),
add_new_songs_button: Button::new(
GuiElemCfg::default(),
|_| vec![GuiAction::OpenAddSongsMenu],
[Label::new(
GuiElemCfg::default(),
"search for new songs".to_string(),
Color::WHITE,
None,
Vec2::new(0.5, 0.5),
)],
),
keybinds: vec![], keybinds: vec![],
keybinds_should_be_updated: Arc::new(AtomicBool::new(true)), keybinds_should_be_updated: Arc::new(AtomicBool::new(true)),
keybinds_updated: false, keybinds_updated: false,
@ -537,7 +550,7 @@ impl GuiElem for Settings {
scrollbox.config_mut().redraw = true; scrollbox.config_mut().redraw = true;
if scrollbox.children_heights.len() == scrollbox.children.len() { if scrollbox.children_heights.len() == scrollbox.children.len() {
for (i, h) in scrollbox.children_heights.iter_mut().enumerate() { for (i, h) in scrollbox.children_heights.iter_mut().enumerate() {
*h = if i == 0 || i >= 7 { *h = if i == 0 || i >= 8 {
info.line_height * 2.0 info.line_height * 2.0
} else { } else {
info.line_height info.line_height

View File

@ -0,0 +1,187 @@
use musicdb_lib::data::{AlbumId, ArtistId};
use speedy2d::{color::Color, dimen::Vec2, Graphics2D};
use crate::{
gui::{DrawInfo, GuiElem, GuiElemCfg},
gui_base::{Button, Panel, ScrollBox},
gui_text::Label,
};
pub struct SongAdder {
pub config: GuiElemCfg,
state: u8,
c_loading: Option<Label>,
c_scroll_box: ScrollBox<Vec<AddableSong>>,
c_background: Panel<()>,
data: Option<Vec<AddSong>>,
}
struct AddSong {
path: String,
path_broken: bool,
artist: Option<ArtistId>,
album: Option<AlbumId>,
}
impl SongAdder {
pub fn new(
mut config: GuiElemCfg,
no_animations: bool,
line_height: f32,
scroll_sensitivity_pixels: f64,
scroll_sensitivity_lines: f64,
scroll_sensitivity_pages: f64,
) -> Self {
config.redraw = true;
Self {
config,
state: 0,
c_loading: Some(Label::new(
GuiElemCfg::default(),
format!("Loading..."),
Color::GRAY,
None,
Vec2::new(0.5, 0.5),
)),
c_scroll_box: ScrollBox::new(
GuiElemCfg::default(),
crate::gui_base::ScrollBoxSizeUnit::Pixels,
vec![],
vec![],
line_height,
),
c_background: Panel::with_background(GuiElemCfg::default().w_mouse(), (), Color::BLACK),
data: None,
}
}
}
impl GuiElem for SongAdder {
fn config(&self) -> &GuiElemCfg {
&self.config
}
fn config_mut(&mut self) -> &mut GuiElemCfg {
&mut self.config
}
fn children(&mut self) -> Box<dyn Iterator<Item = &mut dyn GuiElem> + '_> {
Box::new(
[
self.c_loading.as_mut().map(|v| v.elem_mut()),
Some(self.c_scroll_box.elem_mut()),
Some(self.c_background.elem_mut()),
]
.into_iter()
.flatten(),
)
}
fn any(&self) -> &dyn std::any::Any {
self
}
fn any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn elem(&self) -> &dyn GuiElem {
self
}
fn elem_mut(&mut self) -> &mut dyn GuiElem {
self
}
fn draw(&mut self, info: &mut DrawInfo, _g: &mut Graphics2D) {
if self.state < 10 {
self.state += 1;
if self.state == 2 {
self.c_loading = None;
eprintln!("Locking GetCon...");
let mut get_con = info.get_con.lock().unwrap();
eprintln!("Requesting list of unused songs...");
match get_con.find_unused_song_files(None).unwrap() {
Ok(data) => {
eprintln!("Got list of songs.");
self.c_scroll_box.children = data
.iter()
.map(|(path, is_bad)| AddableSong::new(path.to_owned(), *is_bad))
.collect();
self.c_scroll_box.config_mut().redraw = true;
self.data = Some(
data.into_iter()
.map(|(p, b)| AddSong {
path: p,
path_broken: b,
artist: None,
album: None,
})
.collect(),
);
}
Err(e) => {
eprintln!("Got error: {e}");
self.c_loading = Some(Label::new(
GuiElemCfg::default(),
format!("Error:\n{e}"),
Color::RED,
None,
Vec2::new(0.5, 0.5),
));
}
}
}
}
if self.config.redraw {
self.config.redraw = false;
self.c_scroll_box.config_mut().redraw = true;
}
}
}
struct AddableSong {
pub config: GuiElemCfg,
pub c_button: Button<[Label; 1]>,
pub path: String,
pub is_bad: bool,
}
impl AddableSong {
pub fn new(path: String, is_bad: bool) -> Self {
Self {
config: GuiElemCfg::default(),
c_button: Button::new(
GuiElemCfg::default(),
|_| vec![],
[Label::new(
GuiElemCfg::default(),
format!("{path}"),
if is_bad {
Color::LIGHT_GRAY
} else {
Color::WHITE
},
None,
Vec2::new(0.0, 0.5),
)],
),
path,
is_bad,
}
}
}
impl GuiElem for AddableSong {
fn config(&self) -> &GuiElemCfg {
&self.config
}
fn config_mut(&mut self) -> &mut GuiElemCfg {
&mut self.config
}
fn children(&mut self) -> Box<dyn Iterator<Item = &mut dyn GuiElem> + '_> {
Box::new([self.c_button.elem_mut()].into_iter())
}
fn any(&self) -> &dyn std::any::Any {
self
}
fn any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn elem(&self) -> &dyn GuiElem {
self
}
fn elem_mut(&mut self) -> &mut dyn GuiElem {
self
}
}

View File

@ -46,6 +46,8 @@ mod gui_screen;
#[cfg(feature = "speedy2d")] #[cfg(feature = "speedy2d")]
mod gui_settings; mod gui_settings;
#[cfg(feature = "speedy2d")] #[cfg(feature = "speedy2d")]
mod gui_song_adder;
#[cfg(feature = "speedy2d")]
mod gui_statusbar; mod gui_statusbar;
#[cfg(feature = "speedy2d")] #[cfg(feature = "speedy2d")]
mod gui_text; mod gui_text;

14
musicdb-server/Cargo.toml Executable file → Normal file
View File

@ -7,14 +7,14 @@ edition = "2021"
[dependencies] [dependencies]
musicdb-lib = { path = "../musicdb-lib", features = ["playback"] } musicdb-lib = { path = "../musicdb-lib", features = ["playback"] }
axum = { version = "0.6.19", features = ["headers"] }
clap = { version = "4.4.6", features = ["derive"] } clap = { version = "4.4.6", features = ["derive"] }
futures = "0.3.28"
headers = "0.3.8" headers = "0.3.8"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
tokio = { version = "1.0", features = ["full"] } tokio = { version = "1.37.0", optional = true, features = ["rt"] }
tokio-stream = "0.1.14" rocket = { version = "0.5.0", optional = true }
tower = { version = "0.4", features = ["util"] } html-escape = { version = "0.2.13", optional = true }
tower-http = { version = "0.4.0", features = ["fs", "trace"] }
trace = "0.1.7" [features]
default = ["website"]
website = ["dep:tokio", "dep:rocket", "dep:html-escape"]

View File

@ -0,0 +1,6 @@
[build]
pre-build = [
"dpkg --add-architecture $CROSS_DEB_ARCH",
"apt-get update && apt-get --assume-yes install libasound2-dev libasound2-dev:$CROSS_DEB_ARCH"
]
default-target = "aarch64-unknown-linux-gnu"

View File

@ -1,3 +0,0 @@
<h3>\:name</h3>
<button hx-post="/queue/add-album/\:id" hx-swap="none">Queue</button>
\:songs

View File

@ -1 +0,0 @@
<button hx-get="/album-view/\:id" hx-target="#album-view">\:name</button>

View File

@ -1,2 +0,0 @@
<h3>\:name</h3>
\:albums

View File

@ -1,2 +0,0 @@
<h3>Artists</h3>
\:artists

View File

@ -1 +0,0 @@
<button hx-get="/artist-view/\:id" hx-target="#artist-view">\:name</button>

View File

@ -1,3 +0,0 @@
<h2>Queue</h2>
<div>Now Playing: <b>\:currentTitle</b></div>
\:content

View File

@ -1,11 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small>\:name</small>
\?shuffled? (shuffled)\;\;
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,11 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small><b>\:name</b></small>
\?shuffled? (shuffled)\;\;
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small>repeat \:total times</small>
</div>
\:inner
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small><b>repeat \:total times</b></small>
</div>
\:inner
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small>repeat forever</small>
</div>
\:inner
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small><b>repeat forever</b></small>
</div>
\:inner
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small>random</small>
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small><b>random</b></small>
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small>shuffle</small>
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,10 +0,0 @@
<div>
<small>&gt;&gt;</small>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<small><b>shuffle</b></small>
</div>
\:content
<div>
<small>&lt;&lt;</small>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
</div>

View File

@ -1,5 +0,0 @@
<div>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
\:title
</div>

View File

@ -1,5 +0,0 @@
<div>
<button hx-post="/queue/remove/\:path" hx-swap="none">x</button>
<button hx-post="/queue/goto/\:path" hx-swap="none">&#x23F5;</button>
<b>\:title</b>
</div>

View File

@ -1,24 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="light dark">
<script src="https://unpkg.com/htmx.org@1.9.3"></script>
<title>MusicDb</title>
</head>
<body>
<div hx-sse="connect:/sse">
<div hx-sse="swap:playing">(loading)</div>
<button hx-post="/resume" hx-swap="none">&#x23F5;</button>
<button hx-post="/pause" hx-swap="none">&#x23F8;</button>
<button hx-post="/stop" hx-swap="none">&#x23F9;</button>
<button hx-post="/next" hx-swap="none">&#x23ED;</button>
<button hx-post="/queue/clear" hx-swap="none">-</button>
<div hx-sse="swap:queue">(loading)</div>
<div hx-sse="swap:artists">(loading)</div>
<div id="artist-view"></div>
<div id="album-view"></div>
</div>
</body>
</html>

View File

@ -1 +0,0 @@
<button hx-post="/queue/add-song/\:id" hx-swap="none">\:title</button>

View File

@ -1,3 +1,4 @@
#[cfg(feature = "website")]
mod web; mod web;
use std::{ use std::{
@ -5,7 +6,6 @@ use std::{
path::PathBuf, path::PathBuf,
process::exit, process::exit,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread,
}; };
use clap::Parser; use clap::Parser;
@ -47,8 +47,7 @@ struct Args {
advanced_cache_song_lookahead_limit: u32, advanced_cache_song_lookahead_limit: u32,
} }
#[tokio::main] fn main() {
async fn main() {
// parse args // parse args
let args = Args::parse(); let args = Args::parse();
let mut database = if args.init { let mut database = if args.init {
@ -88,11 +87,25 @@ async fn main() {
); );
}; };
if let Some(addr) = &args.web { if let Some(addr) = &args.web {
let (s, mut r) = tokio::sync::mpsc::channel(2); #[cfg(not(feature = "website"))]
let db = Arc::clone(&database); {
thread::spawn(move || run_server(database, Some(s))); let _ = addr;
if let Some(sender) = r.recv().await { eprintln!("Website support requires the 'website' feature to be enabled when compiling the server!");
web::main(db, sender, *addr).await; std::process::exit(80);
}
#[cfg(feature = "website")]
{
let (s, r) = std::sync::mpsc::sync_channel(1);
let db = Arc::clone(&database);
std::thread::spawn(move || {
run_server(database, Some(Box::new(move |c| s.send(c).unwrap())))
});
let sender = r.recv().unwrap();
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(web::main(db, sender, *addr));
} }
} else { } else {
run_server(database, None); run_server(database, None);

File diff suppressed because it is too large Load Diff