Class: Thaum::Button

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

Constant Summary collapse

PressedEvent =
Thaum::Event.define(:label)

Instance Attribute Summary collapse

Attributes included from Sigil

#handler_parent, #rect, #thaum_app

Instance Method Summary collapse

Methods included from Sigil

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

Constructor Details

#initialize(label:, disabled: false) ⇒ Button

Returns a new instance of Button.



11
12
13
14
# File 'lib/thaum/sigils/button.rb', line 11

def initialize(label:, disabled: false)
  @label    = label
  @disabled = disabled
end

Instance Attribute Details

#labelObject (readonly)

Returns the value of attribute label.



9
10
11
# File 'lib/thaum/sigils/button.rb', line 9

def label
  @label
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


16
# File 'lib/thaum/sigils/button.rb', line 16

def disabled?  = @disabled

#focusable?Boolean

Returns:

  • (Boolean)


17
# File 'lib/thaum/sigils/button.rb', line 17

def focusable? = !@disabled

#on_key(event) ⇒ Object



19
20
21
22
23
24
# File 'lib/thaum/sigils/button.rb', line 19

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

#render(canvas:, theme:) ⇒ Object



26
27
28
29
30
# File 'lib/thaum/sigils/button.rb', line 26

def render(canvas:, theme:)
  fg, bg = colors(theme)
  canvas.fill(bg: bg) if bg
  canvas.text(content: @label, fg: fg, bg: bg, align: :center)
end