-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
261 lines (242 loc) · 8.02 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
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
--- === KSheet ===
---
--- Keybindings cheatsheet for current application
---
--- Download: [https://github.com/Hammerspoon/Spoons/raw/master/Spoons/KSheet.spoon.zip](https://github.com/Hammerspoon/Spoons/raw/master/Spoons/KSheet.spoon.zip)
local obj={}
obj.__index = obj
-- Metadata
obj.name = "KSheet"
obj.version = "1.0"
obj.homepage = "https://github.com/Hammerspoon/Spoons"
obj.license = "MIT - https://opensource.org/licenses/MIT"
-- Workaround for "Dictation" menuitem
hs.application.menuGlyphs[148]="fn fn"
obj.commandEnum = {
alt = '⌥',
ctrl = '⌃',
cmd = '⌘',
shift = '⇧',
}
obj.commandEnumOrder = {
'ctrl',
'alt',
'cmd',
'shift',
}
--- KSheet:init()
--- Method
--- Initialize the spoon
function obj:init()
self.sheetView = hs.webview.new({x=0, y=0, w=0, h=0})
self.sheetView:windowTitle("CheatSheets")
self.sheetView:windowStyle("utility")
self.sheetView:allowGestures(true)
self.sheetView:allowNewWindows(false)
self.sheetView:level(hs.drawing.windowLevels.tornOffMenu)
local cscreen = hs.screen.mainScreen()
local cres = cscreen:fullFrame()
self.sheetView:frame({
x = cres.x+cres.w*0.15/2,
y = cres.y+cres.h*0.25/2,
w = cres.w*0.85,
h = cres.h*0.75
})
end
local function processMenuItems(menustru)
local menu = ""
for pos,val in pairs(menustru) do
if type(val) == "table" then
-- TODO: Remove menubar items with no shortcuts in them
if val.AXRole == "AXMenuBarItem" and type(val.AXChildren) == "table" then
menu = menu .. "<ul class='col col" .. pos .. "'>"
menu = menu .. "<li class='title'><strong>" .. val.AXTitle .. "</strong></li>"
menu = menu .. processMenuItems(val.AXChildren[1])
menu = menu .. "</ul>"
elseif val.AXRole == "AXMenuItem" and not val.AXChildren then
if not (val.AXMenuItemCmdChar == '' and val.AXMenuItemCmdGlyph == '') then
local CmdModifiers = ''
local template = {}
for key, value in pairs(val.AXMenuItemCmdModifiers) do
--CmdModifiers = CmdModifiers .. obj.commandEnum[value]
template[value] = obj.commandEnum[value]
end
local newCmdModifiers = ''
for key, value in pairs(obj.commandEnumOrder) do
newCmdModifiers = newCmdModifiers..(template[value] or ' ')
end
--print("old:'"..CmdModifiers.."'")
--print("new:'"..newCmdModifiers.."'")
local CmdChar = val.AXMenuItemCmdChar
local CmdGlyph = hs.application.menuGlyphs[val.AXMenuItemCmdGlyph] or ''
local CmdKeys = CmdChar .. CmdGlyph
menu = menu .. "<li><div class='cmdModifiers'>" .. newCmdModifiers .. " " .. CmdKeys .. "</div><div class='cmdtext'>" .. " " .. val.AXTitle .. "</div></li>"
end
elseif val.AXRole == "AXMenuItem" and type(val.AXChildren) == "table" then
menu = menu .. processMenuItems(val.AXChildren[1])
end
end
end
return menu
end
local function generateHtml(application)
local app_title = application:title()
local menuitems_tree = application:getMenuItems()
local allmenuitems = processMenuItems(menuitems_tree)
local fontSize = '16'
local html = [[
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
*{margin:0; padding:0;}
html, body{
background-color:#eee;
font-family: "Verdana", arial;
font-size: ]]..fontSize..[[px;
}
a{
text-decoration:none;
color:#000;
font-size:]]..fontSize..[[px;
}
li.title{ text-align:center;}
ul, li{list-style: inside none; padding: 0 0 5px;}
footer{
position: fixed;
left: 0;
right: 0;
height: 48px;
background-color:#eee;
}
header{
position: fixed;
top: 0;
left: 0;
right: 0;
height:48px;
background-color:#eee;
z-index:99;
}
footer{ bottom: 0; }
header hr,
footer hr {
border: 0;
height: 0;
border-top: 1px solid rgba(0, 0, 0, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}
.title{
padding: 15px;
}
li.title{padding: 0 10px 15px}
.content{
padding: 0 0 15px;
font-size:]]..fontSize..[[px;
overflow:hidden;
}
.content.maincontent{
position: relative;
height: 577px;
margin-top: 46px;
}
.content > .col{
width: 31%;
padding:20px 0 20px 20px;
}
li:after{
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.cmdModifiers{
width: 80px;
padding-right: 15px;
text-align: left;
float: left;
font-family: monospace;
}
.cmdtext{
float: left;
overflow: hidden;
width: 240px;
font-family: "Verdana";
}
</style>
</head>
<body>
<header>
<div class="title"><strong>]] .. app_title .. [[</strong></div>
<hr />
</header>
<div class="content maincontent">]] .. allmenuitems .. [[</div>
<br>
<footer>
<hr />
<div class="content" >
<div class="col">
by <a href="https://github.com/dharmapoudel" target="_parent">dharma poudel</a>
</div>
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/2.2.2/isotope.pkgd.min.js"></script>
<script type="text/javascript">
var elem = document.querySelector('.content');
var iso = new Isotope( elem, {
// options
itemSelector: '.col',
layoutMode: 'masonry'
});
</script>
</body>
</html>
]]
return html
end
--- KSheet:show()
--- Method
--- Show current application's keybindings in a view.
function obj:show()
local capp = hs.application.frontmostApplication()
local webcontent = generateHtml(capp)
self.sheetView:html(webcontent)
self.sheetView:show()
end
--- KSheet:hide()
--- Method
--- Hide the cheatsheet view.
function obj:hide()
self.sheetView:hide()
end
--- KSheet:toggle()
--- Method
--- Alternatively show/hide the cheatsheet view.
function obj:toggle()
if self.sheetView and self.sheetView:hswindow() and self.sheetView:hswindow():isVisible() then
self:hide()
else
self:show()
end
end
--- KSheet:bindHotkeys(mapping)
--- Method
--- Binds hotkeys for KSheet
---
--- Parameters:
--- * mapping - A table containing hotkey modifier/key details for the following items:
--- * show - Show the keybinding view
--- * hide - Hide the keybinding view
--- * toggle - Show if hidden, hide if shown
function obj:bindHotkeys(mapping)
local actions = {
toggle = hs.fnutils.partial(self.toggle, self),
show = hs.fnutils.partial(self.show, self),
hide = hs.fnutils.partial(self.hide, self)
}
hs.spoons.bindHotkeysToSpec(actions, mapping)
end
return obj