diff --git a/README.md b/README.md index 0806dc6..db1ac26 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,8 @@ have them configurable. * `d` deletes the currently selected playlist * Tracks and playlists can be played using `Return` and queued using `Space` * `s` will save or remove the currently selected track to your library -* `o` will open a detail view for the selected item +* `o` will open a detail view or context menu for the selected item +* `Shift-o` will open a context menu for the currently playing track * `a` will open the album view for the selected item * `A` will open the artist view for the selected item * `Backspace` closes the current view diff --git a/src/command.rs b/src/command.rs index 9a8c3bd..ca3f8a0 100644 --- a/src/command.rs +++ b/src/command.rs @@ -66,7 +66,7 @@ pub enum Command { Shuffle(Option), Share(TargetMode), Back, - Open, + Open(TargetMode), Goto(GotoMode), Move(MoveMode, Option), Shift(ShiftMode, Option), @@ -123,7 +123,14 @@ pub fn parse(input: &str) -> Option { "play" => Some(Command::Play), "delete" => Some(Command::Delete), "back" => Some(Command::Back), - "open" => Some(Command::Open), + "open" => args + .get(0) + .and_then(|target| match *target { + "selected" => Some(TargetMode::Selected), + "current" => Some(TargetMode::Current), + _ => None, + }) + .map(Command::Open), "search" => args.get(0).map(|query| Command::Search(query.to_string())), "shift" => { let amount = args.get(1).and_then(|amount| amount.parse().ok()); diff --git a/src/commands.rs b/src/commands.rs index 6aba3d5..0f94a7a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -122,7 +122,7 @@ impl CommandManager { | Command::Save | Command::Delete | Command::Back - | Command::Open + | Command::Open(_) | Command::Goto(_) => Ok(None), _ => Err("Unknown Command".into()), } @@ -192,9 +192,12 @@ impl CommandManager { let mut kb = HashMap::new(); kb.insert("q".into(), Command::Quit); - kb.insert("P".into(), Command::TogglePlay); - kb.insert("R".into(), Command::Playlists(PlaylistCommands::Update)); - kb.insert("S".into(), Command::Stop); + kb.insert("Shift+p".into(), Command::TogglePlay); + kb.insert( + "Shift+r".into(), + Command::Playlists(PlaylistCommands::Update), + ); + kb.insert("Shift+s".into(), Command::Stop); kb.insert("<".into(), Command::Previous); kb.insert(">".into(), Command::Next); kb.insert("c".into(), Command::Clear); @@ -216,7 +219,8 @@ impl CommandManager { kb.insert("F3".into(), Command::Focus("library".into())); kb.insert("Backspace".into(), Command::Back); - kb.insert("o".into(), Command::Open); + kb.insert("o".into(), Command::Open(TargetMode::Selected)); + kb.insert("Shift+o".into(), Command::Open(TargetMode::Current)); kb.insert("a".into(), Command::Goto(GotoMode::Album)); kb.insert("A".into(), Command::Goto(GotoMode::Artist)); diff --git a/src/ui/contextmenu.rs b/src/ui/contextmenu.rs new file mode 100644 index 0000000..f52d238 --- /dev/null +++ b/src/ui/contextmenu.rs @@ -0,0 +1,53 @@ +use std::sync::Arc; + +use cursive::view::ViewWrapper; +use cursive::views::{Dialog, SelectView}; +use cursive::Cursive; + +use library::Library; +use queue::Queue; +use traits::ListItem; +use ui::layout::Layout; +use ui::modal::Modal; + +pub struct ContextMenu { + dialog: Modal, +} + +impl ContextMenu { + pub fn new(item: &Box, queue: Arc, library: Arc) -> Self { + let mut content: SelectView> = SelectView::new().autojump(); + if let Some(a) = item.artist() { + content.add_item("Artist", Box::new(a)) + } + if let Some(a) = item.album(queue.clone()) { + content.add_item("Album", Box::new(a)) + } + + // open detail view of artist/album + content.set_on_submit(move |s: &mut Cursive, selected: &Box| { + s.pop_layer(); + let queue = queue.clone(); + let library = library.clone(); + s.call_on_id("main", move |v: &mut Layout| { + let view = selected.open(queue, library); + if let Some(view) = view { + v.push_view(view) + } + }); + }); + + let dialog = Dialog::new() + .title(item.display_left()) + .dismiss_button("Cancel") + .padding((1, 1, 1, 0)) + .content(content); + Self { + dialog: Modal::new(dialog), + } + } +} + +impl ViewWrapper for ContextMenu { + wrap_impl!(self.dialog: Modal); +} diff --git a/src/ui/listview.rs b/src/ui/listview.rs index 89defc9..64b0702 100644 --- a/src/ui/listview.rs +++ b/src/ui/listview.rs @@ -18,6 +18,7 @@ use track::Track; use traits::{IntoBoxedViewExt, ListItem, ViewExt}; use ui::album::AlbumView; use ui::artist::ArtistView; +use ui::contextmenu::ContextMenu; pub type Paginator = Box>>) + Send + Sync>; pub struct Pagination { @@ -352,14 +353,31 @@ impl ViewExt for ListView { _ => {} } } - Command::Open => { - let mut content = self.content.write().unwrap(); - if let Some(item) = content.get_mut(self.selected) { - let queue = self.queue.clone(); - let library = self.library.clone(); - if let Some(view) = item.open(queue, library) { - return Ok(CommandResult::View(view)); + Command::Open(mode) => { + let queue = self.queue.clone(); + let library = self.library.clone(); + let target: Option> = match mode { + TargetMode::Current => { + self.queue.get_current().and_then(|t| Some(t.as_listitem())) } + TargetMode::Selected => { + let content = self.content.read().unwrap(); + content + .get(self.selected) + .and_then(|t| Some(t.as_listitem())) + } + }; + + // if item has a dedicated view, show it; otherwise open the context menu + if let Some(target) = target { + let view = target.open(queue.clone(), library.clone()); + return match view { + Some(view) => Ok(CommandResult::View(view)), + None => { + let contextmenu = ContextMenu::new(&target, queue, library); + Ok(CommandResult::Modal(Box::new(contextmenu))) + } + }; } } Command::Goto(mode) => { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 3c9e2a3..7be3962 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,5 +1,6 @@ pub mod album; pub mod artist; +pub mod contextmenu; pub mod layout; pub mod library; pub mod listview;