Class: Tuile::Component::Button
- Inherits:
-
Component
- Object
- Component
- Tuile::Component::Button
- Includes:
- HasCaption
- 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.display_width + 4.
A narrower #rect truncates the label with an ellipsis; a wider one leaves
a tail that focuses but doesn't activate (see #extent).
Instance Attribute Summary collapse
-
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click).
Instance Method Summary collapse
-
#caption ⇒ StyledString
Read through this method, never
@caption— the ivar stays nil until the first non-empty set (#caption= short-circuits when unchanged). -
#caption= ⇒ void
Sets the caption and invalidates the component.
-
#extent ⇒ Rect
The cells the button actually paints: one row,
caption.display_width + 4columns, clipped to #rect. - #focusable? ⇒ Boolean
-
#handle_key(key) ⇒ Boolean
@param
key. - #handle_mouse(event) ⇒ void
-
#initialize(caption = nil, &on_click) ⇒ Button
constructor
@param
caption— the button's label, coerced the same way HasCaption#caption= coerces it. - #repaint ⇒ void
- #tab_stop? ⇒ Boolean
Constructor Details
#initialize(caption = nil, &on_click) ⇒ Button
@param caption — the button's label, coerced the same way HasCaption#caption= coerces it.
24 25 26 27 28 |
# File 'lib/tuile/component/button.rb', line 24 def initialize(caption = nil, &on_click) super() self.caption = caption @on_click = on_click end |
Instance Attribute Details
#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.
33 34 35 |
# File 'lib/tuile/component/button.rb', line 33 def on_click @on_click end |
Instance Method Details
#caption ⇒ StyledString
Read through this method, never @caption — the ivar stays nil until
the first non-empty set (#caption= short-circuits when unchanged).
@return — the caption; empty when never set.
2213 |
# File 'sig/tuile.rbs', line 2213
def caption: () -> StyledString
|
#caption= ⇒ void
This method returns an undefined value.
Sets the caption and invalidates the component. No-op when unchanged. A
String is parsed via StyledString.parse (embedded ANSI is honored);
a StyledString is used as-is; nil clears it.
@param new_caption
2220 |
# File 'sig/tuile.rbs', line 2220
def caption=: ((String | StyledString)? new_caption) -> void
|
#extent ⇒ Rect
The cells the button actually paints: one row, caption.display_width + 4
columns, clipped to Tuile::Component#rect. Both the focus highlight and the click hit
test use it, so a click on the blank tail of an over-wide rect — or on a
lower row, when the rect is taller than one — does not fire #on_click.
It still focuses: Tuile::Component#handle_mouse's click-to-focus is ungated
by geometry. Same rule as Checkbox#extent, which documents the two
traps behind it.
59 |
# File 'lib/tuile/component/button.rb', line 59 def extent = Rect.new(rect.left, rect.top, [caption.display_width + 4, rect.width].min, 1) |
#focusable? ⇒ Boolean
35 |
# File 'lib/tuile/component/button.rb', line 35 def focusable? = true |
#handle_key(key) ⇒ Boolean
@param key
41 42 43 44 45 46 47 48 49 |
# File 'lib/tuile/component/button.rb', line 41 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.
Fires #on_click on a left click within #extent; super runs first, so
a click anywhere in Tuile::Component#rect still focuses.
@param event
65 66 67 68 69 70 |
# File 'lib/tuile/component/button.rb', line 65 def handle_mouse(event) super return unless event. == :left && extent.contains?(event.point) @on_click&.call end |
#repaint ⇒ void
This method returns an undefined value.
73 74 75 76 77 78 79 80 |
# File 'lib/tuile/component/button.rb', line 73 def repaint super return if rect.empty? label = (StyledString.plain("[ ") + caption + StyledString.plain(" ]")).ellipsize(rect.width) label = label.with_bg(screen.theme.active_bg_color) if active? draw_line(rect.left, rect.top, label) end |
#tab_stop? ⇒ Boolean
37 |
# File 'lib/tuile/component/button.rb', line 37 def tab_stop? = true |