-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
308 lines (285 loc) · 10.6 KB
/
init.lua
File metadata and controls
308 lines (285 loc) · 10.6 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
--- === WinWin ===
---
--- Windows manipulation
---
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/WinWin.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/WinWin.spoon.zip)
local obj={}
obj.__index = obj
-- Metadata
obj.name = "WinWin"
obj.version = "1.0"
obj.author = "ashfinal <ashfinal@gmail.com>"
obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT"
-- Windows manipulation history. Only the last operation is stored.
obj.history = {}
--- WinWin.gridparts
--- Variable
--- An integer specifying how many gridparts the screen should be divided into. Defaults to 30.
obj.gridparts = 30
obj.paths = {}
obj.paths.base = os.getenv('HOME')
obj.paths.hs = getPathAbsolute(obj.paths.base, '.hammerspoon')
-- Internal function used to find our location, so we know where to load files from
local function script_path()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
obj.spoonPath = script_path()
-- combine two path
local function getPathAbsolute(path1, path2)
return hs.fs.pathToAbsolute(string.format("%s/%s",path1,path2))
end
--get user home dir file
local function getUserFilePathhAbsolute(path)
return hs.fs.pathToAbsolute(string.format("%s/%s",os.getenv('HOME'),path))
end
--read user customer config
customer =dofile(getPathAbsolute(obj.paths.hs ,'private/winwin.lua'))
--if user config does not esist
if not customer then
customer = dofile(obj.spoonPath.."/winwin.lua")
end
if customer.bindKey then
customer.bindKey()
end
--- WinWin:stepMove(direction)
--- Method
--- Move the focused window in the `direction` by on step. The step scale equals to the width/height of one gridpart.
---
--- Parameters:
--- * direction - A string specifying the direction, valid strings are: `left`, `right`, `up`, `down`.
function obj:stepMove(direction)
local cwin = hs.window.focusedWindow()
if cwin then
local cscreen = cwin:screen()
local cres = cscreen:fullFrame()
local stepw = cres.w/obj.gridparts
local steph = cres.h/obj.gridparts
local wtopleft = cwin:topLeft()
if direction == "left" then
cwin:setTopLeft({x=wtopleft.x-stepw, y=wtopleft.y})
elseif direction == "right" then
cwin:setTopLeft({x=wtopleft.x+stepw, y=wtopleft.y})
elseif direction == "up" then
cwin:setTopLeft({x=wtopleft.x, y=wtopleft.y-steph})
elseif direction == "down" then
cwin:setTopLeft({x=wtopleft.x, y=wtopleft.y+steph})
end
else
hs.alert.show("No focused window!")
end
end
--- WinWin:stepResize(direction)
--- Method
--- Resize the focused window in the `direction` by on step.
---
--- Parameters:
--- * direction - A string specifying the direction, valid strings are: `left`, `right`, `up`, `down`.
function obj:stepResize(direction)
local cwin = hs.window.focusedWindow()
if cwin then
local cscreen = cwin:screen()
local cres = cscreen:fullFrame()
local stepw = cres.w/obj.gridparts
local steph = cres.h/obj.gridparts
local wsize = cwin:size()
if direction == "left" then
cwin:setSize({w=wsize.w-stepw, h=wsize.h})
elseif direction == "right" then
cwin:setSize({w=wsize.w+stepw, h=wsize.h})
elseif direction == "up" then
cwin:setSize({w=wsize.w, h=wsize.h-steph})
elseif direction == "down" then
cwin:setSize({w=wsize.w, h=wsize.h+steph})
end
else
hs.alert.show("No focused window!")
end
end
--- WinWin:stash()
--- Method
--- Stash current windows's position and size.
---
local function isInHistory(windowid)
for idx,val in ipairs(obj.history) do
if val[1] == windowid then
return idx
end
end
return false
end
function obj:stash()
local cwin = hs.window.focusedWindow()
local winid = cwin:id()
local winf = cwin:frame()
local id_idx = isInHistory(winid)
if id_idx then
-- Bring recently used window id up, so they wouldn't get removed because of exceeding capacity
if id_idx == 100 then
local tmptable = obj.history[id_idx]
table.remove(obj.history, id_idx)
table.insert(obj.history, 1, tmptable)
-- Make sure the history for each application doesn't reach the maximum (100 items)
local id_history = obj.history[1][2]
if #id_history > 100 then table.remove(id_history) end
table.insert(id_history, 1, winf)
else
local id_history = obj.history[id_idx][2]
if #id_history > 100 then table.remove(id_history) end
table.insert(id_history, 1, winf)
end
else
-- Make sure the history of window id doesn't reach the maximum (100 items).
if #obj.history > 100 then table.remove(obj.history) end
-- Stash new window id and its first history
local newtable = {winid, {winf}}
table.insert(obj.history, 1, newtable)
end
end
--- WinWin:moveAndResize(option)
--- Method
--- Move and resize the focused window.
---
--- Parameters:
--- * option - A string specifying the option, valid strings are: `halfleft`, `halfright`, `halfup`, `halfdown`, `cornerNW`, `cornerSW`, `cornerNE`, `cornerSE`, `center`, `fullscreen`, `expand`, `shrink`.
function obj:moveAndResize(option)
local cwin = hs.window.focusedWindow()
if cwin then
local cscreen = cwin:screen()
local cres = cscreen:fullFrame()
local stepw = cres.w/obj.gridparts
local steph = cres.h/obj.gridparts
local wf = cwin:frame()
if option == "halfleft" then
cwin:setFrame({x=cres.x, y=cres.y, w=cres.w/2, h=cres.h})
elseif option == "halfright" then
cwin:setFrame({x=cres.x+cres.w/2, y=cres.y, w=cres.w/2, h=cres.h})
elseif option == "halfup" then
cwin:setFrame({x=cres.x, y=cres.y, w=cres.w, h=cres.h/2})
elseif option == "halfdown" then
cwin:setFrame({x=cres.x, y=cres.y+cres.h/2, w=cres.w, h=cres.h/2})
elseif option == "cornerNW" then
cwin:setFrame({x=cres.x, y=cres.y, w=cres.w/2, h=cres.h/2})
elseif option == "cornerNE" then
cwin:setFrame({x=cres.x+cres.w/2, y=cres.y, w=cres.w/2, h=cres.h/2})
elseif option == "cornerSW" then
cwin:setFrame({x=cres.x, y=cres.y+cres.h/2, w=cres.w/2, h=cres.h/2})
elseif option == "cornerSE" then
cwin:setFrame({x=cres.x+cres.w/2, y=cres.y+cres.h/2, w=cres.w/2, h=cres.h/2})
elseif option == "fullscreen" then
cwin:setFrame({x=cres.x, y=cres.y, w=cres.w, h=cres.h})
elseif option == "center" then
cwin:centerOnScreen()
elseif option == "expand" then
cwin:setFrame({x=wf.x-stepw, y=wf.y-steph, w=wf.w+(stepw*2), h=wf.h+(steph*2)})
elseif option == "shrink" then
cwin:setFrame({x=wf.x+stepw, y=wf.y+steph, w=wf.w-(stepw*2), h=wf.h-(steph*2)})
end
else
hs.alert.show("No focused window!")
end
end
--- WinWin:moveToScreen(direction)
--- Method
--- Move the focused window between all of the screens in the `direction`.
---
--- Parameters:
--- * direction - A string specifying the direction, valid strings are: `left`, `right`, `up`, `down`, `next`.
function obj:moveToScreen(direction)
local cwin = hs.window.focusedWindow()
if cwin then
local cscreen = cwin:screen()
if direction == "up" then
cwin:moveOneScreenNorth()
elseif direction == "down" then
cwin:moveOneScreenSouth()
elseif direction == "left" then
cwin:moveOneScreenWest()
elseif direction == "right" then
cwin:moveOneScreenEast()
elseif direction == "next" then
cwin:moveToScreen(cscreen:next())
end
else
hs.alert.show("No focused window!")
end
end
--- WinWin:undo()
--- Method
--- Undo the last window manipulation. Only those "moveAndResize" manipulations can be undone.
---
function obj:undo()
local cwin = hs.window.focusedWindow()
local winid = cwin:id()
-- Has this window been stored previously?
local id_idx = isInHistory(winid)
if id_idx then
-- Bring recently used window id up, so they wouldn't get removed because of exceeding capacity
if id_idx == 100 then
local tmptable = obj.history[id_idx]
table.remove(obj.history, id_idx)
table.insert(obj.history, 1, tmptable)
local id_history = obj.history[1][2]
cwin:setFrame(id_history[1])
-- Rewind the history
local tmpframe = id_history[1]
table.remove(id_history, 1)
table.insert(id_history, tmpframe)
else
local id_history = obj.history[id_idx][2]
cwin:setFrame(id_history[1])
local tmpframe = id_history[1]
table.remove(id_history, 1)
table.insert(id_history, tmpframe)
end
end
end
--- WinWin:redo()
--- Method
--- Redo the window manipulation. Only those "moveAndResize" manipulations can be undone.
---
function obj:redo()
local cwin = hs.window.focusedWindow()
local winid = cwin:id()
-- Has this window been stored previously?
local id_idx = isInHistory(winid)
if id_idx then
-- Bring recently used window id up, so they wouldn't get removed because of exceeding capacity
if id_idx == 100 then
local tmptable = obj.history[id_idx]
table.remove(obj.history, id_idx)
table.insert(obj.history, 1, tmptable)
local id_history = obj.history[1][2]
cwin:setFrame(id_history[#id_history])
-- Play the history
local tmpframe = id_history[#id_history]
table.remove(id_history)
table.insert(id_history, 1, tmpframe)
else
local id_history = obj.history[id_idx][2]
cwin:setFrame(id_history[#id_history])
local tmpframe = id_history[#id_history]
table.remove(id_history)
table.insert(id_history, 1, tmpframe)
end
end
end
--- WinWin:centerCursor()
--- Method
--- Center the cursor on the focused window.
---
function obj:centerCursor()
local cwin = hs.window.focusedWindow()
local wf = cwin:frame()
local cscreen = cwin:screen()
local cres = cscreen:fullFrame()
if cwin then
-- Center the cursor one the focused window
hs.mouse.setAbsolutePosition({x=wf.x+wf.w/2, y=wf.y+wf.h/2})
else
-- Center the cursor on the screen
hs.mouse.setAbsolutePosition({x=cres.x+cres.w/2, y=cres.y+cres.h/2})
end
end
return obj