Skip to content

Commit

Permalink
Allow arrow keys to move away focus from a Slider
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Nov 27, 2023
1 parent 4dc964a commit f3da3ab
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 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 @@ -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.
Expand All @@ -1256,7 +1262,8 @@ impl Default for EventFilter {
fn default() -> Self {
Self {
tab: false,
arrows: false,
horizontal_arrows: false,
vertical_arrows: false,
escape: false,
}
}
Expand All @@ -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,
}
Expand Down
3 changes: 2 additions & 1 deletion crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ impl Memory {
id,
EventFilter {
tab: lock_focus,
arrows: lock_focus,
horizontal_arrows: lock_focus,
vertical_arrows: lock_focus,
escape: false,
},
);
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 f3da3ab

Please sign in to comment.