From f3da3abbdbded10e245d1df06a345baa31784a31 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 27 Nov 2023 10:49:30 +0100 Subject: [PATCH] Allow arrow keys to move away focus from a Slider --- crates/egui/src/data/input.rs | 21 ++++++++++++-------- crates/egui/src/memory.rs | 3 ++- crates/egui/src/widgets/slider.rs | 8 +++++++- crates/egui/src/widgets/text_edit/builder.rs | 6 ++++-- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/egui/src/data/input.rs b/crates/egui/src/data/input.rs index 50073338b3c..3b555c880c7 100644 --- a/crates/egui/src/data/input.rs +++ b/crates/egui/src/data/input.rs @@ -1238,11 +1238,17 @@ pub struct EventFilter { /// Default: `false` pub tab: bool, - /// If `true`, pressing arrows will act on the widget, - /// and NOT move focus away from the focused widget. + /// If `true`, pressing horizontal arrows will act on the + /// widget, and NOT move focus away from the focused widget. + /// + /// Default: `false` + pub horizontal_arrows: bool, + + /// If `true`, pressing vertical arrows will act on the + /// widget, and NOT move focus away from the focused widget. /// /// Default: `false` - pub arrows: bool, + pub vertical_arrows: bool, /// If `true`, pressing escape will act on the widget, /// and NOT surrender focus from the focused widget. @@ -1256,7 +1262,8 @@ impl Default for EventFilter { fn default() -> Self { Self { tab: false, - arrows: false, + horizontal_arrows: false, + vertical_arrows: false, escape: false, } } @@ -1267,10 +1274,8 @@ impl EventFilter { if let Event::Key { key, .. } = event { match key { crate::Key::Tab => self.tab, - crate::Key::ArrowUp - | crate::Key::ArrowRight - | crate::Key::ArrowDown - | crate::Key::ArrowLeft => self.arrows, + crate::Key::ArrowUp | crate::Key::ArrowDown => self.vertical_arrows, + crate::Key::ArrowRight | crate::Key::ArrowLeft => self.horizontal_arrows, crate::Key::Escape => self.escape, _ => true, } diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index 970579806b0..75e5bb32f22 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -675,7 +675,8 @@ impl Memory { id, EventFilter { tab: lock_focus, - arrows: lock_focus, + horizontal_arrows: lock_focus, + vertical_arrows: lock_focus, escape: false, }, ); diff --git a/crates/egui/src/widgets/slider.rs b/crates/egui/src/widgets/slider.rs index 45b0b42e440..431dd6e55c2 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -609,7 +609,13 @@ impl<'a> Slider<'a> { m.set_focus_lock_filter( response.id, EventFilter { - arrows: true, // pressing arrows should not move focus to next widget + // pressing arrows in the orientation of the + // slider should not move focus to next widget + horizontal_arrows: matches!( + self.orientation, + SliderOrientation::Horizontal + ), + vertical_arrows: matches!(self.orientation, SliderOrientation::Vertical), ..Default::default() }, ); diff --git a/crates/egui/src/widgets/text_edit/builder.rs b/crates/egui/src/widgets/text_edit/builder.rs index d96a8a216f7..d397d012cd5 100644 --- a/crates/egui/src/widgets/text_edit/builder.rs +++ b/crates/egui/src/widgets/text_edit/builder.rs @@ -118,8 +118,10 @@ impl<'t> TextEdit<'t> { desired_width: None, desired_height_rows: 4, event_filter: EventFilter { - arrows: true, // moving the cursor is really important - tab: false, // tab is used to change focus, not to insert a tab character + // moving the cursor is really important + horizontal_arrows: true, + vertical_arrows: true, + tab: false, // tab is used to change focus, not to insert a tab character ..Default::default() }, cursor_at_end: true,