Class: Tuile::Component::TextField

Inherits:
TextInput show all
Defined in:
lib/tuile/component/text_field.rb

Overview

A single-line text input field with hardware-cursor caret.

The field does not scroll. Any keystroke that would make Tuile::Component::TextInput#text longer than ‘rect.width - 1` (the last column is reserved for the caret past the last char) is rejected.

The caret is a logical index in ‘0..text.length`. The hardware cursor is positioned by Screen after each repaint cycle when this component is focused; see #cursor_position.

Constant Summary

Constants inherited from TextInput

Tuile::Component::TextInput::ACTIVE_BG_SGR, Tuile::Component::TextInput::INACTIVE_BG_SGR

Instance Attribute Summary collapse

Attributes inherited from TextInput

#caret, #on_change, #on_escape, #text

Attributes inherited from Tuile::Component

#key_shortcut, #parent, #rect

Instance Method Summary collapse

Methods inherited from TextInput

#empty?, #focusable?, #handle_key, #tab_stop?

Methods inherited from Tuile::Component

#active=, #active?, #attached?, #children, #content_size, #depth, #find_shortcut_component, #focus, #focusable?, #handle_key, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #root, #screen, #tab_stop?

Constructor Details

#initializeTextField

Returns a new instance of TextField.



15
16
17
18
19
20
# File 'lib/tuile/component/text_field.rb', line 15

def initialize
  super
  @on_key_up = nil
  @on_key_down = nil
  @on_enter = nil
end

Instance Attribute Details

#on_enterProc, ...

Optional callback fired when ENTER is pressed. When set, ENTER is consumed by the field; when nil, ENTER falls through to the parent (default behavior).

Returns:

  • (Proc, Method, nil)

    no-arg callable, or nil.



40
41
42
# File 'lib/tuile/component/text_field.rb', line 40

def on_enter
  @on_enter
end

#on_key_downProc, ...

Optional callback fired when the DOWN arrow key is pressed. When set, DOWN is consumed by the field; when nil, DOWN falls through to the parent (default behavior). Only triggered by Keys::DOWN_ARROW, not by ‘j`, since `j` is a printable character inserted into Tuile::Component::TextInput#text.

Returns:

  • (Proc, Method, nil)

    no-arg callable, or nil.



34
35
36
# File 'lib/tuile/component/text_field.rb', line 34

def on_key_down
  @on_key_down
end

#on_key_upProc, ...

Optional callback fired when the UP arrow key is pressed. When set, UP is consumed by the field; when nil, UP falls through to the parent (default behavior). Only triggered by Keys::UP_ARROW, not by ‘k`, since `k` is a printable character inserted into Tuile::Component::TextInput#text.

Returns:

  • (Proc, Method, nil)

    no-arg callable, or nil.



27
28
29
# File 'lib/tuile/component/text_field.rb', line 27

def on_key_up
  @on_key_up
end

Instance Method Details

#cursor_positionPoint?

Returns:



43
44
45
46
47
# File 'lib/tuile/component/text_field.rb', line 43

def cursor_position
  return nil unless rect.width.positive?

  Point.new(rect.left + @caret, rect.top)
end

#handle_mouse(event) ⇒ void

This method returns an undefined value.

Parameters:



51
52
53
54
55
56
# File 'lib/tuile/component/text_field.rb', line 51

def handle_mouse(event)
  super
  return unless event.button == :left && rect.contains?(event.point)

  self.caret = (event.x - rect.left).clamp(0, @text.length)
end

#repaintvoid

This method returns an undefined value.



59
60
61
62
63
64
65
# File 'lib/tuile/component/text_field.rb', line 59

def repaint
  return if rect.empty?

  bg = active? ? ACTIVE_BG_SGR : INACTIVE_BG_SGR
  padded = @text + (" " * (rect.width - @text.length))
  screen.print TTY::Cursor.move_to(rect.left, rect.top), bg, padded, Ansi::RESET
end