Skip to content

Custom key binding support for text_editor#2522

Merged
hecrj merged 2 commits into
masterfrom
text-editor-keybindings
Jul 27, 2024
Merged

Custom key binding support for text_editor#2522
hecrj merged 2 commits into
masterfrom
text-editor-keybindings

Conversation

@hecrj

@hecrj hecrj commented Jul 26, 2024

Copy link
Copy Markdown
Member

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 force-pushed the text-editor-keybindings branch from d4b8f48 to e73c8b0 Compare July 26, 2024 09:02
@hecrj hecrj merged commit c173fa1 into master Jul 27, 2024
@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