Class: Tuile::Component::Button
- Inherits:
-
Tuile::Component
- Object
- Tuile::Component
- Tuile::Component::Button
- Defined in:
- lib/tuile/component/button.rb
Overview
A clickable button. Activated by Enter, Space, or a left mouse click; fires the #on_click callback. Renders as ‘[ caption ]` on a single row; the background is highlighted when the button is focused so the user can see which button is active.
Buttons are tab stops — Tab and Shift+Tab will land on them as part of the standard focus cycle. Click-to-focus also works via the inherited #handle_mouse.
Assign a #rect (typically by the surrounding Layout) wide enough to show ‘[ caption ]`; #content_size reports that natural width.
Instance Attribute Summary collapse
-
#caption ⇒ String
The button’s label.
-
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click).
Attributes inherited from Tuile::Component
Instance Method Summary collapse
-
#content_size ⇒ Size
Natural width is ‘caption.length + 4` to fit `[ caption ]`; height is 1.
- #focusable? ⇒ Boolean
- #handle_key(key) ⇒ Boolean
- #handle_mouse(event) ⇒ void
-
#initialize(caption = "") { ... } ⇒ Button
constructor
A new instance of Button.
- #repaint ⇒ void
- #tab_stop? ⇒ Boolean
Methods inherited from Tuile::Component
#active=, #active?, #attached?, #children, #cursor_position, #depth, #find_shortcut_component, #focus, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #root, #screen
Constructor Details
#initialize(caption = "") { ... } ⇒ Button
Returns a new instance of Button.
19 20 21 22 23 |
# File 'lib/tuile/component/button.rb', line 19 def initialize(caption = "", &on_click) super() @caption = caption.to_s @on_click = on_click end |
Instance Attribute Details
#caption ⇒ String
Returns the button’s label.
26 27 28 |
# File 'lib/tuile/component/button.rb', line 26 def caption @caption end |
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click). The callable receives no arguments.
31 32 33 |
# File 'lib/tuile/component/button.rb', line 31 def on_click @on_click end |
Instance Method Details
#content_size ⇒ Size
Returns natural width is ‘caption.length + 4` to fit `[ caption ]`; height is 1.
49 |
# File 'lib/tuile/component/button.rb', line 49 def content_size = Size.new(@caption.length + 4, 1) |
#focusable? ⇒ Boolean
43 |
# File 'lib/tuile/component/button.rb', line 43 def focusable? = true |
#handle_key(key) ⇒ Boolean
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tuile/component/button.rb', line 53 def handle_key(key) return false unless active? return true if super case key when Keys::ENTER, " " @on_click&.call true else false end end |
#handle_mouse(event) ⇒ void
This method returns an undefined value.
68 69 70 71 72 73 |
# File 'lib/tuile/component/button.rb', line 68 def handle_mouse(event) super return unless event. == :left && rect.contains?(event.point) @on_click&.call end |
#repaint ⇒ void
This method returns an undefined value.
76 77 78 79 80 81 82 83 |
# File 'lib/tuile/component/button.rb', line 76 def repaint super return if rect.empty? label = "[ #{@caption} ]"[0, rect.width] styled = active? ? Rainbow(label).bg(:darkslategray) : label screen.print TTY::Cursor.move_to(rect.left, rect.top), styled end |
#tab_stop? ⇒ Boolean
45 |
# File 'lib/tuile/component/button.rb', line 45 def tab_stop? = true |