-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
2503 lines (2336 loc) · 106 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
local function scriptPath()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("(.*/)")
end
--]]
local EnhancedSpaces = {}
EnhancedSpaces.author = "Franz B. <[email protected]>"
EnhancedSpaces.homepage = "https://github.com/franzbu/EnhancedSpaces.spoon"
EnhancedSpaces.license = "MIT"
EnhancedSpaces.name = "EnhancedSpaces"
EnhancedSpaces.version = "0.9.60.1"
--EnhancedSpaces.spoonPath = scriptPath()
function EnhancedSpaces:tableToMap(table)
local map = {}
for _, v in pairs(table) do
map[v] = true
end
return map
end
function EnhancedSpaces:getWindowUnderMouse()
local my_pos = hs.geometry.new(hs.mouse.absolutePosition())
local my_screen = hs.mouse.getCurrentScreen()
return hs.fnutils.find(hs.window.orderedWindows(), function(w)
return my_screen == w:screen() and my_pos:inside(w:frame())
end)
end
function EnhancedSpaces:buttonNameToEventType(name, optionName)
if name == 'left' then return hs.eventtap.event.types.leftMouseDown end
if name == 'right' then return hs.eventtap.event.types.rightMouseDown end
error(optionName .. ': only "left" and "right" mouse button supported, got ' .. name)
end
function EnhancedSpaces:new(options)
hs.window.animationDuration = 0
options = options or {}
pM = options.outerPadding or 5
local innerPadding = options.innerPadding or 5
pI = innerPadding / 2
menuModifier1 = options.menuModifier1 or { 'alt' }
menuModifier2 = options.menuModifier2 or { 'ctrl' }
menuModifier3 = options.menuModifier3 or self:mergeModifiers(menuModifier1, menuModifier2)
menuTitles = options.menuTitles or { swap = 'Swap', send = "Send Window", get = "Get Window", help = 'Help', about = 'About', hammerspoon = 'Hammerspoon' }
hammerspoonMenu = options.hammerspoonMenu or false
hammerspoonMenuItems = options.hammerspoonMenuItems or { reload = "Reload Config", config = "Open Config", console = 'Console', preferences = 'Preferences', about = 'About Hammerspoon', update = 'Check for Updates...', relaunch = 'Relaunch Hammerspoon', quit = 'Quit Hammerspoon' }
popupModifier = options.popupModifier or nil
mbMainPopupKey = options.mbMainPopupKey or nil
mbSendPopupKey = options.mbSendPopupKey or nil
mbGetPopupKey = options.mbGetPopupKey or nil
mbSwapPopupKey = options.mbSwapPopupKey or nil
modifier1 = options.modifier1 or { 'alt' }
modifier2 = options.modifier2 or { 'ctrl' }
modifier1_2 = self:mergeModifiers(modifier1, modifier2)
modifierReference = options.modifierReference or { 'ctrl', 'shift' }
deReferenceKey = options.deReferenceKey or '0'
modifierMS = options.modifierMS or modifier2
modifierMSKeys = options.modifierMSKeys or { 'a', 's', 'd', 'f', 'q', 'w' }
openAppMSpace = options.openAppMSpace or nil
modifierSwitchWin = options.modifierSwitchWin or modifier1
modifierSwitchWinKeys = options.modifierSwitchWinKeys or { 'a', 'q' }
modifierSnap1 = options.modifierSnap1 or { 'cmd', 'alt' }
modifierSnap2 = options.modifierSnap2 or { 'cmd', 'ctrl' }
modifierSnap3 = options.modifierSnap3 or { 'cmd', 'shift' }
modifierSnapKeys = options.modifierSnapKeys or {
-- modifierSnapKey1
{{'a1','1'},{'a2','2'},{'a3','3'},{'a4','4'},{'a5','5'},{'a6','6'},{'a7','7'},{'a8','8'}},
-- modifierSnapKey2
{{'b1','1'},{'b2','2'},{'b3','3'},{'b4','4'},{'b5','5'},{'b6','6'},{'b7','7'},{'b8','8'},{'b9','9'},{'b10','0'},{'b11','o'},{'b12','p'}},
-- modifierSnapKey3
{{'c1','1'},{'c2','2'},{'c3','3'},{'c4','4'},{'c5','5'},{'c6','6'},{'c7','7'},{'c8','8'},{'c9','9'},{'c10','0'},{'c11','o'},{'c12','p'}},
}
-- switch to mSpace
modifierSwitchMS = options.modifierSwitchMS or modifier1
-- move window to mSpace
modifierMoveWinMSpace = options.modifierMoveWinMSpace or modifier1_2
local margin = options.margin or 0.3
resizeMargin = margin * 100 / 2
useResize = options.resize or false
ratioMSpaces = options.ratioMSpaces or 0.8
mspaces = options.mSpaces or { '1', '2', '3' }
currentMSpace = self:indexOf(options.MSpaces, options.startMSpace) or 2
gridIndicator = options.gridIndicator or { 20, 1, 0, 0, 0.33 }
customWallpaper = options.customWallpaper or false
wallpapers = {}
if customWallpaper then
wallpapers = self:createWallpapers()
else
for i = 1, #mspaces do
--wallpapers[i] = hs.image.imageFromPath(hs.configdir .. '/Spoons/EnhancedSpaces.spoon/wallpapers/default.jpg')
wallpapers[i] = hs.image.imageFromURL(hs.screen.mainScreen():desktopImageURL())
end
end
startupCommands = options.startupCommands or nil
swapModifier = options.swapModifier or { 'alt' }
swapKey = options.swapKey or 's'
swapSwitchFocus = options.swapSwitchFocus or false
-- mSpace Control
mSpaceControlModifier = options.mSpaceControlModifier or { 'alt' }
mSpaceControlKey = options.mSpaceControlKey or 'a'
mSpaceControlShow = options.mSpaceControlShow or mspaces
mSpaceControlConfig = options.mSpaceControlConfig or { 50, 0, 0, 0, 0.9 }
if mSpaceControlConfig[1] < 1 then mSpaceControlConfig[1] = 1 end
mSpaceControlFrame = options.mSpaceControlFrame or { 3, 1, 0, 0, 1, }
mSpaceControlHideHSC = options.mSpaceControlHideHSC or false -- hide Hammerspoon Console
mSpaceControlWinOpacity = options.mSpaceControlWinOpacity or 1
-- switcher
switcher = dofile(hs.spoons.resourcePath('lib/window_switcher.lua'))
switcherConfig = options.switcherConfig or {
textColor = { 0.9, 0.9, 0.9 },
fontName = 'Lucida Grande',
textSize = 16, -- in screen points
highlightColor = { 0.8, 0.5, 0, 0.8 }, -- highlight color for the selected window
backgroundColor = { 0.3, 0.3, 0.3, 0.5 },
onlyActiveApplication = false, -- only show windows of the active application
showTitles = true, -- show window titles
titleBackgroundColor = { 0, 0, 0 },
showThumbnails = true, -- show window thumbnails
selectedThumbnailSize = 284, -- size of window thumbnails in screen points
showSelectedThumbnail = true, -- show a larger thumbnail for the currently selected window
thumbnailSize = 112,
showSelectedTitle = false, -- show larger title for the currently selected window
}
--window_filter.lua: windows to disregard
SKIP_APPS_TRANSIENT_WINDOWS = options.SKIP_APPS_TRANSIENT_WINDOWS or {
'Spotlight', 'Notification Center', 'loginwindow', 'ScreenSaverEngine', 'PressAndHold',
'PopClip','Isolator', 'CheatSheet', 'CornerClickBG', 'Moom', 'CursorSense Manager',
'Music Manager', 'Google Drive', 'Dropbox', '1Password mini', 'Colors for Hue', 'MacID',
'CrashPlan menu bar', 'Flux', 'Jettison', 'Bartender', 'SystemPal', 'BetterSnapTool', 'Grandview', 'Radium',
'MenuMetersApp', 'DemoPro', 'DockHelper', 'Maccy', 'Albert', 'Alfred',
}
local moveResize = {
disabledApps = self:tableToMap(options.disabledApps or {}),
moveStartMouseEvent = self:buttonNameToEventType('left', 'moveMouseButton'),
resizeStartMouseEvent = self:buttonNameToEventType('right', 'resizeMouseButton'),
}
setmetatable(moveResize, self)
self.__index = self
moveResize.clickHandler = hs.eventtap.new(
{
hs.eventtap.event.types.leftMouseDown,
hs.eventtap.event.types.rightMouseDown,
},
moveResize:handleClick()
)
moveResize.cancelHandler = hs.eventtap.new(
{
hs.eventtap.event.types.leftMouseUp,
hs.eventtap.event.types.rightMouseUp,
},
moveResize:handleCancel()
)
moveResize.dragHandler = hs.eventtap.new(
{
hs.eventtap.event.types.leftMouseDragged,
hs.eventtap.event.types.rightMouseDragged,
},
moveResize:handleDrag()
)
autohideDock = self:getDockAutohide()
maxFF = hs.screen.mainScreen():fullFrame()
if autohideDock then -- no dock
max = hs.screen.mainScreen():frame()
heightMB = maxFF.h - max.h
heightDock = 0
hs.timer.doAfter(0.00001, function()
self:initiateAtStart()
self:refreshWinTables()
moveResize.clickHandler:start()
return moveResize
end)
else -- with dock
local hfmd = hs.screen.mainScreen():frame() -- height frame with menu bar and dock in it
self:setDockAutohide(true)
hs.timer.doAfter(0.00001, function()
local hfm = hs.screen.mainScreen():frame() -- height frame with menu bar in it
heightDock = hfm.h - hfmd.h
heightMB = maxFF.h - hfm.h
max = hfmd
self:initiateAtStart()
self:refreshWinTables()
moveResize.clickHandler:start()
return moveResize
end)
end
end
function EnhancedSpaces:initiateAtStart()
filter = dofile(hs.spoons.resourcePath('lib/window_filter.lua'))
filter_all = filter.new()
winAll = filter_all:getWindows()--hs.window.sortByFocused)
winMSpaces = {}
for i = 1, #winAll do
winMSpaces[i] = {}
winMSpaces[i].win = winAll[i]
winMSpaces[i].appName = winAll[i]:application():name() -- ':application():name()' causes errors if used 'later', mostly when creating menu
winMSpaces[i].snapshot = {}
winMSpaces[i].mspace = {}
winMSpaces[i].frame = {}
for k = 1, #mspaces do
winMSpaces[i].frame[k] = winAll[i]:frame()
winMSpaces[i].snapshot[k] = winAll[i]:snapshot():setSize({w = winAll[i]:size().w / 2, h = winAll[i]:size().h / 2})
if k == currentMSpace then
winMSpaces[i].mspace[k] = true
else
winMSpaces[i].mspace[k] = false
end
end
end
windowsOnCurrentMS = {} -- always up-to-date list of windows on current mSpace
_windowsOnCurrentMS = {} -- without active window for switching
windowsNotOnCurrentMS = {}
menubar = hs.menubar.new(true, "A"):setTitle(mspaces[currentMSpace])
menubar:setTooltip("mSpace")
-- recover windows at start
for i = 1, #winAll do
-- in case window is not on current mSpace, move it; i.e., if on current mSpace, don't resize
if winAll[i]:topLeft().x >= max.w - 1 then -- don't touch windows that are on current screen, even if they are in openAppMSpace
if self:indexOpenAppMSpace(winAll[i]) ~= nil then -- te be recovered according to openAppMSpace
self:assignMS(winAll[i], false)
else -- this means that window was on another mSpace, but is not in openAppMSpace -- window in 'hiding spot'
-- move window to middle of the current mSpace
winMSpaces[self:getPosWinMSpaces(winAll[i])].frame[currentMSpace] = hs.geometry.rect(max.w / 2 - winAll[i]:frame().w / 2, max.h / 2 - winAll[i]:frame().h / 2, winAll[i]:frame().w, winAll[i]:frame().h) -- put window in middle of screen
end
end
end
-- watchdogs
filter.default:subscribe(filter.windowNotOnScreen, function(w)
--print('____________ windowNotOnScreen ____________')
hs.timer.doAfter(0.0000001, function() --delay, otherwise 'filter_all = hs.window.filter.new()' not ready after closing of windows (in certain situations)
if not enteredFullscreen then
if w:frame().h ~= maxFF.h then
self:refreshWinTables()
end
end
end)
hs.timer.doAfter(1, function()
if not enteredFullscreen then
if windowsOnCurrentMS ~= nil and #windowsOnCurrentMS >= 1 then
windowsOnCurrentMS[1]:focus() -- activate last active window on current mSpace when closing/minimizing one
end
end
self:refreshWinTables()
end)
-- for avoiding switching of focus after force-closing a window in fullscreen-mode: set 'enteredFullscreen' to false if fullscreen window has been force-closed (then 'fullscreenedWindowID' is not present)
-- when force-closing window, 'enteredFullscreen' needs to be set to 'false'
if w:id() == fullscreenedWindowID then
hs.timer.doAfter(5, function()
enteredFullscreen = false
self:refreshWinTables()
end)
end
end)
filter.default:subscribe(filter.windowOnScreen, function(w)
hs.timer.doAfter(0.0000001, function()
if not enteredFullscreen then -- 'windowOnScreen' is triggered when leaving fullscreen, which is hereby counteracted
--print('____________ windowOnScreen ____________')-- .. winMSpaces[self:getPosWinMSpaces(w)].appName)
if self:indexOpenAppMSpace(w) ~= nil and not self:contextMenuTelegram() then -- with Telegram context menu open, other windows aren't assigned mSpaces when opened
self:refreshWinTables()
self:moveMiddleAfterMouseMinimized(w)
self:assignMS(w, true)
w:focus()
else
self:refreshWinTables()
w:focus()
end
end
end)
end)
filter.default:subscribe(filter.windowFocused, function(w)
--print('____________ windowFocused ____________ ' .. winMSpaces[self:getPosWinMSpaces(w)].appName)
self:refreshSnapshots(currentMSpace)
if w:frame().h == maxFF.h and w:frame().w == max.w then
enteredFullscreen = true
fullscreenedWindowID = w:id()
end
--if not enteredFullscreen and w:frame().h ~= maxFF.h then
if w:frame().h ~= maxFF.h and not boolMSpaceControl then -- and not enteredFullscreen then
hs.timer.doAfter(0.0000001, function()
self:refreshWinTables()
self:cmdTabFocus(w)
end)
end
--end)
end)
-- 'window_filter.lua' has been adjusted: 'local WINDOWMOVED_DELAY=0.01' instead of '0.5' to get rid of delay
filter.default:subscribe(filter.windowMoved, function(w)
hs.timer.doAfter(0.0000001, function()
self:refreshSnapshots(currentMSpace)
--print('____________ windowMoved ____________' .. winMSpaces[self:getPosWinMSpaces(w)].appName .. ', ' .. w:frame().h)
if w:frame().h == maxFF.h and w:frame().w == max.w then
enteredFullscreen = true
fullscreenedWindowID = w:id()
elseif not enteredFullscreen and not boolMSpaceControl then
self:adjustWinFrame(w)
self:refreshWinTables()
end
end)
end)
-- next 2 filters are for avoiding calling self:assignMS(_, true) after unfullscreening a window ('windowOnScreen' is called for each window after a window gets unfullscreened)
enteredFullscreen = false
fullscreenedWindowID = 0
---[[ -- doesn't get triggered reliably; workaround has been implemented instead
filter.default:subscribe(filter.windowFullscreened, function(w)
--print('_____!!!_______ windowFullscreened ____________' .. winMSpaces[self:getPosWinMSpaces(w)].appName)
enteredFullscreen = true
fullscreenedWindowID = w:id()
end)
--]]
filter.default:subscribe(filter.windowUnfullscreened, function(w)
--print('____________ windowUnfullscreened ____________' .. winMSpaces[self:getPosWinMSpaces(w)].appName)
hs.timer.doAfter(0.5, function()
w:focus()
enteredFullscreen = false
self:refreshWinTables()
end)
end)
--[[ --screenwatcher: stops working if screen resolution is changed a couple of time
boolStart = true -- for 'hs.screen.watcher.new' not to get triggered at start
local screenwatcher = hs.screen.watcher.new(function()
if not boolStart then
boolStart = true
print('!!! screenwatcher...')
autohideDock = self:getDockAutohide()
maxFF = hs.screen.mainScreen():fullFrame()
if autohideDock then -- no dock
max = hs.screen.mainScreen():frame()
heightMB = maxFF.h - max.h
heightDock = 0
else -- with dock
local hfmd = hs.screen.mainScreen():frame() -- height frame with menu bar and dock in it
self:setDockAutohide(true)
hs.timer.doAfter(0.1, function()
local hfm = hs.screen.mainScreen():frame() -- height frame with menu bar in it
heightDock = hfm.h - hfmd.h
heightMB = maxFF.h - hfm.h
max = hfmd
end)
hs.timer.doAfter(0.1, function()
self:setDockAutohide(false)
end)
hs.timer.doAfter(1, function()
--self:setDockAutohide(false)
boolStart = false
end)
end
end
end)
screenwatcher:start()
--]]
switcher = switcher.new()
switcher.ui.textColor = switcherConfig.textColor
switcher.ui.fontName = switcherConfig.fontName
switcher.ui.textSize = switcherConfig.textSize
switcher.ui.highlightColor = switcherConfig.highlightColor
switcher.ui.backgroundColor = switcherConfig.backgroundColor
switcher.ui.onlyActiveApplication = switcherConfig.onlyActiveApplication
switcher.ui.showTitles = switcherConfig.showTitles
switcher.ui.titleBackgroundColor = switcherConfig.titleBackgroundColor
switcher.ui.showThumbnails = switcherConfig.showThumbnails
switcher.ui.selectedThumbnailSize = switcherConfig.selectedThumbnailSize
switcher.ui.showSelectedThumbnail = switcherConfig.showSelectedThumbnail
switcher.ui.thumbnailSize = switcherConfig.thumbnailSize
switcher.ui.showSelectedTitle = switcherConfig.showSelectedTitle
-- cycle through windows of current mSpace
if modifierSwitchWin[1] ~= '' then
hs.hotkey.bind(modifierSwitchWin, modifierSwitchWinKeys[1], function()
self:refreshWinTables() -- for using up-to-date window tables (after force-closing apps this could be an issue otherwiese)
switcherChangeFocus = true
winGiveFocus = switcher:next(windowsOnCurrentMS)
end)
hs.hotkey.bind({modifierSwitchWin[1], 'shift' }, modifierSwitchWinKeys[1], function()
self:refreshWinTables() -- for using up-to-date window tables (after force-closing apps this could be an issue otherwiese)
switcherChangeFocus = true
winGiveFocus = switcher:previous(windowsOnCurrentMS) --reverse order
end)
-- 'subscribe', watchdog for releasing { 'alt' } -> to give focus to selected window (without all windows along the way would be given focus, which would falsify tables containing windows in order of "FocusedLast"
prevModifierSwitchWin = nil
keyboardTrackerSwitchWin = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e)
local flags = self:eventToArray(e:getFlags())
-- since on modifierSwitchWin release the flag is 'nil', var 'prevModifierSwitchWin' is used
if switcherChangeFocus and self:modifiersEqual(prevModifierSwitchWin, modifierSwitchWin) and winGiveFocus ~= nil then
winGiveFocus:focus()
switcherChangeFocus = false
end
prevModifierSwitchWin = flags
end)
keyboardTrackerSwitchWin:start()
-- cycle through windows of current mSpace with the exception of active one, then swap
switcherSwapWindows = false
if swapModifier[1] ~= '' then
hs.hotkey.bind(swapModifier, swapKey, function()
self:refreshWinTables() -- for using up-to-date window tables (after force-closing apps this could be an issue otherwiese)
win1 = winAll[1]
switcherSwapWindows = true
win2 = switcher:next(_windowsOnCurrentMS)
end)
hs.hotkey.bind({swapModifier[1], 'shift' }, swapKey, function()
self:refreshWinTables() -- for using up-to-date window tables (after force-closing apps this could be an issue otherwiese)
win1 = winAll[1]
switcherSwapWindows = true
win2 = switcher:previous(_windowsOnCurrentMS) --reverse order
end)
-- 'subscribe', watchdog for releasing swapModifier
prevModifierSwap = nil
keyboardTrackerSwapWin = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e)
local flags = self:eventToArray(e:getFlags())
-- since on swapModifier release the flag is 'nil', var 'prevModifierSwap' is used
if switcherSwapWindows and self:modifiersEqual(prevModifierSwap, swapModifier) and win2 ~= nil then
local frameWin1 = winMSpaces[self:getPosWinMSpaces(win1)].frame[currentMSpace]
winMSpaces[self:getPosWinMSpaces(win1)].frame[currentMSpace] = winMSpaces[self:getPosWinMSpaces(win2)].frame[currentMSpace]
winMSpaces[self:getPosWinMSpaces(win2)].frame[currentMSpace] = frameWin1
if swapSwitchFocus then
hs.timer.doAfter(0.001, function()
win2:focus()
end)
else -- focus stays with app in new place - still, focus needs to shift back and forth for window tables such as windowsOnCurrentMS to move a window also up the ranking order if it has been 'passively' chosen (when switching places)
win2:focus()
win1:focus()
end
self:refreshWinTables()
switcherSwapWindows = false
end
prevModifierSwap = flags
end)
keyboardTrackerSwapWin:start()
end
end
if modifierSwitchWin[1] ~= '' then
-- cycle through references of one window
hs.hotkey.bind(modifierSwitchWin, modifierSwitchWinKeys[2], function()
pos = self:getPosWinMSpaces(hs.window.focusedWindow())
local nextFR = self:getnextMSpaceNumber(currentMSpace)
while not winMSpaces[pos].mspace[nextFR] do
if nextFR == #mspaces then
nextFR = 1
else
nextFR = nextFR + 1
end
end
self:goToSpace(nextFR)
winMSpaces[pos].win:focus()
end)
end
-- cycle through mSpaces
mSpaceCyclePos = currentMSpace
mSpaceCycleCount = 0
if mSpaceControlModifier[1] ~= '' then
hs.hotkey.bind(mSpaceControlModifier, mSpaceControlKey, function()
if not boolMSpaceControl then
self:mSpaceControl()
else
frameCanvas[mSpaceCyclePos]:delete()
mSpaceCyclePos = self:getnextMSpaceNumber(mSpaceCyclePos)
frameCanvas[mSpaceCyclePos]:show()
canvasMSpaceControl[mSpaceCyclePos]:show()
for i = 1, #canvasWin do
canvasWin[i]:show()
end
end
mSpaceCycleCount = mSpaceCycleCount + 1
end)
-- reverse order by additionally pressing 'shift'
mSpaceControlModifierReverse = self:mergeModifiers(mSpaceControlModifier, { 'shift' })
hs.hotkey.bind(mSpaceControlModifierReverse, mSpaceControlKey, function()
if not boolMSpaceControl then
self:mSpaceControl()
else
frameCanvas[mSpaceCyclePos]:delete()
mSpaceCyclePos = self:getprevMSpaceNumber(mSpaceCyclePos)
frameCanvas[mSpaceCyclePos]:show()
canvasMSpaceControl[mSpaceCyclePos]:show()
for i = 1, #canvasWin do
canvasWin[i]:show()
end
end
mSpaceCycleCount = mSpaceCycleCount + 1
end)
prevmSpaceControlModifier = nil
keyboardTrackerMSpaceControl = hs.eventtap.new({ hs.eventtap.event.types.flagsChanged }, function(e)
local flags = self:eventToArray(e:getFlags())
-- since on mSpaceControlModifier release the flag is 'nil', var 'prevmSpaceControlModifier' is used
if (self:modifiersEqual(prevmSpaceControlModifier, mSpaceControlModifier) or self:modifiersEqual(prevmSpaceControlModifier, mSpaceControlModifierReverse) or self:modifiersEqual(prevmSpaceControlModifier, { 'shift'}) or self:modifiersEqual(prevmSpaceControlModifier, { 'alt'})) and flags[1] == nil and boolMSpaceControl and mSpaceCycleCount > 1 then
self:goToSpace(mSpaceCyclePos)
hs.timer.doAfter(0.0000001, function()
boolMSpaceControl = false
mSpaceCycleCount = 0
end)
for i = 1, #canvasMSpaceControl do
canvasMSpaceControl[i]:delete()
frameCanvas[i]:delete()
end
for i = 1, #canvasWin do
canvasWin[i]:delete()
end
baseCanvas:delete()
end
prevmSpaceControlModifier = flags
end)
keyboardTrackerMSpaceControl:start()
-- pressing 'Esc' closes mSpace Control
if mSpaceControlModifier[1] ~= '' then
keyboardTrackerMSpaceControlEsc = hs.eventtap.new({ hs.eventtap.event.types.keyDown }, function(e)
if e:getKeyCode() == 53 and boolMSpaceControl then
boolMSpaceControl = false
mSpaceCycleCount = 0
for i = 1, #canvasMSpaceControl do
canvasMSpaceControl[i]:delete()
frameCanvas[i]:delete()
end
for i = 1, #canvasWin do
canvasWin[i]:delete()
end
baseCanvas:delete()
refreshMSpaces() -- refresh mSpace
end
end)
keyboardTrackerMSpaceControlEsc:start()
end
end
if modifierReference[1] ~= '' then
-- reference/dereference windows to/from mspaces, goto mspaces
for i = 1, #mspaces do
hs.hotkey.bind(modifierReference, mspaces[i], function()
self:refWinMSpace(i)
end)
end
-- de-reference
hs.hotkey.bind(modifierReference, deReferenceKey, function()
self:derefWinMSpace()
end)
end
if modifierMS[1] ~= '' then
-- switching spaces/moving windows
hs.hotkey.bind(modifierMS, modifierMSKeys[1], function() -- previous space (incl. cycle)
currentMSpace = self:getprevMSpaceNumber(currentMSpace)
self:goToSpace(currentMSpace)
end)
hs.hotkey.bind(modifierMS, modifierMSKeys[2], function() -- next space (incl. cycle)
currentMSpace = self:getnextMSpaceNumber(currentMSpace)
self:goToSpace(currentMSpace)
end)
hs.hotkey.bind(modifierMS, modifierMSKeys[5], function() -- move active window to previous space and switch there (incl. cycle)
-- move window to prev space and switch there
self:moveToSpace(self:getprevMSpaceNumber(currentMSpace), currentMSpace, true)
currentMSpace = self:getprevMSpaceNumber(currentMSpace)
self:goToSpace(currentMSpace)
end)
hs.hotkey.bind(modifierMS, modifierMSKeys[6], function() -- move active window to next space and switch there (incl. cycle)
-- move window to next space and switch there
self:moveToSpace(self:getnextMSpaceNumber(currentMSpace), currentMSpace, true)
currentMSpace = self:getnextMSpaceNumber(currentMSpace)
self:goToSpace(currentMSpace)
end)
hs.hotkey.bind(modifierMS, modifierMSKeys[3], function() -- move active window to previous space (incl. cycle)
-- move window to prev space
self:moveToSpace(self:getprevMSpaceNumber(currentMSpace), currentMSpace, true)
end)
hs.hotkey.bind(modifierMS, modifierMSKeys[4], function() -- move active window to next space (incl. cycle)
-- move window to next space
self:moveToSpace(self:getnextMSpaceNumber(currentMSpace), currentMSpace, true)
end)
end
-- goto mspaces directly with 'modifierSwitchMS-<name of mspace>'
if modifierSwitchMS[1] ~= '' then
for i = 1, #mspaces do
hs.hotkey.bind(modifierSwitchMS, mspaces[i], function()
self:goToSpace(i)
end)
end
end
-- move window to specific mSpace
if modifierMoveWinMSpace[1] ~= '' then
for i = 1, #mspaces do
hs.hotkey.bind(modifierMoveWinMSpace, mspaces[i], function() -- move active window to next space and switch there (incl. cycle)
self:moveToSpace(i, currentMSpace, true)
end)
end
end
-- keyboard shortcuts - snapping windows into grid postions
if modifierSnap1[1] ~= '' then
for i = 1, #modifierSnapKeys[1] do
hs.hotkey.bind(modifierSnap1, modifierSnapKeys[1][i][2], function()
hs.window.focusedWindow():move(self:snap(modifierSnapKeys[1][i][1]), nil, false, 0)
end)
end
end
if modifierSnap2[1] ~= '' then
for i = 1, #modifierSnapKeys[2] do
hs.hotkey.bind(modifierSnap2, modifierSnapKeys[2][i][2], function()
hs.window.focusedWindow():move(self:snap(modifierSnapKeys[2][i][1]), nil, false, 0)
end)
end
end
if modifierSnap3[1] ~= '' then
for i = 1, #modifierSnapKeys[3] do
hs.hotkey.bind(modifierSnap3, modifierSnapKeys[3][i][2], function()
hs.window.focusedWindow():move(self:snap(modifierSnapKeys[3][i][1]), nil, false, 0)
end)
end
end
-- popup menus
if popupModifier ~= nil and mbMainPopupKey ~= nil then
hs.hotkey.bind(popupModifier, mbMainPopupKey, function()
mbMainPopup:popupMenu(hs.mouse.absolutePosition() )
end)
end
if popupModifier ~= nil and mbSendPopupKey ~= nil then
hs.hotkey.bind(popupModifier, mbSendPopupKey, function()
mbSendPopup:popupMenu(hs.mouse.absolutePosition() )
end)
end
if popupModifier ~= nil and mbGetPopupKey ~= nil then
hs.hotkey.bind(popupModifier, mbGetPopupKey, function()
mbGetPopup:popupMenu(hs.mouse.absolutePosition() )
end)
end
if popupModifier ~= nil and mbSwapPopupKey ~= nil then
hs.hotkey.bind(popupModifier, mbSwapPopupKey, function()
mbSwapPopup:popupMenu(hs.mouse.absolutePosition() )
end)
end
-- startup commands
if startupCommands ~= nil then
for i = 1, #startupCommands do
os.execute(startupCommands[i])
end
end
if not autohideDock then
-- has to be triggered later than 'self:setDockAutohide(true)'
self:setDockAutohide(false)
end
--[[
hs.timer.doAfter(1, function()
boolStart = false
end)
--]]
end
-- mSpace Control: prepare wallpapers
---[[
function EnhancedSpaces:createWallpapers()
local wp = {}
for i = 1, #mspaces do
if hs.fs.displayName(hs.configdir .. '/Spoons/EnhancedSpaces.spoon/wallpapers/' .. mspaces[i] .. '.jpg') then
wp[i] = hs.image.imageFromPath(hs.configdir .. '/Spoons/EnhancedSpaces.spoon/wallpapers/' .. mspaces[i] .. '.jpg')
else
wp[i] = hs.image.imageFromPath(hs.configdir .. '/Spoons/EnhancedSpaces.spoon/wallpapers/default.jpg')
end
end
return wp
end
--]]
--[[
function EnhancedSpaces:createWallpapers()
local wp = {}
for i = 1, #mspaces do
if hs.fs.displayName(hs.spoons.resourcePath('wallpapers/' .. mspaces[i] .. '.jpg')) then
hs.alert.show('safasdf')
wp[i] = hs.image.imageFromPath(hs.spoons.resourcePath('wallpapers/' .. mspaces[i] .. '.jpg'))
else
wp[i] = hs.image.imageFromPath(hs.spoons.resourcePath('wallpapers/default.jpg'))
end
end
return wp
end
--]]
-- mSpace Control
boolMSpaceControl = false
canvasMSpaceControl = {} -- canvases containing one mSpace preview each
frameCanvas = {} -- frame for highlighting current mSpace
canvasWin = {} --
function EnhancedSpaces:mSpaceControl()
boolMSpaceControl = true
-- background canvas
baseCanvas = hs.canvas:new()
baseCanvas:insertElement(
{
action = 'fill',
type = 'rectangle',
fillColor = {
red = mSpaceControlConfig[2],
green = mSpaceControlConfig[3],
blue = mSpaceControlConfig[4],
alpha = mSpaceControlConfig[5]
},
trackMouseDown = true,
}, 1)
--baseCanvas:canvasMouseEvents(true, false, false, false) -- ([down], [up], [enterExit], [move])
baseCanvas:mouseCallback(function() -- (canvas object, event, id, x, y)
self:goToSpace(currentMSpace)
hs.timer.doAfter(0.0000001, function()
boolMSpaceControl = false
mSpaceCycleCount = 0
end)
for i = 1, #canvasMSpaceControl do
canvasMSpaceControl[i]:delete()
frameCanvas[i]:delete()
end
for i = 1, #canvasWin do
canvasWin[i]:delete()
end
baseCanvas:delete()
end)
baseCanvas:frame(hs.geometry.new(0, 0, maxFF.w, maxFF.h))
baseCanvas:show()
-- canvases with previews of mSpaces
for i = 1, #mSpaceControlShow do
canvasMSpaceControl[i] = hs.canvas:new()
canvasMSpaceControl[i]:insertElement(
{
image = wallpapers[i],
imageScaling = 'scaleToFit',
type = 'image',
trackMouseDown = true,
}, 1)
--canvasMSpaceControl[i]:canvasMouseEvents(true, false, false, false) -- ([down], [up], [enterExit], [move])
canvasMSpaceControl[i]:mouseCallback(function() -- (canvas object, event, id, x, y)
self:goToSpace(self:indexOf(mspaces, mSpaceControlShow[i]))
hs.timer.doAfter(0.0000000001, function() -- prevent watchdogs 'windowFocused' and 'windowMoved' from being triggered
boolMSpaceControl = false
mSpaceCycleCount = 0
end)
for j = 1, #canvasMSpaceControl do
canvasMSpaceControl[j]:delete()
frameCanvas[j]:delete()
end
for j = 1, #canvasWin do
canvasWin[j]:delete()
end
baseCanvas:delete()
end)
end
if #mSpaceControlShow <= 4 then
mSpaceControlX = 2
mSpaceControlY = 2
elseif #mSpaceControlShow <= 6 then
mSpaceControlX = 2
mSpaceControlY = 3
elseif #mSpaceControlShow <= 9 then
mSpaceControlX = 3
mSpaceControlY = 3
elseif #mSpaceControlShow <= 12 then
mSpaceControlX = 3
mSpaceControlY = 4
elseif #mSpaceControlShow <= 16 then
mSpaceControlX = 4
mSpaceControlY = 4
elseif #mSpaceControlShow <= 20 then
mSpaceControlX = 4
mSpaceControlY = 5
elseif #mSpaceControlShow <= 25 then
mSpaceControlX = 5
mSpaceControlY = 5
elseif #mSpaceControlShow <= 30 then
mSpaceControlX = 5
mSpaceControlY = 6
else
mSpaceControlX = math.ceil(math.sqrt(#mSpaceControlShow))
mSpaceControlY = mSpaceControlX
end
local padH = mSpaceControlConfig[1] / 1000 * max.w / mSpaceControlY
local ft = mSpaceControlFrame[1] -- frame thickness
local screenRatio = maxFF.h / max.w
local mSpacePreviewW = (max.w - 2 * padH) / mSpaceControlY - 2 * padH
local mSpacePreviewH = mSpacePreviewW * screenRatio
local padV = (maxFF.h - mSpaceControlX * mSpacePreviewH) / (mSpaceControlX + 1)
local k = 1
for i = 1, mSpaceControlX do
for j = 1, mSpaceControlY do
if k > #mSpaceControlShow then break end
canvasMSpaceControl[k]:frame(hs.geometry.new(
2 * padH + (j - 1) * mSpacePreviewW + (j-1) * 2 * padH, -- x
i * padV + (i - 1) * mSpacePreviewH, -- y
mSpacePreviewW, -- w
mSpacePreviewH -- h
))
canvasMSpaceControl[k]:show()
-- frame around current mSpace
if mSpaceControlFrame[1] ~= '' then
frameCanvas[k] = hs.canvas:new()
frameCanvas[k]:insertElement(
{
action = 'fill',
type = 'rectangle',
fillColor = {
red = mSpaceControlFrame[2],
green = mSpaceControlFrame[3],
blue = mSpaceControlFrame[4],
alpha = mSpaceControlFrame[5],
},
trackMouseDown = true,
}, 1)
--frameCanvas[k]:canvasMouseEvents(true, false, false, false) -- ([down], [up], [enterExit], [move])
frameCanvas[k]:mouseCallback(function() -- (canvas object, event, id, x, y)
self:goToSpace(currentMSpace)
hs.timer.doAfter(0.0000001,
function() -- prevent watchdogs windowFocused and windowMoved from being triggered
boolMSpaceControl = false
mSpaceCycleCount = 0
end)
for o = 1, #canvasMSpaceControl do
canvasMSpaceControl[o]:delete()
frameCanvas[o]:delete()
end
for o = 1, #canvasWin do
canvasWin[o]:delete()
end
baseCanvas:delete()
end)
frameCanvas[k]:frame(hs.geometry.new(
canvasMSpaceControl[k]:frame().x - ft,
canvasMSpaceControl[k]:frame().y - ft,
canvasMSpaceControl[k]:frame().w + 2 * ft,
canvasMSpaceControl[k]:frame().h + 2 * ft
))
end
k = k + 1
end
end
frameCanvas[currentMSpace]:show() -- show at the end, so frame is on top of adjacent mSpaces
canvasMSpaceControl[currentMSpace]:show() -- necessary, otherwise frameCanvas would be on top
-- insert windows
for i = 1, #mspaces do
local ratioW = canvasMSpaceControl[i]:frame().w / max.w
local ratioH = canvasMSpaceControl[i]:frame().h / max.h
for j = #winMSpaces, 1, -1 do -- last focused 'painted' last, so it is in foreground
if winMSpaces[j].mspace[i] then
table.insert(canvasWin, hs.canvas:new())
canvasWin[#canvasWin]:insertElement(
{
image = winMSpaces[j].snapshot[i],
imageAlpha = mSpaceControlWinOpacity,
type = 'image',
imageScaling = 'scaleToFit',
trackMouseDown = true,
id = winMSpaces[j].win:id(),
}, 1)
--canvasWin[#canvasWin]:canvasMouseEvents(true, false, false, false) -- ([down], [up], [enterExit], [move]) -- id is always '_canvas_' and therefore not usable here
canvasWin[#canvasWin]:mouseCallback(function(_, _, id) -- (canvas object, event, id, x, y)
self:goToSpace(i)
-- unreliable for giving focus to window on clicked canvasWin: winMSpaces[j] -> 'winMSpaces[j].win:id()' handed as 'id' works
-- reason unclear: winMSpaces[j] unreliable,
for o = 1, #winMSpaces do
if winMSpaces[o].win:id() == id then
winMSpaces[o].win:focus()
hs.mouse.absolutePosition(hs.geometry.point(
winMSpaces[o].win:frame().x + winMSpaces[o].win:frame().w / 2,
winMSpaces[o].win:frame().y + winMSpaces[o].win:frame().h / 2
))
break
end
end
-- cleaning up
hs.timer.doAfter(0.0000001, function()
boolMSpaceControl = false
mSpaceCycleCount = 0
end)
for n = 1, #canvasMSpaceControl do
canvasMSpaceControl[n]:delete()
frameCanvas[n]:delete()
end
for n = 1, #canvasWin do
canvasWin[n]:delete()
end
baseCanvas:delete()
end)
canvasWin[#canvasWin]:frame(hs.geometry.new(
winMSpaces[j].frame[i].x * ratioW + canvasMSpaceControl[i]:frame().x,
winMSpaces[j].frame[i].y * ratioH - heightMB * ratioH + canvasMSpaceControl[i]:frame().y,
winMSpaces[j].frame[i].w * ratioW,
winMSpaces[j].frame[i].h * ratioH
))
canvasWin[#canvasWin]:show()
end
end
end
self:goToSpace(currentMSpace)
end
function EnhancedSpaces:refreshMenu()
mainMenu = {
{
title = "mSpace Control",
fn = function(mods) self:mSpaceControl() end
},
{
title = "mSpaces",
menu = self:createMSpaceMenu(),
},
{ title = "-" },
{
title = menuTitles.swap, disabled = self:trueIfZero(_windowsOnCurrentMS),
menu = self:createSwapWindowMenu(),
},
{ title = "-" },
{
title = self:getToggleRefWindow()[1], disabled = self:getToggleRefWindow()[2],
menu = self:createToggleRefMenu(),
},
{ title = "-" },
{
title = menuTitles.send, disabled = self:trueIfZero(windowsOnCurrentMS),
menu = self:createSendWindowMenu(),
},
{
title = menuTitles.get, disabled = self:trueIfZero(windowsNotOnCurrentMS),
menu = self:createGetWindowMenu(),
},
{ title = "-" },
{ title = menuTitles.help, fn = function() os.execute('/usr/bin/open https://github.com/franzbu/EnhancedSpaces.spoon/blob/main/README.md') end },
{ title = menuTitles.about, fn = function() hs.dialog.blockAlert('EnhancedSpaces', 'v0.9.60.1\n\n\nMakes you more productive.\nUse your time for what really matters.') end },
{ title = "-" },
{
title = self:hsTitle(), --image = hs.image.imageFromPath(hs.configdir .. '/Spoons/EnhancedSpaces.spoon/images/hs.png'):setSize({ h = 15, w = 15 }),
menu = self:hsMenu(),
},
}
menubar:setMenu(mainMenu)
mbMainPopup = hs.menubar.new(false)
mainPopupMenu = {
{
title = "mSpaces",
menu = self:createMSpaceMenu(),
},
{ title = "-" },