| 1 | use crate::app::{App, AppResult, 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![ |
| 18 | Constraint::Percentage(0), |
| 19 | // Constraint::Percentage(88), |
| 20 | Constraint::Percentage(93), |
| 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 total_songs = app.conn.conn.stats().unwrap().songs.to_string(); |
| 61 | let list = List::new(app.conn.songs_filenames.clone()) |
| 62 | .block( |
| 63 | Block::default() |
| 64 | .title("Song List".green().bold()) |
| 65 | .title( |
| 66 | Title::from(format!("Total Songs: {}", total_songs).bold().green()) |
| 67 | .alignment(Alignment::Right), |
| 68 | ) |
| 69 | .borders(Borders::ALL), |
| 70 | ) |
| 71 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 72 | .highlight_symbol(">>") |
| 73 | .repeat_highlight_symbol(true); |
| 74 | |
| 75 | song_state.select(Some(app.song_list.index)); |
| 76 | frame.render_stateful_widget(list, size, &mut song_state); |
| 77 | } |
| 78 | |
| 79 | /// draws playing queue |
| 80 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { |
| 81 | let mut queue_state = ListState::default(); |
| 82 | let title = Block::default().title(Title::from("Play Queue".green().bold())); |
| 83 | let list = List::new(app.queue_list.list.clone()) |
| 84 | .block(title.borders(Borders::ALL)) |
| 85 | .highlight_style(Style::new().add_modifier(Modifier::REVERSED)) |
| 86 | .highlight_symbol(">>") |
| 87 | .repeat_highlight_symbol(true); |
| 88 | |
| 89 | queue_state.select(Some(app.queue_list.index)); |
| 90 | frame.render_stateful_widget(list, size, &mut queue_state); |
| 91 | } |
| 92 | |
| 93 | /// draws all playlists |
| 94 | fn draw_playlists(frame: &mut Frame, app: &mut App, size: Rect) { |
| 95 | let mut state = ListState::default(); |
| 96 | |
| 97 | let title = Block::default().title(Title::from("Playlist".green().bold())); |
| 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( |
| 123 | Title::from( |
| 124 | format!( |
| 125 | "{}/{}", |
| 126 | app.conn.elapsed.as_secs(), |
| 127 | app.conn.total_duration.as_secs() |
| 128 | ) |
| 129 | .cyan() |
| 130 | .bold(), |
| 131 | ) |
| 132 | .alignment(Alignment::Right), |
| 133 | ) |
| 134 | .borders(Borders::ALL); |
| 135 | |
| 136 | // .title(Title::from(app.conn.conn.status().unwrap_or_default().volume.to_string().yellow())).title_alignment(Alignment::Right); |
| 137 | let progress_bar = LineGauge::default() |
| 138 | .block(title.borders(Borders::ALL)) |
| 139 | .gauge_style( |
| 140 | Style::default() |
| 141 | .fg(Color::LightBlue) |
| 142 | .bg(Color::Gray) |
| 143 | .add_modifier(Modifier::BOLD), |
| 144 | ) |
| 145 | .line_set(symbols::line::THICK) |
| 146 | .ratio(app.conn.get_progress_ratio()); |
| 147 | |
| 148 | frame.render_widget(progress_bar, size); |
| 149 | } |