Class: Potty::Widgets::Panel

Inherits:
Container show all
Defined in:
lib/potty/widgets/panel.rb

Overview

A bordered, optionally titled container. Stacks its children vertically inside a one-cell border frame.

Instance Attribute Summary collapse

Attributes inherited from Container

#children

Attributes inherited from Base

#app, #focused, #parent, #rect

Instance Method Summary collapse

Methods inherited from Container

#add, #focusable_widgets, #on_layout, #tick

Methods inherited from Base

#activate, #blur, #can_focus?, #deactivate, #focus, #handle_escape, #handle_key, #hide, #layout, #on_blur, #on_focus, #on_layout, #show, #theme, #tick, #visible=, #visible?

Methods included from Events

#emit, #listeners?, #off, #on

Constructor Details

#initialize(app, title: nil, style: :single, color: :normal, spacing: 0) ⇒ Panel

Returns a new instance of Panel.



13
14
15
16
17
18
# File 'lib/potty/widgets/panel.rb', line 13

def initialize(app, title: nil, style: :single, color: :normal, spacing: 0)
  super(app, spacing: spacing)
  @title = title
  @style = style
  @color = color
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



11
12
13
# File 'lib/potty/widgets/panel.rb', line 11

def color
  @color
end

#styleObject

Returns the value of attribute style.



11
12
13
# File 'lib/potty/widgets/panel.rb', line 11

def style
  @style
end

#titleObject

Returns the value of attribute title.



11
12
13
# File 'lib/potty/widgets/panel.rb', line 11

def title
  @title
end

Instance Method Details

#layout_childrenObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/potty/widgets/panel.rb', line 30

def layout_children
  inner = Layout::Rect.new(
    @rect.x + 1,
    @rect.y + 1,
    [@rect.width - 2, 0].max,
    [@rect.height - 2, 0].max
  )
  rects = Layout.stack(inner, @children, spacing: @spacing)
  @children.zip(rects).each { |child, rect| child.layout(rect) }
end

#preferred_height(width) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/potty/widgets/panel.rb', line 20

def preferred_height(width)
  inner_w = [width - 2, 0].max
  inner = if @children.empty?
            0
          else
            @children.sum { |c| c.preferred_height(inner_w) } + @spacing * (@children.size - 1)
          end
  inner + 2 # top + bottom border
end

#render(window) ⇒ Object



41
42
43
44
45
46
# File 'lib/potty/widgets/panel.rb', line 41

def render(window)
  return unless @visible && @rect

  Border.draw(window, @rect, style: @style, attr: theme[@color], title: @title)
  super # render children inside the frame
end