Implement up, down, current track

This commit is contained in:
chmanie 2023-05-27 14:21:17 +02:00
parent ea5597aab2
commit af0dba7a25
1 changed files with 53 additions and 7 deletions

View File

@ -65,6 +65,34 @@ trait ListView {
} }
} }
fn down(&mut self) {
if self.is_empty() {
return;
}
if let Some(i) = self.selected() {
let next = if i < self.get_size() - 15 {
i + 15
} else {
self.get_size() - 1
};
self.select(Some(next));
} else {
self.select(Some(0));
}
}
fn up(&mut self) {
if self.is_empty() {
return;
}
if let Some(i) = self.selected() {
let prev = if i < 15 { 0 } else { i - 15 };
self.select(Some(prev));
} else {
self.select(Some(0));
}
}
fn is_selected(&self) -> bool { fn is_selected(&self) -> bool {
self.selected().is_some() self.selected().is_some()
} }
@ -87,6 +115,7 @@ enum UiItemKind {
} }
struct UiItem { struct UiItem {
active: bool,
uuid: String, uuid: String,
title: String, title: String,
kind: UiItemKind, kind: UiItemKind,
@ -143,10 +172,12 @@ impl QueueView {
self.list = queue self.list = queue
.tracks .tracks
.iter() .iter()
.map(|t| UiItem { .enumerate()
.map(|(i, t)| UiItem {
uuid: t.uuid.clone(), uuid: t.uuid.clone(),
title: t.title.clone(), title: format!("{} - {}", t.artist, t.title),
kind: UiItemKind::Track, kind: UiItemKind::Track,
active: i == queue.current as usize,
}) })
.collect(); .collect();
} }
@ -233,8 +264,9 @@ impl LibraryView {
.iter() .iter()
.map(|t| UiItem { .map(|t| UiItem {
uuid: t.uuid.clone(), uuid: t.uuid.clone(),
title: t.title.clone(), title: format!("{} - {}", t.artist, t.title),
kind: UiItemKind::Track, kind: UiItemKind::Track,
active: false,
}) })
.collect(); .collect();
} else { } else {
@ -246,6 +278,7 @@ impl LibraryView {
uuid: c.uuid.clone(), uuid: c.uuid.clone(),
title: c.title.clone(), title: c.title.clone(),
kind: UiItemKind::Node, kind: UiItemKind::Node,
active: false,
}) })
.collect(); .collect();
} }
@ -330,7 +363,7 @@ enum MessageFromUi {
async fn orchestrate<'a>( async fn orchestrate<'a>(
(tx, rx): (Sender<MessageToUi>, Receiver<MessageFromUi>), (tx, rx): (Sender<MessageToUi>, Receiver<MessageFromUi>),
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let mut rpc_client = rpc::RpcClient::connect("http://192.168.178.28:50051").await?; let mut rpc_client = rpc::RpcClient::connect("http://192.168.178.32:50051").await?;
if let Some(root_node) = rpc_client.get_library_node("/").await? { if let Some(root_node) = rpc_client.get_library_node("/").await? {
tx.send(MessageToUi::ReplaceLibraryNode(root_node.clone())); tx.send(MessageToUi::ReplaceLibraryNode(root_node.clone()));
@ -461,6 +494,12 @@ fn run_ui(tx: Sender<MessageFromUi>, rx: Receiver<MessageToUi>) {
(UiFocus::Library, KeyModifiers::NONE, KeyCode::Char('k')) => { (UiFocus::Library, KeyModifiers::NONE, KeyCode::Char('k')) => {
app.library.prev(); app.library.prev();
} }
(UiFocus::Library, KeyModifiers::CONTROL, KeyCode::Char('d')) => {
app.library.down();
}
(UiFocus::Library, KeyModifiers::CONTROL, KeyCode::Char('u')) => {
app.library.up();
}
(UiFocus::Library, KeyModifiers::NONE, KeyCode::Char('h')) => { (UiFocus::Library, KeyModifiers::NONE, KeyCode::Char('h')) => {
app.library.ascend(&tx); app.library.ascend(&tx);
} }
@ -476,6 +515,12 @@ fn run_ui(tx: Sender<MessageFromUi>, rx: Receiver<MessageToUi>) {
(UiFocus::Queue, KeyModifiers::NONE, KeyCode::Char('k')) => { (UiFocus::Queue, KeyModifiers::NONE, KeyCode::Char('k')) => {
app.queue.prev(); app.queue.prev();
} }
(UiFocus::Queue, KeyModifiers::CONTROL, KeyCode::Char('d')) => {
app.queue.down();
}
(UiFocus::Queue, KeyModifiers::CONTROL, KeyCode::Char('u')) => {
app.queue.up();
}
(UiFocus::Queue, KeyModifiers::NONE, KeyCode::Enter) => { (UiFocus::Queue, KeyModifiers::NONE, KeyCode::Enter) => {
app.queue.play_selected(&tx); app.queue.play_selected(&tx);
} }
@ -514,7 +559,6 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.library .library
.list .list
.iter() .iter()
// FIXME: why to_string() ??
.map(|i| ListItem::new(Span::from(i.title.to_string()))) .map(|i| ListItem::new(Span::from(i.title.to_string())))
.collect(); .collect();
@ -541,8 +585,10 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
.queue .queue
.list .list
.iter() .iter()
// FIXME: why to_string() ?? .map(|i| {
.map(|i| ListItem::new(Span::from(i.title.to_string()))) let color = if i.active { Color::Red } else { Color::Reset };
ListItem::new(Span::from(i.title.to_string())).style(Style::default().fg(color))
})
.collect(); .collect();
let queue_list = List::new(queue_items) let queue_list = List::new(queue_items)