Class: Clogs::Slot

Inherits:
Drawable
  • Object
show all
Defined in:
lib/clogs/drawables/slot.rb

Overview

A slot: the container drawable that does Shoes' layout.

Shoes has exactly two layout modes and they are both simple:

stack - children are placed one below another, each as wide as the slot
flow  - children are placed left to right and wrap when they run out of
      room, like words in a paragraph

Anything with an explicit left and top is lifted out of that flow and placed at those coordinates relative to the slot, which is how Shoes programs do absolute positioning.

Direct Known Subclasses

Flow, Mask, Stack, Widget

Instance Attribute Summary

Attributes inherited from Drawable

#abs_x, #abs_y, #children, #height, #parent, #shoes_linkable_id, #styles, #width, #x, #y

Instance Method Summary collapse

Methods inherited from Drawable

#add_child, #app, #clickable?, #contains?, #destroy_self, #draw, #each_peer, #focus_gained, #focus_lost, #focusable?, for_shoes_class, #hidden?, #initialize, #margin, #needs_layout!, #notify, #on_click, #on_key, #on_mouse_move, #on_release, #positioned?, #properties_changed, #redraw!, #remove_child, #requested_height, #requested_width, #set_parent, #style

Constructor Details

This class inherits a constructor from Clogs::Drawable

Instance Method Details

#decorationsObject

Backgrounds and borders are children in the Shoes tree but they are not laid out: they cover the whole slot.



28
29
30
# File 'lib/clogs/drawables/slot.rb', line 28

def decorations
  @children.select { |c| c.is_a?(Background) || c.is_a?(Border) }
end

#flow?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/clogs/drawables/slot.rb', line 18

def flow?
  false
end

#laid_out_childrenObject



32
33
34
# File 'lib/clogs/drawables/slot.rb', line 32

def laid_out_children
  @children.reject { |c| c.is_a?(Background) || c.is_a?(Border) || c.hidden? }
end

#layout_flow(kids, content_width) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/clogs/drawables/slot.rb', line 73

def layout_flow(kids, content_width)
  pl, pt, = padding
  x = 0
  y = 0
  line_height = 0
  kids.each do |child|
    cml, cmt, cmr, cmb = child.margin
    child.measure(content_width)
    advance = cml + child.width + cmr
    if x.positive? && x + advance > content_width
      y += line_height
      x = 0
      line_height = 0
    end
    child.x = pl + x + cml
    child.y = pt + y + cmt
    x += advance
    line_height = [line_height, cmt + child.height + cmb].max
  end
  y + line_height
end

#layout_stack(kids, content_width) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/clogs/drawables/slot.rb', line 59

def layout_stack(kids, content_width)
  _pl, pt, = padding
  pl = padding[0]
  y = pt
  kids.each do |child|
    cml, cmt, _cmr, cmb = child.margin
    child.measure(content_width)
    child.x = pl + cml
    child.y = y + cmt
    y += cmt + child.height + cmb
  end
  y - pt
end

#measure(available_width) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clogs/drawables/slot.rb', line 36

def measure(available_width)
  ml, _mt, mr, _mb = margin
  pl, pt, pr, pb = padding

  outer_width = requested_width(available_width) || (available_width - ml - mr)
  outer_width = 0 if outer_width.negative?
  content_width = [outer_width - pl - pr, 0].max

  flowed, positioned = laid_out_children.partition { |c| !c.positioned? }
  content_height = flow? ? layout_flow(flowed, content_width) : layout_stack(flowed, content_width)

  positioned.each do |child|
    child.measure(content_width)
    child.x = pl + Style.dimension(child.style(:left), content_width).to_i
    child.y = pt + Style.dimension(child.style(:top), outer_width).to_i
  end

  @content_width = content_width
  @width = outer_width
  @height = requested_height(available_width) || (content_height + pt + pb)
  decorations.each { |d| d.measure_for_slot(@width, @height) }
end

#paddingObject



22
23
24
# File 'lib/clogs/drawables/slot.rb', line 22

def padding
  @padding ||= Style.paddings(@styles)
end

#paint(painter, ox, oy) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/clogs/drawables/slot.rb', line 95

def paint(painter, ox, oy)
  @abs_x = ox + @x
  @abs_y = oy + @y

  decorations.grep(Background).each { |d| d.paint_for_slot(painter, @abs_x, @abs_y, @width, @height) }
  @children.each do |child|
    next if child.hidden? || child.is_a?(Background) || child.is_a?(Border)

    child.paint(painter, @abs_x, @abs_y)
  end
  decorations.grep(Border).each { |d| d.paint_for_slot(painter, @abs_x, @abs_y, @width, @height) }
end