| 1 | use crate::app::{App, AppResult}; |
| 2 | use ratatui::{prelude::*, widgets::*}; |
| 3 | |
| 4 | /// Renders the user interface widgets |
| 5 | pub fn render(app: &mut App, frame: &mut Frame) { |
| 6 | // This is where you add new widgets. |
| 7 | // See the following resources: |
| 8 | // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html |
| 9 | // - https://github.com/ratatui-org/ratatui/tree/master/examples |
| 10 | |
| 11 | // Layout |
| 12 | let main_layout = Layout::default() |
| 13 | .direction(Direction::Vertical) |
| 14 | .constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)]) |
| 15 | .split(frame.size()); |
| 16 | |
| 17 | let outer_layout = Layout::default() |
| 18 | .direction(Direction::Horizontal) |
| 19 | .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) |
| 20 | .split(main_layout[0]); |
| 21 | |
| 22 | let inner_layout = Layout::default() |
| 23 | .direction(Direction::Vertical) |
| 24 | .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) |
| 25 | .split(outer_layout[1]); |
| 26 | |
| 27 | draw_song_list(frame, app, outer_layout[0]); |
| 28 | draw_queue(frame, app, inner_layout[0]); |
| 29 | draw_playlists(frame, app, inner_layout[1]); |
| 30 | |
| 31 | // Status |
| 32 | // let song = app |
| 33 | // .conn |
| 34 | // .now_playing().unwrap() |
| 35 | // .unwrap_or_else(|| "No Title Found".to_string()); |
| 36 | // |
| 37 | // let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap(); |
| 38 | // |
| 39 | // let mut lines = vec![]; |
| 40 | // lines.push(Line::from(vec![ |
| 41 | // Span::styled("Current: ", Style::default().fg(Color::Red)), |
| 42 | // Span::styled(song, Style::default().fg(Color::Yellow)), |
| 43 | // Span::styled( |
| 44 | // format!("[{}/{}]", elapsed.as_secs(), total.as_secs()), |
| 45 | // Style::default().fg(Color::Yellow), |
| 46 | // ), |
| 47 | // ])); |
| 48 | // let status = Paragraph::new(Text::from(lines)) |
| 49 | // .block(Block::default().title("Status").borders(Borders::ALL)); |
| 50 | // frame.render_widget(status, main_layout[1]); |
| 51 | } |
| 52 | |
| 53 | /// draws list of songs |
| 54 | fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) { |
| 55 | let mut song_state = ListState::default(); |
| 56 | let list = List::new(app.conn.songs_filenames.clone()) |
| 57 | .block(Block::default().title("Song List").borders(Borders::ALL)) |
| 58 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 59 | .highlight_symbol(">>") |
| 60 | .repeat_highlight_symbol(true); |
| 61 | |
| 62 | song_state.select(Some(app.song_list.index)); |
| 63 | frame.render_stateful_widget(list, size, &mut song_state); |
| 64 | } |
| 65 | |
| 66 | /// draws playing queue |
| 67 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { |
| 68 | let mut queue_state = ListState::default(); |
| 69 | let list = List::new(app.play_deque.clone()) |
| 70 | .block(Block::default().title("Play Queue").borders(Borders::ALL)) |
| 71 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 72 | .highlight_symbol(">>") |
| 73 | .repeat_highlight_symbol(true); |
| 74 | |
| 75 | app.update_queue(); |
| 76 | frame.render_stateful_widget(list, size, &mut queue_state); |
| 77 | } |
| 78 | |
| 79 | /// draws all playlists |
| 80 | fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) { |
| 81 | let mut state = ListState::default(); |
| 82 | let list = List::new(app.pl_list.list.clone()) |
| 83 | .block(Block::default().title("Playlists").borders(Borders::ALL)) |
| 84 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 85 | .highlight_symbol(">>") |
| 86 | .repeat_highlight_symbol(true); |
| 87 | |
| 88 | state.select(Some(app.pl_list.index)); |
| 89 | frame.render_stateful_widget(list, size, &mut state); |
| 90 | } |