From 6235d564e406bb4ac9c8882801e51d1865fd654c Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Tue, 22 Sep 2020 19:20:03 +0100 Subject: [PATCH] load audio from user data dir and create UI --- Cargo.toml | 1 + src/main.rs | 75 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 61 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6437aa7..d3c6c79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ authors = ["Max Bradbury "] edition = "2018" [dependencies] +dirs = "^3.0.1" gio = "^0" gtk = "^0" rodio = "^0.11.0" diff --git a/src/main.rs b/src/main.rs index a1e4b16..2f79ee9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,29 +14,74 @@ fn build_ui(application: >k::Application) { window.set_title("lull"); window.set_border_width(SPACING as u32); window.set_position(gtk::WindowPosition::Center); - window.set_default_size(400, 100); + window.set_default_size(256, 256); let vertical = gtk::Box::new(Orientation::Vertical, SPACING); + vertical.set_homogeneous(true); + window.add(&vertical); - // todo create user dir if it does not exist - - // todo open user dir in file browser if it is empty - - // todo iterate over audio files in user dir - // todo create row for each audio file - - window.show_all(); - let device = rodio::default_output_device().unwrap(); - let file = File::open("/home/max/being-harsh.mp3") - .expect("Couldn't open audio file"); + let mut data_dir = dirs::data_dir().expect("Couldn't find user data directory"); - let source = rodio::Decoder::new(BufReader::new(file)) - .expect("Couldn't decode audio"); + data_dir.push("ruin/lull"); - rodio::play_raw(&device, source.convert_samples()); + if !data_dir.exists() { + std::fs::create_dir_all(&data_dir).expect("Couldn't create lull data directory"); + } + + let paths = std::fs::read_dir(&data_dir) + .expect("Couldn't read from lull data directory"); + + for path in paths { + let path = path.unwrap().path(); + let name: &str = path.file_stem().unwrap().to_str().unwrap(); + + let file = File::open(&path) + .expect("Couldn't open audio file"); + + let source = rodio::Decoder::new(BufReader::new(file)) + .expect("Couldn't decode audio"); + + rodio::play_raw(&device, source.convert_samples()); + + let row = gtk::Box::new(Orientation::Horizontal, SPACING); + row.set_homogeneous(true); + + let label = gtk::Label::new(Some(name)); + row.add(&label); + + let adjustment = gtk::Adjustment::new( + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0 + ); + + let slider = gtk::Scale::new( + Orientation::Horizontal, + Some(&adjustment) + ); + + slider.set_draw_value(false); + + row.add(&slider); + + vertical.add(&row); + } + + let row_add = gtk::Box::new(Orientation::Horizontal, SPACING); + row_add.set_homogeneous(true); + + let button_open_dir = gtk::Button::with_label("add more"); + row_add.add(&button_open_dir); + + vertical.add(&row_add); + + window.show_all(); } fn main() {