-
Notifications
You must be signed in to change notification settings - Fork 17
/
init.lua
950 lines (826 loc) · 32.7 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
--- === PaperWM.spoon ===
---
--- Tile windows horizontally. Inspired by PaperWM Gnome extension.
---
--- # Usage
---
--- `PaperWM:start()` will begin automatically tiling new and existing windows.
--- `PaperWM:stop()` will release control over windows.
---
--- Set `PaperWM.window_gap` to the number of pixels to space between windows and
--- the top and bottom screen edges.
---
--- Overwrite `PaperWM.window_filter` to ignore specific applications. For example:
---
--- ```
--- PaperWM.window_filter = PaperWM.window_filter:setAppFilter("Finder", false)
--- PaperWM:start() -- restart for new window filter to take effect
--- ```
---
--- # Limitations
---
--- MacOS does not allow a window to be moved fully off-screen. Windows that would
--- be tiled off-screen are placed in a margin on the left and right edge of the
--- screen. They are still visible and clickable.
---
--- It's difficult to detect when a window is dragged from one space or screen to
--- another. Use the move_window_N commands to move windows between spaces and
--- screens.
---
--- Arrange screens vertically to prevent windows from bleeding into other screens.
---
---
--- Download: [https://github.com/mogenson/PaperWM.spoon](https://github.com/mogenson/PaperWM.spoon)
local WindowFilter <const> = hs.window.filter
local Window <const> = hs.window
local Spaces <const> = hs.spaces
local Screen <const> = hs.screen
local Timer <const> = hs.timer
local Rect <const> = hs.geometry.rect
local Watcher <const> = hs.uielement.watcher
local partial <const> = hs.fnutils.partial
local PaperWM = {}
PaperWM.__index = PaperWM
-- Metadata
PaperWM.name = "PaperWM"
PaperWM.version = "0.4"
PaperWM.author = "Michael Mogenson"
PaperWM.homepage = "https://github.com/mogenson/PaperWM.spoon"
PaperWM.license = "MIT - https://opensource.org/licenses/MIT"
PaperWM.default_hotkeys = {
stop_events = { { "ctrl", "alt", "cmd", "shift" }, "q" },
refresh_windows = { { "ctrl", "alt", "cmd", "shift" }, "r" },
focus_left = { { "ctrl", "alt", "cmd" }, "left" },
focus_right = { { "ctrl", "alt", "cmd" }, "right" },
focus_up = { { "ctrl", "alt", "cmd" }, "up" },
focus_down = { { "ctrl", "alt", "cmd" }, "down" },
swap_left = { { "ctrl", "alt", "cmd", "shift" }, "left" },
swap_right = { { "ctrl", "alt", "cmd", "shift" }, "right" },
swap_up = { { "ctrl", "alt", "cmd", "shift" }, "up" },
swap_down = { { "ctrl", "alt", "cmd", "shift" }, "down" },
center_window = { { "ctrl", "alt", "cmd" }, "c" },
full_width = { { "ctrl", "alt", "cmd" }, "f" },
cycle_width = { { "ctrl", "alt", "cmd" }, "r" },
cycle_height = { { "ctrl", "alt", "cmd", "shift" }, "r" },
slurp_in = { { "ctrl", "alt", "cmd" }, "i" },
barf_out = { { "ctrl", "alt", "cmd" }, "o" },
switch_space_1 = { { "ctrl", "alt", "cmd" }, "1" },
switch_space_2 = { { "ctrl", "alt", "cmd" }, "2" },
switch_space_3 = { { "ctrl", "alt", "cmd" }, "3" },
switch_space_4 = { { "ctrl", "alt", "cmd" }, "4" },
switch_space_5 = { { "ctrl", "alt", "cmd" }, "5" },
switch_space_6 = { { "ctrl", "alt", "cmd" }, "6" },
switch_space_7 = { { "ctrl", "alt", "cmd" }, "7" },
switch_space_8 = { { "ctrl", "alt", "cmd" }, "8" },
switch_space_9 = { { "ctrl", "alt", "cmd" }, "9" },
move_window_1 = { { "ctrl", "alt", "cmd", "shift" }, "1" },
move_window_2 = { { "ctrl", "alt", "cmd", "shift" }, "2" },
move_window_3 = { { "ctrl", "alt", "cmd", "shift" }, "3" },
move_window_4 = { { "ctrl", "alt", "cmd", "shift" }, "4" },
move_window_5 = { { "ctrl", "alt", "cmd", "shift" }, "5" },
move_window_6 = { { "ctrl", "alt", "cmd", "shift" }, "6" },
move_window_7 = { { "ctrl", "alt", "cmd", "shift" }, "7" },
move_window_8 = { { "ctrl", "alt", "cmd", "shift" }, "8" },
move_window_9 = { { "ctrl", "alt", "cmd", "shift" }, "9" }
}
-- filter for windows to manage
PaperWM.window_filter = WindowFilter.new():setOverrideFilter({
visible = true,
fullscreen = false,
hasTitlebar = true,
allowRoles = "AXStandardWindow"
})
-- number of pixels between windows
PaperWM.window_gap = 8
-- ratios to use when cycling widths and heights, golden ratio by default
PaperWM.window_ratios = { 0.23607, 0.38195, 0.61804 }
-- size of the on-screen margin to place off-screen windows
PaperWM.screen_margin = 1
-- logger
PaperWM.logger = hs.logger.new(PaperWM.name)
-- constants
local Direction <const> = {
LEFT = -1,
RIGHT = 1,
UP = -2,
DOWN = 2,
WIDTH = 3,
HEIGHT = 4
}
-- array of windows sorted from left to right
local window_list = {} -- 3D array of tiles in order of [space][x][y]
local index_table = {} -- dictionary of {space, x, y} with window id for keys
local ui_watchers = {} -- dictionary of uielement watchers with window id for keys
-- current focused window
local focused_window = nil
local function getSpace(index)
local layout = Spaces.allSpaces()
for _, screen in ipairs(Screen.allScreens()) do
local screen_uuid = screen:getUUID()
local num_spaces = #layout[screen_uuid]
if num_spaces >= index then return layout[screen_uuid][index] end
index = index - num_spaces
end
end
local function getFirstVisibleWindow(columns, screen)
local x = screen:frame().x
for _, windows in ipairs(columns or {}) do
local window = windows[1] -- take first window in column
if window:frame().x >= x then return window end
end
end
local function getColumn(space, col) return (window_list[space] or {})[col] end
local function getWindow(space, col, row)
return (getColumn(space, col) or {})[row]
end
local function getCanvas(screen)
local screen_frame = screen:frame()
return Rect(screen_frame.x + PaperWM.window_gap,
screen_frame.y + PaperWM.window_gap,
screen_frame.w - (2 * PaperWM.window_gap),
screen_frame.h - (2 * PaperWM.window_gap))
end
local function updateIndexTable(space, column)
local columns = window_list[space] or {}
for col = column, #columns do
for row, window in ipairs(getColumn(space, col)) do
index_table[window:id()] = { space = space, col = col, row = row }
end
end
end
local pending_window = nil
local function windowEventHandler(window, event, self)
self.logger.df("%s for %s", event, window)
local space = nil
--[[ When a new window is created, We first get a windowVisible event but
without a Space. Next we receive a windowFocused event for the window, but
this also sometimes lacks a Space. Our approach is to store the window
pending a Space in the pending_window variable and set a timer to try to add
the window again later. Also schedule the windowFocused handler to run later
after the window was added ]]
--
if event == "windowFocused" then
if pending_window and window == pending_window then
Timer.doAfter(Window.animationDuration,
function()
windowEventHandler(window, event, self)
end)
return
end
focused_window = window
space = Spaces.windowSpaces(window)[1]
elseif event == "windowVisible" or event == "windowUnfullscreened" then
space = self:addWindow(window)
if pending_window and window == pending_window then
pending_window = nil -- tried to add window for the second time
elseif not space then
pending_window = window
Timer.doAfter(Window.animationDuration,
function()
windowEventHandler(window, event, self)
end)
return
end
elseif event == "windowNotVisible" then
self:removeWindow(window) -- destroyed windows don't have a space
elseif event == "windowFullscreened" then
space = self:removeWindow(window, true) -- don't focus new window if fullscreened
elseif event == "AXWindowMoved" or event == "AXWindowResized" then
space = Spaces.windowSpaces(window)[1]
end
if space then self:tileSpace(space) end
end
function PaperWM:start()
-- check for some settings
if not Spaces.screensHaveSeparateSpaces() then
self.logger.e(
"please check 'Displays have separate Spaces' in System Preferences -> Mission Control")
end
-- clear state
window_list = {}
index_table = {}
ui_watchers = {}
-- populate window list, index table, ui_watchers, and set initial layout
self:refreshWindows()
-- listen for window events
self.window_filter:subscribe({
WindowFilter.windowFocused, WindowFilter.windowVisible,
WindowFilter.windowNotVisible, WindowFilter.windowFullscreened,
WindowFilter.windowUnfullscreened
}, function(window, _, event) windowEventHandler(window, event, self) end)
return self
end
function PaperWM:stop()
-- stop events
self.window_filter:unsubscribeAll()
for _, watcher in pairs(ui_watchers) do watcher:stop() end
return self
end
function PaperWM:tileColumn(windows, bounds, h, w, id, h4id)
local last_window, frame
for _, window in ipairs(windows) do
frame = window:frame()
w = w or frame.w -- take given width or width of first window
if bounds.x then -- set either left or right x coord
frame.x = bounds.x
elseif bounds.x2 then
frame.x = bounds.x2 - w
end
if h then -- set height if given
if id and h4id and window:id() == id then
frame.h = h4id -- use this height for window with id
else
frame.h = h -- use this height for all other windows
end
end
frame.y = bounds.y
frame.w = w
frame.y2 = math.min(frame.y2, bounds.y2) -- don't overflow bottom of bounds
self:moveWindow(window, frame)
bounds.y = math.min(frame.y2 + self.window_gap, bounds.y2)
last_window = window
end
-- expand last window height to bottom
if frame.y2 ~= bounds.y2 then
frame.y2 = bounds.y2
self:moveWindow(last_window, frame)
end
return w -- return width of column
end
function PaperWM:tileSpace(space)
if not space or Spaces.spaceType(space) ~= "user" then
self.logger.e("current space invalid")
return
end
-- find screen for space
local screen = Screen(Spaces.spaceDisplay(space))
if not screen then
self.logger.e("no screen for space")
return
end
-- if focused window is in space, tile from that
focused_window = focused_window or Window.focusedWindow()
local anchor_window = (focused_window and
(Spaces.windowSpaces(focused_window)[1] == space)) and
focused_window or
getFirstVisibleWindow(window_list[space], screen)
if not anchor_window then
self.logger.e("no anchor window in space")
return
end
local anchor_index = index_table[anchor_window:id()]
if not anchor_index then
self.logger.e("anchor index not found")
if self:addWindow(anchor_window) == space then
self.logger.d("added missing window")
anchor_index = index_table[anchor_window:id()]
else
return -- bail
end
end
-- get some global coordinates
local screen_frame <const> = screen:frame()
local left_margin <const> = screen_frame.x + self.screen_margin
local right_margin <const> = screen_frame.x2 - self.screen_margin
local canvas <const> = getCanvas(screen)
-- make sure anchor window is on screen
local anchor_frame = anchor_window:frame()
anchor_frame.x = math.max(anchor_frame.x, canvas.x)
anchor_frame.w = math.min(anchor_frame.w, canvas.w)
anchor_frame.h = math.min(anchor_frame.h, canvas.h)
if anchor_frame.x2 > canvas.x2 then
anchor_frame.x = canvas.x2 - anchor_frame.w
end
-- adjust anchor window column
local column = getColumn(space, anchor_index.col)
if not column then
self.logger.e("no anchor window column")
return
end
-- TODO: need a minimum window height
if #column == 1 then
anchor_frame.y, anchor_frame.h = canvas.y, canvas.h
self:moveWindow(anchor_window, anchor_frame)
else
local n = #column - 1 -- number of other windows in column
local h =
math.max(0, canvas.h - anchor_frame.h - (n * self.window_gap)) // n
local bounds = {
x = anchor_frame.x,
x2 = nil,
y = canvas.y,
y2 = canvas.y2
}
self:tileColumn(column, bounds, h, anchor_frame.w, anchor_window:id(),
anchor_frame.h)
end
-- tile windows from anchor right
local x = math.min(anchor_frame.x2 + self.window_gap, right_margin)
for col = anchor_index.col + 1, #(window_list[space] or {}) do
local bounds = { x = x, x2 = nil, y = canvas.y, y2 = canvas.y2 }
local column_width = self:tileColumn(getColumn(space, col), bounds)
x = math.min(x + column_width + self.window_gap, right_margin)
end
-- tile windows from anchor left
local x2 = math.max(anchor_frame.x - self.window_gap, left_margin)
for col = anchor_index.col - 1, 1, -1 do
local bounds = { x = nil, x2 = x2, y = canvas.y, y2 = canvas.y2 }
local column_width = self:tileColumn(getColumn(space, col), bounds)
x2 = math.max(x2 - column_width - self.window_gap, left_margin)
end
end
function PaperWM:refreshWindows()
-- get all windows across spaces
local all_windows = self.window_filter:getWindows()
local retile_spaces = {} -- spaces that need to be retiled
for _, window in ipairs(all_windows) do
local index = index_table[window:id()]
if not index then
-- add window
local space = self:addWindow(window)
if space then retile_spaces[space] = true end
elseif index.space ~= Spaces.windowSpaces(window)[1] then
-- move to window list in new space
self:removeWindow(window)
local space = self:addWindow(window)
if space then retile_spaces[space] = true end
end
end
-- retile spaces
for space, _ in pairs(retile_spaces) do self:tileSpace(space) end
end
function PaperWM:addWindow(add_window)
-- check if window is already in window list
if index_table[add_window:id()] then return end
local space = Spaces.windowSpaces(add_window)[1]
if not space then
self.logger.e("add window does not have a space")
return
end
if not window_list[space] then window_list[space] = {} end
-- find where to insert window
local add_column = 1
-- when addWindow() is called from a window created event:
-- focused_window from previous window focused event will not be add_window
-- hs.window.focusedWindow() will return add_window
-- new window focused event for add_window has not happened yet
if focused_window and
((index_table[focused_window:id()] or {}).space == space) and
(focused_window:id() ~= add_window:id()) then
add_column = index_table[focused_window:id()].col + 1 -- insert to the right
else
local x = add_window:frame().center.x
for col, windows in ipairs(window_list[space]) do
if x < windows[1]:frame().center.x then
add_column = col
break
end
end
end
-- add window
table.insert(window_list[space], add_column, { add_window })
-- update index table
updateIndexTable(space, add_column)
-- subscribe to window moved events
local watcher = add_window:newWatcher(
function(window, event, _, self)
windowEventHandler(window, event, self)
end, self)
watcher:start({ Watcher.windowMoved, Watcher.windowResized })
ui_watchers[add_window:id()] = watcher
return space
end
function PaperWM:removeWindow(remove_window, skip_new_window_focus)
-- get index of window
local remove_index = index_table[remove_window:id()]
if not remove_index then
self.logger.e("remove index not found")
return
end
if not skip_new_window_focus then -- find nearby window to focus
for _, direction in ipairs({
Direction.DOWN, Direction.UP, Direction.LEFT, Direction.RIGHT
}) do if self:focusWindow(direction, remove_index) then break end end
end
-- remove window
table.remove(window_list[remove_index.space][remove_index.col],
remove_index.row)
if #window_list[remove_index.space][remove_index.col] == 0 then
table.remove(window_list[remove_index.space], remove_index.col)
end
-- remove watcher
ui_watchers[remove_window:id()] = nil
-- update index table
index_table[remove_window:id()] = nil
updateIndexTable(remove_index.space, remove_index.col)
-- remove if space is empty
if #window_list[remove_index.space] == 0 then
window_list[remove_index.space] = nil
end
return remove_index.space -- return space for removed window
end
function PaperWM:focusWindow(direction, focused_index)
if not focused_index then
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return false end
-- get focused window index
focused_index = index_table[focused_window:id()]
end
if not focused_index then
self.logger.e("focused index not found")
return false
end
-- get new focused window
local new_focused_window
if direction == Direction.LEFT or direction == Direction.RIGHT then
-- walk down column, looking for match in neighbor column
for row = focused_index.row, 1, -1 do
new_focused_window = getWindow(focused_index.space,
focused_index.col + direction, row)
if new_focused_window then break end
end
elseif direction == Direction.UP or direction == Direction.DOWN then
new_focused_window = getWindow(focused_index.space, focused_index.col,
focused_index.row + (direction // 2))
end
if not new_focused_window then
self.logger.d("new focused window not found")
return false
end
-- focus new window, windowFocused event will be emited immediately
new_focused_window:focus()
return true
end
function PaperWM:swapWindows(direction)
-- use focused window as source window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
-- get focused window index
local focused_index = index_table[focused_window:id()]
if not focused_index then
self.logger.e("focused index not found")
return
end
if direction == Direction.LEFT or direction == Direction.RIGHT then
-- get target windows
local target_index = { col = focused_index.col + direction }
local target_column = window_list[focused_index.space][target_index.col]
if not target_column then
self.logger.d("target column not found")
return
end
-- swap place in window list
local focused_column =
window_list[focused_index.space][focused_index.col]
window_list[focused_index.space][target_index.col] = focused_column
window_list[focused_index.space][focused_index.col] = target_column
-- update index table
for row, window in ipairs(target_column) do
index_table[window:id()] = {
space = focused_index.space,
col = focused_index.col,
row = row
}
end
for row, window in ipairs(focused_column) do
index_table[window:id()] = {
space = focused_index.space,
col = target_index.col,
row = row
}
end
-- swap frames
local focused_frame = focused_window:frame()
local target_frame = target_column[1]:frame()
if direction == Direction.LEFT then
focused_frame.x = target_frame.x
target_frame.x = focused_frame.x2 + self.window_gap
else -- Direction.RIGHT
target_frame.x = focused_frame.x
focused_frame.x = target_frame.x2 + self.window_gap
end
for _, window in ipairs(target_column) do
local frame = window:frame()
frame.x = target_frame.x
self:moveWindow(window, frame)
end
for _, window in ipairs(focused_column) do
local frame = window:frame()
frame.x = focused_frame.x
self:moveWindow(window, frame)
end
elseif direction == Direction.UP or direction == Direction.DOWN then
-- get target window
local target_index = {
space = focused_index.space,
col = focused_index.col,
row = focused_index.row + (direction // 2)
}
local target_window = getWindow(target_index.space, target_index.col,
target_index.row)
if not target_window then
self.logger.d("target window not found")
return
end
-- swap places in window list
window_list[target_index.space][target_index.col][target_index.row] =
focused_window
window_list[focused_index.space][focused_index.col][focused_index.row] =
target_window
-- update index table
index_table[target_window:id()] = focused_index
index_table[focused_window:id()] = target_index
-- swap frames
local focused_frame = focused_window:frame()
local target_frame = target_window:frame()
if direction == Direction.UP then
focused_frame.y = target_frame.y
target_frame.y = focused_frame.y2 + self.window_gap
else -- Direction.DOWN
target_frame.y = focused_frame.y
focused_frame.y = target_frame.y2 + self.window_gap
end
self:moveWindow(focused_window, focused_frame)
self:moveWindow(target_window, target_frame)
end
-- update layout
self:tileSpace(focused_index.space)
end
function PaperWM:centerWindow()
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
-- get global coordinates
local focused_frame = focused_window:frame()
local screen_frame = focused_window:screen():frame()
-- center window
focused_frame.x = screen_frame.x + (screen_frame.w // 2) -
(focused_frame.w // 2)
self:moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
self:tileSpace(space)
end
function PaperWM:setWindowFullWidth()
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
-- fullscreen window width
local canvas = getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
focused_frame.x, focused_frame.w = canvas.x, canvas.w
self:moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
self:tileSpace(space)
end
function PaperWM:cycleWindowSize(direction)
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
local function findNewSize(area_size, frame_size)
local sizes = {}
for index, ratio in ipairs(self.window_ratios) do
sizes[index] = ratio * (area_size + self.window_gap) - self.window_gap
end
-- find new size
local new_size = sizes[1]
for _, size in ipairs(sizes) do
if size > frame_size + 10 then
new_size = size
break
end
end
return new_size
end
local canvas = getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
if direction == Direction.WIDTH then
local new_width = findNewSize(canvas.w, focused_frame.w)
focused_frame.x = focused_frame.x + ((focused_frame.w - new_width) // 2)
focused_frame.w = new_width
elseif direction == Direction.HEIGHT then
local new_height = findNewSize(canvas.h, focused_frame.h)
focused_frame.y = math.max(canvas.y, focused_frame.y +
((focused_frame.h - new_height) // 2))
focused_frame.h = new_height
focused_frame.y = focused_frame.y -
math.max(0, focused_frame.y2 - canvas.y2)
end
-- apply new size
self:moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
self:tileSpace(space)
end
function PaperWM:slurpWindow()
-- TODO paperwm behavior:
-- add top window from column to the right to bottom of current column
-- if no colum to the right and current window is only window in current column,
-- add current window to bottom of column to the left
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
-- get window index
local focused_index = index_table[focused_window:id()]
if not focused_index then
self.logger.e("focused index not found")
return
end
-- get column to left
local column = window_list[focused_index.space][focused_index.col - 1]
if not column then
self.logger.d("column not found")
return
end
-- remove window
table.remove(window_list[focused_index.space][focused_index.col],
focused_index.row)
if #window_list[focused_index.space][focused_index.col] == 0 then
table.remove(window_list[focused_index.space], focused_index.col)
end
-- append to end of column
table.insert(column, focused_window)
-- update index table
local num_windows = #column
index_table[focused_window:id()] = {
space = focused_index.space,
col = focused_index.col - 1,
row = num_windows
}
updateIndexTable(focused_index.space, focused_index.col)
-- adjust window frames
local canvas = getCanvas(focused_window:screen())
local bounds = {
x = column[1]:frame().x,
x2 = nil,
y = canvas.y,
y2 = canvas.y2
}
local h = math.max(0, canvas.h - ((num_windows - 1) * self.window_gap)) //
num_windows
self:tileColumn(column, bounds, h)
-- update layout
self:tileSpace(focused_index.space)
end
function PaperWM:barfWindow()
-- TODO paperwm behavior:
-- remove bottom window of current column
-- place window into a new column to the right--
-- get current focused window
focused_window = focused_window or Window.focusedWindow()
if not focused_window then return end
-- get window index
local focused_index = index_table[focused_window:id()]
if not focused_index then
self.logger.e("focused index not found")
return
end
-- get column
local column = window_list[focused_index.space][focused_index.col]
if #column == 1 then
self.logger.d("only window in column")
return
end
-- remove window and insert in new column
table.remove(column, focused_index.row)
table.insert(window_list[focused_index.space], focused_index.col + 1,
{ focused_window })
-- update index table
updateIndexTable(focused_index.space, focused_index.col)
-- adjust window frames
local num_windows = #column
local canvas = getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
local bounds = { x = focused_frame.x, x2 = nil, y = canvas.y, y2 = canvas.y2 }
local h = math.max(0, canvas.h - ((num_windows - 1) * self.window_gap)) //
num_windows
focused_frame.y = canvas.y
focused_frame.x = focused_frame.x2 + self.window_gap
focused_frame.h = canvas.h
self:moveWindow(focused_window, focused_frame)
self:tileColumn(column, bounds, h)
-- update layout
self:tileSpace(focused_index.space)
end
function PaperWM:switchToSpace(index)
local space = getSpace(index)
if not space then
self.logger.d("space not found")
return
end
-- find a window to focus in new space
local windows = Spaces.windowsForSpace(space)
for _, id in ipairs(windows) do
local index = index_table[id]
if index then
-- https://github.com/Hammerspoon/hammerspoon/issues/370
-- raise app before focusing window
local window = getWindow(index.space, index.col, index.row)
local app = window:application()
app:activate()
Timer.usleep(10000)
window:focus()
break
end
end
Spaces.gotoSpace(space)
end
function PaperWM:moveWindowToSpace(index)
focused_window = focused_window or Window.focusedWindow()
if not focused_window then
self.logger.d("focused window not found")
return
end
local focused_index = index_table[focused_window:id()]
if not focused_index then
self.logger.e("focused index not found")
return
end
local new_space = getSpace(index)
if not new_space then
self.logger.d("space not found")
return
end
if Spaces.spaceType(new_space) ~= "user" then
self.logger.d("space is invalid")
return
end
local screen = Screen(Spaces.spaceDisplay(new_space))
if not screen then
self.logger.d("screen not found")
return
end
-- cache a local copy, removeWindow() will clear global focused_window
local focused_window = focused_window
local old_space = self:removeWindow(focused_window)
if not old_space then
self.logger.e("can't remove focused window")
return
end
Spaces.moveWindowToSpace(focused_window, new_space)
self:addWindow(focused_window)
self:tileSpace(old_space)
self:tileSpace(new_space)
Spaces.gotoSpace(new_space)
end
function PaperWM:moveWindow(window, frame)
-- greater than 0.017 hs.window animation step time
local padding <const> = 0.02
local watcher = ui_watchers[window:id()]
if not watcher then
self.logger.e("window does not have ui watcher")
return
end
if frame == window:frame() then
self.logger.v("no change in window frame")
return
end
watcher:stop()
window:setFrame(frame)
Timer.doAfter(Window.animationDuration + padding, function()
watcher:start({ Watcher.windowMoved, Watcher.windowResized })
end)
end
PaperWM.actions = {
stop_events = partial(PaperWM.stop, PaperWM),
refresh_windows = partial(PaperWM.refreshWindows, PaperWM),
focus_left = partial(PaperWM.focusWindow, PaperWM, Direction.LEFT),
focus_right = partial(PaperWM.focusWindow, PaperWM, Direction.RIGHT),
focus_up = partial(PaperWM.focusWindow, PaperWM, Direction.UP),
focus_down = partial(PaperWM.focusWindow, PaperWM, Direction.DOWN),
swap_left = partial(PaperWM.swapWindows, PaperWM, Direction.LEFT),
swap_right = partial(PaperWM.swapWindows, PaperWM, Direction.RIGHT),
swap_up = partial(PaperWM.swapWindows, PaperWM, Direction.UP),
swap_down = partial(PaperWM.swapWindows, PaperWM, Direction.DOWN),
center_window = partial(PaperWM.centerWindow, PaperWM),
full_width = partial(PaperWM.setWindowFullWidth, PaperWM),
cycle_width = partial(PaperWM.cycleWindowSize, PaperWM, Direction.WIDTH),
cycle_height = partial(PaperWM.cycleWindowSize, PaperWM, Direction.HEIGHT),
slurp_in = partial(PaperWM.slurpWindow, PaperWM),
barf_out = partial(PaperWM.barfWindow, PaperWM),
switch_space_1 = partial(PaperWM.switchToSpace, PaperWM, 1),
switch_space_2 = partial(PaperWM.switchToSpace, PaperWM, 2),
switch_space_3 = partial(PaperWM.switchToSpace, PaperWM, 3),
switch_space_4 = partial(PaperWM.switchToSpace, PaperWM, 4),
switch_space_5 = partial(PaperWM.switchToSpace, PaperWM, 5),
switch_space_6 = partial(PaperWM.switchToSpace, PaperWM, 6),
switch_space_7 = partial(PaperWM.switchToSpace, PaperWM, 7),
switch_space_8 = partial(PaperWM.switchToSpace, PaperWM, 8),
switch_space_9 = partial(PaperWM.switchToSpace, PaperWM, 9),
move_window_1 = partial(PaperWM.moveWindowToSpace, PaperWM, 1),
move_window_2 = partial(PaperWM.moveWindowToSpace, PaperWM, 2),
move_window_3 = partial(PaperWM.moveWindowToSpace, PaperWM, 3),
move_window_4 = partial(PaperWM.moveWindowToSpace, PaperWM, 4),
move_window_5 = partial(PaperWM.moveWindowToSpace, PaperWM, 5),
move_window_6 = partial(PaperWM.moveWindowToSpace, PaperWM, 6),
move_window_7 = partial(PaperWM.moveWindowToSpace, PaperWM, 7),
move_window_8 = partial(PaperWM.moveWindowToSpace, PaperWM, 8),
move_window_9 = partial(PaperWM.moveWindowToSpace, PaperWM, 9)
}
function PaperWM:bindHotkeys(mapping)
local spec = self.actions
hs.spoons.bindHotkeysToSpec(spec, mapping)
end
return PaperWM