Skip to content

Commit

Permalink
feat: Highlight now playing song in playlist (#156)
Browse files Browse the repository at this point in the history
* Add playing indicator to playlist

* Implement highlight selected row
  • Loading branch information
nick42d authored Aug 29, 2024
1 parent cb5345c commit 87bc21f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions youtui/src/app/ui/browser/artistalbums/albumsongs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,10 @@ impl TableView for AlbumSongsPanel {
fn get_headings(&self) -> Box<(dyn Iterator<Item = &'static str> + 'static)> {
Box::new(["#", "Album", "Song", "Duration", "Year"].into_iter())
}

fn get_highlighted_row(&self) -> Option<usize> {
None
}
}
impl SortableTableView for AlbumSongsPanel {
fn get_sortable_columns(&self) -> &[usize] {
Expand Down
2 changes: 2 additions & 0 deletions youtui/src/app/ui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ fn draw_help(f: &mut Frame, w: &YoutuiWindow, state: &mut TableState, chunk: Rec
);
}

// At this stage, this is the most efficient way to call this function.
#[allow(clippy::too_many_arguments)]
fn draw_generic_scrollable_table<'a, T: IntoIterator<Item = Row<'a>>>(
f: &mut Frame,
table_items: T,
Expand Down
17 changes: 16 additions & 1 deletion youtui/src/app/ui/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,19 @@ impl TableView for Playlist {
}
fn get_items(&self) -> Box<dyn ExactSizeIterator<Item = TableItem> + '_> {
Box::new(self.list.get_list_iter().enumerate().map(|(i, ls)| {
Box::new(iter::once((i + 1).to_string().into()).chain(ls.get_fields_iter()))
let first_field = if Some(i) == self.get_cur_playing_index() {
match self.play_status {
PlayState::NotPlaying => ">>>".to_string(),
PlayState::Playing(_) => "".to_string(),
PlayState::Paused(_) => "".to_string(),
PlayState::Stopped => ">>>".to_string(),
PlayState::Error(_) => ">>>".to_string(),
PlayState::Buffering(_) => "".to_string(),
}
} else {
(i + 1).to_string()
};
Box::new(iter::once(first_field.to_string().into()).chain(ls.get_fields_iter()))
as Box<dyn Iterator<Item = Cow<str>>>
}))
}
Expand All @@ -161,6 +173,9 @@ impl TableView for Playlist {
.into_iter(),
)
}
fn get_highlighted_row(&self) -> Option<usize> {
self.get_cur_playing_index()
}
}

impl ActionHandler<PlaylistAction> for Playlist {
Expand Down
4 changes: 3 additions & 1 deletion youtui/src/app/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ pub trait TableView: Scrollable + Loadable {
// need for this in both Table and List
fn get_title(&self) -> Cow<str>;
fn get_layout(&self) -> &[BasicConstraint];
// A row can be highlighted.
fn get_highlighted_row(&self) -> Option<usize>;
// TODO: Consider if generics <T: Iterator> can be used instead of dyn Iterator.
fn get_items(&self) -> Box<dyn ExactSizeIterator<Item = TableItem> + '_>;
// XXX: This doesn't need to be so fancy - could return a static slice.
fn get_headings(&self) -> Box<dyn Iterator<Item = &'static str>>;
// Not a particularyl useful function for a sortabletableview
// Not a particularly useful function for a sortabletableview
fn len(&self) -> usize {
self.get_items().len()
}
Expand Down
11 changes: 9 additions & 2 deletions youtui/src/app/view/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
};
use ratatui::{
prelude::{Margin, Rect},
style::{Modifier, Style},
style::{Modifier, Style, Stylize},
symbols::{block, line},
text::Line,
widgets::{
Expand Down Expand Up @@ -108,8 +108,15 @@ where
{
// Set the state to the currently selected item.
state.select(Some(table.get_selected_item()));
let cur_highlighted = table.get_highlighted_row();
// TODO: theming
let table_items = table.get_items().map(Row::new);
let table_items = table.get_items().enumerate().map(|(idx, items)| {
if Some(idx) == cur_highlighted {
Row::new(items).bold().italic()
} else {
Row::new(items)
}
});
let number_items = table.len();
// Minus for height of block and heading.
let table_height = chunk.height.saturating_sub(4) as usize;
Expand Down

0 comments on commit 87bc21f

Please sign in to comment.