Class: Tuile::Component::Button

Inherits:
Tuile::Component show all
Defined in:
lib/tuile/component/button.rb

Overview

A clickable button. Activated by Enter, Space, or a left mouse click; fires the #on_click callback. Renders as ‘[ caption ]` on a single row; the background is highlighted when the button is focused so the user can see which button is active.

Buttons are tab stops — Tab and Shift+Tab will land on them as part of the standard focus cycle. Click-to-focus also works via the inherited #handle_mouse.

Assign a #rect (typically by the surrounding Layout) wide enough to show ‘[ caption ]`; #content_size reports that natural width.

Instance Attribute Summary collapse

Attributes inherited from Tuile::Component

#key_shortcut, #parent, #rect

Instance Method Summary collapse

Methods inherited from Tuile::Component

#active=, #active?, #attached?, #children, #cursor_position, #depth, #find_shortcut_component, #focus, #keyboard_hint, #on_child_removed, #on_focus, #on_tree, #root, #screen

Constructor Details

#initialize(caption = "") { ... } ⇒ Button

Returns a new instance of Button.

Parameters:

  • caption (String) (defaults to: "")

    the button’s label.

Yields:

  • optional ‘on_click` callback; same as assigning #on_click=.



19
20
21
22
23
# File 'lib/tuile/component/button.rb', line 19

def initialize(caption = "", &on_click)
  super()
  @caption = caption.to_s
  @on_click = on_click
end

Instance Attribute Details

#captionString

Returns the button’s label.

Returns:

  • (String)

    the button’s label.



26
27
28
# File 'lib/tuile/component/button.rb', line 26

def caption
  @caption
end

#on_clickProc, ...

Callback fired when the button is activated (Enter, Space, or left-click). The callable receives no arguments.

Returns:

  • (Proc, Method, nil)

    no-arg callable, or nil.



31
32
33
# File 'lib/tuile/component/button.rb', line 31

def on_click
  @on_click
end

Instance Method Details

#content_sizeSize

Returns natural width is ‘caption.length + 4` to fit `[ caption ]`; height is 1.

Returns:

  • (Size)

    natural width is ‘caption.length + 4` to fit `[ caption ]`; height is 1.



49
# File 'lib/tuile/component/button.rb', line 49

def content_size = Size.new(@caption.length + 4, 1)

#focusable?Boolean

Returns:

  • (Boolean)


43
# File 'lib/tuile/component/button.rb', line 43

def focusable? = true

#handle_key(key) ⇒ Boolean

Parameters:

  • key (String)

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tuile/component/button.rb', line 53

def handle_key(key)
  return false unless active?
  return true if super

  case key
  when Keys::ENTER, " "
    @on_click&.call
    true
  else
    false
  end
end

#handle_mouse(event) ⇒ void

This method returns an undefined value.

Parameters:



68
69
70
71
72
73
# File 'lib/tuile/component/button.rb', line 68

def handle_mouse(event)
  super
  return unless event.button == :left && rect.contains?(event.point)

  @on_click&.call
end

#repaintvoid

This method returns an undefined value.



76
77
78
79
80
81
82
83
# File 'lib/tuile/component/button.rb', line 76

def repaint
  super
  return if rect.empty?

  label = "[ #{@caption} ]"[0, rect.width]
  styled = active? ? Rainbow(label).bg(:darkslategray) : label
  screen.print TTY::Cursor.move_to(rect.left, rect.top), styled
end

#tab_stop?Boolean

Returns:

  • (Boolean)


45
# File 'lib/tuile/component/button.rb', line 45

def tab_stop? = true