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