From 24ccc9bd82faf790c9389793507cd18eaa48ab8c Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 28 Feb 2024 15:19:07 -0700 Subject: [PATCH] Edit location and rename key bindings --- src/app.rs | 21 +++++++++++++++++++++ src/dialog.rs | 3 +++ src/key_bind.rs | 6 ++++-- src/tab.rs | 7 +++++++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9da949b..a6a3cbc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -44,6 +44,7 @@ pub enum Action { About, Copy, Cut, + EditLocation, HistoryNext, HistoryPrevious, LocationUp, @@ -74,6 +75,7 @@ impl Action { Action::About => Message::ToggleContextPage(ContextPage::About), Action::Copy => Message::Copy(entity_opt), Action::Cut => Message::Cut(entity_opt), + Action::EditLocation => Message::EditLocation(entity_opt), Action::HistoryNext => Message::TabMessage(None, tab::Message::GoNext), Action::HistoryPrevious => Message::TabMessage(None, tab::Message::GoPrevious), Action::LocationUp => Message::TabMessage(None, tab::Message::LocationUp), @@ -114,6 +116,7 @@ pub enum Message { DialogCancel, DialogComplete, DialogUpdate(DialogPage), + EditLocation(Option), Key(Modifiers, Key), LaunchUrl(String), Modifiers(Modifiers), @@ -739,6 +742,21 @@ impl Application for App { //TODO: panicless way to do this? self.dialog_pages[0] = dialog_page; } + Message::EditLocation(entity_opt) => { + let entity = entity_opt.unwrap_or_else(|| self.tab_model.active()); + if let Some(location) = self.tab_model.data::(entity).and_then(|tab| { + if tab.edit_location.is_none() { + Some(tab.location.clone()) + } else { + None + } + }) { + return self.update(Message::TabMessage( + Some(entity), + tab::Message::EditLocation(Some(location)), + )); + } + } Message::Key(modifiers, key) => { let entity = self.tab_model.active(); for (key_bind, action) in self.key_binds.iter() { @@ -1024,6 +1042,9 @@ impl Application for App { self.rescan_tab(entity, tab_path), ])); } + tab::Command::FocusTextInput(id) => { + commands.push(widget::text_input::focus(id)); + } tab::Command::OpenFile(item_path) => { match open::that_detached(&item_path) { Ok(()) => (), diff --git a/src/dialog.rs b/src/dialog.rs index 805bef2..db2cbde 100644 --- a/src/dialog.rs +++ b/src/dialog.rs @@ -572,6 +572,9 @@ impl Application for App { commands .push(Command::batch([self.update_watcher(), self.rescan_tab()])); } + tab::Command::FocusTextInput(id) => { + commands.push(widget::text_input::focus(id)); + } tab::Command::OpenFile(_item_path) => { if self.flags.kind.save() { commands.push(self.update(Message::Save(false))); diff --git a/src/key_bind.rs b/src/key_bind.rs index b17d32d..92cc04c 100644 --- a/src/key_bind.rs +++ b/src/key_bind.rs @@ -49,10 +49,10 @@ pub fn key_binds() -> HashMap { let mut key_binds = HashMap::new(); macro_rules! bind { - ([$($modifier:ident),+ $(,)?], $key:expr, $action:ident) => {{ + ([$($modifier:ident),* $(,)?], $key:expr, $action:ident) => {{ key_binds.insert( KeyBind { - modifiers: vec![$(Modifier::$modifier),+], + modifiers: vec![$(Modifier::$modifier),*], key: $key, }, Action::$action, @@ -62,10 +62,12 @@ pub fn key_binds() -> HashMap { bind!([Ctrl], Key::Character("c".into()), Copy); bind!([Ctrl], Key::Character("x".into()), Cut); + bind!([Ctrl], Key::Character("l".into()), EditLocation); bind!([Alt], Key::Named(Named::ArrowRight), HistoryNext); bind!([Alt], Key::Named(Named::ArrowLeft), HistoryPrevious); bind!([Alt], Key::Named(Named::ArrowUp), LocationUp); bind!([Ctrl], Key::Character("v".into()), Paste); + bind!([], Key::Named(Named::F2), Rename); bind!([Ctrl], Key::Character("a".into()), SelectAll); bind!([Ctrl], Key::Character("w".into()), TabClose); bind!([Ctrl], Key::Character("t".into()), TabNew); diff --git a/src/tab.rs b/src/tab.rs index 9ec67a5..099177a 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -357,6 +357,7 @@ impl Location { pub enum Command { Action(Action), ChangeLocation(String, Location), + FocusTextInput(widget::Id), OpenFile(PathBuf), } @@ -530,6 +531,7 @@ pub struct Tab { pub drag_opt: Option, pub scroll_opt: Option, pub edit_location: Option, + pub edit_location_id: widget::Id, pub history_i: usize, pub history: Vec, pub config: TabConfig, @@ -551,6 +553,7 @@ impl Tab { drag_opt: None, scroll_opt: None, edit_location: None, + edit_location_id: widget::Id::unique(), history_i: 0, history, config, @@ -678,6 +681,9 @@ impl Tab { } }, Message::EditLocation(edit_location) => { + if self.edit_location.is_none() && edit_location.is_some() { + commands.push(Command::FocusTextInput(self.edit_location_id.clone())); + } self.edit_location = edit_location; } Message::GoNext => { @@ -883,6 +889,7 @@ impl Tab { ); row = row.push( widget::text_input("", path.to_string_lossy()) + .id(self.edit_location_id.clone()) .on_input(|input| { Message::EditLocation(Some(Location::Path(PathBuf::from(input)))) })