-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinit.lua
309 lines (267 loc) · 8.51 KB
/
init.lua
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
--[[
Treesome: Binary Tree-based tiling layout for Awesome 3
Github: https://github.com/RobSis/treesome
License: GNU General Public License v2.0
--]]
local awful = require("awful")
local beautiful = require("beautiful")
local Bintree = require("treesome/bintree")
local os = os
local math = math
local ipairs = ipairs
local pairs = pairs
local table = table
local tonumber = tonumber
local tostring = tostring
local type = type
local capi =
{
client = client,
mouse = mouse
}
module("treesome")
name = "treesome"
-- Layout icon
beautiful.layout_treesome = os.getenv("HOME") .. "/.config/awesome/treesome/layout_icon.png"
-- Configuration
local configuration = {
focusFirst = true
}
-- Globals
local trees = {}
local forceSplit = nil
-- get an unique identifier of a window
function hash(client)
return client.window
end
function table_find(tbl, item)
for key, value in pairs(tbl) do
if value == item then return key end
end
return false
end
function table_diff(table1, table2)
local diffList = {}
for i,v in ipairs(table1) do
if table2[i] ~= v then
table.insert(diffList, v)
end
end
if #diffList == 0 then
diffList = nil
end
return diffList
end
-- get ancestors of node with given data
function Bintree:trace(data, path, dir)
if path then
table.insert(path, {split=self.data, direction=dir})
end
if data == self.data then
return path
end
if type(self.left) == "table" then
if (self.left:trace(data, path, "left")) then
return true
end
end
if type(self.right) == "table" then
if (self.right:trace(data, path, "right")) then
return true
end
end
if path then
table.remove(path)
end
end
-- remove all leaves with data that don't appear in given table
function Bintree:filterClients(node, clients)
if node then
if node.data and not table_find(clients, node.data) and
node.data ~= "horizontal" and node.data ~= "vertical" then
self:removeLeaf(node.data)
end
local output = nil
if node.left then
self:filterClients(node.left, clients)
end
if node.right then
self:filterClients(node.right, clients)
end
end
end
function setslave(client)
if not trees[tostring(awful.tag.selected(capi.mouse.screen))] then
awful.client.setslave(client)
end
end
function setmaster(client)
if not trees[tostring(awful.tag.selected(capi.mouse.screen))] then
awful.client.setmaster(client)
end
end
function horizontal()
forceSplit = "horizontal"
end
function vertical()
forceSplit = "vertical"
end
function arrange(p)
local area = p.workarea
local n = #p.clients
local tag = tostring(awful.tag.selected(capi.mouse.screen))
if not trees[tag] then
trees[tag] = {
t = nil,
lastFocus = nil,
clients = nil,
n = 0
}
end
if trees[tag] ~= nil then
focus = capi.client.focus
if focus ~= nil then
if awful.client.floating.get(focus) then
focus = nil
else
trees[tag].lastFocus = focus
end
end
end
-- rearange only on change
local changed = 0
local layoutSwitch = false
if trees[tag].n ~= n then
if math.abs(n - trees[tag].n) > 1 then
layoutSwitch = true
end
if not trees[tag].n or n > trees[tag].n then
changed = 1
else
changed = -1
end
trees[tag].n = n
else
if trees[tag].clients then
local diff = table_diff(p.clients, trees[tag].clients)
if diff and #diff == 2 then
trees[tag].t:swapLeaves(hash(diff[1]), hash(diff[2]))
end
end
end
trees[tag].clients = p.clients
-- some client removed. remove (from) tree
if changed < 0 then
if n > 0 then
local tokens = {}
for i, c in ipairs(p.clients) do
tokens[i] = hash(c)
end
trees[tag].t:filterClients(trees[tag].t, tokens)
else
trees[tag] = nil
end
end
-- some client added. put it in the tree as a sibling of focus
local prevClient = nil
local nextSplit = 0
if changed > 0 then
for i, c in ipairs(p.clients) do
if not trees[tag].t or not trees[tag].t:find(hash(c)) then
if focus == nil then
focus = trees[tag].lastFocus
end
local focusNode = nil
local focusGeometry = nil
local focusId = nil
if trees[tag].t and focus and hash(c) ~= hash(focus) and not layoutSwitch then
-- split focused window
focusNode = trees[tag].t:find(hash(focus))
focusGeometry = focus:geometry()
focusId = hash(focus)
else
-- the layout was switched with more clients to order at once
if prevClient then
focusNode = trees[tag].t:find(hash(prevClient))
nextSplit = (nextSplit + 1) % 2
focusId = hash(prevClient)
else
if not trees[tag].t then
-- create as root
trees[tag].t = Bintree.new(hash(c))
focusId = hash(c)
focusGeometry = {
width = 0,
height = 0
}
end
end
end
if focusNode then
if focusGeometry == nil then
local splits = {"horizontal", "vertical"}
focusNode.data = splits[nextSplit + 1]
else
if (forceSplit ~= nil) then
focusNode.data = forceSplit
else
if (focusGeometry.width <= focusGeometry.height) then
focusNode.data = "vertical"
else
focusNode.data = "horizontal"
end
end
end
if configuration.focusFirst then
focusNode:addLeft(Bintree.new(focusId))
focusNode:addRight(Bintree.new(hash(c)))
else
focusNode:addLeft(Bintree.new(hash(c)))
focusNode:addRight(Bintree.new(focusId))
end
end
end
prevClient = c
end
forceSplit = nil
end
-- Useless gap.
local useless_gap = tonumber(beautiful.useless_gap_width)
if useless_gap == nil then
useless_gap = 0
end
-- draw it
if n >= 1 then
for i, c in ipairs(p.clients) do
local geometry = {
width = area.width - ( useless_gap * 2.0 ),
height = area.height - ( useless_gap * 2.0 ),
x = area.x + useless_gap,
y = area.y + useless_gap
}
local clientNode = trees[tag].t:find(hash(c))
local path = {}
trees[tag].t:trace(hash(c), path)
for i, v in ipairs(path) do
if i < #path then
split = v.split
-- is the client left of right from this node
direction = path[i + 1].direction
if split == "horizontal" then
geometry.width = ( geometry.width - useless_gap ) / 2.0
if direction == "right" then
geometry.x = geometry.x + geometry.width + useless_gap
end
elseif split == "vertical" then
geometry.height = ( geometry.height - useless_gap ) / 2.0
if direction == "right" then
geometry.y = geometry.y + geometry.height + useless_gap
end
end
end
end
local sibling = trees[tag].t:getSibling(hash(c))
c:geometry(geometry)
end
end
end