class BBS::Widget
Base widget. All visual elements inherit from this.
- bounds
-
a
Rect(x, y, width, height) in screen coordinates - parent
-
the enclosing
Container, ornil - id
-
an optional Symbol for lookup via
Container#find
Subclasses override render and optionally handle_event.
Constants
- Rect
-
A rectangle of screen cells:
xandyare the 1-based top-left cell,widthandheightthe size.contains?(px, py)tells whether a cell lies inside it.
Attributes
The BBS::Theme set on this widget, or nil to use the parent’s.
Public Class Methods
# File lib/bbs/widget.rb, line 35 def initialize(id: nil, bounds: nil, theme: nil) @id = id @bounds = bounds || Rect.new(1, 1, 0, 0) @parent = nil @visible = true @focused = false @enabled = true @theme = theme end
Creates a widget.
- id
-
an optional Symbol for
Container#find - bounds
-
a
Rect; defaults to an empty one at 1, 1 - theme
-
a
BBS::Theme; defaults to the parent’s
Public Instance Methods
Source
# File lib/bbs/widget.rb, line 62 def each(&) yield self nil end
Convenience: yield this widget. Subclasses override to walk a tree.
Source
# File lib/bbs/widget.rb, line 46 def focusable? = false
Override in subclasses. By default a widget does not accept focus.
Source
# File lib/bbs/widget.rb, line 59 def handle_event(_event) = nil
Handle a keyboard or mouse event. Return :halt to end the application, :handled to stop propagation, or nil/false to continue.
Source
# File lib/bbs/widget.rb, line 68 def hit_test(x, y) bounds.contains?(x, y) ? self : nil end
Hit test: returns the deepest widget whose bounds contain (x, y).
Source
# File lib/bbs/widget.rb, line 75 def layout(x, y, width, height) @bounds = Rect.new(x, y, width, height) end
Move/resize. Subclasses may override to relayout children.
Source
# File lib/bbs/widget.rb, line 52 def on_blur = nil
Called when this widget loses focus.
Source
# File lib/bbs/widget.rb, line 49 def on_focus = nil
Called when this widget gains focus. Subclasses may override.
Source
# File lib/bbs/widget.rb, line 80 def redraw! @parent&.redraw! end
Request a redraw — set by Application as a callback if needed.
Source
# File lib/bbs/widget.rb, line 55 def render(frame); end
Render this widget into the supplied FrameBuffer.
Protected Instance Methods
Source
# File lib/bbs/widget.rb, line 88 def style(key) (@theme || parent&.theme)&.style(key) end
Returns the SGR style for key from this widget’s theme, or from the parent’s when none is set; nil when neither has a theme.