Edit location and rename key bindings

This commit is contained in:
Jeremy Soller 2024-02-28 15:19:07 -07:00
parent 1dc9b9c1b1
commit 24ccc9bd82
No known key found for this signature in database
GPG key ID: D02FD439211AF56F
4 changed files with 35 additions and 2 deletions

View file

@ -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<segmented_button::Entity>),
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::<Tab>(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(()) => (),

View file

@ -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)));

View file

@ -49,10 +49,10 @@ pub fn key_binds() -> HashMap<KeyBind, Action> {
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<KeyBind, Action> {
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);

View file

@ -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<Point>,
pub scroll_opt: Option<Viewport>,
pub edit_location: Option<Location>,
pub edit_location_id: widget::Id,
pub history_i: usize,
pub history: Vec<Location>,
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))))
})