-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
140 lines (108 loc) · 3.3 KB
/
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
--------------------------------------------------------------------------------
-- GridLayout.spoon
--------------------------------------------------------------------------------
local M = {
name = 'GridLayout',
version = '0.2.1',
author = 'Jesse Leite <[email protected]>',
license = 'MIT <https://opensource.org/licenses/MIT>',
homepage = 'https://github.com/jesseleite/GridLayout.spoon',
}
local state
local events
local helpers
-- Start GridLayout.spoon.
function M:start()
state = dofile(hs.spoons.resourcePath('state.lua'))
events = dofile(hs.spoons.resourcePath('events.lua'))
helpers = dofile(hs.spoons.resourcePath('helpers.lua'))
return M
end
-- Stop GridLayout.spoon.
function M:stop()
events:unsubscribeAll()
state:resetAll()
end
-- Set preset layouts to be managed by this spoon.
-- See README.md for table conventions.
function M:setLayouts(v)
state.layouts = v
return M
end
-- Set apps to be managed by this spoon.
-- See README.md for table conventions.
function M:setApps(v)
state.apps = v
return M
end
-- Alias of hs.grid.setGrid(), in case the user isn't using hs.grid
-- separately, because grid config is required for this spoon.
function M:setGrid(v)
hs.grid.setGrid(v)
return M
end
-- This spoon needs to manage margins because neither hs.layout.apply(),
-- nor hs.grid.getCell(), properly respects margins when set through
-- hs.grid.setMargins(). That said, we still set margins on the
-- hs.grid object, in case user is using hs.grid separately.
function M:setMargins(v)
helpers.grid.setMargins(v)
hs.grid.setMargins(v)
return M
end
-- Open layout selector and apply layout.
function M:selectLayout(layout_key, variant_key)
if layout_key then
return helpers.applyLayout(layout_key, variant_key, state)
end
local choices = {}
for key,layout in pairs(state.layouts) do
table.insert(choices, {
['text'] = layout.name,
['subText'] = layout.description,
['key'] = key,
})
end
local chooser = hs.chooser.new(function(choice)
helpers.applyLayout(choice.key, nil, state)
end)
chooser:searchSubText(true):choices(choices):query(''):show()
end
-- Select and apply next layout variant.
function M:selectNextVariant()
state:selectNextVariant()
helpers.applyLayout(nil, nil, state)
end
-- Bind current app window to a specific cell.
function M:bindToCell()
local layout = state.layouts[state.current_layout_key]
local cells = helpers.listAppsInCells(layout, state)
local choices = {}
for _,cell in pairs(cells) do
table.insert(choices, {
['text'] = 'Cell '..cell.key,
['subText'] = table.concat(cell.apps, ', '),
['cell'] = cell.key,
})
end
local chooser = hs.chooser.new(function(choice)
local app_id = hs.window.focusedWindow():application():bundleID()
local window = hs.window.focusedWindow()
hs.layout.apply({helpers.normalizeElementForApply(app_id, window, choice.cell, layout, state)})
state.addLayoutCustomization(app_id, window, choice.cell)
end)
chooser:searchSubText(true):choices(choices):query(''):show()
end
-- Reset layout customizations
function M:resetLayout()
state:resetLayout()
helpers.applyLayout(nil, nil, state)
return M
end
-- Reset all state
function M:resetAll()
state:resetAll()
helpers.applyLayout(nil, nil, state)
return M
end
return M