From ff87795db0353373a01e027515f09d0f23617a3e Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 10 Jan 2024 10:26:30 -0700 Subject: [PATCH] Proper handling of right click with multiple selection --- src/main.rs | 4 +--- src/tab.rs | 67 ++++++++++++++++++++++++++++------------------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/main.rs b/src/main.rs index d9594d3..b6b17b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -644,9 +644,7 @@ impl Application for App { tab.view(self.core()) .map(move |message| Message::TabMessage(entity, message)), ) - .on_press(move |_point_opt| { - Message::TabMessage(entity, tab::Message::Click(None, false)) - }); + .on_press(move |_point_opt| Message::TabMessage(entity, tab::Message::Click(None))); if tab.context_menu.is_some() { mouse_area = mouse_area .on_right_press(move |_point_opt| Message::TabContextMenu(entity, None)); diff --git a/src/tab.rs b/src/tab.rs index 9c89fdd..e386b4b 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -349,9 +349,10 @@ impl Location { #[derive(Clone, Debug)] pub enum Message { - Click(Option, bool), + Click(Option), Location(Location), Parent, + RightClick(usize), View(View), } @@ -507,35 +508,33 @@ impl Tab { pub fn update(&mut self, message: Message) -> bool { let mut cd = None; match message { - Message::Click(click_i_opt, handle_double_click) => { + Message::Click(click_i_opt) => { if let Some(ref mut items) = self.items_opt { for (i, item) in items.iter_mut().enumerate() { if Some(i) == click_i_opt { item.selected = true; - if handle_double_click { - if let Some(click_time) = item.click_time { - if click_time.elapsed() < DOUBLE_CLICK_DURATION { - match self.location { - Location::Path(_) => { - if item.path.is_dir() { - cd = Some(Location::Path(item.path.clone())); - } else { - let mut command = open_command(&item.path); - match command.spawn() { - Ok(_) => (), - Err(err) => { - log::warn!( - "failed to open {:?}: {}", - item.path, - err - ); - } + if let Some(click_time) = item.click_time { + if click_time.elapsed() < DOUBLE_CLICK_DURATION { + match self.location { + Location::Path(_) => { + if item.path.is_dir() { + cd = Some(Location::Path(item.path.clone())); + } else { + let mut command = open_command(&item.path); + match command.spawn() { + Ok(_) => (), + Err(err) => { + log::warn!( + "failed to open {:?}: {}", + item.path, + err + ); } } } - Location::Trash => { - //TODO: open properties? - } + } + Location::Trash => { + //TODO: open properties? } } } @@ -562,6 +561,16 @@ impl Tab { } } } + Message::RightClick(click_i) => { + if let Some(ref mut items) = self.items_opt { + if !items.get(click_i).map_or(false, |x| x.selected) { + // If item not selected, clear selection on other items + for (i, item) in items.iter_mut().enumerate() { + item.selected = i == click_i; + } + } + } + } Message::View(view) => { self.view = view; } @@ -633,15 +642,13 @@ impl Tab { .width(Length::Fixed(128.0)), ) .style(button_style(item.selected)) - .on_press(Message::Click(Some(i), true)); + .on_press(Message::Click(Some(i))); if self.context_menu.is_some() { children.push(button.into()); } else { children.push( crate::mouse_area::MouseArea::new(button) - .on_right_press_no_capture(move |_point_opt| { - Message::Click(Some(i), false) - }) + .on_right_press_no_capture(move |_point_opt| Message::RightClick(i)) .into(), ); } @@ -721,15 +728,13 @@ impl Tab { .spacing(space_xxs), ) .style(button_style(item.selected)) - .on_press(Message::Click(Some(i), true)); + .on_press(Message::Click(Some(i))); if self.context_menu.is_some() { children.push(button.into()); } else { children.push( crate::mouse_area::MouseArea::new(button) - .on_right_press_no_capture(move |_point_opt| { - Message::Click(Some(i), false) - }) + .on_right_press_no_capture(move |_point_opt| Message::RightClick(i)) .into(), ); }