Class: Tuile::Component::RadioGroup

Inherits:
Component
  • Object
show all
Includes:
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 of the items is its single child, which is where the cursor, scrolling, the scrollbar and per-row mouse hit-testing come from — the group only supplies the List#renderer that puts the marker in front of the label. #list is that list, exposed read-only so an app can tune it (scrollbar_visibility, show_cursor_when_inactive, …) but never swap it out. 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.list.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 its own item, never to its label.

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

Attributes included from HasValue

#on_value_change

Attributes included from HasValidation

#on_error_message_change

Instance Method Summary collapse

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.

Parameters:

  • items: (::Array[untyped]) (defaults to: [])
  • value: (Object, nil) (defaults to: nil)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tuile/component/radio_group.rb', line 71

def initialize(items: [], value: nil)
  super()
  @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.renderer = method(:render_row)
  list.on_item_chosen = ->(_index, item) { self.value = item }
  list.items = items.to_a
  @list = list
  add_child(list, at: 0)
end

Instance Attribute Details

#item_labelProc, Method

@return — item -> row label (a String, StyledString, or anything with #to_s); :to_s by default.

Returns:

  • (Proc, Method)


116
117
118
# File 'lib/tuile/component/radio_group.rb', line 116

def item_label
  @item_label
end

#listList (readonly)

The composed List: an app may tune it — its scrollbar, its cursor, show_cursor_when_inactive — but never replace it, since this group's renderer and selection are wired into this one (design/decisions.md D_has_content). Those knobs are List concepts rather than group concepts, which is why they are reached here instead of forwarded (D_wrapping_field).

Returns:



94
95
96
# File 'lib/tuile/component/radio_group.rb', line 94

def list
  @list
end

Instance Method Details

#clamp_cursor(item_count) ⇒ void

This method returns an undefined value.

Pulls an over-range cursor back onto the last row (row 0 when there are none). List#items= 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.

@param item_count — size of the incoming item list.

Parameters:

  • item_count (Integer)


192
193
194
195
196
197
# File 'lib/tuile/component/radio_group.rb', line 192

def clamp_cursor(item_count)
  cursor = list.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(item_count) if cursor.position >= item_count
end

#clearvoid

This method returns an undefined value.

Resets #value to #empty_value.

An includer whose input can outrun its value (HasBadInput) must clear the input: a field holding bad input already reads empty_value, so inheriting this default — over a #value= that returns early on a no-op set — is a clear that leaves the garbage on screen.



8935
# File 'sig/tuile.rbs', line 8935

def clear: () -> void

#empty?Boolean

Empty of value: a field whose parse is partial reports true while the user is looking at glyphs it could not use, so ask HasBadInput#bad_input? first.

@return — true iff #value equals #empty_value.

Returns:

  • (Boolean)


8927
# File 'sig/tuile.rbs', line 8927

def empty?: () -> bool

#empty_valueObject

@return — the value #empty?/#clear treat as empty; nil unless an includer overrides it.

Returns:

  • (Object)


8939
# File 'sig/tuile.rbs', line 8939

def empty_value: () -> Object

#error_bg_colorColor?

The invalid well, picked up by everything this component paints — including the inner face of a composed field and the List of a group, neither of which forwards anything: both declare no background of their own, so the ordinary chain walks up to this (overrides Tuile::Component#error_bg_color).

Returns:



8970
# File 'sig/tuile.rbs', line 8970

def error_bg_color: () -> Color?

#error_ink?Boolean

Whether to paint the invalid well right now. Its own hook because HasBadInput widens it: a field holding input its value cannot represent is invalid on the face too, even with no verdict written.

Returns:

  • (Boolean)


8975
# File 'sig/tuile.rbs', line 8975

def error_ink?: () -> bool

#error_messageStyledString?

@return — why the field is invalid, or nil when it is not; nil until something sets it.

Returns:



8953
# File 'sig/tuile.rbs', line 8953

def error_message: () -> StyledString?

#error_message=void

This method returns an undefined value.

Sets the verdict and repaints the field in Theme#error_color; nil clears it. No-op (no repaint, no listener) when unchanged. A String is parsed via StyledString.parse, as HasCaption#caption= does.

Safe on a detached field — an app validates a form it assembled but has not mounted, and Tuile::Component#invalidate is already a no-op there.

@param new_message

Parameters:



8963
# File 'sig/tuile.rbs', line 8963

def error_message=: ((String | StyledString)? new_message) -> void

#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 (design/decisions.md D_integer_field).

Returns:

  • (Boolean)


8946
# File 'sig/tuile.rbs', line 8946

def focusable?: () -> bool

#handle_focusvoid

This method returns an undefined value.



104
105
106
107
108
109
# File 'lib/tuile/component/radio_group.rb', line 104

def handle_focus
  super
  # The list is what the arrows drive, so it takes the focus this group
  # was given; the group itself claims only Space.
  screen.focused = list if list.focusable?
end

#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

Parameters:

  • key (String)

Returns:

  • (Boolean)


159
160
161
162
163
164
# File 'lib/tuile/component/radio_group.rb', line 159

def handle_key?(key)
  return false unless key == " "

  select_at(list.cursor.position)
  true
end

#inspect_details::Array[String]

Adds value=… to Tuile::Component#inspect, omitted while the value is nil.

Returns:

  • (::Array[String])


8949
# File 'sig/tuile.rbs', line 8949

def inspect_details: () -> ::Array[String]

#items::Array[untyped]

@return — the presented items.

Returns:

  • (::Array[untyped])


112
# File 'lib/tuile/component/radio_group.rb', line 112

def items = list.items

#items=(new_items) ⇒ void

This method returns an undefined value.

Replaces the presented rows, leaving #value untouched and clamping the cursor back into range.

@param new_items

Parameters:

  • new_items (::Array[untyped])


123
124
125
126
127
128
129
130
# File 'lib/tuile/component/radio_group.rb', line 123

def items=(new_items)
  raise TypeError, "expected Array, got #{new_items.inspect}" unless new_items.is_a?(Array)

  # Before the items land, so the single {List#on_cursor_changed} that
  # {List#items=} fires reports the final row rather than a stale one.
  clamp_cursor(new_items.size)
  list.items = new_items
end

#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.

Parameters:

  • item (Object)

Returns:



202
203
204
205
# File 'lib/tuile/component/radio_group.rb', line 202

def label_for(item)
  label = @item_label.call(item)
  label.is_a?(StyledString) ? label : label.to_s
end

#rect=(new_rect) ⇒ void

This method returns an undefined value.

@param new_rect

Parameters:



98
99
100
101
# File 'lib/tuile/component/radio_group.rb', line 98

def rect=(new_rect)
  super
  list.rect = rect
end

#render_row(item) ⇒ StyledString

@param item

@return — the item's row: its label behind a selection marker. The List calls this at paint time, so the marker tracks #value without re-rendering anything but the visible rows.

Parameters:

  • item (Object)

Returns:



182
183
184
# File 'lib/tuile/component/radio_group.rb', line 182

def render_row(item)
  StyledString.plain(item == value ? "(*) " : "( ) ") + label_for(item)
end

#select_at(index) ⇒ void

This method returns an undefined value.

Selects the item on row index; an index outside #items is ignored — List::Cursor::None's -1 would otherwise select the last item.

@param index

Parameters:

  • index (Integer)


172
173
174
175
176
# File 'lib/tuile/component/radio_group.rb', line 172

def select_at(index)
  return unless index.between?(0, items.size - 1)

  self.value = items[index]
end

#valueObject

@return — the current value; nil until first set.

Returns:

  • (Object)


8920
# File 'sig/tuile.rbs', line 8920

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_valuenil selects nothing; an item outside #items is kept but renders no marked row.

Parameters:

  • new_value (Object, nil)


144
145
146
147
148
149
150
151
# File 'lib/tuile/component/radio_group.rb', line 144

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
  list.refresh_rows
end