File size: 1,878 Bytes
170e15c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
--[[

ColumnLayout.lua - Fixed-width column layout helper

]]--

ColumnLayout = Polo {
  DEFAULT_COLUMN_PADDING = 15,
  DEFAULT_NUM_COLUMNS = 3,
}

function ColumnLayout:init()
  assert(self.render_column, 'render_column function must be provided')
  self.column_padding = self.column_padding or self.DEFAULT_COLUMN_PADDING
  self.margin_bottom = self.margin_bottom or 0
  self.margin_left = self.margin_left or 0
  self.margin_right = self.margin_right or 0
  self.margin_top = self.margin_top or 0
  self.num_columns = self.num_columns or self.DEFAULT_NUM_COLUMNS
  self.width = self.width or 0
end

function ColumnLayout:render()
  local total_padding = (self.num_columns - 1) * self.column_padding
  local total_width = self.width
  if total_width == 0 then
    total_width = self:_get_avail_width()
  end
  local content_width = total_width - self.margin_left - self.margin_right
  local column_width = (content_width - total_padding) / self.num_columns

  self:_horiz_margin(self.margin_left)

  for i = 1, self.num_columns do
    local column = {num = i, width = column_width}

    self:_with_group(function ()
      self:_vert_margin(self.margin_top, column_width)
      self.render_column(column)
      self:_vert_margin(self.margin_bottom, column_width)
    end)

    if i < self.num_columns then
      self:_column_gap(self.column_padding)
    end
  end
end

function ColumnLayout:_column_gap(padding)
  ImGui.SameLine(ctx, 0, padding)
end

function ColumnLayout:_get_avail_width()
  local avail_width, _ = ImGui.GetContentRegionAvail(ctx)
  return avail_width
end

function ColumnLayout:_horiz_margin(margin)
  ImGui.SetCursorPosX(ctx, ImGui.GetCursorPosX(ctx) + margin)
end

function ColumnLayout:_vert_margin(margin, width)
  ImGui.Dummy(ctx, width, margin)
end

function ColumnLayout:_with_group(f)
  ImGui.BeginGroup(ctx)
  app:trap(f)
  ImGui.EndGroup(ctx)
end