Class: Thaum::Checkbox

Inherits:
Object
  • Object
show all
Includes:
Sigil
Defined in:
lib/thaum/sigils/checkbox.rb

Overview

Tri-state boolean: unchecked, checked, indeterminate. Space (or Enter) toggles between unchecked and checked; indeterminate is set programmatically and any user toggle clears it. Emits Checkbox::ChangedEvent with the new :checked value.

Constant Summary collapse

ChangedEvent =
Thaum::Event.define(:checked)

Instance Attribute Summary collapse

Attributes included from Sigil

#handler_parent, #rect, #thaum_app

Instance Method Summary collapse

Methods included from Sigil

#emit, #focusable?, #focused?, #on_blur, #on_focus, #on_mount, #on_mouse, #on_paste, #on_tick, #on_unmount, #on_update, #request_render

Constructor Details

#initialize(checked: false, indeterminate: false, label: nil) ⇒ Checkbox

Returns a new instance of Checkbox.



16
17
18
19
20
# File 'lib/thaum/sigils/checkbox.rb', line 16

def initialize(checked: false, indeterminate: false, label: nil)
  @checked       = checked
  @indeterminate = indeterminate
  @label         = label
end

Instance Attribute Details

#checkedObject (readonly)

Returns the value of attribute checked.



13
14
15
# File 'lib/thaum/sigils/checkbox.rb', line 13

def checked
  @checked
end

#indeterminateObject

Returns the value of attribute indeterminate.



13
14
15
# File 'lib/thaum/sigils/checkbox.rb', line 13

def indeterminate
  @indeterminate
end

#labelObject

Returns the value of attribute label.



14
15
16
# File 'lib/thaum/sigils/checkbox.rb', line 14

def label
  @label
end

Instance Method Details

#checked?Boolean

Returns:

  • (Boolean)


22
# File 'lib/thaum/sigils/checkbox.rb', line 22

def checked?       = @checked

#indeterminate?Boolean

Returns:

  • (Boolean)


23
# File 'lib/thaum/sigils/checkbox.rb', line 23

def indeterminate? = @indeterminate

#on_key(event) ⇒ Object



30
31
32
33
34
35
# File 'lib/thaum/sigils/checkbox.rb', line 30

def on_key(event)
  case event.key
  when " ", :enter then toggle
  else emit(event)
  end
end

#render(canvas:, theme:) ⇒ Object



37
38
39
40
41
# File 'lib/thaum/sigils/checkbox.rb', line 37

def render(canvas:, theme:)
  canvas.fill(bg: theme.bg)
  fg = focused? ? theme.accent : theme.fg
  canvas.text(content: "#{mark} #{@label}".rstrip, fg: fg, bg: theme.bg)
end