This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
forked from nathankot/mjolnir.tiling
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathinit.lua
195 lines (164 loc) · 4.6 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
local tiling = {}
local application = require "hs.application"
local window = require "hs.window"
local screen = require "hs.screen"
local fnutils = require "hs.fnutils"
local geometry = require "hs.geometry"
local alert = require "hs.alert"
local layouts = require "hs.tiling.layouts"
local spaces = {}
local settings = { layouts = {} }
local excluded = {}
-- navigate to layout by name
function tiling.goToLayout(name)
local space = getSpace()
local i = 0
while space.layout ~= name and i < #settings.layouts do
space.layout = space.layoutCycle()
i = i + 1
end
if i < #settings.layouts then
alert.show(space.layout, 1)
apply(space.windows, space.layout)
else
alert.show('Layout ' .. name .. ' does not exist', 1)
end
end
function tiling.toggleFloat(floatfn)
local win = window:focusedWindow()
local id = win:id()
excluded[id] = not excluded[id]
if excluded[id] then
if floatfn then floatfn(win) end
alert.show("Excluding " .. win:title() .. " from tiles")
else
alert.show("Adding " .. win:title() .. " to tiles")
end
local space = getSpace()
apply(space.windows, space.layout)
end
function tiling.addLayout(name, layout)
layouts[name] = layout
setLayouts(layouts)
end
function tiling.set(name, value)
settings[name] = value
end
function tiling.retile()
local space = getSpace()
apply(space.windows, space.layout)
end
function tiling.cycle(direction)
local space = getSpace()
local windows = space.windows
local win = window:focusedWindow() or windows[1]
local direction = direction or 1
local currentIndex = fnutils.indexOf(windows, win)
local layout = space.layout
if not currentIndex then return end
nextIndex = currentIndex + direction
if nextIndex > #windows then
nextIndex = 1
elseif nextIndex < 1 then
nextIndex = #windows
end
windows[nextIndex]:focus()
apply(windows, layout)
end
function tiling.cycleLayout()
local space = getSpace()
space.layout = space.layoutCycle()
alert.show(space.layout, 1)
apply(space.windows, space.layout)
end
function tiling.promote()
local space = getSpace()
local windows = space.windows
local win = window:focusedWindow() or windows[1]
local i = fnutils.indexOf(windows, win)
if not i then return end
local current = table.remove(windows, i)
table.insert(windows, 1, current)
win:focus()
apply(windows, space.layout)
end
function tiling.setMainVertical(val)
if val > 0 and val < 1 then
local space = getSpace()
if space.layout == 'main-vertical-variable' then
space.mainVertical = val
tiling.retile()
end
end
end
function tiling.adjustMainVertical(factor)
local space = getSpace()
if space.layout == 'main-vertical-variable' then
local mainVertical = space.mainVertical
if mainVertical == nil then
mainVertical = 0.5
end
tiling.setMainVertical(mainVertical + factor)
end
end
function apply(windows, layout)
layouts[layout](windows)
end
function isWindowIncluded(win)
onScreen = win:screen() == screen.mainScreen()
standard = win:isStandard()
hasTitle = #win:title() > 0
isTiling = not excluded[win:id()]
return onScreen and standard and hasTitle and isTiling
end
-- Infer a 'space' from our existing spaces
function getSpace()
local windows = fnutils.filter(window.visibleWindows(), isWindowIncluded)
fnutils.each(spaces, function(space)
local matches = 0
fnutils.each(space.windows, function(win)
if fnutils.contains(windows, win) then matches = matches + 1 end
end)
space.matches = matches
end)
table.sort(spaces, function(a, b)
return a.matches > b.matches
end)
local space = {}
if #spaces == 0 or spaces[1].matches == 0 then
space.windows = windows
space.layoutCycle = fnutils.cycle(settings.layouts)
space.layout = settings.layouts[1]
table.insert(spaces, space)
else
space = spaces[1]
end
space.windows = syncWindows(space.windows, windows)
return space
end
function syncWindows(windows, newWindows)
-- Remove any windows no longer around
windows = fnutils.filter(windows, function(win)
return fnutils.contains(newWindows, win)
end)
-- Add any new windows since
fnutils.each(newWindows, function(win)
if fnutils.contains(windows, win) == false then
table.insert(windows, win)
end
end)
-- Remove any bad windows
windows = fnutils.filter(windows, function(win)
return win:isStandard()
end)
return windows
end
function setLayouts(layouts)
local n = 0
for k, v in pairs(layouts) do
n = n + 1
settings.layouts[n] = k
end
end
setLayouts(layouts)
return tiling