-
Notifications
You must be signed in to change notification settings - Fork 0
/
hammerspoon_init.lua
144 lines (122 loc) · 2.83 KB
/
hammerspoon_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
local laptopScreen = "Built-in Retina Display"
hs.window.animationDuration = 0
hs.hotkey.bind({"cmd", "alt"}, "F", function()
local win = hs.window.focusedWindow()
win:setFrame(win:screen():frame())
end)
hs.hotkey.bind({"cmd", "alt"}, "Left", function()
local win = hs.window.focusedWindow()
send("left", win)
end)
hs.hotkey.bind({"cmd", "alt"}, "Right", function()
local win = hs.window.focusedWindow()
send("right", win)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Left", function()
local win = hs.window.focusedWindow()
sendToStack("left", win)
end)
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "Right", function()
local win = hs.window.focusedWindow()
sendToStack("right", win)
end)
-- "left"|"right" -> index -> win
stacks = {left={}, right={}}
function renderStacks()
for name, s in pairs(stacks) do
renderStack(s, name)
end
end
function send(name, win)
local screen = win:screen()
local max = screen:frame()
-- if a window is in the other stack, remove it.
_, other = selectStack(name)
if contains(other, win) then
table.remove(other, find(win, other))
renderStacks()
end
local f = win:frame()
local leftPad = 0
if name == "right" then
leftPad = (max.w / 2)
end
f.x = max.x + leftPad
f.y = max.y
f.w = max.w / 2
f.h = max.h
win:setFrame(f)
end
function sendToStack(name, win)
stack, other = selectStack(name)
-- if a window is in the other stack, remove it.
if contains(other, win) then
table.remove(other, find(win, other))
end
-- if the window is already in this stack, cycle it.
if contains(stack, win) then
shift(stack)
else
-- otherwise, add it to the stack.
table.insert(stack, win)
end
renderStacks()
end
function renderStack(stack, name)
local count = #stack
for i, win in pairs(stack) do
local f = win:frame()
local screen = win:screen()
local max = screen:frame()
local leftPad = 0
if name == "right" then
leftPad = (max.w / 2)
end
local h = max.h / count
f.x = max.x + leftPad
f.y = max.y + (h * (i-1))
f.w = max.w / 2
f.h = h
win:setFrame(f)
end
end
-- returns stack, other
function selectStack(name)
local stacks = shallowcopy(stacks)
local stack = remove(stacks, name)
local other = stacks[next(stacks)]
return stack, other
end
function contains(stack, win)
for i, v in pairs(stack) do
if v == win then
return true
end
end
return false
end
function find(win, stack)
for i, w in pairs(stack) do
if w == win then
return i
end
end
return -1
end
function shift(stack)
first = table.remove(stack, 1)
table.insert(stack, first)
return stack
end
function shallowcopy(table)
local result = {}
for k, v in pairs(table) do
result[k] = v
end
return result
end
function remove(table, key)
v = table[key]
table[key] = nil
return v
end