feature(tui): added delete button to every content type
This commit is contained in:
1 parent
4ddb8f49bb
commit
be01370ab2
6 files changed
+207
-48
No files matched your search
+96
-40
@@ -70,49 +70,56 @@ impl App {
|
||||
if self.focused == FocusedArea::ConfirmDelete {
|
||||
match key_event.code {
|
||||
KeyCode::Enter | KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
if let Some(target) = confirm_popup::pending_target() {
|
||||
match target {
|
||||
confirm_popup::ConfirmTarget::Instance { name } => {
|
||||
match self.instance_manager.delete(&name) {
|
||||
Ok(_) => {
|
||||
self.instances_state.remove_instance(&name);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"Failed to delete instance '{}': {}",
|
||||
name,
|
||||
e
|
||||
);
|
||||
}
|
||||
let focus_after = match confirm_popup::pending_target() {
|
||||
Some(confirm_popup::ConfirmTarget::Instance { name }) => {
|
||||
match self.instance_manager.delete(&name) {
|
||||
Ok(_) => {
|
||||
self.instances_state.remove_instance(&name);
|
||||
}
|
||||
self.focused = FocusedArea::Instances;
|
||||
}
|
||||
confirm_popup::ConfirmTarget::ConfigProfile { profile } => {
|
||||
if let Err(e) = self.delete_config_profile(&profile) {
|
||||
error_buffer::push_error(error_buffer::ErrorEvent {
|
||||
id: 0,
|
||||
level: tracing::Level::ERROR,
|
||||
message: e.to_string(),
|
||||
pushed_at: std::time::Instant::now(),
|
||||
});
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to delete instance '{}': {}", name, e);
|
||||
}
|
||||
self.focused = FocusedArea::Settings;
|
||||
}
|
||||
FocusedArea::Instances
|
||||
}
|
||||
}
|
||||
Some(confirm_popup::ConfirmTarget::ConfigProfile { profile }) => {
|
||||
if let Err(e) = self.delete_config_profile(&profile) {
|
||||
error_buffer::push_error(error_buffer::ErrorEvent {
|
||||
id: 0,
|
||||
level: tracing::Level::ERROR,
|
||||
message: e.to_string(),
|
||||
pushed_at: std::time::Instant::now(),
|
||||
});
|
||||
}
|
||||
FocusedArea::Settings
|
||||
}
|
||||
Some(confirm_popup::ConfirmTarget::Content { name, path }) => {
|
||||
match delete_content_path(&path) {
|
||||
Ok(()) => {
|
||||
self.remove_content_path_from_states(&path);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to delete content '{}': {}", name, e);
|
||||
}
|
||||
}
|
||||
FocusedArea::Content
|
||||
}
|
||||
None => FocusedArea::Instances,
|
||||
};
|
||||
confirm_popup::clear_pending();
|
||||
self.focused = focus_after;
|
||||
return Ok(());
|
||||
}
|
||||
KeyCode::Esc | KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
if matches!(
|
||||
confirm_popup::pending_target(),
|
||||
Some(confirm_popup::ConfirmTarget::ConfigProfile { .. })
|
||||
) {
|
||||
self.focused = FocusedArea::Settings;
|
||||
} else {
|
||||
self.focused = FocusedArea::Instances;
|
||||
}
|
||||
let focus_after = match confirm_popup::pending_target() {
|
||||
Some(confirm_popup::ConfirmTarget::Content { .. }) => FocusedArea::Content,
|
||||
Some(confirm_popup::ConfirmTarget::ConfigProfile { .. }) => {
|
||||
FocusedArea::Settings
|
||||
}
|
||||
_ => FocusedArea::Instances,
|
||||
};
|
||||
confirm_popup::clear_pending();
|
||||
self.focused = focus_after;
|
||||
return Ok(());
|
||||
}
|
||||
_ => {
|
||||
@@ -122,17 +129,41 @@ impl App {
|
||||
}
|
||||
|
||||
// content area delegates to whichever tab is active.
|
||||
// worlds get handle_key_no_toggle because you can't "disable" a world like you can a mod
|
||||
// worlds use the same list navigation without the toggle
|
||||
if self.focused == FocusedArea::Content {
|
||||
if self.content_tab == widgets::content::ContentTab::Logs {
|
||||
if key_event.code == KeyCode::Char('d')
|
||||
&& !self.logs_state.search.active
|
||||
&& !self.logs_state.viewer_search.active
|
||||
{
|
||||
if let Some(pending) = self.logs_state.pending_delete() {
|
||||
confirm_popup::set_pending_content_delete(pending.name, pending.path);
|
||||
self.focused = FocusedArea::ConfirmDelete;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if widgets::logs_viewer::handle_key(&key_event, &mut self.logs_state) {
|
||||
return Ok(());
|
||||
}
|
||||
} else if self.content_tab == widgets::content::ContentTab::Screenshots {
|
||||
if key_event.code == KeyCode::Char('d') && !self.screenshots_state.search.active {
|
||||
if let Some(pending) = self.screenshots_state.pending_delete() {
|
||||
confirm_popup::set_pending_content_delete(pending.name, pending.path);
|
||||
self.focused = FocusedArea::ConfirmDelete;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if widgets::screenshots_grid::handle_key(&key_event, &mut self.screenshots_state) {
|
||||
return Ok(());
|
||||
}
|
||||
} else if self.content_tab == widgets::content::ContentTab::Worlds {
|
||||
if key_event.code == KeyCode::Char('d') && !self.worlds_state.search.active {
|
||||
if let Some(pending) = self.worlds_state.pending_delete() {
|
||||
confirm_popup::set_pending_content_delete(pending.name, pending.path);
|
||||
self.focused = FocusedArea::ConfirmDelete;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if widgets::content::list::handle_key_no_toggle(&key_event, &mut self.worlds_state)
|
||||
{
|
||||
return Ok(());
|
||||
@@ -146,10 +177,17 @@ impl App {
|
||||
widgets::content::ContentTab::Shaders => Some(&mut self.shaders_state),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(state) = state
|
||||
&& widgets::content::list::handle_key(&key_event, state)
|
||||
{
|
||||
return Ok(());
|
||||
if let Some(state) = state {
|
||||
if key_event.code == KeyCode::Char('d') && !state.search.active {
|
||||
if let Some(pending) = state.pending_delete() {
|
||||
confirm_popup::set_pending_content_delete(pending.name, pending.path);
|
||||
self.focused = FocusedArea::ConfirmDelete;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
if widgets::content::list::handle_key(&key_event, state) {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -326,7 +364,7 @@ impl App {
|
||||
{
|
||||
if let Some(instance) = self.instances_state.selected_instance() {
|
||||
let name = instance.name.clone();
|
||||
confirm_popup::set_pending_delete(&name);
|
||||
confirm_popup::set_pending_instance_delete(&name);
|
||||
self.focused = FocusedArea::ConfirmDelete;
|
||||
}
|
||||
}
|
||||
@@ -439,4 +477,22 @@ impl App {
|
||||
.retain(|candidate| candidate != profile);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn remove_content_path_from_states(&mut self, path: &std::path::Path) {
|
||||
self.mods_state.remove_path(path);
|
||||
self.resource_packs_state.remove_path(path);
|
||||
self.shaders_state.remove_path(path);
|
||||
self.worlds_state.remove_path(path);
|
||||
self.screenshots_state.remove_path(path);
|
||||
self.logs_state.remove_path(path);
|
||||
}
|
||||
}
|
||||
|
||||
fn delete_content_path(path: &std::path::Path) -> std::io::Result<()> {
|
||||
match std::fs::metadata(path) {
|
||||
Ok(meta) if meta.is_dir() => std::fs::remove_dir_all(path),
|
||||
Ok(_) => std::fs::remove_file(path),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,12 @@ pub struct ContentListState {
|
||||
content_ext: Option<&'static str>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PendingContentDelete {
|
||||
pub name: String,
|
||||
pub path: std::path::PathBuf,
|
||||
}
|
||||
|
||||
impl Default for ContentListState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@@ -322,6 +328,16 @@ impl ContentListState {
|
||||
.map(|(i, _)| i)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn pending_delete(&self) -> Option<PendingContentDelete> {
|
||||
let filtered = self.filtered_indices();
|
||||
let real_idx = self.list_state.selected.and_then(|i| filtered.get(i))?;
|
||||
let entry = self.entries.get(*real_idx)?;
|
||||
Some(PendingContentDelete {
|
||||
name: entry.name.clone(),
|
||||
path: entry.path.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ContentListState {
|
||||
@@ -487,6 +503,19 @@ impl ContentListState {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove_path(&mut self, path: &Path) {
|
||||
self.entries.retain(|entry| entry.path != path);
|
||||
if let Some(sel) = self.list_state.selected {
|
||||
let visible_count = self.filtered_indices().len();
|
||||
if visible_count == 0 {
|
||||
self.list_state.selected = None;
|
||||
} else {
|
||||
self.list_state.selected = Some(sel.min(visible_count.saturating_sub(1)));
|
||||
}
|
||||
}
|
||||
self.update_scrollbar();
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_search_keys(key_event: &KeyEvent, state: &mut ContentListState) -> bool {
|
||||
|
||||
@@ -148,12 +148,14 @@ pub fn render(
|
||||
ContentTab::Mods | ContentTab::ResourcePacks | ContentTab::Shaders => &[
|
||||
("j/k", " navigate"),
|
||||
("⏎", " toggle"),
|
||||
("d", " delete"),
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
],
|
||||
ContentTab::Worlds => &[
|
||||
("j/k", " navigate"),
|
||||
("d", " delete"),
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
@@ -161,6 +163,7 @@ pub fn render(
|
||||
ContentTab::Screenshots => &[
|
||||
("Shift+HJKL", " grid"),
|
||||
("⏎", " open"),
|
||||
("d", " delete"),
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
@@ -170,6 +173,7 @@ pub fn render(
|
||||
&[
|
||||
("j/k", " scroll"),
|
||||
("g/G", " top/bottom"),
|
||||
("d", " delete"),
|
||||
("Esc", " back"),
|
||||
("/", " search"),
|
||||
]
|
||||
@@ -177,6 +181,7 @@ pub fn render(
|
||||
&[
|
||||
("j/k", " navigate"),
|
||||
("⏎", " view"),
|
||||
("d", " delete"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
]
|
||||
|
||||
@@ -228,6 +228,33 @@ impl LogsState {
|
||||
self.viewer_scrollbar_state =
|
||||
ScrollbarState::new(self.viewer_max_scroll).position(self.viewer_scroll);
|
||||
}
|
||||
|
||||
pub fn pending_delete(
|
||||
&self,
|
||||
) -> Option<crate::tui::widgets::content::list::PendingContentDelete> {
|
||||
let index = self.file_index_for_selected()?;
|
||||
let entry = self.entries.get(index)?;
|
||||
Some(crate::tui::widgets::content::list::PendingContentDelete {
|
||||
name: entry.name.clone(),
|
||||
path: entry.path.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn remove_path(&mut self, path: &Path) {
|
||||
self.entries.retain(|entry| entry.path != path);
|
||||
let display_count = self.display_count();
|
||||
if display_count == 0 {
|
||||
self.list_state.selected = None;
|
||||
self.viewer_focused = false;
|
||||
self.selected_path = None;
|
||||
self.viewer_lines.clear();
|
||||
self.viewer_scroll = 0;
|
||||
} else if let Some(sel) = self.list_state.selected {
|
||||
self.list_state.selected = Some(sel.min(display_count.saturating_sub(1)));
|
||||
self.load_selected_content();
|
||||
}
|
||||
self.update_scrollbar();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut LogsState) -> bool {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// "are you sure?" popup for instance deletion. uses global state so the
|
||||
// "are you sure?" popup for destructive actions. uses global state so the
|
||||
// confirmation target persists across render frames.
|
||||
|
||||
use std::sync::LazyLock;
|
||||
@@ -24,16 +24,21 @@ struct ConfirmState {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConfirmTarget {
|
||||
Instance { name: String },
|
||||
ConfigProfile { profile: String },
|
||||
Instance {
|
||||
name: String,
|
||||
},
|
||||
ConfigProfile {
|
||||
profile: String,
|
||||
},
|
||||
Content {
|
||||
name: String,
|
||||
path: std::path::PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
impl ConfirmTarget {
|
||||
fn title(&self) -> String {
|
||||
match self {
|
||||
ConfirmTarget::Instance { name } => format!(" Delete '{}' ", name),
|
||||
ConfirmTarget::ConfigProfile { profile } => format!(" Delete '{}' ", profile),
|
||||
}
|
||||
format!(" Delete '{}' ", self.name())
|
||||
}
|
||||
|
||||
fn body(&self) -> &'static str {
|
||||
@@ -42,13 +47,15 @@ impl ConfirmTarget {
|
||||
ConfirmTarget::ConfigProfile { .. } => {
|
||||
"This will permanently remove this config profile"
|
||||
}
|
||||
ConfirmTarget::Content { .. } => "This will permanently remove the selected item",
|
||||
}
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
pub fn name(&self) -> &str {
|
||||
match self {
|
||||
ConfirmTarget::Instance { name } => name,
|
||||
ConfirmTarget::ConfigProfile { profile } => profile,
|
||||
ConfirmTarget::Content { name, .. } => name,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,6 +75,17 @@ pub fn set_pending_delete(name: impl Into<String>) {
|
||||
set_pending(ConfirmTarget::Instance { name: name.into() });
|
||||
}
|
||||
|
||||
pub fn set_pending_instance_delete(name: impl Into<String>) {
|
||||
set_pending_delete(name);
|
||||
}
|
||||
|
||||
pub fn set_pending_content_delete(name: impl Into<String>, path: impl Into<std::path::PathBuf>) {
|
||||
set_pending(ConfirmTarget::Content {
|
||||
name: name.into(),
|
||||
path: path.into(),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn pending_target() -> Option<ConfirmTarget> {
|
||||
match CONFIRM_STATE.lock() {
|
||||
Ok(s) => s.target.clone(),
|
||||
|
||||
@@ -173,6 +173,30 @@ impl ScreenshotsState {
|
||||
}
|
||||
self.entries.len().div_ceil(self.cols)
|
||||
}
|
||||
|
||||
pub fn pending_delete(
|
||||
&self,
|
||||
) -> Option<crate::tui::widgets::content::list::PendingContentDelete> {
|
||||
let entry = self.entries.get(self.selected)?;
|
||||
Some(crate::tui::widgets::content::list::PendingContentDelete {
|
||||
name: entry.name.clone(),
|
||||
path: entry.path.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn remove_path(&mut self, path: &Path) {
|
||||
self.entries.retain(|entry| entry.path != path);
|
||||
self.protocols.clear();
|
||||
self.requested.clear();
|
||||
|
||||
if self.entries.is_empty() {
|
||||
self.selected = 0;
|
||||
self.scroll_row = 0;
|
||||
} else {
|
||||
self.selected = self.selected.min(self.entries.len().saturating_sub(1));
|
||||
self.ensure_visible();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_key(key_event: &KeyEvent, state: &mut ScreenshotsState) -> bool {
|
||||
|
||||
Reference in new issue
Block a user