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

#content_size, #key_shortcut, #on_theme_changed, #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_content_size_changed, #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
24
# File 'lib/tuile/component/button.rb', line 19

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

Instance Attribute Details

#captionString

Returns the button’s label.

Returns:

  • (String)

    the button’s label.



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

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.



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

def on_click
  @on_click
end

Instance Method Details

#focusable?Boolean

Returns:

  • (Boolean)


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

def focusable? = true

#handle_key(key) ⇒ Boolean

Parameters:

  • key (String)

Returns:

  • (Boolean)


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

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:



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

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.



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

def repaint
  super
  return if rect.empty?

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

#tab_stop?Boolean

Returns:

  • (Boolean)


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

def tab_stop? = true