Class: Chamomile::Widgets::HorizontalLayout

Inherits:
Object
  • Object
show all
Defined in:
lib/chamomile/frame.rb

Overview

Horizontal layout — splits width among children.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHorizontalLayout

Returns a new instance of HorizontalLayout.



189
190
191
# File 'lib/chamomile/frame.rb', line 189

def initialize
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



187
188
189
# File 'lib/chamomile/frame.rb', line 187

def children
  @children
end

Instance Method Details

#panel(title = nil, border: :rounded, width: nil, &block) ⇒ Object



193
194
195
196
197
198
# File 'lib/chamomile/frame.rb', line 193

def panel(title = nil, border: :rounded, width: nil, &block)
  p = Panel.new(title: title, border: border, width: width)
  block.call(p) if block
  @children << p
  p
end

#render(width: 80, height: 24) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/chamomile/frame.rb', line 206

def render(width: 80, height: 24)
  return "" if @children.empty?

  child_width = width / @children.length
  parts = @children.map { |c| c.render(width: child_width, height: height) }

  # Join horizontally line-by-line
  all_lines = parts.map { |p| p.split("\n", -1) }
  max_lines = all_lines.map(&:length).max || 0
  all_lines.each { |ls| ls << "" while ls.length < max_lines }

  (0...max_lines).map do |row|
    all_lines.map { |ls| ls[row] || "" }.join
  end.join("\n")
end

#text(content) ⇒ Object



200
201
202
203
204
# File 'lib/chamomile/frame.rb', line 200

def text(content)
  t = Text.new(content: content.to_s)
  @children << t
  t
end