class BBS::Widgets::Button
A push button. Label syntax: “Save” or “&Save” (the ampersand marks the hotkey letter). Pressing Alt+letter, or Enter, Space or a click while focused, fires on_click.
Attributes
- label
-
the button text; an
&marks the hotkey letter on_click-
callable invoked as
call(button)when the button fires; returning :halt ends the application
- label
-
the button text; an
&marks the hotkey letter on_click-
callable invoked as
call(button)when the button fires; returning :halt ends the application
Public Class Methods
# File lib/bbs/widgets/button.rb, line 17 def initialize(label: 'OK', on_click: nil, **kw) super(**kw) @label = label @on_click = on_click end
Creates a button with label (default 'OK') and on_click.
BBS::Widget::new
Public Instance Methods
Source
# File lib/bbs/widgets/button.rb, line 31 def display_label = Hotkey.plain(@label)
The label without its & marker.
Source
# File lib/bbs/widgets/button.rb, line 47 def fire return :handled unless enabled @on_click&.call(self) == :halt ? :halt : :handled end
Source
# File lib/bbs/widgets/button.rb, line 24 def focusable? = true
True: a button takes the focus.
Source
# File lib/bbs/widgets/button.rb, line 56 def handle_event(event) return nil unless enabled return fire if event.key? && [:enter, ' '].include?(event.key) Widgets.click_in?(event, bounds) ? fire : nil end
Fires the button on Enter, Space, or a left click inside its bounds; returns the result of fire. Returns nil for other events and when disabled.
Source
# File lib/bbs/widgets/button.rb, line 28 def hotkey = Hotkey.letter(@label)
The hotkey letter after the & in the label, downcased, or nil when the label has none.
Source
# File lib/bbs/widgets/button.rb, line 36 def render(frame) return if bounds.width <= 0 blank(frame, 0, sgr) text = "[ #{display_label} ]"[0, bounds.width] col = [(bounds.width - text.length) / 2, 0].max hotkey_parts && enabled ? put_with_hotkey(frame, col) : put(frame, col, 0, text, sgr) end
Draws the button as [ label ] centred on the first row of the bounds into frame, in the focused, normal or disabled button style, with the hotkey letter highlighted.