Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

[Work in Progress] Add interactive TextInput widget, Interaction* types for interactive widgets #639

Closed
wants to merge 1 commit into from

Conversation

dymk
Copy link

@dymk dymk commented Jul 6, 2022

Description

  • Add widget TextInput - a single-line stateful widget
  • Add trait InteractionHandler when crossterm is used
  • Add trait InteractiveWidget for indicating that the widget is interactive

InteractiveWidget is added so Frame can be passed in by mut reference to the widget's render function. This is used for drawing the cursor for widgets which would like to do such a thing. A new trait is needed, or else backwards compatibility would be broken if the existing Widget or StatefulWidget traits are changed.

The InteractionHandler trait depends on crossterm types, and is thus gated behind the crossterm feature. Widget state objects that want to process user input events take an event (state objects indicate if they're are focused, analogous to Dear imgui's WantCaptureKeyboard), and will return if they consumed the event or not (e.g. should the input be forwarded to the rest of the application).

The TextInput widget and demo is a concise example of how the above Interaction* types are used. It implements a single-line text input field, which responds to a subset of readline (think bash) keyboard shortcuts.

Shortcuts implemented:

  • Go to end of line
  • Go to start of line
  • Delete to start of word under cursor
  • Jump to next word
  • Jump to previous word

My hope is that TextInput can provide the user a simpler and more feature-rich option than the example provided in user_input.rs. While the user_input example is very cool (and indeed what inspired this!), the lack of keyboard controls is intuitive, and it requires the user to implement state tracking.

Consider this a work in progress, as I'd like to get your input on if this would be a good contribution to the project. If so, I'd be happy to spend the time to go add comprehensive docs + doc tests.

Image / Recording

asciicast

Testing guidelines

  • Tested that examples still build when using termion
  • cargo test
  • cargo run --example text_input - touches most features
  • Unit tests added for interactive keyboard control

Checklist

@dymk dymk changed the title Add interactive TextInput widget, Interaction* types for interactive widgets [Work in Progress] Add interactive TextInput widget, Interaction* types for interactive widgets Jul 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants