| 1 | use crate::app::{App, SelectedTab}; |
| 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 layout = Layout::default() |
| 16 | .direction(Direction::Vertical) |
| 17 | .constraints(vec![Constraint::Percentage(93), Constraint::Percentage(7)]) |
| 18 | .split(frame.size()); |
| 19 | |
| 20 | match app.selected_tab { |
| 21 | SelectedTab::Queue => draw_queue(frame, app, layout[0]), |
| 22 | SelectedTab::Playlists => draw_playlists(frame, app, layout[0]), |
| 23 | SelectedTab::DirectoryBrowser => draw_directory_browser(frame, app, layout[0]), |
| 24 | } |
| 25 | |
| 26 | draw_progress_bar(frame, app, layout[1]); |
| 27 | } |
| 28 | |
| 29 | /// Draws the file tree browser |
| 30 | fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) { |
| 31 | let mut song_state = ListState::default(); |
| 32 | let total_songs = app.conn.conn.stats().unwrap().songs.to_string(); |
| 33 | let mut list: Vec<String> = vec![]; |
| 34 | for (t, s) in app.browser.filetree.iter() { |
| 35 | if t == "file" { |
| 36 | list.push(s.to_string()); |
| 37 | } else { |
| 38 | list.push(format!("[{}]", *s)); |
| 39 | } |
| 40 | } |
| 41 | let list = List::new(list) |
| 42 | .block( |
| 43 | Block::default() |
| 44 | .title(format!("File Browser: {}", app.browser.path.clone()).bold()) |
| 45 | .title( |
| 46 | Title::from(format!("Total Songs: {}", total_songs).bold().green()) |
| 47 | .alignment(Alignment::Center), |
| 48 | ) |
| 49 | .title( |
| 50 | Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) |
| 51 | .alignment(Alignment::Right), |
| 52 | ) |
| 53 | .borders(Borders::ALL), |
| 54 | ) |
| 55 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 56 | .highlight_symbol(">>") |
| 57 | .repeat_highlight_symbol(true) |
| 58 | .scroll_padding(20); |
| 59 | |
| 60 | song_state.select(Some(app.browser.selected)); |
| 61 | frame.render_stateful_widget(list, size, &mut song_state); |
| 62 | } |
| 63 | |
| 64 | /// draws playing queue |
| 65 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { |
| 66 | let mut queue_state = ListState::default(); |
| 67 | let title = Block::default() |
| 68 | .title(Title::from("Play Queue".green().bold())) |
| 69 | .title( |
| 70 | Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) |
| 71 | .alignment(Alignment::Right), |
| 72 | ); |
| 73 | let list = List::new(app.queue_list.list.clone()) |
| 74 | .block(title.borders(Borders::ALL)) |
| 75 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 76 | .highlight_symbol(">>") |
| 77 | .repeat_highlight_symbol(true); |
| 78 | |
| 79 | queue_state.select(Some(app.queue_list.index)); |
| 80 | frame.render_stateful_widget(list, size, &mut queue_state); |
| 81 | } |
| 82 | |
| 83 | /// draws all playlists |
| 84 | fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) { |
| 85 | let mut state = ListState::default(); |
| 86 | |
| 87 | let title = Block::default() |
| 88 | .title(Title::from("Playlist".green().bold())) |
| 89 | .title( |
| 90 | Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) |
| 91 | .alignment(Alignment::Right), |
| 92 | ); |
| 93 | |
| 94 | let list = List::new(app.pl_list.list.clone()) |
| 95 | .block(title.borders(Borders::ALL)) |
| 96 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 97 | .highlight_symbol(">>") |
| 98 | .repeat_highlight_symbol(true); |
| 99 | |
| 100 | state.select(Some(app.pl_list.index)); |
| 101 | frame.render_stateful_widget(list, size, &mut state); |
| 102 | } |
| 103 | |
| 104 | /// Draws Progress Bar |
| 105 | fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) { |
| 106 | let song = app |
| 107 | .conn |
| 108 | .now_playing() |
| 109 | .unwrap() |
| 110 | .unwrap_or_else(|| "No Title Found".to_string()); |
| 111 | |
| 112 | let state = &app.conn.state; |
| 113 | // let (elapsed, total) = app.conn.conn.status().unwrap().time.unwrap_or_default(); |
| 114 | |
| 115 | let title = Block::default() |
| 116 | .title(Title::from(format!("{}: ", state).red().bold())) |
| 117 | .title(Title::from(song.green().bold())) |
| 118 | .title( |
| 119 | Title::from( |
| 120 | format!( |
| 121 | "{}/{}", |
| 122 | app.conn.elapsed.as_secs(), |
| 123 | app.conn.total_duration.as_secs() |
| 124 | ) |
| 125 | .cyan() |
| 126 | .bold(), |
| 127 | ) |
| 128 | .alignment(Alignment::Right), |
| 129 | ) |
| 130 | .borders(Borders::ALL); |
| 131 | |
| 132 | // .title(Title::from(app.conn.conn.status().unwrap_or_default().volume.to_string().yellow())).title_alignment(Alignment::Right); |
| 133 | let progress_bar = LineGauge::default() |
| 134 | .block(title.borders(Borders::ALL)) |
| 135 | .gauge_style( |
| 136 | Style::default() |
| 137 | .fg(Color::LightBlue) |
| 138 | .bg(Color::Gray) |
| 139 | .add_modifier(Modifier::BOLD), |
| 140 | ) |
| 141 | .line_set(symbols::line::THICK) |
| 142 | .ratio(app.conn.get_progress_ratio()); |
| 143 | |
| 144 | frame.render_widget(progress_bar, size); |
| 145 | } |