Class: Potty::Widgets::CheckboxGroup
- Defined in:
- lib/potty/widgets/checkbox_group.rb
Overview
Multi-select list of options, one row each. Up/down move a cursor, Space/Enter toggles the option under it. The RadioGroup sibling for “choose any number”. Emits :change(selected_values) on each toggle.
Instance Attribute Summary collapse
-
#on_change ⇒ Object
Returns the value of attribute on_change.
Attributes inherited from Base
#app, #focused, #parent, #rect
Instance Method Summary collapse
- #can_focus? ⇒ Boolean
- #handle_key(ch) ⇒ Object
-
#initialize(app, options: [], selected: [], on_change: nil) ⇒ CheckboxGroup
constructor
A new instance of CheckboxGroup.
- #options ⇒ Object
- #preferred_height(_width) ⇒ Object
- #render(window) ⇒ Object
-
#selected ⇒ Object
A snapshot of the selected values (safe to store).
- #selected?(value) ⇒ Boolean
Methods inherited from Base
#activate, #blur, #deactivate, #focus, #handle_escape, #hide, #layout, #on_blur, #on_focus, #on_layout, #show, #theme, #tick, #visible=, #visible?
Methods included from Events
#emit, #listeners?, #off, #on
Constructor Details
#initialize(app, options: [], selected: [], on_change: nil) ⇒ CheckboxGroup
Returns a new instance of CheckboxGroup.
15 16 17 18 19 20 21 |
# File 'lib/potty/widgets/checkbox_group.rb', line 15 def initialize(app, options: [], selected: [], on_change: nil) super(app) @options = normalize() @selected = Array(selected).dup @cursor = 0 @on_change = on_change end |
Instance Attribute Details
#on_change ⇒ Object
Returns the value of attribute on_change.
13 14 15 |
# File 'lib/potty/widgets/checkbox_group.rb', line 13 def on_change @on_change end |
Instance Method Details
#can_focus? ⇒ Boolean
23 24 25 |
# File 'lib/potty/widgets/checkbox_group.rb', line 23 def can_focus? true end |
#handle_key(ch) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/potty/widgets/checkbox_group.rb', line 44 def handle_key(ch) case ch when Keys::UP move(-1) when Keys::DOWN move(1) when Keys::SPACE, *Keys::ENTERS toggle(@cursor) else return false end true end |
#options ⇒ Object
27 28 29 |
# File 'lib/potty/widgets/checkbox_group.rb', line 27 def @options end |
#preferred_height(_width) ⇒ Object
40 41 42 |
# File 'lib/potty/widgets/checkbox_group.rb', line 40 def preferred_height(_width) @options.size end |
#render(window) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/potty/widgets/checkbox_group.rb', line 58 def render(window) return unless @visible && @rect @options.each_with_index do |opt, i| break if i >= @rect.height mark = selected?(opt[:value]) ? "[\u2713]" : "[ ]" on_cursor = @focused && i == @cursor attr = on_cursor ? theme.attr(:selected, bold: true) : theme[:normal] window.setpos(@rect.y + i, @rect.x) window.attron(attr) { window.addstr("#{mark} #{opt[:label]}"[0, @rect.width]) } end end |
#selected ⇒ Object
A snapshot of the selected values (safe to store).
32 33 34 |
# File 'lib/potty/widgets/checkbox_group.rb', line 32 def selected @selected.dup end |
#selected?(value) ⇒ Boolean
36 37 38 |
# File 'lib/potty/widgets/checkbox_group.rb', line 36 def selected?(value) @selected.include?(value) end |