-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathwindow-management.lua
233 lines (204 loc) · 6.92 KB
/
window-management.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
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
-- -----------------------------------------------------------------------
-- ** Something Global ** --
-- -----------------------------------------------------------------------
-- Comment out this following line if you wish to see animations
local windowMeta = {}
window = require "hs.window"
hs.window.animationDuration = 0
grid = require "hs.grid"
grid.setMargins('0, 0')
module = {}
-- Set screen watcher, in case you connect a new monitor, or unplug a monitor
screens = {}
screenArr = {}
local screenwatcher = hs.screen.watcher.new(function()
screens = hs.screen.allScreens()
end)
screenwatcher:start()
-- Construct list of screens
indexDiff = 0
for index=1,#hs.screen.allScreens() do
local xIndex,yIndex = hs.screen.allScreens()[index]:position()
screenArr[xIndex] = hs.screen.allScreens()[index]
end
-- Find lowest screen index, save to indexDiff if negative
hs.fnutils.each(screenArr, function(e)
local currentIndex = hs.fnutils.indexOf(screenArr, e)
if currentIndex < 0 and currentIndex < indexDiff then
indexDiff = currentIndex
end
end)
-- Set screen grid depending on resolution
-- TODO: set grid according to pixels
for _index,screen in pairs(hs.screen.allScreens()) do
if screen:frame().w / screen:frame().h > 2 then
-- 10 * 4 for ultra wide screen
grid.setGrid('10 * 4', screen)
else
if screen:frame().w < screen:frame().h then
-- 4 * 8 for vertically aligned screen
grid.setGrid('4 * 8', screen)
else
-- 8 * 4 for normal screen
grid.setGrid('8 * 4', screen)
end
end
end
-- Some constructors, just for programming
function Cell(x, y, w, h)
return hs.geometry(x, y, w, h)
end
-- Bind new method to windowMeta
function windowMeta.new()
local self = setmetatable(windowMeta, {
-- Treate table like a function
-- Event listener when windowMeta() is called
__call = function (cls, ...)
return cls.new(...)
end,
})
self.window = window.focusedWindow()
self.screen = window.focusedWindow():screen()
self.windowGrid = grid.get(self.window)
self.screenGrid = grid.getGrid(self.screen)
return self
end
-- -----------------------------------------------------------------------
-- ** ALERT: GEEKS ONLY, GLHF :C ** --
-- ** Keybinding configurations locate at bottom ** --
-- -----------------------------------------------------------------------
module.maximizeWindow = function ()
local this = windowMeta.new()
hs.grid.maximizeWindow(this.window)
end
module.centerOnScreen = function ()
local this = windowMeta.new()
this.window:centerOnScreen(this.screen)
end
module.throwLeft = function ()
local this = windowMeta.new()
this.window:moveOneScreenWest(true, true)
end
module.throwRight = function ()
local this = windowMeta.new()
this.window:moveOneScreenEast(true, true)
end
module.leftHalf = function ()
local this = windowMeta.new()
local cell = Cell(0, 0, 0.5 * this.screenGrid.w, this.screenGrid.h)
grid.set(this.window, cell, this.screen)
end
module.rightHalf = function ()
local this = windowMeta.new()
local cell = Cell(0.5 * this.screenGrid.w, 0, 0.5 * this.screenGrid.w, this.screenGrid.h)
grid.set(this.window, cell, this.screen)
end
-- Windows-like cycle left
module.cycleLeft = function ()
local this = windowMeta.new()
-- Check if this window is on left or right
if this.windowGrid.x == 0 then
local currentIndex = hs.fnutils.indexOf(screenArr, this.screen)
local previousScreen = screenArr[(currentIndex - indexDiff - 1) % #hs.screen.allScreens() + indexDiff]
this.window:moveToScreen(previousScreen)
module.rightHalf()
else
module.leftHalf()
end
end
-- Windows-like cycle right
module.cycleRight = function ()
local this = windowMeta.new()
-- Check if this window is on left or right
if this.windowGrid.x == 0 then
module.rightHalf()
else
local currentIndex = hs.fnutils.indexOf(screenArr, this.screen)
local nextScreen = screenArr[(currentIndex - indexDiff + 1) % #hs.screen.allScreens() + indexDiff]
this.window:moveToScreen(nextScreen)
module.leftHalf()
end
end
module.topHalf = function ()
local this = windowMeta.new()
local cell = Cell(0, 0, this.screenGrid.w, 0.5 * this.screenGrid.h)
grid.set(this.window, cell, this.screen)
end
module.bottomHalf = function ()
local this = windowMeta.new()
local cell = Cell(0, 0.5 * this.screenGrid.h, this.screenGrid.w, 0.5 * this.screenGrid.h)
grid.set(this.window, cell, this.screen)
end
module.rightToLeft = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y, this.windowGrid.w - 1, this.windowGrid.h)
if this.windowGrid.w > 1 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Small Enough :)")
end
end
module.rightToRight = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y, this.windowGrid.w + 1, this.windowGrid.h)
if this.windowGrid.w < this.screenGrid.w - this.windowGrid.x then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Touching Right Edge :|")
end
end
module.bottomUp = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y, this.windowGrid.w, this.windowGrid.h - 1)
if this.windowGrid.h > 1 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Small Enough :)")
end
end
module.bottomDown = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y, this.windowGrid.w, this.windowGrid.h + 1)
if this.windowGrid.h < this.screenGrid.h - this.windowGrid.y then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Touching Bottom Edge :|")
end
end
module.leftToLeft = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x - 1, this.windowGrid.y, this.windowGrid.w + 1, this.windowGrid.h)
if this.windowGrid.x > 0 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Touching Left Edge :|")
end
end
module.leftToRight = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x + 1, this.windowGrid.y, this.windowGrid.w - 1, this.windowGrid.h)
if this.windowGrid.w > 1 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Small Enough :)")
end
end
module.topUp = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y - 1, this.windowGrid.w, this.windowGrid.h + 1)
if this.windowGrid.y > 0 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Touching Top Edge :|")
end
end
module.topDown = function ()
local this = windowMeta.new()
local cell = Cell(this.windowGrid.x, this.windowGrid.y + 1, this.windowGrid.w, this.windowGrid.h - 1)
if this.windowGrid.h > 1 then
grid.set(this.window, cell, this.screen)
else
hs.alert.show("Small Enough :)")
end
end
return module