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

Custom key binding support for text_editor #2522

Merged
merged 2 commits into from
Jul 27, 2024
Merged

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Jul 26, 2024

This PR introduces custom key binding support for the text_editor widget.

You can use the new key_binding method, which accepts a closure that takes a KeyPress and optionally produces a Binding. You can obtain the default binding behavior with Binding::from_key_press.

For instance, let's say we want to produce a Submit message only when Enter is pressed without the Shift modifier:

text_editor(&self.content)
    .key_binding(|event| {
        let modifiers = event.modifiers;

        match text_editor::Binding::from_key_press(event) {
            Some(text_editor::Binding::Enter) if !modifiers.shift() => {
                Some(text_editor::Binding::Custom(Message::Submit))
            }
            binding => binding,
        }
    });

The Binding enum has a Sequence(Vec<Binding>) variant that can be used to produce combinations as well.

I believe these changes should allow users to implement custom key mappings in their editors, like Vim or Kakoune.

@hecrj hecrj added feature New feature or request text widget addition labels Jul 26, 2024
@hecrj hecrj added this to the 0.13 milestone Jul 26, 2024
@hecrj hecrj merged commit c173fa1 into master Jul 27, 2024
24 checks passed
@hecrj hecrj deleted the text-editor-keybindings branch July 27, 2024 13:56
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.

1 participant