Class: MittensUi::Switch

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

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.

Examples:

Basic switch

switch = MittensUi::Switch.new
switch.activate { |s| puts "Switch toggled: #{s.status}" }

Using the alias

switch = MittensUi::Switch.new
switch.on { |s| puts "Current state: #{s.status}" }

Checking switch state

switch = MittensUi::Switch.new
puts switch.status  # => :off

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 = {}) ⇒ 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.

Parameters:

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

    configuration options

Options Hash (options):

  • :width (Symbol) — default: :full

    column width in the layout grid. Accepted values are :full, :half, :third, :quarter

  • :defer_render (Boolean) — default: false

    when true, skips auto-rendering into the layout. Use when passing to a container like HBox.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mittens_ui/switch.rb', line 37

def initialize(options = {})
  @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, options)
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.

Examples:

Respond to toggle

switch = MittensUi::Switch.new
switch.activate do |s|
  puts "Switch is now #{s.status}"
end

Yields:

  • (switch)

    passes the Switch instance to the block

Yield Parameters:

  • switch (Switch)

    the Switch widget that was toggled

See Also:



67
68
69
70
71
# File 'lib/mittens_ui/switch.rb', line 67

def activate
  @switch.signal_connect('notify::active') do |switch_widget|
    yield(self)
  end
end

#statusSymbol

Returns the current state of the switch.

Examples:

Check current state

switch = MittensUi::Switch.new
switch.status  # => :off

switch.on { |s| puts s.status }  # => :on (when toggled)

Returns:

  • (Symbol)

    :on if the switch is active, :off if inactive



97
98
99
# File 'lib/mittens_ui/switch.rb', line 97

def status
  @switch.active? ? :on : :off
end