Class: MittensUi::Checkbox

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

Overview

A checkbox widget that can be toggled on and off. Wraps Gtk::CheckButton.

Examples:

Basic checkbox

cb = MittensUi::Checkbox.new(label: "Enable notifications")
cb.toggle { puts "toggled!" }

With associated value

cb = MittensUi::Checkbox.new(label: "Accept terms")
cb.value = "accepted"
cb.toggle { puts "value: #{cb.value}" }

Instance Attribute Summary collapse

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

Creates a new Checkbox widget.

Parameters:

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

    configuration options

Options Hash (options):

  • :label (String) — default: "Checkbox"

    the checkbox label text

  • :width (Symbol) — default: :full

    column width in the layout grid

  • :defer_render (Boolean) — default: false

    skip auto-rendering into layout



26
27
28
29
30
31
32
# File 'lib/mittens_ui/checkbox.rb', line 26

def initialize(options = {})
  label    = options.fetch(:label, 'Checkbox')
  @value   = nil
  @checkbox = Gtk::CheckButton.new
  @checkbox.set_label(label.to_s)
  super(@checkbox, options)
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



18
19
20
# File 'lib/mittens_ui/checkbox.rb', line 18

def value
  @value
end

Instance Method Details

#checked?Boolean

Returns whether the checkbox is currently checked.

Returns:

  • (Boolean)


48
49
50
# File 'lib/mittens_ui/checkbox.rb', line 48

def checked?
  @checkbox.active?
end

#toggle { ... } ⇒ void

This method returns an undefined value.

Connects a block to the toggle event. Called whenever the checkbox is checked or unchecked.

Yields:

  • called when the checkbox state changes



39
40
41
42
43
# File 'lib/mittens_ui/checkbox.rb', line 39

def toggle
  @checkbox.signal_connect('toggled') do
    yield
  end
end