Added displaying of currently playing song
parent
3f15bad5f3
commit
2d142e0657
|
|
@ -1,5 +1,6 @@
|
|||
use std::{fs::File, path::PathBuf};
|
||||
|
||||
use rmp::PlaylistElement;
|
||||
use rodio::{OutputStream, OutputStreamHandle, Sink};
|
||||
|
||||
use super::decoder::{Decoder, DecoderImpl, SourceWrapper};
|
||||
|
|
@ -9,12 +10,15 @@ pub struct Playback {
|
|||
_stream: OutputStream,
|
||||
_stream_handle: OutputStreamHandle,
|
||||
sink: Sink,
|
||||
pub current: Option<Player>,
|
||||
}
|
||||
|
||||
pub enum PlaybackState {
|
||||
Empty,
|
||||
Playing,
|
||||
Paused,
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Player {
|
||||
pub track: PlaylistElement,
|
||||
pub is_paused: bool,
|
||||
pub duration: Option<f32>,
|
||||
pub position: Option<f32>,
|
||||
}
|
||||
|
||||
impl Playback {
|
||||
|
|
@ -26,17 +30,7 @@ impl Playback {
|
|||
_stream,
|
||||
_stream_handle,
|
||||
sink,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_state(&self) -> PlaybackState {
|
||||
// TODO: We know if it's playing but not what is playing. Figure it out later
|
||||
if self.sink.empty() {
|
||||
return PlaybackState::Empty;
|
||||
}
|
||||
match self.sink.is_paused() {
|
||||
true => PlaybackState::Paused,
|
||||
false => PlaybackState::Playing,
|
||||
current: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +54,12 @@ impl Playback {
|
|||
|
||||
match source {
|
||||
Some(source) => {
|
||||
self.current = Some(Player {
|
||||
track: path,
|
||||
is_paused: false,
|
||||
duration: source.total_duration().map(|dur| dur.as_secs_f32()),
|
||||
position: None,
|
||||
});
|
||||
self.sink.clear();
|
||||
self.sink.append(source);
|
||||
self.sink.play();
|
||||
|
|
@ -70,10 +70,11 @@ impl Playback {
|
|||
|
||||
/// Toggle playback pause if possible.
|
||||
pub fn toggle_pause(&mut self) {
|
||||
match self.get_state() {
|
||||
PlaybackState::Empty => (),
|
||||
PlaybackState::Playing => self.sink.pause(),
|
||||
PlaybackState::Paused => self.sink.play(),
|
||||
if let Some(current) = &self.current {
|
||||
match current.is_paused {
|
||||
true => self.sink.play(),
|
||||
false => self.sink.pause(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
src/ui.rs
11
src/ui.rs
|
|
@ -33,12 +33,11 @@ fn draw_playlist<B: Backend>(f: &mut Frame<B>, app: &App, area: Rect) {
|
|||
.state
|
||||
.selected()
|
||||
.map_or(false, |selected| index == selected);
|
||||
// let playing = app
|
||||
// .player_state
|
||||
// .currently_playing
|
||||
// .clone()
|
||||
// .map_or(false, |currently_playing| currently_playing == *path);
|
||||
let playing = false;
|
||||
let playing = app
|
||||
.playback
|
||||
.current
|
||||
.clone()
|
||||
.map_or(false, |current| current.track == *path);
|
||||
let mut style = Style::default();
|
||||
match (selected, playing) {
|
||||
(true, false) => {
|
||||
|
|
|
|||
Loading…
Reference in New Issue