From 344d3e73c04b96dc929d2fa14cd1b23618a68249 Mon Sep 17 00:00:00 2001 From: Remigiusz Micielski Date: Fri, 6 Sep 2024 10:51:16 +0200 Subject: [PATCH] selection bottom bar --- rm-main/src/tui/tabs/torrents/mod.rs | 17 +++++++++ rm-main/src/tui/tabs/torrents/task_manager.rs | 11 ++++-- rm-main/src/tui/tabs/torrents/tasks/mod.rs | 2 ++ .../src/tui/tabs/torrents/tasks/selection.rs | 36 +++++++++++++++++++ rm-shared/src/action.rs | 1 + 5 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 rm-main/src/tui/tabs/torrents/tasks/selection.rs diff --git a/rm-main/src/tui/tabs/torrents/mod.rs b/rm-main/src/tui/tabs/torrents/mod.rs index 9bfc7ad..99ce9f4 100644 --- a/rm-main/src/tui/tabs/torrents/mod.rs +++ b/rm-main/src/tui/tabs/torrents/mod.rs @@ -113,6 +113,7 @@ impl Component for TorrentsTab { .iter_mut() .for_each(|t| t.is_selected = false); self.table_manager.selected_torrents_ids.drain(..); + self.task_manager.default(); self.ctx.send_action(Action::Render); return ComponentAction::Nothing; } @@ -137,6 +138,12 @@ impl Component for TorrentsTab { A::Confirm => self.show_details_popup(), A::Select => { self.table_manager.select_current_torrent(); + if !self.table_manager.selected_torrents_ids.is_empty() { + self.task_manager + .select(self.table_manager.selected_torrents_ids.len()); + } else { + self.task_manager.default(); + } self.ctx.send_action(Action::Render); } A::Pause => self.pause_current_torrent(), @@ -211,6 +218,16 @@ impl Component for TorrentsTab { UpdateAction::UpdateCurrentTorrent(_) => { self.popup_manager.handle_update_action(action) } + UpdateAction::CancelTorrentTask => { + if !self.table_manager.selected_torrents_ids.is_empty() { + self.task_manager + .select(self.table_manager.selected_torrents_ids.len()); + } else { + self.task_manager.default(); + } + self.ctx + .send_update_action(UpdateAction::SwitchToNormalMode); + } other => self.task_manager.handle_update_action(other), } } diff --git a/rm-main/src/tui/tabs/torrents/task_manager.rs b/rm-main/src/tui/tabs/torrents/task_manager.rs index 71bde29..497e76b 100644 --- a/rm-main/src/tui/tabs/torrents/task_manager.rs +++ b/rm-main/src/tui/tabs/torrents/task_manager.rs @@ -40,6 +40,7 @@ pub enum CurrentTask { Default(tasks::Default), Status(tasks::Status), Sort(tasks::Sort), + Selection(tasks::Selection), } impl CurrentTask { @@ -86,6 +87,7 @@ impl Component for TaskManager { } CurrentTask::Default(_) => (), CurrentTask::Sort(_) => (), + CurrentTask::Selection(_) => (), }; ComponentAction::Nothing } @@ -119,6 +121,7 @@ impl Component for TaskManager { CurrentTask::Status(status_bar) => status_bar.render(f, rect), CurrentTask::ChangeCategory(category_bar) => category_bar.render(f, rect), CurrentTask::Sort(sort_bar) => sort_bar.render(f, rect), + CurrentTask::Selection(selection_bar) => selection_bar.render(f, rect), } } @@ -166,6 +169,10 @@ impl TaskManager { self.current_task = CurrentTask::Default(tasks::Default::new()); } + pub fn select(&mut self, amount: usize) { + self.current_task = CurrentTask::Selection(tasks::Selection::new(amount)); + } + pub fn sort(&mut self) { self.current_task = CurrentTask::Sort(tasks::Sort::new()); } @@ -198,8 +205,6 @@ impl TaskManager { return; } - self.current_task = CurrentTask::Default(tasks::Default::new()); - self.ctx - .send_update_action(UpdateAction::SwitchToNormalMode); + self.ctx.send_update_action(UpdateAction::CancelTorrentTask); } } diff --git a/rm-main/src/tui/tabs/torrents/tasks/mod.rs b/rm-main/src/tui/tabs/torrents/tasks/mod.rs index b7fec47..edf6291 100644 --- a/rm-main/src/tui/tabs/torrents/tasks/mod.rs +++ b/rm-main/src/tui/tabs/torrents/tasks/mod.rs @@ -4,6 +4,7 @@ mod default; mod delete_torrent; mod filter; mod move_torrent; +mod selection; mod sort; mod status; @@ -13,5 +14,6 @@ pub use default::Default; pub use delete_torrent::Delete; pub use filter::Filter; pub use move_torrent::Move; +pub use selection::Selection; pub use sort::Sort; pub use status::{CurrentTaskState, Status}; diff --git a/rm-main/src/tui/tabs/torrents/tasks/selection.rs b/rm-main/src/tui/tabs/torrents/tasks/selection.rs new file mode 100644 index 0000000..003eaf0 --- /dev/null +++ b/rm-main/src/tui/tabs/torrents/tasks/selection.rs @@ -0,0 +1,36 @@ +use crate::tui::components::{keybinding_style, Component}; +use rm_config::CONFIG; +use rm_shared::action::Action; + +use ratatui::{prelude::*, text::Span}; + +pub struct Selection { + selection_amount: usize, +} + +impl Selection { + pub const fn new(selection_amount: usize) -> Self { + Self { selection_amount } + } +} + +impl Component for Selection { + fn render(&mut self, f: &mut Frame, rect: Rect) { + let mut line = Line::default(); + let mut line_is_empty = true; + + if let Some(keys) = CONFIG.keybindings.get_keys_for_action(Action::Close) { + line_is_empty = false; + line.push_span(Span::styled(keys, keybinding_style())); + line.push_span(Span::raw(" - clear selection")); + } + + if !line_is_empty { + line.push_span(Span::raw(" | ")); + } + + line.push_span(format!("{} selected", self.selection_amount)); + + f.render_widget(line, rect); + } +} diff --git a/rm-shared/src/action.rs b/rm-shared/src/action.rs index 1a70445..9c5ee72 100644 --- a/rm-shared/src/action.rs +++ b/rm-shared/src/action.rs @@ -57,6 +57,7 @@ pub enum UpdateAction { UpdateCurrentTorrent(Box), SearchFilterApply(String), SearchFilterClear, + CancelTorrentTask, // Search Tab SearchStarted, ProviderResult(MagneteaseResult),