Repository navigation
Expand file tree
/
Copy pathhelpers.lua
More file actions
218 lines (176 loc) · 5.52 KB
/
Copy pathhelpers.lua
File metadata and controls
218 lines (176 loc) · 5.52 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
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
local M = {}
-- Extra grid helper, which maybe we can PR to hammerspoon and/or remove later.
M.grid = dofile(hs.spoons.resourcePath('grid.lua'))
-- Legacy cells are strings, while screen-aware cells opt into a keyed table.
local function isScreenAwareCell(cell)
return type(cell) == "table" and (cell.cell ~= nil or cell.screen ~= nil)
end
local function normalizeCellVariants(cell)
if type(cell) == "string" then
return { cell }
end
if isScreenAwareCell(cell) then
return { cell }
end
return cell
end
local function resolveScreen(screenRef)
if screenRef == nil then
return nil
end
return M.grid.resolveScreen(screenRef) or screenRef
end
local function resolveCellVariant(layout, cell, state)
local variants = layout.cells[cell]
local variant = variants[state.current_layout_variant] or variants[1]
if isScreenAwareCell(variant) then
return variant.cell, resolveScreen(variant.screen)
end
return variant, nil
end
local function captureWindowOrder()
return {
ordered = hs.window.orderedWindows(),
focused = hs.window.focusedWindow(),
}
end
local function restoreWindowOrder(snapshot)
if not snapshot or not snapshot.ordered then
return
end
for i = #snapshot.ordered, 1, -1 do
local win = snapshot.ordered[i]
if win and win:application() and win:isVisible() then
win:raise()
end
end
local focused = snapshot.focused
if focused and focused:application() and focused:isVisible() then
focused:focus()
end
end
-- Apply layout.
function M.applyLayout(key, variant, state)
if key then state.current_layout_key = key end
if variant then state.current_layout_variant = variant end
local windowOrder = captureWindowOrder()
local layout = state.layouts[state.current_layout_key]
if layout.apps then
M.ensureOpenWhenConfigured(layout.apps, state.apps)
M.hideAllWindowsExcept(layout.apps, state.apps)
end
local elements = M.normalizeLayoutForApply(layout, state)
for _,custom in pairs(state.layout_customizations[state.current_layout_key] or {}) do
table.insert(elements, M.normalizeElementForApply(custom.app_id, custom.window, custom.cell, layout, state))
end
hs.layout.apply(elements)
restoreWindowOrder(windowOrder)
end
-- Normalize layout table from spoon convention for use in hs.layout.apply().
function M.normalizeLayoutForApply(layout, state)
if not layout.apps then
return layout
end
local normalized = {}
for app,config in pairs(layout.apps) do
table.insert(normalized, M.normalizeElementForApply(
state.apps[app].id,
state.apps[app].window,
config.cell,
layout,
state
))
end
return normalized
end
-- Apply single layout element for use in hs.layout.apply.
function M.normalizeElementForApply(app_id, window, cell, layout, state)
local cellVariant, screen = resolveCellVariant(layout, cell, state)
return {
app_id,
window,
screen,
nil,
M.grid.getCellWithMargins(cellVariant, screen),
options = {
absolute_x = true,
absolute_y = true,
},
}
end
-- Ensure app is open if `open = true` in layout.apps configuration.
-- Also ensure app is unhidden.
function M.ensureOpenWhenConfigured(layoutApps, allApps)
for name,config in pairs(layoutApps) do
local app
if config.open then
app = hs.application.open(allApps[name].id or name, 10, true)
else
app = hs.application.get(allApps[name].id or name)
end
if app then
app:unhide()
end
end
end
-- Hide all windows except those relevant to the layout.apps configuration.
function M.hideAllWindowsExcept(layoutApps, allApps)
local allowedIds = {}
for name,_ in pairs(layoutApps) do
table.insert(allowedIds, allApps[name].id)
end
for _,window in pairs(hs.window.visibleWindows()) do
local app = window:application()
local found = hs.fnutils.find(allowedIds, function(allowedId)
return allowedId == app:bundleID()
end)
if not found then
app:hide()
end
end
end
-- List apps in cells
function M.listAppsInCells(layout, state)
local cells = {}
for key,_ in pairs(layout.cells) do
cells[key] = {
['key'] = key,
['apps'] = {},
}
end
for app,config in pairs(layout.apps) do
table.insert(cells[config.cell].apps, app)
end
for key,config in pairs(state.layout_customizations[state.current_layout_key] or {}) do
if config.window:application() then
table.insert(cells[config.cell].apps, config.window:application():name()..' ('..config.window:title()..', '..config.window:id()..')')
else
state.layout_customizations[state.current_layout_key][key] = nil
print("cleaned up non-existent window layout config")
-- TODO: Extract config validate and cleanup logic, so that this can be used elsewhere
end
end
return cells
end
-- Normalize layouts
function M.normalizeLayouts(layouts)
local normalized = layouts or {}
-- Normalize cell strings to variant-friendly tables
for layoutKey,layout in ipairs(layouts or {}) do
local cells = {}
for _,cell in ipairs(layout.cells or {}) do
table.insert(cells, normalizeCellVariants(cell))
end
normalized[layoutKey].cells = cells
end
return normalized
end
-- Apply bind to cell
function M.applyBindToCell(key, state)
local app_id = hs.window.focusedWindow():application():bundleID()
local window = hs.window.focusedWindow()
local layout = state.layouts[state.current_layout_key]
hs.layout.apply({M.normalizeElementForApply(app_id, window, key, layout, state)})
state.addLayoutCustomization(app_id, window, key)
end
return M