Class: Potty::Layout

Inherits:
Object
  • Object
show all
Defined in:
lib/potty/layout.rb

Overview

Layout engine for positioning and sizing widgets

Defined Under Namespace

Classes: Rect

Class Method Summary collapse

Class Method Details

.fill(container_rect) ⇒ Object

Fill available space



43
44
45
# File 'lib/potty/layout.rb', line 43

def self.fill(container_rect)
  container_rect.dup
end

.split_horizontal(container_rect, ratio: 0.5) ⇒ Object

Horizontal split



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/potty/layout.rb', line 25

def self.split_horizontal(container_rect, ratio: 0.5)
  split_x = container_rect.x + (container_rect.width * ratio).to_i
  left = Rect.new(
    container_rect.x,
    container_rect.y,
    split_x - container_rect.x,
    container_rect.height
  )
  right = Rect.new(
    split_x,
    container_rect.y,
    container_rect.width - (split_x - container_rect.x),
    container_rect.height
  )
  [left, right]
end

.stack(container_rect, widgets, spacing: 0) ⇒ Object

Vertical stack layout



14
15
16
17
18
19
20
21
22
# File 'lib/potty/layout.rb', line 14

def self.stack(container_rect, widgets, spacing: 0)
  y = container_rect.y
  widgets.map do |widget|
    height = widget.preferred_height(container_rect.width)
    rect = Rect.new(container_rect.x, y, container_rect.width, height)
    y += height + spacing
    rect
  end
end