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:
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
#content_size, #key_shortcut, #on_theme_changed, #parent, #rect
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, #cursor_position, #depth, #find_shortcut_component, #focus, #handle_mouse, #keyboard_hint, #on_child_content_size_changed, #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.
96 97 98 99 100 101 |
# File 'lib/tuile/component/text_input.rb', line 96 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 |