Class: MittensUi::RadioButton

Inherits:
Core
  • Object
show all
Defined in:
lib/mittens_ui/radiobutton.rb

Overview

A group of mutually exclusive radio button options. Wraps Gtk::CheckButton with group linking so only one option can be selected at a time. GTK4 removed Gtk::RadioButton — CheckButton with a group is the replacement.

Examples:

Basic radio button group

rb = MittensUi::RadioButton.new(
  options: ['Small', 'Medium', 'Large'],
  default: 'Medium'
)
puts rb.selected  # => "Medium"

Vertical layout

rb = MittensUi::RadioButton.new(
  options: ['Red', 'Green', 'Blue'],
  layout: :vertical
)

Reacting to selection change

rb = MittensUi::RadioButton.new(options: ['Yes', 'No'])
rb.on_change { |value| puts "Selected: #{value}" }

Instance Attribute Summary

Attributes inherited from Core

#core_widget

Instance Method Summary collapse

Methods inherited from Core

#hidden?, #hide, #keyboard_shortcut, #remove, #remove_keyboard_shortcut, #render, #shortcuts, #show

Methods included from Helpers

#icon_map, #list_system_icons, #set_margin_from_opts_for

Constructor Details

#initialize(options = {}) ⇒ RadioButton

Creates a new RadioButton group.

Parameters:

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :options (Array<String>) — default: []

    the list of option labels

  • :default (String, nil) — default: nil

    the initially selected option. If nil, the first option is selected by default.

  • :layout (Symbol) — default: :horizontal

    the layout direction. Accepted values are :horizontal and :vertical

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mittens_ui/radiobutton.rb', line 39

def initialize(options = {})
  @option_labels = options[:options] || []
  @default       = options[:default] || @option_labels.first
  @layout        = options[:layout]  || :horizontal
  @on_change     = nil
  @buttons       = {}

  raise ArgumentError, 'RadioButton requires at least one option' if @option_labels.empty?

  @container = Gtk::Box.new(@layout, 8)
  init_buttons
  super(@container, options)
end

Instance Method Details

#on_change {|value| ... } ⇒ void

This method returns an undefined value.

Connects a block to the selection change event.

Yields:

  • (value)

    called when the selected option changes

Yield Parameters:

  • value (String)

    the newly selected option label



73
74
75
# File 'lib/mittens_ui/radiobutton.rb', line 73

def on_change(&block)
  @on_change = block
end

#optionsArray<String>

Returns all option labels in the group.

Returns:

  • (Array<String>)

    the list of option labels



80
81
82
# File 'lib/mittens_ui/radiobutton.rb', line 80

def options
  @option_labels
end

#select(label) ⇒ void

This method returns an undefined value.

Programmatically selects an option by label.

Parameters:

  • label (String)

    the option label to select



64
65
66
# File 'lib/mittens_ui/radiobutton.rb', line 64

def select(label)
  @buttons[label]&.set_active(true)
end

#selectedString?

Returns the currently selected option label.

Returns:

  • (String, nil)

    the selected option, or nil if none selected



56
57
58
# File 'lib/mittens_ui/radiobutton.rb', line 56

def selected
  @buttons.find { |_label, btn| btn.active? }&.first
end