From ab3b5f40d51d59e4dbae864b56430ba74b53cdc7 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 26 Feb 2024 14:31:50 -0700 Subject: [PATCH] Dialog improvements --- examples/dialog.rs | 2 +- src/app.rs | 8 ++------ src/dialog.rs | 31 ++++++++++++++++++++++++++++--- src/tab.rs | 19 ++++++++++++++----- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/examples/dialog.rs b/examples/dialog.rs index 09c9b27..da84fa4 100644 --- a/examples/dialog.rs +++ b/examples/dialog.rs @@ -122,7 +122,7 @@ impl Application for App { let mut button = widget::button::standard("Save File"); if self.dialog_opt.is_none() { button = button.on_press(Message::DialogOpen(DialogKind::SaveFile { - filename: "test".to_string() + filename: "test".to_string(), })); } column = column.push(button); diff --git a/src/app.rs b/src/app.rs index 8cca3d1..e9dee48 100644 --- a/src/app.rs +++ b/src/app.rs @@ -30,9 +30,9 @@ use crate::{ fl, home_dir, key_bind::{key_binds, KeyBind}, menu, - util, operation::Operation, tab::{self, ItemMetadata, Location, Tab}, + util, }; #[derive(Clone, Debug)] @@ -881,11 +881,7 @@ impl Application for App { match command.spawn() { Ok(_) => (), Err(err) => { - log::warn!( - "failed to open {:?}: {}", - item_path, - err - ); + log::warn!("failed to open {:?}: {}", item_path, err); } } } diff --git a/src/dialog.rs b/src/dialog.rs index d752bb0..4640bf0 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -341,6 +341,7 @@ impl Application for App { let mut tab = Tab::new(location, TabConfig::default()); tab.dialog = Some(flags.kind.clone()); + tab.view = tab::View::List; let mut app = App { core, @@ -457,15 +458,38 @@ impl Application for App { } } } + + // Ensure selection is allowed + //TODO: improve tab logic so this doesn't block the open button so often + for path in paths.iter() { + let path_is_dir = path.is_dir(); + if path_is_dir != self.flags.kind.is_dir() { + if path_is_dir && paths.len() == 1 { + // If the only selected item is a directory and we are selecting files, cd to it + let message = Message::TabMessage(tab::Message::Location( + Location::Path(path.clone()), + )); + return self.update(message); + } else { + // Otherwise, this is not a legal selection + return Command::none(); + } + } + } + + // If there are proper matching items, return them if !paths.is_empty() { self.result_opt = Some(DialogResult::Open(paths)); return window::close(self.main_window_id()); - } else if self.flags.kind.is_dir() { + } + + // If we are in directory mode, return the current directory + if self.flags.kind.is_dir() { match &self.tab.location { Location::Path(tab_path) => { self.result_opt = Some(DialogResult::Open(vec![tab_path.clone()])); return window::close(self.main_window_id()); - }, + } _ => {} } } @@ -513,7 +537,8 @@ impl Application for App { log::warn!("Action {:?} not supported in dialog", action); } tab::Command::ChangeLocation(_tab_title, _tab_path) => { - commands.push(Command::batch([self.update_watcher(), self.rescan_tab()])); + commands + .push(Command::batch([self.update_watcher(), self.rescan_tab()])); } tab::Command::OpenFile(_item_path) => { commands.push(self.update(Message::Open)); diff --git a/src/tab.rs b/src/tab.rs index 74e3ec4..b75bfdf 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -536,7 +536,7 @@ impl Tab { location, context_menu: None, items_opt: None, - view: View::Grid, + view: View::List, dialog: None, drag_opt: None, scroll_opt: None, @@ -566,10 +566,6 @@ impl Tab { }; println!("{:?}", rect); - let (row, col) = match self.view { - View::Grid => (0, 0), - View::List => (0, 0), - }; for (i, item) in items.iter_mut().enumerate() { item.selected = false; //TODO @@ -585,6 +581,19 @@ impl Tab { if let Some(ref mut items) = self.items_opt { for (i, item) in items.iter_mut().enumerate() { if Some(i) == click_i_opt { + // Filter out selection if it does not match dialog kind + if let Some(dialog) = &self.dialog { + let item_is_dir = item.path.is_dir(); + if item_is_dir != dialog.is_dir() { + // Allow selecting folder if dialog is for files to make it + // possible to double click + //TODO: clear any other selection when selecting a folder + if !item_is_dir { + continue; + } + } + } + item.selected = true; if let Some(click_time) = item.click_time { if click_time.elapsed() < DOUBLE_CLICK_DURATION {