-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow_manager.lua
215 lines (190 loc) · 7.46 KB
/
window_manager.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
-- -----------------------------------------------------------------------
-- ** HammerSpoon Config File by S1ngS1ng with ❤️ ** --
-- -----------------------------------------------------------------------
-- *** Please refer to README.MD for instructions. Cheers! *** --
-- -----------------------------------------------------------------------
-- ** Something Global ** --
-- -----------------------------------------------------------------------
-- Uncomment this following line if you don't wish to see animations
-- hs.window.animationDuration = 0
grid = require "hs.grid"
grid.setMargins('0, 0')
-- Set screen watcher, in case you connect a new monitor, or unplug a monitor
screens = {}
local screenwatcher = hs.screen.watcher.new(function()
screens = hs.screen.allScreens()
end)
screenwatcher:start()
-- 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
-- Please leave a comment if you have any suggestions
-- I know this looks weird, but it works :C
current = {}
function init()
current.win = hs.window.focusedWindow()
current.scr = hs.window.focusedWindow():screen()
end
function current:new()
init()
o = {}
setmetatable(o, self)
o.window, o.screen = self.win, self.scr
o.screenGrid = grid.getGrid(self.scr)
o.windowGrid = grid.get(self.win)
return o
end
-- -----------------------------------------------------------------------
-- ** ALERT: GEEKS ONLY, GLHF :C ** --
-- ** Keybinding configurations locate at bottom ** --
-- -----------------------------------------------------------------------
local function maximizeWindow()
local this = current:new()
hs.grid.maximizeWindow(this.window)
end
local function centerOnScreen()
local this = current:new()
this.window:centerOnScreen(this.screen)
end
local function throwLeft()
local this = current:new()
this.window:moveOneScreenWest(true)
end
local function throwRight()
local this = current:new()
this.window:moveOneScreenEast(true)
end
local function leftHalf()
local this = current:new()
local cell = Cell(0, 0, 0.5 * this.screenGrid.w, this.screenGrid.h)
grid.set(this.window, cell, this.screen)
this.window.setShadows(true)
end
local function rightHalf()
local this = current: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
local function topHalf()
local this = current:new()
local cell = Cell(0, 0, this.screenGrid.w, 0.5 * this.screenGrid.h)
grid.set(this.window, cell, this.screen)
end
local function bottomHalf()
local this = current: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
local function rightToLeft()
local this = current: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
local function rightToRight()
local this = current: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
local function bottomUp()
local this = current: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
local function bottomDown()
local this = current: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
local function leftToLeft()
local this = current: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
local function leftToRight()
local this = current: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
local function topUp()
local this = current: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
local function topDown()
local this = current: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
-- -----------------------------------------------------------------------
-- ** Key Binding ** --
-- -----------------------------------------------------------------------
key = require "hs.hotkey"
------------------------ * Move window to screen * -----------------------
hs.hotkey.bind({"ctrl", "alt"}, "left", throwLeft)
hs.hotkey.bind({"ctrl", "alt"}, "right", throwRight)
-------------------- * Set Window Position on screen* --------------------
hs.hotkey.bind({"ctrl", "alt", "cmd" }, "m", maximizeWindow) -- ⌃⌥⌘ + M
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "c", centerOnScreen) -- ⌃⌥⌘ + C
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "left", leftHalf) -- ⌃⌥⌘ + ←
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "right", rightHalf) -- ⌃⌥⌘ + →
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "up", topHalf) -- ⌃⌥⌘ + ↑
hs.hotkey.bind({"ctrl", "alt", "cmd"}, "down", bottomHalf) -- ⌃⌥⌘ + ↓
-------------------- * Set Window Position on screen* --------------------
hs.hotkey.bind({"ctrl", "alt", "shift"}, "left", rightToLeft) -- ⌃⌥⇧ + ←
hs.hotkey.bind({"ctrl", "alt", "shift"}, "right", rightToRight) -- ⌃⌥⇧ + →
hs.hotkey.bind({"ctrl", "alt", "shift"}, "up", bottomUp) -- ⌃⌥⇧ + ↑
hs.hotkey.bind({"ctrl", "alt", "shift"}, "down", bottomDown) -- ⌃⌥⇧ + ↓
-------------------- * Set Window Position on screen* --------------------
hs.hotkey.bind({"alt", "cmd", "shift"}, "left", leftToLeft) -- ⌥⌘⇧ + ←
hs.hotkey.bind({"alt", "cmd", "shift"}, "right", leftToRight) -- ⌥⌘⇧ + →
hs.hotkey.bind({"alt", "cmd", "shift"}, "up", topUp) -- ⌥⌘⇧ + ↑
hs.hotkey.bind({"alt", "cmd", "shift"}, "down", topDown) -- ⌥⌘⇧ + ↓