Module: Chamomile::Application::ClassMethods
- Defined in:
- lib/chamomile/application.rb
Instance Method Summary collapse
-
#_dispatch_event(instance, msg) ⇒ Object
Returns command (a callable) or nil.
- #_event_handlers ⇒ Object
- #_key_handlers ⇒ Object
- #inherited(subclass) ⇒ Object
-
#on_blur(&block) ⇒ Object
Register a handler for blur events.
-
#on_focus(&block) ⇒ Object
Register a handler for focus events.
-
#on_key(*keys, &block) ⇒ Object
Register a handler for one or more key values.
-
#on_mouse(&block) ⇒ Object
Register a handler for mouse events.
-
#on_paste(&block) ⇒ Object
Register a handler for paste events.
-
#on_resize(&block) ⇒ Object
Register a handler for terminal resize events.
-
#on_tick(&block) ⇒ Object
Register a handler for tick events.
Instance Method Details
#_dispatch_event(instance, msg) ⇒ Object
Returns command (a callable) or nil.
The block's return value is the command. If you need to return a command
(like quit or tick), ensure it is the last expression in the block:
on_key("q") { @cleaning_up = true; quit } # correct — quit is last
on_key("q") { quit; @cleaning_up = true } # BUG — quit is discarded
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/chamomile/application.rb', line 114 def _dispatch_event(instance, msg) handler = case msg when KeyEvent then _key_handlers[msg.key] when ResizeEvent then _event_handlers[:resize] when TickEvent then _event_handlers[:tick] when MouseEvent then _event_handlers[:mouse] when FocusEvent then _event_handlers[:focus] when BlurEvent then _event_handlers[:blur] when PasteEvent then _event_handlers[:paste] end return nil unless handler result = instance.instance_exec(msg, &handler) # Only return the result if it's a callable command (Proc/Lambda). # Handler blocks often return non-command values (e.g., @count += 1 returns an Integer). result.respond_to?(:call) ? result : nil end |
#_event_handlers ⇒ Object
101 102 103 |
# File 'lib/chamomile/application.rb', line 101 def _event_handlers @_event_handlers ||= {} end |
#_key_handlers ⇒ Object
96 97 98 |
# File 'lib/chamomile/application.rb', line 96 def _key_handlers @_key_handlers ||= {} end |
#inherited(subclass) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/chamomile/application.rb', line 133 def inherited(subclass) super # Copy handlers to subclass so parent handlers are inherited subclass.instance_variable_set(:@_key_handlers, _key_handlers.dup) subclass.instance_variable_set(:@_event_handlers, _event_handlers.dup) end |
#on_blur(&block) ⇒ Object
Register a handler for blur events.
on_blur { @focused = false }
84 85 86 |
# File 'lib/chamomile/application.rb', line 84 def on_blur(&block) _event_handlers[:blur] = block end |
#on_focus(&block) ⇒ Object
Register a handler for focus events.
on_focus { @focused = true }
77 78 79 |
# File 'lib/chamomile/application.rb', line 77 def on_focus(&block) _event_handlers[:focus] = block end |
#on_key(*keys, &block) ⇒ Object
Register a handler for one or more key values. Keys can be symbols (:up, :down, :enter, etc.) or strings ("q", "k").
on_key(:up, "k") { @count += 1 }
on_key("q") { quit }
47 48 49 50 51 |
# File 'lib/chamomile/application.rb', line 47 def on_key(*keys, &block) keys.each do |key| _key_handlers[key] = block end end |
#on_mouse(&block) ⇒ Object
Register a handler for mouse events.
on_mouse { |e| handle_click(e) if e.press? }
70 71 72 |
# File 'lib/chamomile/application.rb', line 70 def on_mouse(&block) _event_handlers[:mouse] = block end |
#on_paste(&block) ⇒ Object
Register a handler for paste events.
on_paste { |e| @clipboard = e.content }
91 92 93 |
# File 'lib/chamomile/application.rb', line 91 def on_paste(&block) _event_handlers[:paste] = block end |
#on_resize(&block) ⇒ Object
Register a handler for terminal resize events.
on_resize { |e| @width = e.width; @height = e.height }
56 57 58 |
# File 'lib/chamomile/application.rb', line 56 def on_resize(&block) _event_handlers[:resize] = block end |
#on_tick(&block) ⇒ Object
Register a handler for tick events.
on_tick { refresh_data }
63 64 65 |
# File 'lib/chamomile/application.rb', line 63 def on_tick(&block) _event_handlers[:tick] = block end |