-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMiroWindowsManager.lua
More file actions
266 lines (234 loc) · 8.43 KB
/
MiroWindowsManager.lua
File metadata and controls
266 lines (234 loc) · 8.43 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
-- Copyright (c) 2018 Miro Mannino
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this
-- software and associated documentation files (the "Software"), to deal in the Software
-- without restriction, including without limitation the rights to use, copy, modify, merge,
-- publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
-- to whom the Software is furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--- === MiroWindowsManager ===
---
--- With this script you will be able to move the window in halves and in corners using your keyboard and mainly using arrows. You would also be able to resize them by thirds, quarters, or halves.
---
--- Official homepage for more info and documentation: [https://github.com/miromannino/miro-windows-manager](https://github.com/miromannino/miro-windows-manager)
---
--- Download: [https://github.com/miromannino/miro-windows-manager/raw/master/MiroWindowsManager.spoon.zip](https://github.com/miromannino/miro-windows-manager/raw/master/MiroWindowsManager.spoon.zip)
---
local obj={}
obj.__index = obj
-- Metadata
obj.name = "MiroWindowsManager"
obj.version = "1.1"
obj.author = "Miro Mannino <miro.mannino@gmail.com>"
obj.homepage = "https://github.com/miromannino/miro-windows-management"
obj.license = "MIT - https://opensource.org/licenses/MIT"
--- MiroWindowsManager.sizes
--- Variable
--- The sizes that the window can have.
--- The sizes are expressed as dividend of the entire screen's size.
--- For example `{2, 3, 3/2}` means that it can be 1/2, 1/3 and 2/3 of the total screen's size
obj.sizes = {2, 3, 3/2}
--- MiroWindowsManager.fullScreenSizes
--- Variable
--- The sizes that the window can have in full-screen.
--- The sizes are expressed as dividend of the entire screen's size.
--- For example `{1, 4/3, 2}` means that it can be 1/1 (hence full screen), 3/4 and 1/2 of the total screen's size
obj.fullScreenSizes = {1, 4/3, 2}
--- MiroWindowsManager.GRID
--- Variable
--- The screen's size using `hs.grid.setGrid()`
--- This parameter is used at the spoon's `:init()`
obj.GRID = {w = 24, h = 24}
obj._pressed = {
up = false,
down = false,
left = false,
right = false
}
function obj:_exitFullScreen(win)
local win = win or hs.window.focusedWindow()
if win:isFullScreen() then
win:setFullScreen(false)
end
end
function obj:_nextStep(dim, offs, cb)
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local axis = dim == 'w' and 'x' or 'y'
local oppDim = dim == 'w' and 'h' or 'w'
local oppAxis = dim == 'w' and 'y' or 'x'
local id = win:id()
local screen = win:screen()
cell = hs.grid.get(win, screen)
local nextSize = self.sizes[1]
for i=1,#self.sizes do
if cell[dim] == self.GRID[dim] / self.sizes[i] and
(cell[axis] + (offs and cell[dim] or 0)) == (offs and self.GRID[dim] or 0)
then
nextSize = self.sizes[(i % #self.sizes) + 1]
break
end
end
cb(cell, nextSize)
if cell[oppAxis] ~= 0 and cell[oppAxis] + cell[oppDim] ~= self.GRID[oppDim] then
cell[oppDim] = self.GRID[oppDim]
cell[oppAxis] = 0
end
hs.grid.set(win, cell, screen)
end
end
function obj:_nextFullScreenStep()
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local id = win:id()
local screen = win:screen()
cell = hs.grid.get(win, screen)
local nextSize = self.fullScreenSizes[1]
for i=1,#self.fullScreenSizes do
if cell.w == self.GRID.w / self.fullScreenSizes[i] and
cell.h == self.GRID.h / self.fullScreenSizes[i] and
cell.x == (self.GRID.w - self.GRID.w / self.fullScreenSizes[i]) / 2 and
cell.y == (self.GRID.h - self.GRID.h / self.fullScreenSizes[i]) / 2 then
nextSize = self.fullScreenSizes[(i % #self.fullScreenSizes) + 1]
break
end
end
cell.w = self.GRID.w / nextSize
cell.h = self.GRID.h / nextSize
cell.x = (self.GRID.w - self.GRID.w / nextSize) / 2
cell.y = (self.GRID.h - self.GRID.h / nextSize) / 2
hs.grid.set(win, cell, screen)
end
end
function obj:_moveNextScreenStep()
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
local id = win:id()
local screen = win:screen()
if win:isFullScreen() then
win:setFullScreen(false)
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 1)
else
win:move(win:frame():toUnitRect(screen:frame()), screen:next(), true, 0)
end
end
end
function obj:_fullDimension(dim)
if hs.window.focusedWindow() then
local win = hs.window.frontmostWindow()
self:_exitFullScreen(win)
local id = win:id()
local screen = win:screen()
cell = hs.grid.get(win, screen)
if (dim == 'x') then
cell = '0,0 ' .. self.GRID.w .. 'x' .. self.GRID.h
else
cell[dim] = self.GRID[dim]
cell[dim == 'w' and 'x' or 'y'] = 0
end
hs.grid.set(win, cell, screen)
end
end
--- MiroWindowsManager:bindHotkeys()
--- Method
--- Binds hotkeys for Miro's Windows Manager
--- Parameters:
--- * mapping - A table containing hotkey details for the following items:
--- * up: for the up action (usually {hyper, "up"})
--- * right: for the right action (usually {hyper, "right"})
--- * down: for the down action (usually {hyper, "down"})
--- * left: for the left action (usually {hyper, "left"})
--- * fullscreen: for the full-screen action (e.g. {hyper, "f"})
--- * nextscreen: for the multi monitor next screen action (e.g. {hyper, "n"})
---
--- A configuration example can be:
--- ```
--- local hyper = {"ctrl", "alt", "cmd"}
--- spoon.MiroWindowsManager:bindHotkeys({
--- up = {hyper, "up"},
--- right = {hyper, "right"},
--- down = {hyper, "down"},
--- left = {hyper, "left"},
--- fullscreen = {hyper, "f"}
--- nextscreen = {hyper, "n"}
--- })
--- ```
function obj:bindHotkeys(mapping)
hs.inspect(mapping)
print("Bind Hotkeys for Miro's Windows Manager")
hs.hotkey.bind(mapping.down[1], mapping.down[2], function ()
self._pressed.down = true
if self._pressed.up then
self:_fullDimension('h')
else
self:_nextStep('h', true, function (cell, nextSize)
cell.y = self.GRID.h - self.GRID.h / nextSize
cell.h = self.GRID.h / nextSize
end)
end
end, function ()
self._pressed.down = false
end)
hs.hotkey.bind(mapping.right[1], mapping.right[2], function ()
self._pressed.right = true
if self._pressed.left then
self:_fullDimension('w')
else
self:_nextStep('w', true, function (cell, nextSize)
cell.x = self.GRID.w - self.GRID.w / nextSize
cell.w = self.GRID.w / nextSize
end)
end
end, function ()
self._pressed.right = false
end)
hs.hotkey.bind(mapping.left[1], mapping.left[2], function ()
self._pressed.left = true
if self._pressed.right then
self:_fullDimension('w')
else
self:_nextStep('w', false, function (cell, nextSize)
cell.x = 0
cell.w = self.GRID.w / nextSize
end)
end
end, function ()
self._pressed.left = false
end)
hs.hotkey.bind(mapping.up[1], mapping.up[2], function ()
self._pressed.up = true
if self._pressed.down then
self:_fullDimension('h')
else
self:_nextStep('h', false, function (cell, nextSize)
cell.y = 0
cell.h = self.GRID.h / nextSize
end)
end
end, function ()
self._pressed.up = false
end)
hs.hotkey.bind(mapping.fullscreen[1], mapping.fullscreen[2], function ()
self:_nextFullScreenStep()
end)
hs.hotkey.bind(mapping.nextscreen[1], mapping.nextscreen[2], function ()
self:_moveNextScreenStep()
end)
end
function obj:init()
print("Initializing Miro's Windows Manager")
hs.grid.setGrid(obj.GRID.w .. 'x' .. obj.GRID.h)
hs.grid.MARGINX = 0
hs.grid.MARGINY = 0
end
return obj