Skip to content

Conversation

lipish
Copy link

@lipish lipish commented Oct 21, 2025

Add submit_on_enter Option for Multi-line Input

🎯 Summary

This PR adds a new submit_on_enter configuration option to InputState that allows customizing Enter key behavior in multi-line text inputs.

🔧 What Changed

  • Added submit_on_enter: bool field to InputState struct
  • Added .submit_on_enter(bool) builder method for easy configuration
  • Modified enter() method to respect the new flag
  • Fixed key binding from "secondary-enter" to "shift-enter" for better compatibility

✨ Feature Behavior

When submit_on_enter = true:

  • Enter: Emits PressEnter event without inserting newline (submit behavior)
  • Shift+Enter: Inserts newline with auto-indent

When submit_on_enter = false (default):

  • Both Enter and Shift+Enter insert newline (preserves existing behavior)

💡 Use Case

This feature is essential for chat applications and form inputs where:

  • ✅ Enter should submit/send the message
  • ✅ Shift+Enter should allow multi-line text entry

This is a common UX pattern in modern chat applications (Discord, Slack, ChatGPT, etc.)

📝 Example Usage

use gpui_component::input::{InputEvent, InputState, TextInput};

let input_state = cx.new(|cx| {
    InputState::new(window, cx)
        .multi_line()
        .auto_grow(3, 8)
        .submit_on_enter(true)  // 👈 New option!
});

// Subscribe to the event
let _subscriptions = vec![cx.subscribe_in(&input_state, window, {
    move |this, _, ev: &InputEvent, window, cx| match ev {
        InputEvent::PressEnter { secondary } => {
            if !secondary {
                // Handle submit (Enter was pressed)
                this.send_message(window, cx);
            }
            // Shift+Enter newline is handled internally
        }
        _ => {}
    }
})];

✅ Backward Compatibility

This change is fully backward compatible:

  • ✅ Default behavior (submit_on_enter = false) remains unchanged
  • ✅ No breaking changes to existing APIs
  • ✅ Only adds new optional functionality
  • ✅ All existing code continues to work without modification

🧪 Testing

Tested manually with:

  • Multi-line input with submit_on_enter = true
  • Multi-line input with submit_on_enter = false (default)
  • Auto-grow input behavior
  • Code editor mode

📚 Related

  • Addresses common UX requirement for chat interfaces
  • Complements existing multi-line input features

🤔 Questions for Reviewers

  1. Should we add unit tests for this feature? (Happy to add them)
  2. Any suggestions for improving the API design?
  3. Should this be documented in the README or a separate guide?

Thank you for reviewing! 🙏

This commit adds a new `submit_on_enter` configuration option to `InputState`
that allows users to customize Enter key behavior in multi-line text inputs.

## What Changed

- Added `submit_on_enter: bool` field to `InputState` struct
- Added `.submit_on_enter(bool)` builder method for configuration
- Modified `enter()` method to respect the new flag
- Fixed key binding from "secondary-enter" to "shift-enter" for better compatibility

## Behavior

When `submit_on_enter = true`:
- **Enter**: Emits `PressEnter` event without inserting newline (submit behavior)
- **Shift+Enter**: Inserts newline with auto-indent

When `submit_on_enter = false` (default):
- Both Enter and Shift+Enter insert newline (preserves existing behavior)

## Use Case

This feature is essential for chat applications and form inputs where:
- Enter should submit/send the message
- Shift+Enter should allow multi-line text entry

## Example Usage

```rust
let input_state = cx.new(|cx| {
    InputState::new(window, cx)
        .multi_line()
        .auto_grow(3, 8)
        .submit_on_enter(true)  // Enable Enter to submit
});

// Subscribe to the event
cx.subscribe_in(&input_state, window, {
    move |this, _, ev: &InputEvent, window, cx| match ev {
        InputEvent::PressEnter { secondary } => {
            if !secondary {
                // Handle submit
                this.send_message(window, cx);
            }
        }
        _ => {}
    }
});
```

## Backward Compatibility

This change is fully backward compatible:
- Default behavior (`submit_on_enter = false`) remains unchanged
- No breaking changes to existing APIs
- Only adds new optional functionality

Fixes: N/A (new feature)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant