Class: Potty::Widgets::Container
- Defined in:
- lib/potty/widgets/container.rb
Overview
A widget that holds child widgets and lays them out within its own rect. Render, tick, and focus traversal recurse into children, so a View’s flat ‘@widgets` array can now contain arbitrarily nested structure (a VBox of HBoxes of fields, etc.) while the View itself stays unchanged.
Subclasses implement ‘layout_children` (assign each child a rect) and `preferred_height`.
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
Attributes inherited from Base
#app, #focused, #parent, #rect
Instance Method Summary collapse
- #add(*widgets) ⇒ Object (also: #<<)
-
#focusable_widgets ⇒ Object
Focusable leaf descendants, in visual order (depth-first).
-
#initialize(app, spacing: 0) ⇒ Container
constructor
A new instance of Container.
-
#layout_children ⇒ Object
Override in subclasses.
- #on_layout ⇒ Object
- #render(window) ⇒ Object
- #tick(now) ⇒ Object
Methods inherited from Base
#activate, #blur, #can_focus?, #deactivate, #focus, #handle_escape, #handle_key, #hide, #layout, #on_blur, #on_focus, #preferred_height, #show, #theme, #visible=, #visible?
Methods included from Events
#emit, #listeners?, #off, #on
Constructor Details
#initialize(app, spacing: 0) ⇒ Container
Returns a new instance of Container.
19 20 21 22 23 |
# File 'lib/potty/widgets/container.rb', line 19 def initialize(app, spacing: 0) super(app) @children = [] @spacing = spacing end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
17 18 19 |
# File 'lib/potty/widgets/container.rb', line 17 def children @children end |
Instance Method Details
#add(*widgets) ⇒ Object Also known as: <<
25 26 27 28 29 30 31 |
# File 'lib/potty/widgets/container.rb', line 25 def add(*) .flatten.each do |w| w.parent = self @children << w end self end |
#focusable_widgets ⇒ Object
Focusable leaf descendants, in visual order (depth-first).
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/potty/widgets/container.rb', line 35 def @children.flat_map do |child| if child.is_a?(Container) child. elsif child.can_focus? [child] else [] end end end |
#layout_children ⇒ Object
Override in subclasses.
52 53 54 |
# File 'lib/potty/widgets/container.rb', line 52 def layout_children # no-op end |
#on_layout ⇒ Object
47 48 49 |
# File 'lib/potty/widgets/container.rb', line 47 def on_layout layout_children end |
#render(window) ⇒ Object
56 57 58 59 60 |
# File 'lib/potty/widgets/container.rb', line 56 def render(window) return unless @visible && @rect @children.each { |child| child.render(window) if child.visible? } end |
#tick(now) ⇒ Object
62 63 64 |
# File 'lib/potty/widgets/container.rb', line 62 def tick(now) @children.each { |child| child.tick(now) } end |