Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Home directory shortcut #125

Merged
merged 6 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/config/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum KeyBinding {
KeyboardShortcut(egui::KeyboardShortcut),
/// If a pointer button should be used as the keybinding
PointerButton(egui::PointerButton),
/// If a text event should be used as the keybinding.
Text(String),
}

impl KeyBinding {
Expand All @@ -28,6 +30,11 @@ impl KeyBinding {
Self::PointerButton(pointer_button)
}

/// Creates a new keybinding where a text event is used.
pub fn text(text: String) -> Self {
Self::Text(text)
}

/// Checks if the keybinding was pressed by the user.
///
/// # Arguments
Expand All @@ -39,16 +46,48 @@ impl KeyBinding {
/// keybindings, however, it is desired that when they are pressed, the text fields
/// lose focus and the keybinding is executed.
pub fn pressed(&self, ctx: &egui::Context, ignore_if_any_focused: bool) -> bool {
let any_focused = ctx.memory(|r| r.focused()).is_some();

// We want to suppress keyboard input when any other widget like
// text fields have focus.
if ignore_if_any_focused && ctx.memory(|r| r.focused()).is_some() {
if ignore_if_any_focused && any_focused {
return false;
}

match self {
KeyBinding::Key(k) => ctx.input(|i| i.key_pressed(*k)),
KeyBinding::KeyboardShortcut(s) => ctx.input_mut(|i| i.consume_shortcut(s)),
KeyBinding::PointerButton(b) => ctx.input(|i| i.pointer.button_clicked(*b)),
KeyBinding::Text(s) => ctx.input_mut(|i| {
// We force to suppress the text events when any other widget has focus
if any_focused {
return false;
}

let mut found_item: Option<usize> = None;

for (i, text) in i
.events
.iter()
.filter_map(|ev| match ev {
egui::Event::Text(t) => Some(t),
_ => None,
})
.enumerate()
{
if text == s {
found_item = Some(i);
break;
}
}

if let Some(index) = found_item {
i.events.remove(index);
return true;
}

false
}),
}
}
}
Expand All @@ -72,6 +111,8 @@ pub struct FileDialogKeyBindings {
pub new_folder: Vec<KeyBinding>,
/// Shortcut to text edit the current path
pub edit_path: Vec<KeyBinding>,
/// Shortcut to switch to the home directory and text edit the current path
pub home_edit_path: Vec<KeyBinding>,
/// Shortcut to move the selection one item up
pub selection_up: Vec<KeyBinding>,
/// Shortcut to move the selection one item down
Expand Down Expand Up @@ -115,6 +156,10 @@ impl Default for FileDialogKeyBindings {
reload: vec![KeyBinding::key(egui::Key::F5)],
new_folder: vec![KeyBinding::keyboard_shortcut(Modifiers::CTRL, Key::N)],
edit_path: vec![KeyBinding::key(Key::Slash)],
home_edit_path: vec![
KeyBinding::keyboard_shortcut(Modifiers::SHIFT, egui::Key::Backtick),
KeyBinding::text("~".to_string()),
],
selection_up: vec![KeyBinding::key(Key::ArrowUp)],
selection_down: vec![KeyBinding::key(Key::ArrowDown)],
}
Expand Down
17 changes: 10 additions & 7 deletions src/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,8 +1250,6 @@ impl FileDialog {
return;
}

// Whether to activate the text input widget
let mut activate = false;
ui.input(|inp| {
// We stop if any modifier is active besides only shift
if inp.modifiers.any() && !inp.modifiers.shift_only() {
Expand All @@ -1265,13 +1263,9 @@ impl FileDialog {
_ => None,
}) {
self.search_value.push_str(text);
activate = true;
self.init_search = true;
}
});

if activate {
self.init_search = true;
}
}

/// Updates the left panel of the dialog. Including the list of the user directories (Places)
Expand Down Expand Up @@ -1814,6 +1808,15 @@ impl FileDialog {
self.open_path_edit();
}

if FileDialogKeyBindings::any_pressed(ctx, &keybindings.home_edit_path, true) {
if let Some(dirs) = &self.user_directories {
if let Some(home) = dirs.home_dir() {
let _ = self.load_directory(home.to_path_buf().as_path());
self.open_path_edit();
}
}
}

if FileDialogKeyBindings::any_pressed(ctx, &keybindings.selection_up, false) {
self.exec_keybinding_selection_up();

Expand Down
Loading