Allow arrow keys to move away focus from a Slider (#3641)

This commit is contained in:
Fredrik Fornwall 2024-01-06 17:39:34 +01:00 committed by GitHub
parent 9faf4b44ff
commit 37762f72ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 11 deletions

View File

@ -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 arrows: bool,
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 vertical_arrows: bool,
/// If `true`, pressing escape will act on the widget,
/// and NOT surrender focus from the focused widget.
@ -1594,7 +1600,8 @@ impl Default for EventFilter {
fn default() -> Self {
Self {
tab: false,
arrows: false,
horizontal_arrows: false,
vertical_arrows: false,
escape: false,
}
}
@ -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,
}

View File

@ -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()
},
);

View File

@ -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,