Class: Charming::Layout::Pane

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

Overview

Pane is a leaf layout node: a single rectangle with optional border, padding, and styling, containing a piece of content (a string, a View, or a block evaluated in the view’s context). Panes with a ‘name` and `behavior.focus: true` are registered as focusable slots in the controller’s focus ring.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, content: nil, block: nil, view: nil, geometry: PaneGeometry.new, style: PaneStyle.new, behavior: PaneBehavior.new) ⇒ Pane

name is the focus slot identifier. content (or a block) is the body; view is the view used for instance_exec when the block is given. geometry, style, and behavior are value objects that own sizing, styling, and render-time flags.



16
17
18
19
20
21
# File 'lib/charming/presentation/layout/pane.rb', line 16

def initialize(name: nil, content: nil, block: nil, view: nil,
  geometry: PaneGeometry.new, style: PaneStyle.new,
  behavior: PaneBehavior.new)
  @name, @content, @block, @view = name, content, block, view
  @geometry, @style, @behavior = geometry, style, behavior
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/charming/presentation/layout/pane.rb', line 10

def name
  @name
end

Instance Method Details

#add_child(_node) ⇒ Object

Raises ArgumentError — panes are leaves and cannot contain layout children.

Raises:

  • (ArgumentError)


24
25
26
# File 'lib/charming/presentation/layout/pane.rb', line 24

def add_child(_node)
  raise ArgumentError, "pane cannot contain layout children"
end

#focusable_namesObject

Returns [name] when the pane is focusable and named, otherwise [].



29
30
31
# File 'lib/charming/presentation/layout/pane.rb', line 29

def focusable_names
  (@behavior.focus && name) ? [name] : []
end

#mouse_targets(rect) ⇒ Object

Returns the mouse target represented by this pane, if it has a name.



34
35
36
37
38
# File 'lib/charming/presentation/layout/pane.rb', line 34

def mouse_targets(rect)
  return [] unless name

  [{name: name, rect: rect, inner_rect: @geometry.inset(rect)}]
end

#render(rect) ⇒ Object

Renders the pane into rect, applying the configured style, border, and padding around the evaluated content.



42
43
44
45
# File 'lib/charming/presentation/layout/pane.rb', line 42

def render(rect)
  inner = @geometry.inset(rect)
  outer_style(inner).render(rendered_content(inner))
end