Skip to content

Commit

Permalink
playlists are scrollable now
Browse files Browse the repository at this point in the history
  • Loading branch information
Bluemi committed Jan 14, 2022
1 parent 1e4f5ec commit 253c06a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ pub fn load_playlists() -> Vec<Playlist> {
let mut playlists = Vec::new();
for entry in get_dir_entries(&playlists_directory) {
if entry.is_file {
playlists.push(Playlist::from_file(&entry.path));
if let Ok(playlist) = Playlist::from_file(&entry.path) {
playlists.push(playlist);
}
}
}
playlists
Expand All @@ -61,6 +63,7 @@ pub struct FileManagerCache {
pub struct PlaylistManagerCache {
pub view: PlaylistView,
pub shown_playlist_index: usize,
pub playlist_scroll_position: usize,
pub scroll_cursor_positions: HashMap<PlaylistID, (usize, usize)>
}

Expand Down Expand Up @@ -100,6 +103,7 @@ impl Cache {
},
playlist_manager_cache: PlaylistManagerCache {
view: PlaylistView::Overview,
playlist_scroll_position: 0,
shown_playlist_index: 0,
scroll_cursor_positions: HashMap::new(),
},
Expand Down
21 changes: 14 additions & 7 deletions src/playlist_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::collections::HashMap;

pub struct PlaylistManager {
pub shown_playlist_index: usize,
playlist_scroll_position: usize,
pub playlists: Vec<Playlist>,
pub view: PlaylistView,
scroll_cursor_positions: HashMap<PlaylistID, (usize, usize)>,
Expand All @@ -30,6 +31,7 @@ impl PlaylistManager {
pub fn new(playlists: Vec<Playlist>, cache: &PlaylistManagerCache) -> PlaylistManager {
PlaylistManager {
shown_playlist_index: cache.shown_playlist_index,
playlist_scroll_position: cache.playlist_scroll_position,
playlists,
view: cache.view,
scroll_cursor_positions: cache.scroll_cursor_positions.clone(),
Expand All @@ -39,6 +41,7 @@ impl PlaylistManager {
pub fn create_cache(&self) -> PlaylistManagerCache {
PlaylistManagerCache {
view: self.view,
playlist_scroll_position: self.playlist_scroll_position,
shown_playlist_index: self.shown_playlist_index,
scroll_cursor_positions: self.scroll_cursor_positions.clone(),
}
Expand Down Expand Up @@ -97,9 +100,8 @@ impl PlaylistManager {
pub fn move_down(&mut self, num_rows: usize) {
match self.view {
PlaylistView::Overview => {
if self.shown_playlist_index < self.playlists.len() - 1 {
self.shown_playlist_index += 1;
}
let next_index = (self.shown_playlist_index + 1).min(self.playlists.len().checked_sub(1).unwrap_or(0));
self.set_playlist_cursor_position(next_index, num_rows);
}
PlaylistView::Playlist => {
if let Some(playlist_id) = self.get_mut_shown_playlist().map(|p| p.id) {
Expand All @@ -113,9 +115,7 @@ impl PlaylistManager {
pub fn move_up(&mut self, num_rows: usize) {
match self.view {
PlaylistView::Overview => {
if self.shown_playlist_index > 0 {
self.shown_playlist_index -= 1;
}
self.set_playlist_cursor_position(self.shown_playlist_index.checked_sub(1).unwrap_or(0), num_rows);
}
PlaylistView::Playlist => {
if let Some(playlist_id) = self.get_mut_shown_playlist().map(|p| p.id) {
Expand All @@ -140,11 +140,18 @@ impl PlaylistManager {
}
}

pub fn set_playlist_cursor_position(&mut self, cursor_position: usize, num_rows: usize) {
if cursor_position < self.playlists.len() {
self.shown_playlist_index = cursor_position;
self.playlist_scroll_position = self.playlist_scroll_position.clamp(cursor_position.checked_sub(num_rows-1).unwrap_or(0), cursor_position);
}
}

pub fn get_render_object(&self, play_state: &PlayState, song_buffer: &SongBuffer) -> RenderObject {
let mut render_object = RenderObject::new(Alignment::Left);

// add overview panel
let mut overview_panel = RenderPanel::new(0);
let mut overview_panel = RenderPanel::new(self.playlist_scroll_position);
for (index, playlist) in self.playlists.iter().enumerate() {
let (foreground_color, background_color) = if play_state.is_playlist_played(index) {
if index == self.shown_playlist_index {
Expand Down
4 changes: 2 additions & 2 deletions src/song/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub struct Playlist {
}

impl Playlist {
pub fn from_file(path: &Path) -> Playlist {
pub fn from_file(path: &Path) -> Result<Playlist, serde_json::Error> {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap()
serde_json::from_reader(reader)
}

pub fn dump_to_file(&self, path: &Path) {
Expand Down

0 comments on commit 253c06a

Please sign in to comment.