Class: MittensUi::RadioButton
- 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.
Instance Attribute Summary
Attributes inherited from Core
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RadioButton
constructor
Creates a new RadioButton group.
-
#on_change {|value| ... } ⇒ void
Connects a block to the selection change event.
-
#options ⇒ Array<String>
Returns all option labels in the group.
-
#select(label) ⇒ void
Programmatically selects an option by label.
-
#selected ⇒ String?
Returns the currently selected option label.
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.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mittens_ui/radiobutton.rb', line 39 def initialize( = {}) @option_labels = [:options] || [] @default = [:default] || @option_labels.first @layout = [:layout] || :horizontal @on_change = nil @buttons = {} raise ArgumentError, 'RadioButton requires at least one option' if @option_labels.empty? @container = Gtk::Box.new(@layout, 8) super(@container, ) end |
Instance Method Details
#on_change {|value| ... } ⇒ void
This method returns an undefined value.
Connects a block to the selection change event.
73 74 75 |
# File 'lib/mittens_ui/radiobutton.rb', line 73 def on_change(&block) @on_change = block end |
#options ⇒ Array<String>
Returns all option labels in the group.
80 81 82 |
# File 'lib/mittens_ui/radiobutton.rb', line 80 def @option_labels end |
#select(label) ⇒ void
This method returns an undefined value.
Programmatically selects an option by label.
64 65 66 |
# File 'lib/mittens_ui/radiobutton.rb', line 64 def select(label) @buttons[label]&.set_active(true) end |
#selected ⇒ String?
Returns the currently selected option label.
56 57 58 |
# File 'lib/mittens_ui/radiobutton.rb', line 56 def selected @buttons.find { |_label, btn| btn.active? }&.first end |