Repository navigation
Expand file tree
/
Copy pathWindowPlacer.lua
More file actions
327 lines (267 loc) · 10.1 KB
/
Copy pathWindowPlacer.lua
File metadata and controls
327 lines (267 loc) · 10.1 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
---
--- Generated by Luanalysis
--- Created by muescha.
--- DateTime: 26.01.23 02:12
---
--- done with ChatGPT :)
--- can you write me an hammerspoon lua script where i can place a window on
--- main screen on macbook and the other one on halfscreen (top and bottom)
--- of my portrait mode external monitor.
---
--- can it done more generic for the external monitor, so that it find out by
--- self if a monitor external, and if it is in portrait mode it should place
--- on top or bottom and if it is in landscape mode it should be placed on left and right
fileInfo()
local units = require("hs.geometry").rect
debugTable(hs.screen.allScreens())
for k, v in pairs(hs.screen.allScreens()) do
print(v:currentMode().desc .. ": " .. v:name())
end
-- Layout grid: 1 unit = 1/GRID of the screen
-- 12 covers halves, thirds, quarters, sixths and twelfths
local GRID = 12
-- chars per grid unit in the info line; must be even so every X is exactly centered
local INFO_CHARS_PER_UNIT = 4
local function to_unit(values, rotate)
local start = values.start / GRID
local size = (values.stop - values.start) / GRID
if rotate then
return units(start, 0, size, 1)
else
return units(0, start, 1, size)
end
end
-- TODO make this as setup
-- grid: 0 . . 3 4 . 6 . 8 9 10 . 12
-- 1/4 1/3 1/2 2/3 3/4 5/6
local layout_setup = {
{ key = "4", start = 0, stop = 12 }, -- max
{ key = "1", start = 0, stop = 4 },
{ key = "2", start = 4, stop = 8 },
{ key = "3", start = 8, stop = 12 },
{ key = "5", start = 0, stop = 8 },
{ key = "g", start = 8, stop = 10 },
{ key = "6", start = 4, stop = 10 },
{ key = "7", start = 4, stop = 12 }, -- udemy
{ key = "8", start = 6, stop = 12 }, -- bottom
{ key = "a", start = 0, stop = 6 }, -- top
{ key = "b", start = 6, stop = 10 },
{ key = "c", start = 10, stop = 12 },
{ key = "d", start = 0, stop = 10 },
{ key = "e", start = 6, stop = 9 },
{ key = "f", start = 9, stop = 10 },
}
for _, layout_entry in ipairs(layout_setup) do
local prefix = "WindowPlacer " .. tostring(layout_entry.key) .. ": "
local range = tostring(layout_entry.start) .. " → " .. tostring(layout_entry.stop)
assert(math.type(layout_entry.start) == "integer" and math.type(layout_entry.stop) == "integer",
prefix .. range .. " needs whole numbers")
assert(layout_entry.start < layout_entry.stop,
prefix .. range .. " is reversed")
assert(0 <= layout_entry.start and layout_entry.stop <= GRID,
prefix .. range .. " is outside 0 → " .. GRID)
end
-- Builds the info line for every layout, e.g. "+-------+===X===+-------+"
-- Anchor rule: every inner boundary draws a vertical line from the first row
-- where it is a boundary down to the last such row; skipped inside a '=' range.
local function buildLayoutInfos(setup)
-- grid position -> column in the info line
local function column(position)
return position * INFO_CHARS_PER_UNIT
end
local lastColumn = column(GRID)
-- for every inner anchor: first and last row where it is a boundary
local anchorFirstRow = {}
local anchorLastRow = {}
for rowIndex, layout_entry in ipairs(setup) do
for _, anchor in ipairs({ layout_entry.start, layout_entry.stop }) do
if anchor > 0 and anchor < GRID then
anchorFirstRow[anchor] = anchorFirstRow[anchor] or rowIndex
anchorLastRow[anchor] = rowIndex
end
end
end
for rowIndex, layout_entry in ipairs(setup) do
local rangeStart = layout_entry.start
local rangeStop = layout_entry.stop
local chars = {} -- chars[0] .. chars[lastColumn], one char per column
-- background '-' and the selected range '='
for col = 0, lastColumn do
local insideRange = col >= column(rangeStart) and col <= column(rangeStop)
chars[col] = insideRange and "=" or "-"
end
-- outer borders and own boundaries
for _, boundary in ipairs({ 0, GRID, rangeStart, rangeStop }) do
chars[column(boundary)] = "+"
end
-- anchor lines, but never through the selected range
for anchor, firstRow in pairs(anchorFirstRow) do
local rowInSpan = rowIndex >= firstRow and rowIndex <= anchorLastRow[anchor]
local outsideRange = anchor < rangeStart or anchor > rangeStop
if rowInSpan and outsideRange then
chars[column(anchor)] = "+"
end
end
chars[column(rangeStart + rangeStop) // 2] = "X"
layout_entry.info = table.concat(chars, "", 0, lastColumn)
end
end
buildLayoutInfos(layout_setup)
local layout_keys = {
maximize = "4",
top = "a",
bottom = "8",
udemy = "7",
}
-- TODO make this as config
local screens = {
internal = "Built%-in",
monitor_1 = "Thunderbolt",
monitor_2 = "5120x1440",
}
local layout = {
portrait = {},
landscape = {},
}
local function formatLayoutInfo(info, key, symbols)
return "key " .. key .. ": " .. info
:gsub("%+([=X])", symbols.left .. "%1") -- ←→ ◄ ► ◂▸ ‹› ╞╡
:gsub("([=X])%+", "%1".. symbols.right)
:gsub("X", key)
:gsub("%+", symbols.middle) -- ·¦│
:gsub("%-", " ")
:gsub("=", "─")
end
local function getSymbols(text)
local symbol_sets = {
{ left = "╞", right = "╡", middle = " " },
{ left = "╞", right = "╡", middle = "·" },
{ left = "╞", right = "╡", middle = "│" },
{ left = "←", right = "→", middle = " " },
{ left = "‹", right = "›", middle = " " },
}
math.randomseed(os.time())
local layout_number = math.random(#symbol_sets)
local symbols = symbol_sets[layout_number]
return { layout = layout_number, set = symbols }
end
local function createLayoutInfo(layout_setup)
local symbols = getSymbols()
local layout_info = "Layout (" .. symbols.layout .."): "
for _, v in ipairs(layout_setup) do
layout_info = layout_info .. "\n" .. formatLayoutInfo(v.info, v.key, symbols.set)
end
return layout_info
end
for _, v in ipairs(layout_setup) do
layout.portrait[v.key] = to_unit(v, false)
layout.landscape[v.key] = to_unit(v, true)
end
local function framesEqual(f1, f2, tolerance)
tolerance = tolerance or 1
return math.abs(f1.x - f2.x) < tolerance
and math.abs(f1.y - f2.y) < tolerance
and math.abs(f1.w - f2.w) < tolerance
and math.abs(f1.h - f2.h) < tolerance
end
local function windowMoveToUnit(window, screen, newUnit)
if window:screen() ~= screen then
return
end
local expectedFrame = screen:fromUnitRect(newUnit)
local actualFrame = window:frame()
if framesEqual(actualFrame, expectedFrame) then
debugInfo("No Adjustment needed...")
return false
end
debugInfo("Adjustment needed...")
window:moveToUnit(newUnit)
return true
end
local function windowPlace(window, screen, newUnit)
window:moveToScreen(screen)
-- retryWhile(function() return windowMoveToUnit(window, screen, newUnit) end)
retryWhileOnComplete(
function()
return windowMoveToUnit(window, screen, newUnit)
end,
function(success)
print(success and "Success: placed the window on right place!" or "Failed to place windows!")
end
)
end
-- Window carousel, see docs/2026-10-31-window-carousel-concept.md
local carousel_keys = {
add = "k", -- Carousel
layout = "l", -- Layout
hide = "h", -- Hide
remove = "r", -- Remove
unload = "x", -- Close
}
local carousel = require("Helpers.WindowCarousel").new({
screen = function() return hs.screen.find(screens.monitor_2) end,
place = windowPlace,
keys = carousel_keys,
})
local function windowMoveToKey(screenId, key)
local screen = hs.screen.find(screenId)
if screen == nil then
hs.alert.show("No screen available for keyword: " .. screenId)
return
end
local window = hs.window.focusedWindow()
local direction = screen:rotate() == 0 and "landscape" or "portrait"
carousel:forget(window)
windowPlace(window, screen, layout[direction][key])
end
local function startFn()
local window = hs.window.frontmostWindow()
local layout_info = createLayoutInfo(layout_setup) .. "\n\n" .. carousel:info(hs.window.focusedWindow())
debugInfo(layout_info)
hs.alert.show(layout_info, { textFont = "Menlo" }, window, "do-not-close")
end
local function exitFn()
hs.alert.closeAll()
end
local hkbm = hotkeybindmodal(hyper, "w", keyInfo("place on Big Monitor "), startFn, exitFn)
for _, v in ipairs(layout_setup) do
local key = v.key
local move = function()
windowMoveToKey(screens.monitor_2, key)
hkbm:exit()
end
hkbm:bind({}, key, move)
hkbm:bind(hyper, key, move)
end
local carousel_actions = {
[carousel_keys.add] = function(window) carousel:add(window) end,
[carousel_keys.layout] = function() carousel:show() end,
[carousel_keys.hide] = function() carousel:hide() end,
[carousel_keys.remove] = function(window) carousel:remove(window) end,
[carousel_keys.unload] = function(window) carousel:unload(window) end,
}
for key, action in pairs(carousel_actions) do
local run = function()
hkbm:exit()
action(hs.window.focusedWindow())
end
hkbm:bind({}, key, run)
hkbm:bind(hyper, key, run)
end
local opts_keys = {
SCREEN = 1,
LAYOUT = 2,
DESC = 3,
}
local layout_non_modal = {
["1"] = { screens.monitor_1, layout_keys.top, "place on one half of monitor" },
["2"] = { screens.monitor_1, layout_keys.bottom, "place on other half of monitor" },
["3"] = { screens.internal, layout_keys.maximize, "place on main screen" },
["4"] = { screens.monitor_1, layout_keys.maximize, "place fullscreen on monitor" },
["8"] = { screens.monitor_1, layout_keys.udemy, "place 2/3 on monitor (Udemy Mode)" },
}
for key, opts in pairs(layout_non_modal) do
hs.hotkey.bind(hyper, key, keyInfo(opts[opts_keys.DESC]), function()
windowMoveToKey(opts[opts_keys.SCREEN], opts[opts_keys.LAYOUT])
end)
end