Class: Potty::Widgets::Button
Overview
Focusable push button. Space/Enter fires :press. Pass on_press: for a one-liner, or wire it with button.on(:press) { … }.
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#label ⇒ Object
Returns the value of attribute label.
Attributes inherited from Base
#app, #focused, #parent, #rect
Instance Method Summary collapse
- #can_focus? ⇒ Boolean
- #handle_key(ch) ⇒ Object
-
#initialize(app, label: '', color: :normal, on_press: nil) ⇒ Button
constructor
A new instance of Button.
- #preferred_height(_width) ⇒ Object
- #press ⇒ Object
- #render(window) ⇒ Object
Methods inherited from Base
#activate, #blur, #deactivate, #focus, #handle_escape, #hide, #layout, #on_blur, #on_focus, #on_layout, #show, #theme, #tick, #visible=, #visible?
Methods included from Events
#emit, #listeners?, #off, #on
Constructor Details
#initialize(app, label: '', color: :normal, on_press: nil) ⇒ Button
Returns a new instance of Button.
13 14 15 16 17 18 |
# File 'lib/potty/widgets/button.rb', line 13 def initialize(app, label: '', color: :normal, on_press: nil) super(app) @label = label @color = color on(:press, &on_press) if on_press end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
11 12 13 |
# File 'lib/potty/widgets/button.rb', line 11 def color @color end |
#label ⇒ Object
Returns the value of attribute label.
11 12 13 |
# File 'lib/potty/widgets/button.rb', line 11 def label @label end |
Instance Method Details
#can_focus? ⇒ Boolean
20 21 22 |
# File 'lib/potty/widgets/button.rb', line 20 def can_focus? true end |
#handle_key(ch) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/potty/widgets/button.rb', line 32 def handle_key(ch) case ch when Keys::SPACE, *Keys::ENTERS press true else false end end |
#preferred_height(_width) ⇒ Object
24 25 26 |
# File 'lib/potty/widgets/button.rb', line 24 def preferred_height(_width) 1 end |
#press ⇒ Object
28 29 30 |
# File 'lib/potty/widgets/button.rb', line 28 def press emit(:press, self) end |
#render(window) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/potty/widgets/button.rb', line 42 def render(window) return unless @visible && @rect text = "[ #{@label} ]"[0, @rect.width] attr = @focused ? theme.attr(:selected, bold: true) : theme[@color] window.setpos(@rect.y, @rect.x) window.attron(attr) { window.addstr(text) } end |