Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: arrow keys won't move focus away from TextEdit #3352

Merged
merged 3 commits into from
Sep 18, 2023

Conversation

emilk
Copy link
Owner

@emilk emilk commented Sep 18, 2023

This PR introduces Event::set_focus_lock_filter which lets you tell egui what events a widget has exclusive access to:

/// Controls which events that a focused widget will have exclusive access to.
///
/// Currently this only controls a few special keyboard events,
/// but in the future this `struct` should be extended into a full callback thing.
///
/// Any events not covered by the filter are given to the widget, but are not exclusive.
#[derive(Clone, Copy, Debug)]
pub struct EventFilter {
    /// If `true`, pressing tab will act on the widget,
    /// and NOT move focus away from the focused widget.
    /// 
    /// Default: `false`
    pub tab: bool,

    /// If `true`, pressing arrows will act on the widget,
    /// and NOT move focus away from the focused widget.
    /// 
    /// Default: `false`
    pub arrows: bool,

    /// If `true`, pressing escape will act on the widget,
    /// and NOT surrender focus from the focused widget.
    /// 
    /// Default: `false`
    pub escape: bool,
}

Future work

The event filter was the simplest possible for what is needed today. In the future we should replace it with some sort of callback.

Alternatives

Another way to solve this would be that widgets mark events as handled/consumed. The widget-focus-move logic runs at the end of the frame, so it would just ignored consumed events.

One downside with that would be requiring attaching some bool to each event, which would require quite a bit of a refactor.

Another problem is that it won't handle the case when several widgets are interested in events, since there is no guarantee which orders widgets will be executed (it is immediate mode!). The only thing that could work in that case is an event filter.

@emilk emilk added the egui label Sep 18, 2023
@emilk emilk merged commit ceb8723 into master Sep 18, 2023
34 checks passed
@emilk emilk deleted the emilk/fix-tab-focus branch September 18, 2023 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merge of "change focused widgets with arrow keys" broke l/r keys use while editing single line text fields
1 participant