| 1 | use crate::app::{App, AppResult}; |
| 2 | use ratatui::{ |
| 3 | prelude::*, |
| 4 | widgets::{block::Title, *}, |
| 5 | }; |
| 6 | |
| 7 | /// Renders the user interface widgets |
| 8 | pub fn render(app: &mut App, frame: &mut Frame) { |
| 9 | // This is where you add new widgets. |
| 10 | // See the following resources: |
| 11 | // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html |
| 12 | // - https://github.com/ratatui-org/ratatui/tree/master/examples |
| 13 | |
| 14 | // Layout |
| 15 | let main_layout = Layout::default() |
| 16 | .direction(Direction::Vertical) |
| 17 | .constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)]) |
| 18 | .split(frame.size()); |
| 19 | |
| 20 | let outer_layout = Layout::default() |
| 21 | .direction(Direction::Horizontal) |
| 22 | .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) |
| 23 | .split(main_layout[0]); |
| 24 | |
| 25 | let inner_layout = Layout::default() |
| 26 | .direction(Direction::Vertical) |
| 27 | .constraints(vec![Constraint::Percentage(50), Constraint::Percentage(50)]) |
| 28 | .split(outer_layout[1]); |
| 29 | |
| 30 | draw_song_list(frame, app, outer_layout[0]); |
| 31 | draw_queue(frame, app, inner_layout[0]); |
| 32 | draw_playlists(frame, app, inner_layout[1]); |
| 33 | draw_progress_bar(frame, app, main_layout[1]); |
| 34 | } |
| 35 | |
| 36 | /// draws list of songs |
| 37 | fn draw_song_list(frame: &mut Frame, app: &mut App, size: Rect) { |
| 38 | let mut song_state = ListState::default(); |
| 39 | let list = List::new(app.conn.songs_filenames.clone()) |
| 40 | .block( |
| 41 | Block::default() |
| 42 | .title("Song List".green().bold()) |
| 43 | .borders(Borders::ALL), |
| 44 | ) |
| 45 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 46 | .highlight_symbol(">>") |
| 47 | .repeat_highlight_symbol(true); |
| 48 | |
| 49 | song_state.select(Some(app.song_list.index)); |
| 50 | frame.render_stateful_widget(list, size, &mut song_state); |
| 51 | } |
| 52 | |
| 53 | /// draws playing queue |
| 54 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { |
| 55 | let mut queue_state = ListState::default(); |
| 56 | let title = Block::default() |
| 57 | .title(Title::from("Play Queue".green().bold())) |
| 58 | .title("Shift + ▲ ▼ to scroll, Shift + Enter to play".yellow()); |
| 59 | let list = List::new(app.queue_list.list.clone()) |
| 60 | .block(title.borders(Borders::ALL)) |
| 61 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 62 | .highlight_symbol(">>") |
| 63 | .repeat_highlight_symbol(true); |
| 64 | |
| 65 | app.update_queue(); |
| 66 | queue_state.select(Some(app.queue_list.index)); |
| 67 | frame.render_stateful_widget(list, size, &mut queue_state); |
| 68 | } |
| 69 | |
| 70 | /// draws all playlists |
| 71 | fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) { |
| 72 | let mut state = ListState::default(); |
| 73 | |
| 74 | let title = Block::default() |
| 75 | .title(Title::from("Playlist".green().bold())) |
| 76 | .title("▲ ▼ to scroll, Use ► to add playlist to queue".yellow()); |
| 77 | let list = List::new(app.pl_list.list.clone()) |
| 78 | .block(title.borders(Borders::ALL)) |
| 79 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 80 | .highlight_symbol(">>") |
| 81 | .repeat_highlight_symbol(true); |
| 82 | |
| 83 | state.select(Some(app.pl_list.index)); |
| 84 | frame.render_stateful_widget(list, size, &mut state); |
| 85 | } |
| 86 | |
| 87 | // Progress Bar |
| 88 | fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) { |
| 89 | let song = app |
| 90 | .conn |
| 91 | .now_playing() |
| 92 | .unwrap() |
| 93 | .unwrap_or_else(|| "No Title Found".to_string()); |
| 94 | |
| 95 | let state = &app.conn.state; |
| 96 | // let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap_or_default(); |
| 97 | |
| 98 | let title = Block::default() |
| 99 | .title(Title::from(format!("{}: ", state).red().bold())) |
| 100 | .title(Title::from(song.green().bold())); |
| 101 | let progress_bar = LineGauge::default() |
| 102 | .block(title.borders(Borders::ALL)) |
| 103 | .gauge_style( |
| 104 | Style::default() |
| 105 | .fg(Color::LightBlue) |
| 106 | .bg(Color::Gray) |
| 107 | .add_modifier(Modifier::BOLD), |
| 108 | ) |
| 109 | .line_set(symbols::line::THICK) |
| 110 | .ratio(0.2); |
| 111 | |
| 112 | frame.render_widget(progress_bar, size); |
| 113 | } |