Class: Tuile::Component::Button
- Inherits:
-
Component
- Object
- Component
- Tuile::Component::Button
- Defined in:
- lib/tuile/component/button.rb,
sig/tuile.rbs
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 ] — that natural width is caption.length + 4.
Instance Attribute Summary collapse
-
#caption ⇒ String
@return — the button's label.
-
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click).
Instance Method Summary collapse
- #focusable? ⇒ Boolean
-
#handle_key(key) ⇒ Boolean
@param
key. -
#handle_mouse(event) ⇒ void
@param
event. -
#initialize(caption = "", &on_click) ⇒ Button
constructor
@param
caption— the button's label. - #repaint ⇒ void
- #tab_stop? ⇒ Boolean
Constructor Details
#initialize(caption = "", &on_click) ⇒ Button
@param caption — the button's label.
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
@return — 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.
@return — no-arg callable, or nil.
31 32 33 |
# File 'lib/tuile/component/button.rb', line 31 def on_click @on_click end |
Instance Method Details
#focusable? ⇒ Boolean
43 |
# File 'lib/tuile/component/button.rb', line 43 def focusable? = true |
#handle_key(key) ⇒ Boolean
@param key
49 50 51 52 53 54 55 56 57 |
# File 'lib/tuile/component/button.rb', line 49 def handle_key(key) case key when Keys::ENTER, " " @on_click&.call true else false end end |
#handle_mouse(event) ⇒ void
This method returns an undefined value.
@param event
61 62 63 64 65 66 |
# File 'lib/tuile/component/button.rb', line 61 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.
69 70 71 72 73 74 75 76 |
# File 'lib/tuile/component/button.rb', line 69 def repaint super return if rect.empty? label = "[ #{@caption} ]"[0, rect.width] styled = active? ? StyledString.styled(label, bg: screen.theme.active_bg_color) : StyledString.plain(label) screen.buffer.set_line(rect.left, rect.top, styled) end |
#tab_stop? ⇒ Boolean
45 |
# File 'lib/tuile/component/button.rb', line 45 def tab_stop? = true |