Class: Charming::Presentation::Layout::Split

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/presentation/layout/split.rb

Overview

Split divides a parent Rect among its child nodes horizontally or vertically. Children with a configured ‘width`/`height` are placed at that fixed size; children without a fixed size share the remaining space according to their `grow` weight.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(direction:, gap: 0, width: nil, height: nil, grow: nil) ⇒ Split

direction is ‘:horizontal` or `:vertical`. gap (in cells) separates children. width/height are optional fixed dimensions for the split as a whole. grow is the weight for distributing remaining space (used when this Split is a child of another Split).



17
18
19
20
21
22
23
24
# File 'lib/charming/presentation/layout/split.rb', line 17

def initialize(direction:, gap: 0, width: nil, height: nil, grow: nil)
  @direction = direction.to_sym
  @gap = gap.to_i
  @width = width
  @height = height
  @grow = grow
  @children = []
end

Instance Attribute Details

#growObject (readonly)

The fixed width/height of the split (when set) and the grow weight for the split itself.



11
12
13
# File 'lib/charming/presentation/layout/split.rb', line 11

def grow
  @grow
end

#heightObject (readonly)

The fixed width/height of the split (when set) and the grow weight for the split itself.



11
12
13
# File 'lib/charming/presentation/layout/split.rb', line 11

def height
  @height
end

#widthObject (readonly)

The fixed width/height of the split (when set) and the grow weight for the split itself.



11
12
13
# File 'lib/charming/presentation/layout/split.rb', line 11

def width
  @width
end

Instance Method Details

#add_child(node) ⇒ Object

Appends node (a child Split or Pane) to this Split.



27
28
29
# File 'lib/charming/presentation/layout/split.rb', line 27

def add_child(node)
  children << node
end

#focusable_namesObject

Returns the flattened list of focusable names from all child nodes.



32
33
34
# File 'lib/charming/presentation/layout/split.rb', line 32

def focusable_names
  children.flat_map(&:focusable_names)
end

#render(rect) ⇒ Object

Renders each child into its own sub-rect, then overlays them on a blank canvas of the parent’s dimensions.



38
39
40
41
42
43
44
45
46
# File 'lib/charming/presentation/layout/split.rb', line 38

def render(rect)
  frame = UI.place("", width: rect.width, height: rect.height)

  child_rects(rect).zip(children).each do |child_rect, child|
    frame = UI.overlay(frame, child.render(child_rect), top: child_rect.y - rect.y, left: child_rect.x - rect.x)
  end

  frame
end