Krolyxon
Commit 3f6b2bf

rmptuisrcui.rs

95 lines · 3.7 KB · Rust

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