Input widget
This module provides text input widgets with cursor support, selection, and keyboard navigation.
Types
BorderStyle = enum NoBorder, SingleBorder, DoubleBorder, RoundedBorder
- Border style options
Input = ref object of Widget state*: InputState placeholder*: string normalStyle*: Style focusedStyle*: Style placeholderStyle*: Style cursorStyle*: Style selectionStyle*: Style borderStyle*: BorderStyle borderNormalStyle*: Style borderFocusedStyle*: Style maxLength*: int readOnly*: bool password*: bool onTextChanged*: proc (text: string) onEnter*: proc (text: string) onFocus*: proc () onBlur*: proc () onKeyPress*: proc (key: KeyEvent): bool
- Text input widget
InputState = object text*: string cursor*: int selection*: tuple[start, stop: int] offset*: int focused*: bool
Procs
proc clearSelection(widget: Input) {....raises: [], tags: [], forbids: [].}
- Clear text selection
proc deleteSelection(widget: Input) {....raises: [Exception], tags: [RootEffect], forbids: [].}
- Delete selected text
proc deleteText(widget: Input; start: int; length: int) {....raises: [Exception], tags: [RootEffect], forbids: [].}
- Delete text range
proc getCursorPosition(widget: Input; area: Rect): tuple[x, y: int, visible: bool] {....raises: [], tags: [], forbids: [].}
- Get the screen cursor position for this input widget Returns the absolute screen coordinates and visibility
proc getSelection(widget: Input): tuple[start, stop: int] {....raises: [], tags: [], forbids: [].}
- Get selection range (normalized)
proc handleKeyEvent(widget: Input; event: KeyEvent): bool {....raises: [Exception], tags: [RootEffect], forbids: [].}
- Handle keyboard input for the input widget Returns true if the event was handled
proc hasSelection(widget: Input): bool {....raises: [], tags: [], forbids: [].}
- Check if there's a text selection
proc input(placeholder: string = ""; normalStyle: Style = style(White, Reset); focusedStyle: Style = style(White, Blue); placeholderStyle: Style = style(BrightBlack, Reset); cursorStyle: Style = style(Black, White); selectionStyle: Style = style(White, BrightBlue); borderStyle: BorderStyle = NoBorder; borderNormalStyle: Style = style(BrightBlack, Reset); borderFocusedStyle: Style = style(Blue, Reset); maxLength: int = 0; readOnly: bool = false; password: bool = false; onTextChanged: proc (text: string) = nil; onEnter: proc (text: string) = nil; onFocus: proc () = nil; onBlur: proc () = nil; onKeyPress: proc (key: KeyEvent): bool = nil): Input {. ...raises: [], tags: [], forbids: [].}
- Convenience constructor for Input widget
proc insertText(widget: Input; text: string; pos: int = -1) {. ...raises: [Exception], tags: [RootEffect], forbids: [].}
- Insert text at position (or at cursor if pos = -1)
proc newInput(placeholder: string = ""; normalStyle: Style = style(White, Reset); focusedStyle: Style = style(White, Blue); placeholderStyle: Style = style(BrightBlack, Reset); cursorStyle: Style = style(Black, White); selectionStyle: Style = style(White, BrightBlue); borderStyle: BorderStyle = NoBorder; borderNormalStyle: Style = style(BrightBlack, Reset); borderFocusedStyle: Style = style(Blue, Reset); maxLength: int = 0; readOnly: bool = false; password: bool = false; onTextChanged: proc (text: string) = nil; onEnter: proc (text: string) = nil; onFocus: proc () = nil; onBlur: proc () = nil; onKeyPress: proc (key: KeyEvent): bool = nil): Input {....raises: [], tags: [], forbids: [].}
- Create a new Input widget
proc passwordInput(placeholder: string = "Password"; maxLength: int = 0; onEnter: proc (text: string) = nil; onFocus: proc () = nil; onBlur: proc () = nil; onTextChanged: proc (text: string) = nil): Input {. ...raises: [], tags: [], forbids: [].}
- Create a password input widget
proc readOnlyInput(text: string; normalStyle: Style = style(BrightBlack, Reset)): Input {. ...raises: [Exception], tags: [RootEffect], forbids: [].}
- Create a read-only input widget
proc searchInput(placeholder: string = "Search..."; onTextChanged: proc (text: string) = nil; onEnter: proc (text: string) = nil; onFocus: proc () = nil; onBlur: proc () = nil): Input {....raises: [], tags: [], forbids: [].}
- Create a search input widget
proc withEventHandlers(widget: Input; onTextChanged: proc (text: string) = nil; onEnter: proc (text: string) = nil; onFocus: proc () = nil; onBlur: proc () = nil; onKeyPress: proc (key: KeyEvent): bool = nil): Input {. ...raises: [], tags: [], forbids: [].}
- Create a copy with different event handlers
proc withMaxLength(widget: Input; maxLength: int): Input {....raises: [], tags: [], forbids: [].}
- Create a copy with different max length
proc withPlaceholder(widget: Input; placeholder: string): Input {....raises: [], tags: [], forbids: [].}
- Create a copy with different placeholder
Methods
method getMinSize(widget: Input): Size {....raises: [], tags: [], forbids: [].}
- Get minimum size for input widget
method getPreferredSize(widget: Input; available: Size): Size {....raises: [], tags: [], forbids: [].}
- Get preferred size for input widget