egui_file_dialog/config/
keybindings.rs1#[derive(Debug, Clone)]
3pub enum KeyBinding {
4 Key(egui::Key),
6 KeyboardShortcut(egui::KeyboardShortcut),
8 PointerButton(egui::PointerButton),
10 Text(String),
12}
13
14impl KeyBinding {
15 pub const fn key(key: egui::Key) -> Self {
17 Self::Key(key)
18 }
19
20 pub const fn keyboard_shortcut(modifiers: egui::Modifiers, logical_key: egui::Key) -> Self {
22 Self::KeyboardShortcut(egui::KeyboardShortcut {
23 modifiers,
24 logical_key,
25 })
26 }
27
28 pub const fn pointer_button(pointer_button: egui::PointerButton) -> Self {
30 Self::PointerButton(pointer_button)
31 }
32
33 pub const fn text(text: String) -> Self {
35 Self::Text(text)
36 }
37
38 pub fn pressed(&self, ctx: &egui::Context, ignore_if_any_focused: bool) -> bool {
49 let any_focused = ctx.memory(egui::Memory::focused).is_some();
50
51 if ignore_if_any_focused && any_focused {
54 return false;
55 }
56
57 match self {
58 Self::Key(k) => ctx.input(|i| i.key_pressed(*k)),
59 Self::KeyboardShortcut(s) => ctx.input_mut(|i| i.consume_shortcut(s)),
60 Self::PointerButton(b) => ctx.input(|i| i.pointer.button_clicked(*b)),
61 Self::Text(s) => ctx.input_mut(|i| {
62 if any_focused {
64 return false;
65 }
66
67 let mut found_item: Option<usize> = None;
68
69 for (i, text) in i
70 .events
71 .iter()
72 .filter_map(|ev| match ev {
73 egui::Event::Text(t) => Some(t),
74 _ => None,
75 })
76 .enumerate()
77 {
78 if text == s {
79 found_item = Some(i);
80 break;
81 }
82 }
83
84 if let Some(index) = found_item {
85 i.events.remove(index);
86 return true;
87 }
88
89 false
90 }),
91 }
92 }
93}
94
95#[derive(Debug, Clone)]
97pub struct FileDialogKeyBindings {
98 pub submit: Vec<KeyBinding>,
100 pub cancel: Vec<KeyBinding>,
102 pub parent: Vec<KeyBinding>,
104 pub back: Vec<KeyBinding>,
106 pub forward: Vec<KeyBinding>,
108 pub reload: Vec<KeyBinding>,
110 pub new_folder: Vec<KeyBinding>,
112 pub edit_path: Vec<KeyBinding>,
114 pub home_edit_path: Vec<KeyBinding>,
116 pub selection_up: Vec<KeyBinding>,
118 pub selection_down: Vec<KeyBinding>,
120 pub select_all: Vec<KeyBinding>,
122}
123
124impl FileDialogKeyBindings {
125 pub fn any_pressed(
127 ctx: &egui::Context,
128 keybindings: &Vec<KeyBinding>,
129 suppress_if_any_focused: bool,
130 ) -> bool {
131 for keybinding in keybindings {
132 if keybinding.pressed(ctx, suppress_if_any_focused) {
133 return true;
134 }
135 }
136
137 false
138 }
139}
140
141impl Default for FileDialogKeyBindings {
142 fn default() -> Self {
143 use egui::{Key, Modifiers, PointerButton};
144
145 Self {
146 submit: vec![KeyBinding::key(Key::Enter)],
147 cancel: vec![KeyBinding::key(Key::Escape)],
148 parent: vec![KeyBinding::keyboard_shortcut(Modifiers::ALT, Key::ArrowUp)],
149 back: vec![
150 KeyBinding::pointer_button(PointerButton::Extra1),
151 KeyBinding::keyboard_shortcut(Modifiers::ALT, Key::ArrowLeft),
152 KeyBinding::key(Key::Backspace),
153 ],
154 forward: vec![
155 KeyBinding::pointer_button(PointerButton::Extra2),
156 KeyBinding::keyboard_shortcut(Modifiers::ALT, Key::ArrowRight),
157 ],
158 reload: vec![KeyBinding::key(egui::Key::F5)],
159 new_folder: vec![KeyBinding::keyboard_shortcut(Modifiers::COMMAND, Key::N)],
160 edit_path: vec![KeyBinding::key(Key::Slash)],
161 home_edit_path: vec![
162 KeyBinding::keyboard_shortcut(Modifiers::SHIFT, egui::Key::Backtick),
163 KeyBinding::text("~".to_string()),
164 ],
165 selection_up: vec![KeyBinding::key(Key::ArrowUp)],
166 selection_down: vec![KeyBinding::key(Key::ArrowDown)],
167 select_all: vec![KeyBinding::keyboard_shortcut(Modifiers::COMMAND, Key::A)],
168 }
169 }
170}