class BBS::Widgets::Checkbox
A [x] / [ ] check box with a label.
Attributes
- label
-
the text after the box
- checked
-
whether the box is ticked
on_change-
callable invoked as
call(checked, checkbox)when the caller toggles the box
- label
-
the text after the box
- checked
-
whether the box is ticked
on_change-
callable invoked as
call(checked, checkbox)when the caller toggles the box
- label
-
the text after the box
- checked
-
whether the box is ticked
on_change-
callable invoked as
call(checked, checkbox)when the caller toggles the box
Public Class Methods
# File lib/bbs/widgets/checkbox.rb, line 14 def initialize(label: '', checked: false, on_change: nil, **kw) super(**kw) @label = label @checked = checked @on_change = on_change end
Creates a check box with label, ticked when checked.
Calls superclass method
BBS::Widget::new
Public Instance Methods
Source
# File lib/bbs/widgets/checkbox.rb, line 22 def focusable? = true
True: a check box takes the focus.
Source
# File lib/bbs/widgets/checkbox.rb, line 33 def handle_event(event) return nil unless enabled && event.key? && [:enter, ' '].include?(event.key) @checked = !@checked @on_change&.call(@checked, self) :handled end
Toggles the box on Enter or Space and calls on_change; returns :handled. Returns nil for other events and when disabled.
Source
# File lib/bbs/widgets/checkbox.rb, line 25 def render(frame) sgr = focused ? style(:button_focused) : style(:text) frame.move(bounds.x, bounds.y) frame.write("#{@checked ? '[x]' : '[ ]'} #{@label}"[0, bounds.width], sgr: sgr) end
Draws the box and label on the first row of the bounds into frame.