"manage sounds" button; infinitely looping audio

This commit is contained in:
Max Bradbury 2020-09-23 12:10:03 +01:00
parent 40c100e65b
commit 755b5769ac
1 changed files with 24 additions and 12 deletions

View File

@ -1,13 +1,27 @@
use gio::prelude::*;
use gtk::prelude::*;
use gtk::Orientation;
use rodio::Sink;
use rodio::{Sink, Source};
use std::env::args;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::process::Command;
const SPACING: i32 = 16;
fn get_data_dir() -> PathBuf {
let mut data_dir = dirs::data_dir().expect("Couldn't find user data directory");
data_dir.push("ruin/lull");
if !data_dir.exists() {
std::fs::create_dir_all(&data_dir).expect("Couldn't create lull data directory");
}
data_dir
}
fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::new(application);
@ -23,15 +37,7 @@ fn build_ui(application: &gtk::Application) {
let device = rodio::default_output_device().unwrap();
let mut data_dir = dirs::data_dir().expect("Couldn't find user data directory");
data_dir.push("ruin/lull");
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)
let paths = std::fs::read_dir(get_data_dir())
.expect("Couldn't read from lull data directory");
for path in paths {
@ -44,6 +50,8 @@ fn build_ui(application: &gtk::Application) {
let source = rodio::Decoder::new(BufReader::new(file))
.expect("Couldn't decode audio");
let source = source.repeat_infinite();
let sink = Sink::new(&device);
sink.append(source);
sink.pause();
@ -72,7 +80,6 @@ fn build_ui(application: &gtk::Application) {
slider.connect_value_changed(move |scale| {
let volume = scale.get_value();
println!("volume: {}", volume);
if volume == 0. {
sink.pause();
@ -90,7 +97,12 @@ fn build_ui(application: &gtk::Application) {
let row_add = gtk::Box::new(Orientation::Horizontal, SPACING);
row_add.set_homogeneous(true);
let button_open_dir = gtk::Button::with_label("add more");
let button_open_dir = gtk::Button::with_label("manage sounds");
button_open_dir.connect_clicked(|_|{
let mut file_manager = Command::new("xdg-open");
file_manager.arg(get_data_dir());
file_manager.output().unwrap();
});
row_add.add(&button_open_dir);
vertical.add(&row_add);