-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
481 lines (438 loc) · 15.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
-- Preload the modules we're going to need
require("hs.chooser")
require("hs.webview")
require("hs.drawing")
require("hs.mjomatic")
-- Storage for persistent screen objects
local presentationControl = nil
local presentationScreen = nil
local slideBackground = nil
local slideHeader = nil
local slideBody = nil
local slideFooter = nil
local slideModal = nil
-- Configuration for persistent screen objects
local slideHeaderFont = nil
local slideHeaderSize = nil
local slideBodyFont = nil
local slideBodySize = nil
local slideFooterFont = nil
local slideFooterSize = nil
-- Metadata for slide progression
local startSlide = 1
local currentSlide = 0
-- Storage for transient screen objects
local refs = {}
-- List of safe default apps
local defaultapps = {"Calculator", "Chess", "Notes", "Dictionary"}
local mjomatic1={
" CCCChhhh",
" CCCChhhh",
"NNNNNNDDDDD",
"",
"C Calculator",
"h Chess",
"N Notes",
"D Dictionary"
}
local mjomatic2={
" hhhNNNNN",
" hhhNNNNN",
"CCCCCDDDDDD",
"",
"C Calculator",
"h Chess",
"N Notes",
"D Dictionary"
}
function launchdefaultapps()
for k,v in pairs(defaultapps) do
hs.application.launchOrFocus(v)
end
end
function killdefaultapps()
for k,v in pairs(defaultapps) do
local app = hs.application.get(v)
if app then
app:kill()
end
end
end
function makewebview(name, place, url, html)
if refs[name] then
return refs[name]
else
print("Creating webview "..name)
local frame = presentationScreen:fullFrame()
local webViewRect
if place == "right" then
local x = frame["x"] + ((frame["w"] - 100)*0.66) + 10
local y = slideHeader:frame()["y"] + slideHeader:frame()["h"] + 10
local w = ((frame["w"] - 100)*0.33) - 10
local h = slideBody:frame()["h"]
webViewRect = hs.geometry.rect(x, y, w, h)
elseif place == "body" then
webViewRect = hs.geometry.rect(frame["x"] + 50,
slideHeader:frame()["y"] + slideHeader:frame()["h"] + 10,
(frame["w"] - 100),
(frame["h"] / 10) * 8 - (frame["h"] / 12))
end
local webview = hs.webview.new(webViewRect)
webview:setLevel(hs.drawing.windowLevels["normal"]+1)
if url then
webview:url(url)
elseif html then
webview:html(html)
else
webview:html("NO CONTENT!")
end
refs[name] = webview
return webview
end
end
-- Definitions of the slides
local slides = {
{
["header"] = "Hammerspoon",
["body"] = [[Staggeringly powerful desktop automation]],
["enterFn"] = function()
killdefaultapps()
local webview = makewebview("titleSlideWebview", "right", "http://www.hammerspoon.org/go/", nil)
webview:show(0.3)
print("Entered title slide")
end,
["exitFn"] = function()
print("Hiding webview")
local webview = refs["titleSlideWebview"]
webview:hide(0.2)
print("Exited title slide")
end,
},
{
["header"] = "Who am I?",
["body"] = [[Peter van Dijk, PowerDNS (lots of Lua in all our products!), contributor to Hammerspoon predecessors, mostly passively involved in Hammerspoon development.]]
},
{
["header"] = "What is it?",
["body"] = [[Hammerspoon exposes many OS X system APIs to a Lua environment, so you can script your environment.]]
},
{
["header"] = "History",
["body"] = [[Hammerspoon is a fork of Mjolnir by Steven Degutis. Mjolnir aims to be a very minimal application, with its extensions hosted externally and managed using a Lua package manager. We wanted to provide a more integrated experience.]]
},
{
["header"] = "A comparison",
["enterFn"] = function()
local webview = makewebview("comparisonSlideWebview", "body", "https://github.com/sdegutis/mjolnir#mjolnir-vs-other-apps", nil)
webview:show(0.3)
end,
["exitFn"] = function()
local webview = refs["comparisonSlideWebview"]
webview:hide(0.2)
end,
},
{
["header"] = "So what is it for",
["body"] = [[• Window management
• Reacting to all kinds of events
• WiFi, USB, path/file changes
• Interacting with applications (menus)
• Drawing custom interfaces on the screen
• URL handling/mangling]]
},
{
["header"] = "Window management (1)",
["body"] = "Just launching some apps",
["enterFn"] = function()
launchdefaultapps()
end
},
{
["header"] = "Window management (2)",
["body"] = "Mjomatic config:\n"..table.concat(mjomatic1, "\n"),
["enterFn"] = function ()
for k,v in pairs(defaultapps) do
local app = hs.application.get(v)
if app then
local window = app:mainWindow()
if window then
window:moveToScreen(presentationScreen)
end
end
end
-- local webview = makewebview("mjomaticSlideWebview", "body", nil, "<pre>Mjomatic config:\n"..table.concat(mjomatic1, "\n").."</pre>")
-- webview:show(0.3)
end,
-- ["exitFn"] = function()
-- local webview = refs["mjomaticSlideWebview"]
-- webview:hide(0.2)
-- end
},
{
["header"] = "Window management (3)",
["body"] = "Mjomatic config:\n"..table.concat(mjomatic2, "\n"),
["exitFn"] = function()
killdefaultapps()
end
},
{
["header"] = "Responding to WiFi events",
["enterFn"] = function()
local webview = makewebview("wifiwatcherSlideWebview", "body", nil, [[<pre>
wifiwatcher = hs.wifi.watcher.new(function()
print"wifiwatcher fired"
local network = hs.wifi.currentNetwork()
if network then
hs.alert("joined wifi network "..network)
else
hs.alert("wifi disconnected")
end
if network == "Fibonacci" then
hs.application.launchOrFocus("Twitter")
else
local app = hs.application.get("Twitter")
if app then
app:kill9()
end
end
end)
wifiwatcher:start()
</pre>]])
webview:show(0.3)
end,
["exitFn"] = function()
local webview = refs["wifiwatcherSlideWebview"]
webview:hide(0.2)
end
},
{
["header"] = "Handling URL events",
["enterFn"] = function()
local webview = makewebview("URLSlideWebview", "body", nil, '<img src="https://cloud.githubusercontent.com/assets/353427/9669248/c37c6f26-527d-11e5-9299-41b3cdcb4a04.png">')
webview:show(0.3)
end,
["exitFn"] = function()
local webview = refs["URLSlideWebview"]
webview:hide(0.2)
end
},
{
["header"] = "Command line interface",
["enterFn"] = function()
local webview = makewebview("IPCSlideWebview", "body", nil, '<img src="https://cloud.githubusercontent.com/assets/525838/12647663/84e93d26-c5d6-11e5-846f-d7a1b7bcdba9.png">')
webview:show(0.3)
end,
["exitFn"] = function()
local webview = refs["IPCSlideWebview"]
webview:hide(0.2)
end
},
{
["header"] = "Other modules",
["body"] = [[alert appfinder applescript application audiodevice battery brightness caffeinate chooser drawing eventtap expose geometry grid hints host hotkey http httpserver image itunes javascript layout location menubar messages milight mouse notify pasteboard pathwatcher redshift screen sound spaces speech spotify tabs task timer uielement urlevent usb webview wifi]]
},
{
["header"] = "LuaSkin",
["enterFn"] = function()
local webview = makewebview("LuaSkinSlideWebview", "body", "https://github.com/Hammerspoon/hammerspoon/issues/749#issuecomment-173610148", nil)
webview:show(0.3)
end,
["exitFn"] = function()
local webview = refs["LuaSkinSlideWebview"]
webview:hide(0.2)
end
},
{
["header"] = "Questions?"
}
}
-- Draw a slide on the screen, creating persistent screen objects if necessary
function renderSlide(slideNum)
print("renderSlide")
if not slideNum then
slideNum = currentSlide
end
print(" slide number: "..slideNum)
local slideData = slides[slideNum]
local frame = presentationScreen:fullFrame()
if not slideBackground then
slideBackground = hs.drawing.rectangle(frame)
slideBackground:setLevel(hs.drawing.windowLevels["normal"])
slideBackground:setFillColor(hs.drawing.color.hammerspoon["osx_yellow"])
slideBackground:setFill(true)
slideBackground:show(0.2)
end
if not slideHeader then
slideHeader = hs.drawing.text(hs.geometry.rect(frame["x"] + 50,
frame["y"] + 50,
frame["w"] - 100,
frame["h"] / 10),
"")
slideHeader:setTextColor(hs.drawing.color.x11["black"])
slideHeader:setTextSize(slideHeaderSize)
slideHeader:orderAbove(slideBackground)
end
slideHeader:setText(slideData["header"])
slideHeader:show(0.5)
if not slideBody then
slideBody = hs.drawing.text(hs.geometry.rect(frame["x"] + 50,
slideHeader:frame()["y"] + slideHeader:frame()["h"] + 10,
(frame["w"] - 100)*0.66,
(frame["h"] / 10) * 8),
"")
slideBody:setTextColor(hs.drawing.color.x11["black"])
slideBody:setTextSize(slideBodySize)
slideBody:orderAbove(slideBackground)
end
slideBody:setText(slideData["body"] or "")
slideBody:show(0.5)
if not slideFooter then
slideFooter = hs.drawing.text(hs.geometry.rect(frame["x"] + 50,
frame["y"] + frame["h"] - 50 - slideFooterSize,
frame["w"] - 100,
frame["h"] / 25),
"Hammerspoon: Staggeringly powerful desktop automation")
slideFooter:setTextColor(hs.drawing.color.x11["black"])
slideFooter:setTextSize(slideFooterSize)
slideFooter:orderAbove(slideBackground)
slideFooter:show(0.5)
end
end
-- Move one slide forward
function nextSlide()
if currentSlide < #slides then
if slides[currentSlide] and slides[currentSlide]["exitFn"] then
print("running exitFn for slide")
slides[currentSlide]["exitFn"]()
end
currentSlide = currentSlide + 1
renderSlide()
if slides[currentSlide] and slides[currentSlide]["enterFn"] then
print("running enterFn for slide")
slides[currentSlide]["enterFn"]()
end
end
end
-- Move one slide back
function previousSlide()
if currentSlide > 1 then
if slides[currentSlide] and slides[currentSlide]["exitFn"] then
print("running exitFn for slide")
slides[currentSlide]["exitFn"]()
end
currentSlide = currentSlide - 1
renderSlide()
if slides[currentSlide] and slides[currentSlide]["enterFn"] then
print("running enterFn for slide")
slides[currentSlide]["enterFn"]()
end
end
end
-- Exit the presentation
function endPresentation()
hs.caffeinate.set("displayIdle", false, true)
if slides[currentSlide] and slides[currentSlide]["exitFn"] then
print("running exitFn for slide")
slides[currentSlide]["exitFn"]()
end
slideHeader:hide(0.5)
slideBody:hide(0.5)
slideFooter:hide(0.5)
slideBackground:hide(1)
hs.timer.doAfter(1, function()
slideHeader:delete()
slideBody:delete()
slideFooter:delete()
slideBackground:delete()
slideModal:exit()
end)
end
-- Prepare the modal hotkeys for the presentation
function setupModal()
print("setupModal")
slideModal = hs.hotkey.modal.new({}, nil, nil)
slideModal:bind({}, "left", previousSlide)
slideModal:bind({}, "right", nextSlide)
slideModal:bind({}, "escape", endPresentation)
slideModal:bind({}, "M", function()
for k,v in pairs(defaultapps) do
local app = hs.application.get(v)
if app then
local window = app:mainWindow()
if window then
window:raise()
end
end
end
hs.mjomatic.go(mjomatic1)
end)
slideModal:bind({}, "N", function()
for k,v in pairs(defaultapps) do
local app = hs.application.get(v)
if app then
local window = app:mainWindow()
if window then
window:raise()
end
end
end
hs.mjomatic.go(mjomatic2)
end)
slideModal:enter()
end
-- Callback for when we've chosen a screen to present on
function didChooseScreen(choice)
if not choice then
print("Chooser cancelled")
return
end
print("didChooseScreen: "..choice["text"])
presentationScreen = hs.screen.find(choice["uuid"])
if not presentationScreen then
hs.notify.show("Unable to find that screen, using primary screen")
presentationScreen = hs.screen.primaryScreen()
else
print("Found screen")
end
setupModal()
local frame = presentationScreen:fullFrame()
slideHeaderSize = frame["h"] / 15
slideBodySize = frame["h"] / 22
slideFooterSize = frame["h"] / 30
nextSlide()
end
-- Prepare a table of screens for hs.chooser
function screensToChoices()
print("screensToChoices")
local choices = hs.fnutils.map(hs.screen.allScreens(), function(screen)
local name = screen:name()
local id = screen:id()
local image = screen:snapshot()
local mode = screen:currentMode()["desc"]
return {
["text"] = name,
["subText"] = mode,
["uuid"] = id,
["image"] = image,
}
end)
return choices
end
-- Initiate the hs.chosoer for choosing a screen to present on
function chooseScreen()
print("chooseScreen")
local chooser = hs.chooser.new(didChooseScreen)
chooser:choices(screensToChoices)
chooser:show()
end
-- Prepare the presentation
function setupPresentation()
print("setupPresentation")
hs.caffeinate.set("displayIdle", true, true)
chooseScreen()
end
-- Create a menubar object to initiate the presentation
presentationControl = hs.menubar.new()
--presentationControl:setIcon(hs.image.imageFromName(hs.image.systemImageNames["EnterFullScreenTemplate"]))
presentationControl:setIcon(hs.image.imageFromName("NSComputer"))
presentationControl:setMenu({{ title = "Start Presentation", fn = setupPresentation }})