class BBS::Container
A Widget that holds children. It renders them in order, so a later child draws over an earlier one, and passes hit tests and themes down to them.
Attributes
The child widgets as an Array, in drawing order.
Public Class Methods
Source
# File lib/bbs/container.rb, line 11 def initialize(**kw) super @children = [] end
Creates an empty container. Takes the same keywords as Widget.new.
Public Instance Methods
Source
# File lib/bbs/container.rb, line 19 def add(child) child.parent = self @children << child child end
Appends child and makes this container its parent.
Returns child.
Source
# File lib/bbs/container.rb, line 32 def clear @children.each { |c| c.parent = nil } @children.clear end
Removes all children and clears their parent.
Source
# File lib/bbs/container.rb, line 55 def find(id) walk { |w| return w if w.id == id } nil end
Returns the first widget in the tree whose id equals id, or nil.
Source
# File lib/bbs/container.rb, line 39 def focusable_descendants result = [] walk { |w| result << w if w.focusable? && w.visible } result end
Returns an Array of the visible widgets in the tree (this container and every level below) that accept focus, in walk order.
Source
# File lib/bbs/container.rb, line 69 def hit_test(x, y) return nil unless bounds.contains?(x, y) && visible # iterate top-most last so we test in reverse @children.reverse.each do |child| next unless child.visible hit = child.hit_test(x, y) return hit if hit end self end
Returns the top-most visible widget under the screen cell (x, y): the deepest child hit, this container when no child is hit, or nil when the point is outside the bounds or the container is hidden.
Source
# File lib/bbs/container.rb, line 27 def remove(child) child.parent = nil if @children.delete(child) end
Removes child and clears its parent. Does nothing when child is not one of the children.
Source
# File lib/bbs/container.rb, line 62 def render(frame) @children.each { |c| c.render(frame) if c.visible } end
Renders the visible children into frame, a BBS::FrameBuffer, in order.
Source
# File lib/bbs/container.rb, line 81 def theme=(value) super @children.each { |c| c.theme = value } end
Sets the BBS::Theme on this container and on every child.
Source
# File lib/bbs/container.rb, line 47 def walk(&block) yield self @children.each do |c| c.is_a?(Container) ? c.walk(&block) : (yield c) end end
Yields this container, then every widget below it, depth first in child order. Nested containers are walked in turn.