Skip to content

Commit

Permalink
Allow arrow keys to move away focus from a Slider (#3641)
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall authored Jan 6, 2024
1 parent 9faf4b4 commit 37762f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
21 changes: 13 additions & 8 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1576,11 +1576,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.
Expand All @@ -1594,7 +1600,8 @@ impl Default for EventFilter {
fn default() -> Self {
Self {
tab: false,
arrows: false,
horizontal_arrows: false,
vertical_arrows: false,
escape: false,
}
}
Expand All @@ -1605,10 +1612,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,
}
Expand Down
8 changes: 7 additions & 1 deletion crates/egui/src/widgets/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},
);
Expand Down
6 changes: 4 additions & 2 deletions crates/egui/src/widgets/text_edit/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 37762f7

Please sign in to comment.