Class: Tuile::Component::TextInput
- Inherits:
-
Tuile::Component
- Object
- Tuile::Component
- Tuile::Component::TextInput
- Defined in:
- lib/tuile/component/text_input.rb
Overview
Abstract base for editable text components (TextField, TextArea).
Holds the shared state — a mutable #text buffer, a #caret index, #on_change and #on_escape callbacks — and the keyboard machinery that single-line and multi-line inputs both need: ESC handling, LEFT/RIGHT caret movement, CTRL+LEFT/CTRL+RIGHT word jumps, and the ‘focusable?`/`tab_stop?` flags.
Subclasses implement the layout-specific pieces (#cursor_position, #repaint) and add their own keys (HOME/END, ENTER, UP/DOWN, printable insertion) by overriding the protected #handle_text_input_key hook — ‘super` falls through to the common navigation handling.
The mutation pipeline is a template method: #text= and #caret= detect no-ops, mutate state, fire #on_change, and invalidate. Subclasses inject their own behavior via two protected hooks:
Constant Summary collapse
- ACTIVE_BG_SGR =
256-color SGR for the focused-button highlight (matches what ‘Rainbow(…).bg(:darkslategray)` emits, which is what Button#repaint uses for its focused state).
"\e[48;5;59m"- INACTIVE_BG_SGR =
256-color SGR for the unfocused field’s “well”: index 238 sits in the grayscale ramp (~#444444), bright enough to stand out against non-pure-black terminal themes (Gruvbox/Solarized/OneDark base backgrounds sit in the #1d–#2d range), and still distinctly darker than the active highlight at index 59 (~#5f5f5f). Rainbow’s RGB-to-256 mapping snaps everything dark to palette index 16 (terminal black), so we emit the escape directly to reach the ramp.
"\e[48;5;238m"
Instance Attribute Summary collapse
-
#caret ⇒ Integer
Caret index in ‘0..text.length`.
-
#on_change ⇒ Proc, ...
Optional callback fired whenever #text changes.
-
#on_escape ⇒ Proc, ...
Callback fired when ESC is pressed.
-
#text ⇒ String
Current text contents.
Attributes inherited from Tuile::Component
Instance Method Summary collapse
-
#empty? ⇒ Boolean
True iff #text is the empty string.
- #focusable? ⇒ Boolean
-
#handle_key(key) ⇒ Boolean
Handles a key.
-
#initialize ⇒ TextInput
constructor
A new instance of TextInput.
- #tab_stop? ⇒ Boolean
Methods inherited from Tuile::Component
#active=, #active?, #attached?, #children, #content_size, #cursor_position, #depth, #find_shortcut_component, #focus, #handle_mouse, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #repaint, #root, #screen
Constructor Details
#initialize ⇒ TextInput
Returns a new instance of TextInput.
29 30 31 32 33 34 35 |
# File 'lib/tuile/component/text_input.rb', line 29 def initialize super @text = +"" @caret = 0 @on_change = nil @on_escape = method(:default_on_escape) end |
Instance Attribute Details
#caret ⇒ Integer
Returns caret index in ‘0..text.length`.
44 45 46 |
# File 'lib/tuile/component/text_input.rb', line 44 def caret @caret end |
#on_change ⇒ Proc, ...
50 51 52 |
# File 'lib/tuile/component/text_input.rb', line 50 def on_change @on_change end |
#on_escape ⇒ Proc, ...
Callback fired when ESC is pressed. Defaults to a closure that clears focus (‘screen.focused = nil`) so ESC visibly cancels text entry instead of bubbling to the parent — and, in particular, instead of reaching the screen’s default ESC-to-quit handler. Set to nil to let ESC fall through to the parent again; set to any other callable to replace the default.
58 59 60 |
# File 'lib/tuile/component/text_input.rb', line 58 def on_escape @on_escape end |
#text ⇒ String
Returns current text contents.
38 39 40 |
# File 'lib/tuile/component/text_input.rb', line 38 def text @text end |
Instance Method Details
#empty? ⇒ Boolean
Returns true iff #text is the empty string.
41 |
# File 'lib/tuile/component/text_input.rb', line 41 def empty? = @text.empty? |
#focusable? ⇒ Boolean
60 |
# File 'lib/tuile/component/text_input.rb', line 60 def focusable? = true |
#handle_key(key) ⇒ Boolean
Handles a key. Returns false when the component is inactive. Otherwise first runs the Tuile::Component#handle_key shortcut search via ‘super`, then delegates to #handle_text_input_key.
111 112 113 114 115 116 |
# File 'lib/tuile/component/text_input.rb', line 111 def handle_key(key) return false unless active? return true if super handle_text_input_key(key) end |
#tab_stop? ⇒ Boolean
62 |
# File 'lib/tuile/component/text_input.rb', line 62 def tab_stop? = true |