Class: Tuile::Component::RadioGroup
- Inherits:
-
Component
- Object
- Component
- Tuile::Component::RadioGroup
- Includes:
- HasContent, HasValue
- Defined in:
- lib/tuile/component/radio_group.rb,
sig/tuile.rbs
Overview
Single-select from a set of typed items, one row each. Arrows move a cursor; Space, Enter or a left click selects the row under it:
(*) Ascending
( ) Descending <- cursor row, highlighted across the full width
( ) Unsorted
^ the composed {List}'s one-column gutter
rg = Component::RadioGroup.new(items: %w[Ascending Descending Unsorted])
rg.value = "Descending" # or seed it via the ctor
rg.on_value_change = ->(order) { resort(order) }
rg.value # => "Descending"
rg.item_label = ->(o) { o.title } # default :to_s
#value is the selected item itself — of whatever type #items
holds, never its label. nil means nothing is selected: that is the
initial state, and assigning it is the only way back, since Space on the
already-selected row is a no-op rather than a deselect.
Composes rather than subclasses, like ComboBox: a List is its single
HasContent child, which is where the cursor, scrolling, the scrollbar
and per-row mouse hit-testing come from. content is that list, so an app
can tune it (scrollbar_visibility, show_cursor_when_inactive, …). Rows
beyond #rect's height scroll; the inner list is the tab stop, not the
group.
The cursor is chrome
The cursor and the selection are two independent things, as in CheckboxGroup — arrows roam without changing #value, so a listener that resorts a pane fires once on intent instead of once per row crossed. #value= therefore does not move the cursor. An app that wants it parked on the selection parks it:
rg.content.cursor = List::Cursor.new(position: rg.items.index(rg.value))
#items= is the one thing that moves it, clamping it back into range.
items is chrome; value is authoritative
#items= changes only what is presented. It never touches #value and never fires HasValue#on_value_change, and a selected item absent from #items renders no marked row while surviving intact — so a form saved without the user editing anything changes nothing silently. Keeping the two in sync is the app's job. Same contract as ComboBox#value and CheckboxGroup#value.
Implementation details
Two ==-equal items share one selection, so selecting either marks both
rows; two distinct items that merely render the same label stay
independent, because a row resolves to an item by index.
Rows are (*) /( ) literals, mirroring Checkbox's convention rather
than importing constants from it. ASCII deliberately: (•) would measure
two columns in a terminal configured for East-Asian-Ambiguous glyphs and
shift every row's text, which no test would catch.
UI-thread-confined, like every component (see Screen).
Instance Attribute Summary collapse
-
#item_label ⇒ Proc, Method
@return — item -> row label (a
String, StyledString, or anything with#to_s);:to_sby default. -
#items ⇒ ::Array[untyped]
@return — the presented items.
Attributes included from HasValue
Attributes included from HasContent
Instance Method Summary collapse
-
#clamp_cursor ⇒ void
Pulls an over-range cursor back onto the last row (row 0 when there are none).
-
#clear ⇒ void
Resets #value to #empty_value.
-
#empty? ⇒ Boolean
@return — true iff #value equals #empty_value.
- #empty_value ⇒ Object
-
#focusable? ⇒ Boolean
Input fields are focusable by default (overrides #focusable?); a read-only display field could override back to
false. -
#handle_key(key) ⇒ Boolean
Selects the cursor row on Space.
-
#handle_mouse ⇒ void
@param
event. -
#initialize(items: [], value: nil) ⇒ RadioGroup
constructor
@param
items— the items to present, one row each; also settable via #items=. -
#label_for(item) ⇒ StyledString, String
@param
item. -
#layout(list) ⇒ void
Places the composed list across the whole rect (HasContent hook).
- #on_focus ⇒ void
-
#rebuild_rows ⇒ void
Re-renders every row from the current items, labels and selection.
-
#rect= ⇒ void
@param
rect. -
#select_at(index) ⇒ void
Selects the item on row
index; an index outside #items is ignored. -
#value ⇒ Object
@return — the current value;
niluntil first set. -
#value=(new_value) ⇒ void
Selects
new_value, firing HasValue#on_value_change when it really changed.
Constructor Details
#initialize(items: [], value: nil) ⇒ RadioGroup
@param items — the items to present, one row each; also settable via #items=.
@param value — the initially selected item. Seeds the backing ivar directly, so no listener fires and assignment order doesn't matter to a form helper.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/tuile/component/radio_group.rb', line 70 def initialize(items: [], value: nil) super() @items = items.to_a @item_label = :to_s.to_proc @value = value @on_value_change = nil list = List.new # A List has no cursor at all by default (Cursor::None, position -1). list.cursor = List::Cursor.new list.on_item_chosen = ->(index, _line) { select_at(index) } self.content = list rebuild_rows end |
Instance Attribute Details
#item_label ⇒ Proc, Method
@return — item -> row label (a String, StyledString, or
anything with #to_s); :to_s by default.
90 91 92 |
# File 'lib/tuile/component/radio_group.rb', line 90 def item_label @item_label end |
#items ⇒ ::Array[untyped]
@return — the presented items.
86 87 88 |
# File 'lib/tuile/component/radio_group.rb', line 86 def items @items end |
Instance Method Details
#clamp_cursor ⇒ void
This method returns an undefined value.
Pulls an over-range cursor back onto the last row (row 0 when there are
none). List#lines= leaves a stale cursor alone, which would strand it
off-content: no highlight, a dead Enter, and a Space that resolves to
nil and silently clears the selection.
172 173 174 175 176 177 |
# File 'lib/tuile/component/radio_group.rb', line 172 def clamp_cursor cursor = content.cursor # go_to_last funnels through Cursor#go's clamp(0, nil), so an empty # items list floors at 0 instead of going negative. cursor.go_to_last(@items.size) if cursor.position >= @items.size end |
#clear ⇒ void
This method returns an undefined value.
Resets #value to #empty_value.
3742 |
# File 'sig/tuile.rbs', line 3742
def clear: () -> void
|
#empty? ⇒ Boolean
@return — true iff #value equals #empty_value.
3739 |
# File 'sig/tuile.rbs', line 3739
def empty?: () -> bool
|
#empty_value ⇒ Object
3746 |
# File 'sig/tuile.rbs', line 3746
def empty_value: () -> Object
|
#focusable? ⇒ Boolean
Input fields are focusable by default (overrides Tuile::Component#focusable?);
a read-only display field could override back to false. Only
focusable? lives here — tab_stop? diverges between leaf fields and
composing wrappers, so it stays per-class (DECISIONS.md
D-integer-field).
3753 |
# File 'sig/tuile.rbs', line 3753
def focusable?: () -> bool
|
#handle_key(key) ⇒ Boolean
Selects the cursor row on Space. Nothing else is claimed: the composed List — being the focused component — has already had its chance at the key (its arrows, Home/End, PgUp/PgDn, ^U/^D and Enter), and whatever neither of us wants bubbles on to an ancestor.
@param key
134 135 136 137 138 139 |
# File 'lib/tuile/component/radio_group.rb', line 134 def handle_key(key) return false unless key == " " select_at(content.cursor.position) true end |
#handle_mouse ⇒ void
This method returns an undefined value.
@param event
3756 |
# File 'sig/tuile.rbs', line 3756
def handle_mouse: (MouseEvent event) -> void
|
#label_for(item) ⇒ StyledString, String
@param item
@return — whichever StyledString#+ accepts on the right — so a styled label keeps its spans and a plain one is parsed.
182 183 184 185 |
# File 'lib/tuile/component/radio_group.rb', line 182 def label_for(item) label = @item_label.call(item) label.is_a?(StyledString) ? label : label.to_s end |
#layout(list) ⇒ void
This method returns an undefined value.
Places the composed list across the whole rect (HasContent hook).
@param list
146 |
# File 'lib/tuile/component/radio_group.rb', line 146 def layout(list) = (list.rect = rect) |
#on_focus ⇒ void
This method returns an undefined value.
3761 |
# File 'sig/tuile.rbs', line 3761
def on_focus: () -> void
|
#rebuild_rows ⇒ void
This method returns an undefined value.
Re-renders every row from the current items, labels and selection.
161 162 163 164 165 |
# File 'lib/tuile/component/radio_group.rb', line 161 def rebuild_rows content.lines = @items.map do |item| StyledString.plain(item == value ? "(*) " : "( ) ") + label_for(item) end end |
#rect= ⇒ void
This method returns an undefined value.
@param rect
3759 |
# File 'sig/tuile.rbs', line 3759
def rect=: (Rect rect) -> void
|
#select_at(index) ⇒ void
This method returns an undefined value.
Selects the item on row index; an index outside #items is ignored.
@param index
153 154 155 156 157 |
# File 'lib/tuile/component/radio_group.rb', line 153 def select_at(index) return unless index.between?(0, @items.size - 1) self.value = @items[index] end |
#value ⇒ Object
@return — the current value; nil until first set.
3736 |
# File 'sig/tuile.rbs', line 3736
def value: () -> Object
|
#value=(new_value) ⇒ void
This method returns an undefined value.
Selects new_value, firing HasValue#on_value_change when it really
changed. The cursor stays where it is.
@param new_value — nil selects nothing; an item outside #items is kept but renders no marked row.
119 120 121 122 123 124 125 126 |
# File 'lib/tuile/component/radio_group.rb', line 119 def value=(new_value) # HasValue#value= no-ops on an unchanged value; this guard is what also # skips the row rebuild. return if value == new_value super rebuild_rows end |