From a04bc400517af8c104709182c17a5e2b8682d3a9 Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Sat, 20 Aug 2022 00:40:21 +0200 Subject: [PATCH] Refactor: Extract command handling for `ListView` --- src/ext_traits.rs | 36 ++++++++++++++++++++++++++++++++++++ src/ui/contextmenu.rs | 22 +++------------------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/ext_traits.rs b/src/ext_traits.rs index 74fe6c7..175d1d7 100644 --- a/src/ext_traits.rs +++ b/src/ext_traits.rs @@ -1,5 +1,7 @@ use cursive::views::ViewRef; +use crate::command::{Command, MoveAmount, MoveMode}; +use crate::commands::CommandResult; use crate::ui::layout::Layout; pub trait CursiveExt { @@ -19,3 +21,37 @@ impl CursiveExt for cursive::Cursive { cb(self, layout) } } + +pub trait SelectViewExt { + /// Translates ncspot commands (i.e. navigating in lists) to Cursive + /// `SelectView` actions. + fn handle_command(&mut self, cmd: &Command) -> Result; +} + +impl SelectViewExt for cursive::views::SelectView { + fn handle_command(&mut self, cmd: &Command) -> Result { + match cmd { + Command::Move(mode, amount) => { + let items = self.len(); + match mode { + MoveMode::Up => { + match amount { + MoveAmount::Extreme => self.set_selection(0), + MoveAmount::Integer(amount) => self.select_up(*amount as usize), + }; + Ok(CommandResult::Consumed(None)) + } + MoveMode::Down => { + match amount { + MoveAmount::Extreme => self.set_selection(items), + MoveAmount::Integer(amount) => self.select_down(*amount as usize), + }; + Ok(CommandResult::Consumed(None)) + } + _ => Ok(CommandResult::Consumed(None)), + } + } + _ => Ok(CommandResult::Ignored), + } + } +} diff --git a/src/ui/contextmenu.rs b/src/ui/contextmenu.rs index 68be1a6..acbe885 100644 --- a/src/ui/contextmenu.rs +++ b/src/ui/contextmenu.rs @@ -6,6 +6,7 @@ use cursive::views::{Dialog, NamedView, ScrollView, SelectView}; use cursive::Cursive; use crate::commands::CommandResult; +use crate::ext_traits::SelectViewExt; use crate::library::Library; use crate::model::artist::Artist; use crate::model::playable::Playable; @@ -386,26 +387,9 @@ fn handle_move_command( s.pop_layer(); Ok(CommandResult::Consumed(None)) } - Command::Move(mode, amount) => sel + Command::Move(_, _) => sel .call_on_name(name, |select: &mut SelectView| { - let items = select.len(); - match mode { - MoveMode::Up => { - match amount { - MoveAmount::Extreme => select.set_selection(0), - MoveAmount::Integer(amount) => select.select_up(*amount as usize), - }; - Ok(CommandResult::Consumed(None)) - } - MoveMode::Down => { - match amount { - MoveAmount::Extreme => select.set_selection(items), - MoveAmount::Integer(amount) => select.select_down(*amount as usize), - }; - Ok(CommandResult::Consumed(None)) - } - _ => Ok(CommandResult::Consumed(None)), - } + select.handle_command(cmd) }) .unwrap_or(Ok(CommandResult::Consumed(None))), _ => Ok(CommandResult::Consumed(None)),