Class: MittensUi::Switch
Overview
A toggle switch widget that represents an on/off state. Wraps Gtk::Switch inside a Gtk::Grid for layout consistency.
The switch can be toggled by the user and monitored for state changes via the #activate method. The current state can be queried with #status.
Instance Attribute Summary
Attributes inherited from Core
Instance Method Summary collapse
-
#activate {|switch| ... } ⇒ void
(also: #on)
Registers a callback to be invoked when the switch state changes.
-
#initialize(options = {}) ⇒ Switch
constructor
Creates a new Switch widget initialized to the off state.
-
#status ⇒ Symbol
Returns the current state of the switch.
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 = {}) ⇒ Switch
Creates a new Switch widget initialized to the off state.
The switch is placed inside a grid container for consistent layout with other MittensUi widgets.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mittens_ui/switch.rb', line 37 def initialize( = {}) @switch = Gtk::Switch.new @switch.active = false @grid = Gtk::Grid.new @grid.column_spacing = 1 @grid.row_spacing = 1 @grid.attach(@switch, 0, 0, 1, 1) super(@grid, ) end |
Instance Method Details
#activate {|switch| ... } ⇒ void Also known as: on
This method returns an undefined value.
Registers a callback to be invoked when the switch state changes.
The block receives the Switch instance as an argument, allowing you to access the current state via #status. The callback is triggered every time the user toggles the switch.
67 68 69 70 71 |
# File 'lib/mittens_ui/switch.rb', line 67 def activate @switch.signal_connect('notify::active') do || yield(self) end end |
#status ⇒ Symbol
Returns the current state of the switch.
97 98 99 |
# File 'lib/mittens_ui/switch.rb', line 97 def status @switch.active? ? :on : :off end |