Skip to content

Commit

Permalink
Never see ghost again
Browse files Browse the repository at this point in the history
  • Loading branch information
BonnyAD9 committed Sep 19, 2023
1 parent 3ed1f6b commit 032ee6d
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
1 change: 1 addition & 0 deletions UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
- Numbering in playlist starts from 1
- Songs are now loaded with correct time
- Some button text may not be fully shown
- No more ghosts
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl UampApp {

let mut player = Player::from_config(sender.clone(), &conf);
player.load_config(&conf);
player.remove_deleted(&lib);

let listener = if conf.enable_server() {
match Self::start_server(&conf) {
Expand Down
6 changes: 4 additions & 2 deletions src/config/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum Message {
ShuffleCurrent(bool),
ShowHelp(bool),
PreviousTimeout(Option<Duration>),
ShowRemainingTime(bool)
ShowRemainingTime(bool),
}

#[derive(Clone, Debug, Copy)]
Expand Down Expand Up @@ -324,7 +324,9 @@ impl UampApp {
.previous_timeout_set(config::default_previous_timeout());
}
DefMessage::ShowRemainingTime => {
self.config.show_remaining_time_set(config::default_show_remaining_time());
self.config.show_remaining_time_set(
config::default_show_remaining_time(),
);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,15 @@ impl UampApp {

fn bottom_menu(&self) -> Element {
let song = self.player.now_playing().map(|s| &self.library[s]);
let title = song.map(|s| s.title()).unwrap_or("-");
let artist = song.map(|s| s.artist()).unwrap_or("-");
let (title, artist) = if let Some(s) = song {
if s.is_deleted() {
("-", "-")
} else {
(s.title(), s.artist())
}
} else {
("-", "-")
};

container(grid![
Relative(2.), Relative(1.), Fixed(210.), Relative(1.), Relative(2.);
Expand Down
26 changes: 18 additions & 8 deletions src/gui/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ impl UampApp {
.style(Container::Dark),
wrap_box(
(0..songs.len())
.map(|i| self.song_list_item(i, songs.clone(), numbered))
.filter_map(|i| self.song_list_item(
i,
songs.clone(),
numbered
))
.collect(),
state,
)
Expand All @@ -134,7 +138,7 @@ impl UampApp {
song: usize,
songs: Arc<[SongId]>,
numbered: bool,
) -> Element<'static> {
) -> Option<Element<'static>> {
let text_style = if Some(songs[song]) == self.player.now_playing() {
Text::Contrast
} else {
Expand All @@ -143,6 +147,10 @@ impl UampApp {

let s = &self.library[songs[song]];

if s.is_deleted() {
return None;
}

fn top_text<'a, S>(s: S, portion: u16, style: Text) -> Element<'a>
where
S: Into<Cow<'a, str>>,
Expand Down Expand Up @@ -197,13 +205,15 @@ impl UampApp {
info.padding([0, 10, 0, 10]).into()
};

border(
button(cursor_grad(item).style(CursorGrad::Long))
.padding(0)
.on_press(Msg::PlaySong(song, songs)),
Some(
border(
button(cursor_grad(item).style(CursorGrad::Long))
.padding(0)
.on_press(Msg::PlaySong(song, songs)),
)
.style(Border::SongItem)
.into(),
)
.style(Border::SongItem)
.into()
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/gui/settings/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ pub const SHOW_REMAINING_TIME: SetHelp = SetHelp {
value_type: Some("bool"),
default_value: Some("false"),
reset_message: Some(DefMessage::ShowRemainingTime),
description: "When enabled, the time to the right of the seek slider will \
show the remaining time instead of the total time."
description:
"When enabled, the time to the right of the seek slider will \
show the remaining time instead of the total time.",
};

impl SetHelp {
Expand Down
8 changes: 8 additions & 0 deletions src/player/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ impl Player {
error!("Failed to seek: {e}");
}
}

pub fn remove_deleted(&mut self, lib: &Library) {
let cur = self.now_playing();
self.playlist_mut().remove_deleted(lib);
if let Some(cur) = cur {
self.current_set(self.playlist.iter().position(|i| i == &cur));
}
}
}

impl UampApp {
Expand Down
17 changes: 16 additions & 1 deletion src/player/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use std::{
sync::Arc,
};

use itertools::Itertools;
use serde::{Deserialize, Serialize};

use crate::library::SongId;
use crate::library::{Library, SongId};

/// A playlist, lazily cloned
pub enum Playlist {
Expand Down Expand Up @@ -42,6 +43,20 @@ impl Playlist {
Playlist::Dynamic(v) => v[..].into(),
}
}

pub fn remove_deleted(&mut self, lib: &Library) {
match self {
Playlist::Static(a) => {
*self = Playlist::Dynamic(
a.iter()
.map(|s| *s)
.filter(|s| !lib[*s].is_deleted())
.collect_vec(),
)
}
Playlist::Dynamic(v) => v.retain(|s| !lib[*s].is_deleted()),
}
}
}

impl Default for Playlist {
Expand Down

0 comments on commit 032ee6d

Please sign in to comment.