Class: Tuile::Component::Button
- Inherits:
-
Tuile::Component
- Object
- Tuile::Component
- Tuile::Component::Button
- 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
-
#caption ⇒ String
The button’s label.
-
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click).
Attributes inherited from Tuile::Component
#content_size, #key_shortcut, #on_theme_changed, #parent, #rect
Instance Method Summary collapse
- #focusable? ⇒ Boolean
- #handle_key(key) ⇒ Boolean
- #handle_mouse(event) ⇒ void
-
#initialize(caption = "") { ... } ⇒ Button
constructor
A new instance of Button.
- #repaint ⇒ void
- #tab_stop? ⇒ Boolean
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.
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
#caption ⇒ String
Returns the button’s label.
27 28 29 |
# File 'lib/tuile/component/button.rb', line 27 def caption @caption end |
#on_click ⇒ Proc, ...
Callback fired when the button is activated (Enter, Space, or left-click). The callable receives no arguments.
32 33 34 |
# File 'lib/tuile/component/button.rb', line 32 def on_click @on_click end |
Instance Method Details
#focusable? ⇒ Boolean
45 |
# File 'lib/tuile/component/button.rb', line 45 def focusable? = true |
#handle_key(key) ⇒ 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.
66 67 68 69 70 71 |
# File 'lib/tuile/component/button.rb', line 66 def handle_mouse(event) super return unless event. == :left && rect.contains?(event.point) @on_click&.call end |
#repaint ⇒ void
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
47 |
# File 'lib/tuile/component/button.rb', line 47 def tab_stop? = true |