lull/src/ui/egui.rs

80 lines
2.2 KiB
Rust

use eframe::{egui, epi};
pub(crate) use crate::{Sound, get_sounds, open_file_manager};
use egui::{Color32, Stroke, Vec2};
const COLOUR_BG: Color32 = Color32::from_rgb(76, 58, 78);
const COLOUR_FG: Color32 = Color32::from_rgb(156, 143, 109);
pub struct State {
device: rodio::Device,
sounds: Vec<Sound>,
}
impl Default for State {
fn default() -> Self {
let device = rodio::default_output_device().unwrap();
let sounds = get_sounds(&device);
Self { device, sounds }
}
}
impl epi::App for State {
fn update(&mut self, ctx: &egui::CtxRef, _frame: &mut epi::Frame<'_>) {
let State { device: _device, sounds} = self;
for sound in sounds.into_iter() {
if sound.volume > 0.0 {
sound.sink.set_volume(sound.volume as f32);
sound.sink.play();
} else {
sound.sink.pause();
}
}
egui::CentralPanel::default().frame(egui::Frame {
margin: Vec2 { x: crate::SPACING as f32, y: crate::SPACING as f32 },
corner_radius: 0.0,
shadow: Default::default(),
fill: COLOUR_BG,
stroke: Stroke {
width: 1.0,
color: COLOUR_FG,
}
}).show(ctx, |ui| {
egui::Grid::new("grid")
.spacing([crate::SPACING as f32; 2])
.show(ui, |ui| {
for sound in sounds {
ui.add(
egui::Label::new(&sound.name).text_color(COLOUR_FG)
);
ui.add(
egui::Slider::new(
&mut sound.volume,
0.0..=1.0
).show_value(false)
);
ui.end_row();
}
if ui.add(
egui::Button::new(crate::SOUNDS_BUTTON_LABEL)
.text_color(COLOUR_FG)
).clicked() {
open_file_manager();
}
ui.end_row();
});
});
}
fn name(&self) -> &str {
"lull"
}
}