forked from mogenson/PaperWM.spoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows.lua
More file actions
891 lines (762 loc) · 32.8 KB
/
windows.lua
File metadata and controls
891 lines (762 loc) · 32.8 KB
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
local Rect <const> = hs.geometry.rect
local Screen <const> = hs.screen
local Spaces <const> = hs.spaces
local Timer <const> = hs.timer
local Window <const> = hs.window
local Windows = {}
Windows.__index = Windows
---@enum Direction
local Direction <const> = {
LEFT = -1,
RIGHT = 1,
UP = -2,
DOWN = 2,
NEXT = 3,
PREVIOUS = -3,
WIDTH = 4,
HEIGHT = 5,
ASCENDING = 6,
DESCENDING = 7,
}
Windows.Direction = Direction
---initialize module with reference to PaperWM
---@param paperwm PaperWM
function Windows.init(paperwm)
Windows.PaperWM = paperwm
end
---return the first window that's completely on the screen
---@param space Space space to lookup windows
---@param screen_frame Frame the coordinates of the screen
---@pram direction Direction|nil either LEFT or RIGHT
---@return Window|nil
function Windows.getFirstVisibleWindow(space, screen_frame, direction)
direction = direction or Direction.LEFT
local on_screen_distance = math.huge
local on_screen_closest = nil
local off_screen_distance = -math.huge
local off_screen_closest = nil
for _, windows in ipairs(Windows.PaperWM.state.windowList(space)) do
local window = windows[1] -- take first window in column
local d = (function()
if direction == Direction.LEFT then
return window:frame().x - screen_frame.x
elseif direction == Direction.RIGHT then
return screen_frame.x2 - window:frame().x2
end
end)() or math.huge
if d >= 0 and d < on_screen_distance then
on_screen_distance = d
on_screen_closest = window
end
if d < 0 and d > off_screen_distance then
off_screen_distance = d
off_screen_closest = window
end
end
return on_screen_closest or off_screen_closest
end
---get the gap value for the specified side
---@param side string "top", "bottom", "left", or "right"
---@return number gap size in pixels
function Windows.getGap(side)
local gap = Windows.PaperWM.window_gap
if type(gap) == "number" then
return gap -- backward compatibility with single number
elseif type(gap) == "table" then
return gap[side] or 8 -- default to 8 if missing
else
return 8 -- fallback default
end
end
---get the tileable bounds for a screen
---@param screen Screen
---@return Frame
function Windows.getCanvas(screen)
local screen_frame = screen:frame()
local screen_full_frame = screen:fullFrame()
local left_gap = Windows.getGap("left")
local right_gap = Windows.getGap("right")
local top_gap = Windows.getGap("top")
local bottom_gap = Windows.getGap("bottom")
local external_bar = Windows.PaperWM.external_bar
local external_bar_top = external_bar and external_bar.top
local external_bar_bottom = external_bar and external_bar.bottom
local frame_top = external_bar_top and screen_full_frame.y + external_bar_top or screen_frame.y
local frame_bottom = external_bar_bottom and screen_full_frame.y2 - external_bar_bottom or screen_frame.y2
local frame_height = frame_bottom - frame_top
return Rect(
screen_frame.x + left_gap,
frame_top + top_gap,
screen_frame.w - (left_gap + right_gap),
frame_height - (top_gap + bottom_gap)
)
end
---return default width in pixels for a window based on default_width/app_widths config
---@param window Window
---@return number|nil
function Windows.getAppDefaultWidth(window)
local default_width = Windows.PaperWM.default_width
local app_widths = Windows.PaperWM.app_widths
local has_app_widths = type(app_widths) == "table" and next(app_widths) ~= nil
if not has_app_widths and type(default_width) ~= "number" then return end
local ratio = nil
local app = window:application()
if app and has_app_widths then
local app_name = type(app.name) == "function" and app:name() or nil
local bundle_id = type(app.bundleID) == "function" and app:bundleID() or nil
ratio = app_widths[app_name] or app_widths[bundle_id]
end
if type(ratio) ~= "number" then
ratio = default_width
end
if type(ratio) ~= "number" or ratio <= 0 then return end
local screen = window:screen()
if not screen then return end
local canvas = Windows.getCanvas(screen)
local width = math.floor((canvas.w * ratio) + 0.5)
return math.max(1, math.min(canvas.w, width))
end
---get all windows across all spaces and retile them
function Windows.refreshWindows()
-- get all windows across spaces
local all_windows = Windows.PaperWM.window_filter:getWindows()
local retile_spaces = {} -- spaces that need to be retiled
for _, window in ipairs(all_windows) do
local index = Windows.PaperWM.state.windowIndex(window)
if Windows.PaperWM.floating.isFloating(window) then
-- ignore floating windows
elseif not index then
-- add window
local space = Windows.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, don't focus nearby window
Windows.removeWindow(window, true)
local space = Windows.addWindow(window)
if space then retile_spaces[space] = true end
end
end
-- retile spaces
for space, _ in pairs(retile_spaces) do Windows.PaperWM:tileSpace(space) end
end
---add a new window to be tracked and automatically tiled
---@param add_window Window new window to be added
---@return Space|nil space that contains new window
function Windows.addWindow(add_window)
-- A window with no tabs will have a tabCount of 0 or 1
-- A new tab for a window will have tabCount equal to the total number of tabs
-- All existing tabs in a window will have their tabCount reset to 0
-- We can't query whether an exiting hs.window is a tab or not after creation
local apple <const> = "com.apple"
local safari <const> = "com.apple.Safari"
if add_window:tabCount() > 1
and add_window:application():bundleID():sub(1, #apple) == apple
and add_window:application():bundleID():sub(1, #safari) ~= safari then
-- It's mostly built-in Apple apps like Finder and Terminal whose tabs
-- show up as separate windows. Third party apps like Microsoft Office
-- use tabs that are all contained within one window and tile fine.
hs.notify.show("PaperWM", "Windows with tabs are not supported!",
"See https://github.com/mogenson/PaperWM.spoon/issues/39")
return
end
-- ignore windows that have a zoom button, but are not maximizable
local allow_non_maximizable = Windows.PaperWM.allow_non_maximizable_window
and Windows.PaperWM.allow_non_maximizable_window(add_window)
if not allow_non_maximizable and not add_window:isMaximizable() then
Windows.PaperWM.logger.d("ignoring non-maximizable window")
return
end
-- check if window is already in window list
if Windows.PaperWM.state.windowIndex(add_window) then return end
local space = Spaces.windowSpaces(add_window)[1]
if not space then
Windows.PaperWM.logger.e("add window does not have a space")
return
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 Windows.PaperWM.state.prev_focused_window and
((Windows.PaperWM.state.windowIndex(Windows.PaperWM.state.prev_focused_window) or {}).space == space) and
(Windows.PaperWM.state.prev_focused_window:id() ~= add_window:id()) then -- insert to the right
add_column = Windows.PaperWM.state.windowIndex(Windows.PaperWM.state.prev_focused_window).col + 1
else
local x = add_window:frame().center.x
for col, windows in ipairs(Windows.PaperWM.state.windowList(space)) do
if x < windows[1]:frame().center.x then
add_column = col -- insert left of window
break -- add_window will take this window's column
else -- everything after insert column will be pushed right
add_column = col + 1 -- insert right of window
end
end
end
-- add window
table.insert(Windows.PaperWM.state.windowList(space), add_column, { add_window })
local default_width = Windows.getAppDefaultWidth(add_window)
if default_width then
local frame = add_window:frame()
frame.w = default_width
Windows.moveWindow(add_window, frame)
end
-- subscribe to window moved events
Windows.PaperWM.state.uiWatcherCreate(add_window)
Windows.PaperWM.logger.df("adding window: %s (%d)", add_window:title(), add_window:id())
return space
end
---remove a window from being tracked and automatically tiled
---@param remove_window Window window to be removed
---@param skip_new_window_focus boolean|nil don't focus a nearby window if true
---@return Space|nil space that contained removed window
function Windows.removeWindow(remove_window, skip_new_window_focus)
-- get index of window and remove
local remove_index = Windows.PaperWM.state.windowIndex(remove_window, true)
if not remove_index then
Windows.PaperWM.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 Windows.focusWindow(direction, remove_index) then break end end
end
-- remove window
if remove_window ~= table.remove(
Windows.PaperWM.state.windowList(remove_index.space, remove_index.col), remove_index.row)
then
Windows.PaperWM.logger.ef("removed window %s (%d) doesn't match", remove_window:title(), remove_window:id())
end
-- remove watcher
Windows.PaperWM.state.uiWatcherDelete(remove_window:id())
-- clear window position
Windows.PaperWM.state.xPositions(remove_index.space)[remove_window:id()] = nil
-- clear dangling reference
if Windows.PaperWM.state.prev_focused_window == remove_window then
Windows.PaperWM.state.prev_focused_window = nil
end
Windows.PaperWM.logger.df("removing window: %s (%d)", remove_window:title(), remove_window:id())
return remove_index.space -- return space for removed window
end
---move focus to a new window next to the currently focused window
---@param direction Direction use either Direction UP, DOWN, LEFT, or RIGHT
---@param focused_index Index index of focused window within the windowList
---@return Window?
function Windows.focusWindow(direction, focused_index)
if not focused_index then
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
-- get focused window index
focused_index = Windows.PaperWM.state.windowIndex(focused_window)
end
if not focused_index then
Windows.PaperWM.logger.e("focused index not found")
return
end
-- get new focused window
local new_focused_window = nil
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 = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col + direction, row)
if new_focused_window then break end
end
-- wrap around: if no window found, go to the opposite end
if not new_focused_window and Windows.PaperWM.infinite_loop_window then
local columns = Windows.PaperWM.state.windowList(focused_index.space)
local num_cols = columns and #columns or 0
if num_cols > 1 then
local wrap_col = direction == Direction.LEFT and num_cols or 1
for row = focused_index.row, 1, -1 do
new_focused_window = columns[wrap_col][row]
if new_focused_window then
local windows = table.remove(columns, wrap_col)
table.insert(columns, wrap_col == 1 and num_cols or 1, windows) -- insert wrap column at beginging or end
Windows.PaperWM:tileSpace(focused_index.space) -- tile before focusing to move wrap column
break
end
end
end
end
elseif direction == Direction.UP or direction == Direction.DOWN then
local target_row = focused_index.row + (direction // 2)
new_focused_window = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col, target_row)
-- wrap around: if no window found, go to the opposite end
if not new_focused_window and Windows.PaperWM.infinite_loop_window then
local column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
local num_rows = column and #column or 0
if num_rows > 1 then
new_focused_window = column[direction == Direction.UP and num_rows or 1]
end
end
elseif direction == Direction.NEXT or direction == Direction.PREVIOUS then
local diff = direction // Direction.NEXT -- convert to 1/-1
-- first try above/below in same row
new_focused_window = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col,
focused_index.row + diff)
if not new_focused_window then
-- get the bottom row in the previous column, or the first row in the next column
local adjacent_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col + diff)
if adjacent_column then
local col_idx = 1
if diff < 0 then col_idx = #adjacent_column end
new_focused_window = adjacent_column[col_idx]
end
end
end
if not new_focused_window then
Windows.PaperWM.logger.d("new focused window not found")
return
end
-- focus new window, windowFocused event will be emited immediately
new_focused_window:focus()
-- try to prevent MacOS from stealing focus away to another window
Timer.doAfter(Window.animationDuration, function()
if Window.focusedWindow() ~= new_focused_window then
Windows.PaperWM.logger.df("refocusing window %s", new_focused_window)
new_focused_window:focus()
end
end)
return new_focused_window
end
---focus a window at a specified position
---@param n number window number from left to right and up to down on the current screen
function Windows.focusWindowAt(n)
local screen = Screen.mainScreen()
local space = Spaces.activeSpaces()[screen:getUUID()]
local columns = Windows.PaperWM.state.windowList(space)
if #columns == 0 then return end
local i = 1
for col = 1, #columns do
local column = columns[col]
for row = 1, #column do
if i == n then
column[row]:focus()
return
end
i = i + 1
end
end
end
---focus the first window in the current space
function Windows.focusWindowFirst()
local space = Spaces.focusedSpace()
local window = Windows.PaperWM.state.windowList(space, 1, 1)
if window then window:focus() end
end
---focus the last window in the current space
function Windows.focusWindowLast()
local space = Spaces.focusedSpace()
local columns = Windows.PaperWM.state.windowList(space)
if #columns == 0 then return end
local rows = columns[#columns]
if #rows == 0 then return end
rows[#rows]:focus()
end
---swap the focused window with a window next to it
---if swapping horizontally and the adjacent window is in a column, swap the
---entire column. if swapping vertically and the focused window is in a column,
---swap positions within the column
---@param direction Direction use Direction LEFT, RIGHT, UP, or DOWN
function Windows.swapWindows(direction)
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
local focused_index = Windows.PaperWM.state.windowIndex(focused_window)
if not focused_index then
Windows.PaperWM.logger.e("focused index not found")
return
end
if direction == Direction.LEFT or direction == Direction.RIGHT then
local columns = Windows.PaperWM.state.windowList(focused_index.space)
if not columns then
Windows.PaperWM.logger.ef("no windows on space %d", focused_index.space)
return
end
local current_column = focused_index.col
if not columns[current_column] then
Windows.PaperWM.logger.ef("no current column %d on space %d", current_column, focused_index.space)
return
end
local target_column = focused_index.col + direction
if not columns[target_column] then
Windows.PaperWM.logger.ef("no target column %d on space %d", target_column, focused_index.space)
return
end
-- move focused window to target column location
local focused_frame = focused_window:frame()
local target_frame = columns[target_column][1]:frame()
focused_frame.x = target_frame.x
Windows.moveWindow(focused_window, focused_frame)
-- remove then insert column of windows to swap
local windows = table.remove(columns, current_column)
table.insert(columns, target_column, windows)
elseif direction == Direction.UP or direction == Direction.DOWN then
local windows = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
if not windows then
Windows.PaperWM.logger.ef("no windows in column %d on space %d", focused_index.col, focused_index.space)
return
end
local current_row = focused_index.row
local target_row = focused_index.row + (direction // 2)
-- remove and insert to swap
local window = table.remove(windows, current_row)
table.insert(windows, target_row, window)
end
-- update layout
Windows.PaperWM:tileSpace(focused_index.space)
end
---move the focused window to the center of the screen, horizontally
---don't resize the window or change it's vertical position
function Windows.centerWindow()
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
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)
Windows.moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
Windows.PaperWM:tileSpace(space)
end
---move the focused window to the left or right edge of the canvas
---this makes the focused window the left or right anchor of the tiled viewport
---@param direction Direction use Direction.LEFT or Direction.RIGHT
function Windows.moveToSideEdge(direction)
if direction ~= Direction.LEFT and direction ~= Direction.RIGHT then
Windows.PaperWM.logger.e("direction must be either Direction.LEFT or Direction.RIGHT")
return
end
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
local screen = focused_window:screen()
if not screen then
Windows.PaperWM.logger.d("focused window screen not found")
return
end
local focused_frame = focused_window:frame()
local canvas = Windows.getCanvas(screen)
if direction == Direction.LEFT then
focused_frame.x = canvas.x
else
focused_frame.x = math.max(canvas.x, (canvas.x + canvas.w) - focused_frame.w)
end
Windows.moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
Windows.PaperWM:tileSpace(space)
end
---set the focused window to the width of the screen and cache the original width
---restore the original window size if called again, don't change the height
function Windows.toggleWindowFullWidth()
local width_cache = {}
return function(self)
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
self.logger.d("focused window not found")
return
end
local canvas = Windows.getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
local id = focused_window:id()
local width = width_cache[id]
if width then
-- restore window width
focused_frame.x = canvas.x + ((canvas.w - width) / 2)
focused_frame.w = width
width_cache[id] = nil
else
-- set window to fullscreen width
width_cache[id] = focused_frame.w
focused_frame.x, focused_frame.w = canvas.x, canvas.w
end
-- update layout
Windows.moveWindow(focused_window, focused_frame)
local space = Spaces.windowSpaces(focused_window)[1]
Windows.PaperWM:tileSpace(space)
end
end
---resize the width or height of the window, keeping the other dimension the
---same. cycles through the ratios specified in PaperWM.window_ratios
---@param direction Direction use Direction.WIDTH or Direction.HEIGHT
---@param cycle_direction Direction use Direction.ASCENDING or DESCENDING
function Windows.cycleWindowSize(direction, cycle_direction)
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
local function findNewSize(area_size, frame_size, cycle_direction, dimension)
local gap
if dimension == Direction.WIDTH then
-- For width, use the average of left and right gaps
gap = (Windows.getGap("left") + Windows.getGap("right")) / 2
else
-- For height, use the average of top and bottom gaps
gap = (Windows.getGap("top") + Windows.getGap("bottom")) / 2
end
local sizes = {}
local new_size = nil
if cycle_direction == Direction.ASCENDING then
for index, ratio in ipairs(Windows.PaperWM.window_ratios) do
sizes[index] = ratio * (area_size + gap) - gap
end
-- find new size
new_size = sizes[1]
for _, size in ipairs(sizes) do
if size > frame_size + 10 then
new_size = size
break
end
end
elseif cycle_direction == Direction.DESCENDING then
for index, ratio in ipairs(Windows.PaperWM.window_ratios) do
sizes[index] = ratio * (area_size + gap) - gap
end
-- find new size, starting from the end
new_size = sizes[#sizes] -- Start with the largest size
for i = #sizes, 1, -1 do
if sizes[i] < frame_size - 10 then
new_size = sizes[i]
break
end
end
else
Windows.PaperWM.logger.e(
"cycle_direction must be either Direction.ASCENDING or Direction.DESCENDING")
end
return new_size
end
local canvas = Windows.getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
if direction == Direction.WIDTH then
local new_width = findNewSize(canvas.w, focused_frame.w, cycle_direction, Direction.WIDTH)
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, cycle_direction, Direction.HEIGHT)
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)
else
Windows.PaperWM.logger.e(
"direction must be either Direction.WIDTH or Direction.HEIGHT")
return
end
-- apply new size
Windows.moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
Windows.PaperWM:tileSpace(space)
end
---resize the focused window in a direction by scale amount
---@param direction Direction Direction.WIDTH or Direction.HEIGHT
---@param scale number the percent to change the window size by
function Windows.increaseWindowSize(direction, scale)
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
local canvas = Windows.getCanvas(focused_window:screen())
local focused_frame = focused_window:frame()
if direction == Direction.WIDTH then
local diff = canvas.w * 0.1 * scale
local new_size = math.max(diff, math.min(canvas.w, focused_frame.w + diff))
focused_frame.w = new_size
focused_frame.x = focused_frame.x + ((focused_frame.w - new_size) // 2)
elseif direction == Direction.HEIGHT then
local diff = canvas.h * 0.1 * scale
local new_size = math.max(diff, math.min(canvas.h, focused_frame.h + diff))
focused_frame.h = new_size
focused_frame.y = focused_frame.y -
math.max(0, focused_frame.y2 - canvas.y2)
end
-- apply new size
Windows.moveWindow(focused_window, focused_frame)
-- update layout
local space = Spaces.windowSpaces(focused_window)[1]
Windows.PaperWM:tileSpace(space)
end
---tile a column of windows so they each have an equal height
---@param windows Window[]
local function tile_column_equaly(windows)
-- final column frames should be equal in height
local first_window = windows[1]
local num_windows = #windows
local canvas = Windows.getCanvas(first_window:screen())
local bottom_gap = Windows.getGap("bottom")
local bounds = {
x = first_window:frame().x,
x2 = nil,
y = canvas.y,
y2 = canvas.y2,
}
local h = math.max(0, canvas.h - ((num_windows - 1) * bottom_gap)) // num_windows
Windows.PaperWM.tiling.tileColumn(windows, bounds, h)
end
---take the current focused window and move it into the bottom of
---the column to the left
function Windows.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
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
-- get window index
local focused_index = Windows.PaperWM.state.windowIndex(focused_window)
if not focused_index then
Windows.PaperWM.logger.e("focused index not found")
return
end
-- get current column
local current_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
if not current_column then
Windows.PaperWM.logger.ef("current column %d not found on space %d", focused_index.col, focused_index.space)
return
end
-- get column to left
local target_index = focused_index.col - 1
local target_column = Windows.PaperWM.state.windowList(focused_index.space, target_index)
if not target_column then
Windows.PaperWM.logger.df("target column %d not found on space %d", target_index, focused_index.space)
return
end
-- remove window and append to end of target column
assert(focused_window == table.remove(current_column, focused_index.row))
table.insert(target_column, focused_window)
-- final column frames should be equal in height
local final_column = Windows.PaperWM.state.windowList(focused_index.space, target_index)
tile_column_equaly(final_column)
-- update layout
Windows.PaperWM:tileSpace(focused_index.space)
end
---remove focused window from it's current column and place into
---a new column to the right
function Windows.barfWindow()
-- TODO paperwm behavior:
-- remove bottom window of current column
-- place window into a new column to the right--
-- get current focused window
local focused_window = Window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
-- get window index
local focused_index = Windows.PaperWM.state.windowIndex(focused_window)
if not focused_index then
Windows.PaperWM.logger.e("focused index not found")
return
end
-- get column
local current_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
if not current_column then
Windows.PaperWM.logger.ef("current column %d not found on space %d", focused_index.col, focused_index.space)
return
elseif #current_column == 1 then
Windows.PaperWM.logger.d("only window in column")
return
end
-- remove window and insert in new column
local target_column = focused_index.col + 1
assert(focused_window == table.remove(current_column, focused_index.row))
table.insert(Windows.PaperWM.state.windowList(focused_index.space), target_column, { focused_window })
-- move focused window to target column location
local focused_frame = focused_window:frame()
focused_frame.x = focused_frame.x2 + Windows.getGap("right")
Windows.moveWindow(focused_window, focused_frame)
-- remaining column frames should be equal in height
local final_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
tile_column_equaly(final_column)
-- update layout
Windows.PaperWM:tileSpace(focused_index.space)
end
---evenly split screen the current column and the left column horizontally
function Windows.splitScreen()
local focused_window = hs.window.focusedWindow()
if not focused_window then
Windows.PaperWM.logger.d("focused window not found")
return
end
local focused_index = Windows.PaperWM.state.windowIndex(focused_window)
if not focused_index then
Windows.PaperWM.logger.e("focused index not found")
return
end
-- get left column
local left_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col - 1)
if not left_column then
Windows.PaperWM.logger.d("left column not found")
return
end
-- get current column
local current_column = Windows.PaperWM.state.windowList(focused_index.space, focused_index.col)
if not current_column then
Windows.PaperWM.logger.ef("current column %d not found on space %d", focused_index.col, focused_index.space)
return
end
local canvas = Windows.PaperWM.windows.getCanvas(focused_window:screen())
local half_width = canvas.w // 2
local left_bounds = {
x = canvas.x,
y = canvas.y,
y2 = canvas.y2,
}
local current_bounds = {
x = canvas.x + half_width,
y = canvas.y,
y2 = canvas.y2,
}
Windows.PaperWM.tiling.tileColumn(left_column, left_bounds, nil, half_width - Windows.PaperWM.windows.getGap("left"))
Windows.PaperWM.tiling.tileColumn(current_column, current_bounds, nil, half_width)
Windows.PaperWM.tiling.tileSpace(focused_index.space)
end
---move and resize a window to the coordinates specified by the frame
---disable watchers while window is moving and re-enable after
---@param window Window window to move
---@param frame Frame coordinates to set window size and location
function Windows.moveWindow(window, frame)
-- greater than 0.017 hs.window animation step time
local padding <const> = 0.02
local id = window:id()
if frame == window:frame() then
Windows.PaperWM.logger.v("no change in window frame")
return
end
Windows.PaperWM.state.uiWatcherStop(id)
window:setFrame(frame)
Timer.doAfter(Window.animationDuration + padding, function()
Windows.PaperWM.state.uiWatcherStart(id)
end)
end
return Windows