|
| 1 | +use egui::{ |
| 2 | + style::ScrollAnimation, vec2, Context, Key, KeyboardShortcut, Modifiers, PointerButton, |
| 3 | +}; |
| 4 | + |
| 5 | +fn any_widget_focused(ctx: &Context) -> bool { ctx.memory(|mem| mem.focused().is_some()) } |
| 6 | + |
| 7 | +pub fn enter_pressed(ctx: &Context) -> bool { |
| 8 | + if any_widget_focused(ctx) { |
| 9 | + return false; |
| 10 | + } |
| 11 | + ctx.input_mut(|i| { |
| 12 | + i.key_pressed(Key::Enter) |
| 13 | + || i.key_pressed(Key::Space) |
| 14 | + || i.pointer.button_pressed(PointerButton::Extra2) |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +pub fn back_pressed(ctx: &Context) -> bool { |
| 19 | + if any_widget_focused(ctx) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + ctx.input_mut(|i| { |
| 23 | + i.key_pressed(Key::Backspace) |
| 24 | + || i.key_pressed(Key::Escape) |
| 25 | + || i.pointer.button_pressed(PointerButton::Extra1) |
| 26 | + }) |
| 27 | +} |
| 28 | + |
| 29 | +pub fn up_pressed(ctx: &Context) -> bool { |
| 30 | + if any_widget_focused(ctx) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + ctx.input_mut(|i| i.key_pressed(Key::ArrowUp) || i.key_pressed(Key::W)) |
| 34 | +} |
| 35 | + |
| 36 | +pub fn down_pressed(ctx: &Context) -> bool { |
| 37 | + if any_widget_focused(ctx) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + ctx.input_mut(|i| i.key_pressed(Key::ArrowDown) || i.key_pressed(Key::S)) |
| 41 | +} |
| 42 | + |
| 43 | +pub fn page_up_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key::PageUp)) } |
| 44 | + |
| 45 | +pub fn page_down_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key::PageDown)) } |
| 46 | + |
| 47 | +pub fn home_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key::Home)) } |
| 48 | + |
| 49 | +pub fn end_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key::End)) } |
| 50 | + |
| 51 | +pub fn check_scroll_hotkeys(ui: &mut egui::Ui, include_small_increments: bool) { |
| 52 | + let ui_height = ui.available_rect_before_wrap().height(); |
| 53 | + if up_pressed(ui.ctx()) && include_small_increments { |
| 54 | + ui.scroll_with_delta_animation(vec2(0.0, ui_height / 10.0), ScrollAnimation::none()); |
| 55 | + } else if down_pressed(ui.ctx()) && include_small_increments { |
| 56 | + ui.scroll_with_delta_animation(vec2(0.0, -ui_height / 10.0), ScrollAnimation::none()); |
| 57 | + } else if page_up_pressed(ui.ctx()) { |
| 58 | + ui.scroll_with_delta_animation(vec2(0.0, ui_height), ScrollAnimation::none()); |
| 59 | + } else if page_down_pressed(ui.ctx()) { |
| 60 | + ui.scroll_with_delta_animation(vec2(0.0, -ui_height), ScrollAnimation::none()); |
| 61 | + } else if home_pressed(ui.ctx()) { |
| 62 | + ui.scroll_with_delta_animation(vec2(0.0, f32::INFINITY), ScrollAnimation::none()); |
| 63 | + } else if end_pressed(ui.ctx()) { |
| 64 | + ui.scroll_with_delta_animation(vec2(0.0, -f32::INFINITY), ScrollAnimation::none()); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +pub fn consume_up_key(ctx: &Context) -> bool { |
| 69 | + if any_widget_focused(ctx) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + ctx.input_mut(|i| { |
| 73 | + i.consume_key(Modifiers::NONE, Key::ArrowUp) || i.consume_key(Modifiers::NONE, Key::W) |
| 74 | + }) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn consume_down_key(ctx: &Context) -> bool { |
| 78 | + if any_widget_focused(ctx) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + ctx.input_mut(|i| { |
| 82 | + i.consume_key(Modifiers::NONE, Key::ArrowDown) || i.consume_key(Modifiers::NONE, Key::S) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +const OBJECT_FILTER_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::F); |
| 87 | + |
| 88 | +pub fn consume_object_filter_shortcut(ctx: &Context) -> bool { |
| 89 | + ctx.input_mut(|i| i.consume_shortcut(&OBJECT_FILTER_SHORTCUT)) |
| 90 | +} |
| 91 | + |
| 92 | +const SYMBOL_FILTER_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::S); |
| 93 | + |
| 94 | +pub fn consume_symbol_filter_shortcut(ctx: &Context) -> bool { |
| 95 | + ctx.input_mut(|i| i.consume_shortcut(&SYMBOL_FILTER_SHORTCUT)) |
| 96 | +} |
| 97 | + |
| 98 | +const CHANGE_TARGET_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::T); |
| 99 | + |
| 100 | +pub fn consume_change_target_shortcut(ctx: &Context) -> bool { |
| 101 | + ctx.input_mut(|i| i.consume_shortcut(&CHANGE_TARGET_SHORTCUT)) |
| 102 | +} |
| 103 | + |
| 104 | +const CHANGE_BASE_SHORTCUT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::CTRL, Key::B); |
| 105 | + |
| 106 | +pub fn consume_change_base_shortcut(ctx: &Context) -> bool { |
| 107 | + ctx.input_mut(|i| i.consume_shortcut(&CHANGE_BASE_SHORTCUT)) |
| 108 | +} |
0 commit comments