-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowPlacer.lua
120 lines (97 loc) · 3.56 KB
/
WindowPlacer.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
---
--- 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())
local function to_unit(values, rotate)
if rotate then
return units(values[1], 0, values[2], 1 )
else
return units(0, values[1], 1, values[2] )
end
end
-- TODO make this as setup
local layout_setup = {
["1"] = { 0, 1/3, "+==1==+-----+-----+"},
["2"] = {1/3, 1/3, "+-----+==2==+-----+"},
["3"] = {2/3, 1/3, "+-----+-----+==3==+"},
["4"] = { 0, 1, "+========4========+"},
["5"] = { 0, 2/3, "+=====5=====+-----+"},
["6"] = {1/3, 2/3, "+-----+=====6=====+"},
["7"] = { 0, 1/2, "+=====7==+--------+"},
["8"] = {1/2, 1/2, "+--------+==8=====+"},
["9"] = {2/6, 3/6, "+-----+=====9==+--+"},
["a"] = {5/6, 1/6, "+-----+-----+--=0=+"},
}
local layout_keys = {
maximize = "4",
top = "7",
bottom = "8",
udemy = "6"
}
-- TODO make this as config
local screens = {
internal = "Built%-in",
monitor_1 = 'Thunderbolt',
monitor_2 = '5120x1440'
}
local layout = { ["portrait"] = {}, ["landscape"] = {}}
local layout_info = "Layout: "
local sortedLayoutSetupIterator = helper.table.sortByKeyIterator(layout_setup)
for i, v in sortedLayoutSetupIterator do
layout["portrait"][i] = to_unit(v, false)
layout["landscape"][i] = to_unit(v, true)
layout_info = layout_info .. "\n key "..i..": " .. v[3]:gsub("-"," "):gsub("+"," ")
end
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"
local newUnit = layout[direction][key]
window:moveToScreen(screen)
window:moveToUnit(newUnit)
end
debugInfo(layout_info)
local function startFn()
local window = hs.window.frontmostWindow()
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 key, _ in pairs(layout_setup) do
local move = function()
windowMoveToKey(screens.monitor_2, key)
hkbm:exit()
end
hkbm:bind({}, key, move)
hkbm:bind(hyper, key, move)
end
hs.hotkey.bind(hyper, "3", keyInfo("place on main screen"), function()
windowMoveToKey(screens.internal,layout_keys.maximize)
end)
hs.hotkey.bind(hyper, "4", keyInfo("place fullscreen on monitor"), function()
windowMoveToKey(screens.monitor_1,layout_keys.maximize)
end)
hs.hotkey.bind(hyper, "8", keyInfo("place 2/3 on monitor (Udemy Mode)"), function()
windowMoveToKey(screens.monitor_1,layout_keys.udemy)
end)
hs.hotkey.bind(hyper, "1", keyInfo("place on one half of monitor"), function()
windowMoveToKey(screens.monitor_1,layout_keys.top)
end)
hs.hotkey.bind(hyper, "2", keyInfo("place on other half of monitor"), function()
windowMoveToKey(screens.monitor_1,layout_keys.bottom)
end)