-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackLayout.lua
More file actions
156 lines (138 loc) · 4.73 KB
/
StackLayout.lua
File metadata and controls
156 lines (138 loc) · 4.73 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
StackLayout = {
horizontal = true,
windows = {},
content = nil,
parent = nil,
indexCounter = 0
}
function StackLayout:new(parent, x, y, width, height, horizontal)
local content = window.create(parent, x, y, width, height)
local self = {
content = content,
horizontal = horizontal,
parent = parent,
windows = {},
indexCounter = 0
}
setmetatable(self, {
__index = StackLayout
})
return self
end
function StackLayout:getHeight()
local w, h = self.content.getSize()
return h
end
function StackLayout:getWidth()
local w, h = self.content.getSize()
return w
end
function StackLayout:createWindow(id, length, minLength, title, priority)
if self.windows[id] ~= nil then
print(self.windows[id])
return nil
end
local window = nil
if self.horizontal then
window = Window.Window:new(self.content, 1, 1, minLength, self:getHeight(), title)
window:setMinSize(minLength, self:getHeight())
window:setPrefSize(length, self:getHeight())
else
window = Window.Window:new(self.content, 1, 1, self:getWidth(), minLength, title)
window:setMinSize(self:getWidth(), minLength)
window:setPrefSize(self:getWidth(), length)
end
window:setLayoutId(id)
window:setLayoutIndex(self.indexCounter)
window:setLayoutPriority(priority or 0)
self.indexCounter = self.indexCounter + 1
self.windows[id] = window
self:layout()
return window
end
function StackLayout:sizeLeft(priority, available)
for id, window in pairs(self.windows) do
if window:isVisible() then
local needed = 0
if self.horizontal then
needed = window:getLayoutPriority() > priority and window:getPrefWidth() or window:getMinWidth()
else
needed = window:getLayoutPriority() > priority and window:getPrefHeight() or window:getMinHeight()
end
local required = math.min(available, needed)
available = available - required
end
end
return available
end
function StackLayout:getSortedByValue(tbl, sortFunction)
local keys = {}
for key in pairs(tbl) do
table.insert(keys, key)
end
table.sort(keys, function(a, b)
return sortFunction(tbl[a], tbl[b])
end)
local res = {}
for i, key in ipairs(keys) do
table.insert(res, tbl[key])
end
return res
end
function StackLayout:layout()
local sized = {}
local byPrio = self:getSortedByValue(self.windows, function(a, b)
return a:getLayoutPriority() > b:getLayoutPriority()
end)
local available = self.horizontal and self:getWidth() or self:getHeight()
for i, window in ipairs(byPrio) do
if window:isVisible() then
local needed = 0
if self.horizontal then
needed = window:getMinWidth()
else
needed = window:getMinHeight()
end
local required = math.min(available, needed)
sized[window:getLayoutId()] = required
-- print(string.format("pass1: window: %s, avail: %d, size: %d, prio: %d", window:getLayoutId(), available, required, window:getLayoutPriority()))
available = available - required
end
end
for i, window in ipairs(byPrio) do
if window:isVisible() then
local needed = sized[window:getLayoutId()]
local diff = 0
if self.horizontal then
diff = window:getPrefWidth() - needed
else
diff = window:getPrefWidth() - needed
end
local additional = math.min(available, diff)
sized[window:getLayoutId()] = needed + additional
--print(string.format("pass2: window: %s, avail: %d, size: %d, prio: %d", window:getLayoutId(), available, needed + additional, window:getLayoutPriority()))
available = available - additional
if available <= 0 then
break
end
end
end
local byIndex = self:getSortedByValue(self.windows, function(a, b)
return a:getLayoutIndex() < b:getLayoutIndex()
end)
self.content.clear()
local offset = 1
for i, window in ipairs(byIndex) do
if window:isVisible() then
local size = sized[window:getLayoutId()]
if self.horizontal then
window:reposition(offset, 1, size, self:getHeight())
else
window:reposition(1, offset, self:getWidth(), size)
end
window:redraw()
--print(string.format("align: window: %s, size: %d, offset: %d", window:getLayoutId(), size, offset))
offset = offset + size
end
end
end