| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::app::{App, SelectedTab}; |
| 4 | use ratatui::{ |
| 5 | prelude::*, |
| 6 | widgets::{block::Title, *}, |
| 7 | }; |
| 8 | |
| 9 | #[derive(Debug, PartialEq)] |
| 10 | pub enum InputMode { |
| 11 | Editing, |
| 12 | Normal, |
| 13 | PlaylistRename, |
| 14 | } |
| 15 | |
| 16 | /// Renders the user interface widgets |
| 17 | pub fn render(app: &mut App, frame: &mut Frame) { |
| 18 | // Layout |
| 19 | let layout = Layout::default() |
| 20 | .direction(Direction::Vertical) |
| 21 | .constraints(vec![Constraint::Percentage(93), Constraint::Min(3)]) |
| 22 | .split(frame.size()); |
| 23 | |
| 24 | match app.selected_tab { |
| 25 | SelectedTab::Queue => draw_queue(frame, app, layout[0]), |
| 26 | SelectedTab::Playlists => draw_playlist_viewer(frame, app, layout[0]), |
| 27 | SelectedTab::DirectoryBrowser => draw_directory_browser(frame, app, layout[0]), |
| 28 | } |
| 29 | |
| 30 | match app.inputmode { |
| 31 | InputMode::Normal => { |
| 32 | draw_progress_bar(frame, app, layout[1]); |
| 33 | } |
| 34 | InputMode::Editing => { |
| 35 | draw_search_bar(frame, app, layout[1]); |
| 36 | } |
| 37 | InputMode::PlaylistRename => { |
| 38 | draw_rename_playlist(frame, app, layout[1]); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | if app.playlist_popup { |
| 43 | draw_add_to_playlist(frame, app, layout[0]); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Draws the directory |
| 48 | fn draw_directory_browser(frame: &mut Frame, app: &mut App, size: Rect) { |
| 49 | let total_songs = app.conn.stats.songs.to_string(); |
| 50 | |
| 51 | let rows = app.browser.filetree.iter().enumerate().map(|(i, (t, s))| { |
| 52 | if t == "file" { |
| 53 | let song = app.browser.songs.get(i).unwrap().clone(); |
| 54 | |
| 55 | // metadata |
| 56 | let title = song.clone().title.unwrap_or_else(|| song.clone().file); |
| 57 | let artist = song.clone().artist.unwrap_or_default().cyan(); |
| 58 | let album = song |
| 59 | .tags |
| 60 | .iter() |
| 61 | .filter(|(x, _)| x == "Album") |
| 62 | .map(|(_, l)| l.clone()) |
| 63 | .collect::<Vec<String>>() |
| 64 | .join(""); |
| 65 | |
| 66 | let track = song |
| 67 | .tags |
| 68 | .iter() |
| 69 | .filter(|(x, _)| x == "Track") |
| 70 | .map(|(_, l)| l.clone()) |
| 71 | .collect::<Vec<String>>() |
| 72 | .join(""); |
| 73 | |
| 74 | let time = |
| 75 | App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0))); |
| 76 | |
| 77 | let mut status: bool = false; |
| 78 | for sn in app.queue_list.list.iter() { |
| 79 | if sn.file.contains(s) { |
| 80 | status = true; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | let row = Row::new(vec![ |
| 85 | Cell::from(artist), |
| 86 | Cell::from(track.green()), |
| 87 | Cell::from(title), |
| 88 | Cell::from(album.cyan()), |
| 89 | Cell::from(time.to_string().magenta()), |
| 90 | ]); |
| 91 | |
| 92 | if status { |
| 93 | row.magenta().bold() |
| 94 | } else { |
| 95 | row |
| 96 | } |
| 97 | } else { |
| 98 | let row = Row::new(vec![Cell::from(format!("[{}]", *s))]); |
| 99 | row |
| 100 | } |
| 101 | }); |
| 102 | |
| 103 | let mut state = TableState::new(); |
| 104 | let header = ["Artist", "Track", "Title", "Album", "Time"] |
| 105 | .into_iter() |
| 106 | .map(Cell::from) |
| 107 | .collect::<Row>() |
| 108 | .bold() |
| 109 | .height(1); |
| 110 | let table = Table::new( |
| 111 | rows, |
| 112 | [ |
| 113 | Constraint::Percentage(34), |
| 114 | Constraint::Percentage(3), |
| 115 | Constraint::Min(30), |
| 116 | Constraint::Percentage(30), |
| 117 | Constraint::Percentage(3), |
| 118 | ], |
| 119 | ) |
| 120 | .block( |
| 121 | Block::default() |
| 122 | .title(format!("Song Browser: {}", app.browser.path.clone()).bold()) |
| 123 | .title( |
| 124 | Title::from(format!("Total Songs: {}", total_songs).bold().green()) |
| 125 | .alignment(Alignment::Center), |
| 126 | ) |
| 127 | .title( |
| 128 | Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) |
| 129 | .alignment(Alignment::Right), |
| 130 | ) |
| 131 | .borders(Borders::ALL), |
| 132 | ) |
| 133 | .highlight_style( |
| 134 | Style::default() |
| 135 | .add_modifier(Modifier::REVERSED) |
| 136 | .fg(Color::Cyan) |
| 137 | .bg(Color::Black), |
| 138 | ) |
| 139 | .header(header) |
| 140 | .flex(layout::Flex::Legacy); |
| 141 | |
| 142 | state.select(Some(app.browser.selected)); |
| 143 | frame.render_stateful_widget(table, size, &mut state); |
| 144 | } |
| 145 | |
| 146 | /// draws playing queue |
| 147 | fn draw_queue(frame: &mut Frame, app: &mut App, size: Rect) { |
| 148 | let rows = app.queue_list.list.iter().enumerate().map(|(i, song)| { |
| 149 | // metadata |
| 150 | let title = song.clone().title.unwrap_or_else(|| song.clone().file); |
| 151 | let artist = song.clone().artist.unwrap_or_default().cyan(); |
| 152 | let album = song |
| 153 | .tags |
| 154 | .iter() |
| 155 | .filter(|(x, _)| x == "Album") |
| 156 | .map(|(_, l)| l.clone()) |
| 157 | .collect::<Vec<String>>() |
| 158 | .join(""); |
| 159 | |
| 160 | let track = song |
| 161 | .tags |
| 162 | .iter() |
| 163 | .filter(|(x, _)| x == "Track") |
| 164 | .map(|(_, l)| l.clone()) |
| 165 | .collect::<Vec<String>>() |
| 166 | .join(""); |
| 167 | |
| 168 | let time = App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0))); |
| 169 | |
| 170 | let row = Row::new(vec![ |
| 171 | Cell::from(artist), |
| 172 | Cell::from(track.green()), |
| 173 | Cell::from(title), |
| 174 | Cell::from(album.cyan()), |
| 175 | Cell::from(time.to_string().magenta()), |
| 176 | ]); |
| 177 | |
| 178 | let place = app.conn.current_song.place; |
| 179 | if let Some(pos) = place { |
| 180 | if i == pos.pos as usize { |
| 181 | row.magenta().bold() |
| 182 | } else { |
| 183 | row |
| 184 | } |
| 185 | } else { |
| 186 | row |
| 187 | } |
| 188 | }); |
| 189 | |
| 190 | let mut state = TableState::new(); |
| 191 | let header = ["Artist", "Track", "Title", "Album", "Time"] |
| 192 | .into_iter() |
| 193 | .map(Cell::from) |
| 194 | .collect::<Row>() |
| 195 | .bold() |
| 196 | .height(1); |
| 197 | let table = Table::new( |
| 198 | rows, |
| 199 | [ |
| 200 | Constraint::Percentage(34), |
| 201 | Constraint::Percentage(3), |
| 202 | Constraint::Min(30), |
| 203 | Constraint::Percentage(30), |
| 204 | Constraint::Percentage(3), |
| 205 | ], |
| 206 | ) |
| 207 | .block( |
| 208 | Block::default() |
| 209 | .title(Title::from("Play Queue".green().bold())) |
| 210 | .title(Title::from( |
| 211 | format!("({} items)", app.queue_list.list.len()).bold(), |
| 212 | )) |
| 213 | .title( |
| 214 | Title::from(format!("Volume: {}%", app.conn.volume).bold().green()) |
| 215 | .alignment(Alignment::Right), |
| 216 | ) |
| 217 | .borders(Borders::ALL), |
| 218 | ) |
| 219 | .highlight_style( |
| 220 | Style::default() |
| 221 | .add_modifier(Modifier::REVERSED) |
| 222 | .fg(Color::Cyan) |
| 223 | .bg(Color::Black), |
| 224 | ) |
| 225 | .header(header) |
| 226 | .flex(layout::Flex::Legacy); |
| 227 | |
| 228 | state.select(Some(app.queue_list.index)); |
| 229 | frame.render_stateful_widget(table, size, &mut state); |
| 230 | } |
| 231 | |
| 232 | // Draw search bar |
| 233 | fn draw_search_bar(frame: &mut Frame, app: &mut App, size: Rect) { |
| 234 | // Make the cursor visible and ask ratatui to put it at the specified coordinates after |
| 235 | // rendering |
| 236 | #[allow(clippy::cast_possible_truncation)] |
| 237 | frame.set_cursor( |
| 238 | // Draw the cursor at the current position in the input field. |
| 239 | // This position is can be controlled via the left and right arrow key |
| 240 | size.x + app.search_cursor_pos as u16 + 2, |
| 241 | // Move one line down, from the border to the input line |
| 242 | size.y + 1, |
| 243 | ); |
| 244 | |
| 245 | let input = Paragraph::new("/".to_string() + &app.search_input) |
| 246 | .style(Style::default()) |
| 247 | .block( |
| 248 | Block::default() |
| 249 | .borders(Borders::ALL) |
| 250 | .title("Search Forward: ".bold().green()), |
| 251 | ); |
| 252 | frame.render_widget(input, size); |
| 253 | } |
| 254 | |
| 255 | /// Draws Progress Bar |
| 256 | fn draw_progress_bar(frame: &mut Frame, app: &mut App, size: Rect) { |
| 257 | // Get the current playing song |
| 258 | let song = app |
| 259 | .conn |
| 260 | .now_playing() |
| 261 | .unwrap() |
| 262 | .unwrap_or_else(|| "No Title Found".to_string()); |
| 263 | |
| 264 | // Get the current playing state |
| 265 | let mut state: String = String::new(); |
| 266 | if !app.queue_list.list.is_empty() { |
| 267 | state = app.conn.state.clone(); |
| 268 | state.push(':'); |
| 269 | } |
| 270 | |
| 271 | // Get the current modes |
| 272 | let mut modes_bottom: String = String::new(); |
| 273 | // we do this to check if at least one mode is enabled so we can push "[]" |
| 274 | if app.conn.repeat | app.conn.random { |
| 275 | modes_bottom.push('r'); |
| 276 | } |
| 277 | |
| 278 | if !modes_bottom.is_empty() { |
| 279 | modes_bottom.clear(); |
| 280 | modes_bottom.push('['); |
| 281 | if app.conn.repeat { |
| 282 | modes_bottom.push('r'); |
| 283 | } |
| 284 | if app.conn.random { |
| 285 | modes_bottom.push('z'); |
| 286 | } |
| 287 | modes_bottom.push(']'); |
| 288 | }; |
| 289 | |
| 290 | // get the duration |
| 291 | let duration = if app.conn.total_duration.as_secs() != 0 { |
| 292 | format!( |
| 293 | "[{}/{}]", |
| 294 | App::format_time(app.conn.elapsed), |
| 295 | App::format_time(app.conn.total_duration) |
| 296 | ) |
| 297 | } else { |
| 298 | "".to_string() |
| 299 | }; |
| 300 | |
| 301 | // Define the title |
| 302 | let title = Block::default() |
| 303 | .title(Title::from(state.red().bold())) |
| 304 | .title(Title::from(song.green().bold())) |
| 305 | .title(Title::from(duration.cyan().bold()).alignment(Alignment::Right)) |
| 306 | .title(Title::from(modes_bottom).position(block::Position::Bottom)) |
| 307 | .borders(Borders::ALL); |
| 308 | |
| 309 | let progress_bar = LineGauge::default() |
| 310 | .block(title.borders(Borders::ALL)) |
| 311 | .gauge_style( |
| 312 | Style::default() |
| 313 | .fg(Color::Blue) |
| 314 | .bg(Color::Black) |
| 315 | .add_modifier(Modifier::BOLD), |
| 316 | ) |
| 317 | .line_set(symbols::line::THICK) |
| 318 | .ratio(app.conn.get_progress_ratio()); |
| 319 | |
| 320 | frame.render_widget(progress_bar, size); |
| 321 | } |
| 322 | |
| 323 | fn draw_playlist_viewer(frame: &mut Frame, app: &mut App, area: Rect) { |
| 324 | let layouts = Layout::default() |
| 325 | .direction(Direction::Horizontal) |
| 326 | .constraints(vec![Constraint::Percentage(40), Constraint::Percentage(60)]) |
| 327 | .split(area); |
| 328 | |
| 329 | // Draw list of playlists |
| 330 | let mut state = ListState::default(); |
| 331 | let title = Block::default().title(Title::from("Playlist".green().bold())); |
| 332 | let list = List::new(app.pl_list.list.clone()) |
| 333 | .block(title.borders(Borders::ALL)) |
| 334 | .highlight_style( |
| 335 | Style::new() |
| 336 | .fg(Color::Cyan) |
| 337 | .bg(Color::Black) |
| 338 | .add_modifier(Modifier::BOLD) |
| 339 | .add_modifier(Modifier::REVERSED), |
| 340 | ) |
| 341 | .repeat_highlight_symbol(true); |
| 342 | state.select(Some(app.pl_list.index)); |
| 343 | frame.render_stateful_widget(list, layouts[0], &mut state); |
| 344 | |
| 345 | // Playlist viewer |
| 346 | let pl_name = app.pl_list.list.get(app.pl_list.index).unwrap(); |
| 347 | let songs = app.conn.conn.playlist(pl_name).unwrap(); |
| 348 | let rows = songs.iter().map(|song| { |
| 349 | let title = song.clone().title.unwrap_or_default().cyan(); |
| 350 | let artist = song.clone().artist.unwrap_or_else(|| song.clone().file); |
| 351 | let time = App::format_time(song.clone().duration.unwrap_or_else(|| Duration::new(0, 0))); |
| 352 | |
| 353 | let row = Row::new(vec![ |
| 354 | Cell::from(artist), |
| 355 | Cell::from(title), |
| 356 | Cell::from(time.to_string().green()), |
| 357 | ]); |
| 358 | row |
| 359 | }); |
| 360 | |
| 361 | let title = Block::default() |
| 362 | .title(format!("Content: ({} items)", songs.len()).bold()) |
| 363 | .borders(Borders::ALL); |
| 364 | let table = Table::new( |
| 365 | rows, |
| 366 | vec![ |
| 367 | Constraint::Min(48), |
| 368 | Constraint::Percentage(48), |
| 369 | Constraint::Percentage(4), |
| 370 | ], |
| 371 | ) |
| 372 | .block(title) |
| 373 | .highlight_style( |
| 374 | Style::default() |
| 375 | .add_modifier(Modifier::REVERSED) |
| 376 | .fg(Color::Cyan) |
| 377 | .bg(Color::Black), |
| 378 | ) |
| 379 | .flex(layout::Flex::SpaceBetween); |
| 380 | frame.render_widget(table, layouts[1]); |
| 381 | } |
| 382 | |
| 383 | fn draw_add_to_playlist(frame: &mut Frame, app: &mut App, area: Rect) { |
| 384 | let area = centered_rect(40, 50, area); |
| 385 | let mut state = ListState::default(); |
| 386 | let title = Block::default() |
| 387 | .title(Title::from("Add Selected Item to: ")) |
| 388 | .title(Title::from("<Esc> to Cancel".green().bold()).alignment(Alignment::Right)); |
| 389 | let list = List::new(app.append_list.list.clone()) |
| 390 | .block(title.borders(Borders::ALL)) |
| 391 | .highlight_style( |
| 392 | Style::new() |
| 393 | .fg(Color::Cyan) |
| 394 | .bg(Color::Black) |
| 395 | .add_modifier(Modifier::BOLD) |
| 396 | .add_modifier(Modifier::REVERSED), |
| 397 | ) |
| 398 | .repeat_highlight_symbol(true); |
| 399 | |
| 400 | state.select(Some(app.append_list.index)); |
| 401 | frame.render_widget(Clear, area); //this clears out the background |
| 402 | frame.render_stateful_widget(list, area, &mut state); |
| 403 | } |
| 404 | |
| 405 | fn draw_rename_playlist(frame: &mut Frame, app: &mut App, area: Rect) { |
| 406 | #[allow(clippy::cast_possible_truncation)] |
| 407 | frame.set_cursor( |
| 408 | // Draw the cursor at the current position in the input field. |
| 409 | // This position is can be controlled via the left and right arrow key |
| 410 | area.x + app.pl_cursor_pos as u16 + 2, |
| 411 | // Move one line down, from the border to the input line |
| 412 | area.y + 1, |
| 413 | ); |
| 414 | |
| 415 | let input = Paragraph::new("/".to_string() + &app.pl_newname_input) |
| 416 | .style(Style::default()) |
| 417 | .block( |
| 418 | Block::default() |
| 419 | .borders(Borders::ALL) |
| 420 | .title("Enter New Name: ".bold().green()), |
| 421 | ); |
| 422 | frame.render_widget(input, area); |
| 423 | } |
| 424 | |
| 425 | fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { |
| 426 | let popup_layout = Layout::vertical([ |
| 427 | Constraint::Percentage((100 - percent_y) / 2), |
| 428 | Constraint::Percentage(percent_y), |
| 429 | Constraint::Percentage((100 - percent_y) / 2), |
| 430 | ]) |
| 431 | .split(r); |
| 432 | |
| 433 | Layout::horizontal([ |
| 434 | Constraint::Percentage((100 - percent_x) / 2), |
| 435 | Constraint::Percentage(percent_x), |
| 436 | Constraint::Percentage((100 - percent_x) / 2), |
| 437 | ]) |
| 438 | .split(popup_layout[1])[1] |
| 439 | } |