class BBS::FocusManager
Tracks which widget within a Container currently has focus. Tab / Shift-Tab cycle through its focusable descendants.
Constants
- FOCUS_KEYS
-
The focus moves of Tab and Shift-Tab.
Attributes
The widget that has the focus, or nil.
Public Class Methods
Source
# File lib/bbs/focus_manager.rb, line 12 def initialize(container) @container = container @focused = nil focus_first end
Creates a manager for container, a BBS::Container, and focuses its first candidate.
Public Instance Methods
Source
# File lib/bbs/focus_manager.rb, line 20 def candidates @container.focusable_descendants.select(&:enabled) end
Returns the widgets that can take the focus: the container’s visible, focusable and enabled descendants, in walk order.
Source
# File lib/bbs/focus_manager.rb, line 59 def dispatch(event) return nil unless @focused @focused.handle_event(event) end
Passes event to the focused widget’s handle_event.
Returns the widget’s result, or nil when nothing has the focus.
Source
# File lib/bbs/focus_manager.rb, line 32 def focus(widget) move_focus(widget) if widget&.focusable? && widget.enabled end
Moves the focus to widget when it is focusable and enabled; otherwise leaves the focus where it is. The old widget gets on_blur, the new one on_focus.
Source
# File lib/bbs/focus_manager.rb, line 25 def focus_first move_focus(candidates.first) end
Moves the focus to the first candidate, or clears it when there is none.
Source
# File lib/bbs/focus_manager.rb, line 48 def handle_focus_key(event) move = event.key? && FOCUS_KEYS[event.key] return nil unless move public_send(move) :handled end
Handles Tab (next) and Shift-Tab (previous) in event, a BBS::Event.
Returns :handled when the key moved the focus, nil otherwise.
Source
# File lib/bbs/focus_manager.rb, line 37 def next_focus = step_focus(1, -1)
Moves the focus to the next candidate, wrapping around at the end.
Source
# File lib/bbs/focus_manager.rb, line 40 def prev_focus = step_focus(-1, 0)
Moves the focus to the previous candidate, wrapping around at the start.