Repository navigation
Expand file tree
/
Copy pathwindowMovement.lua
More file actions
195 lines (169 loc) · 5.52 KB
/
Copy pathwindowMovement.lua
File metadata and controls
195 lines (169 loc) · 5.52 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
local function windowMovement(wp)
local margin = wp.configuration.windowMargin
local moveWindowSizes = wp.configuration.windowSizes.move
local centerWindowSizes = wp.configuration.windowSizes.center
local function nextWindowPosition(current, presets)
local closest = presets[1]
local minDiff = math.abs(current - closest)
for _, preset in ipairs(presets) do
local diff = math.abs(current - preset)
if diff < minDiff then
closest = preset
minDiff = diff
end
end
local index = hs.fnutils.indexOf(presets, closest)
return presets[(index % #presets) + 1]
end
local function moveWindow(direction)
local win = hs.window.focusedWindow()
if not win then
wp:showNotification("No focused window!")
return
end
local screen = win:screen()
local screenFrame = screen:frame()
local winFrame = win:frame()
local x, y, w, h = screenFrame.x, screenFrame.y, screenFrame.w, screenFrame.h
if direction == "left" then
local currentWidthPercentage = winFrame.w / w
local nextWidth = nextWindowPosition(currentWidthPercentage, moveWindowSizes)
win:setFrame({
x = x + margin,
y = y + margin,
w = (w * nextWidth) - (2 * margin),
h = h - (2 * margin),
}, 0)
elseif direction == "right" then
local currentWidthPercentage = winFrame.w / w
local nextWidth = nextWindowPosition(currentWidthPercentage, moveWindowSizes)
win:setFrame({
x = x + w - (w * nextWidth) + margin,
y = y + margin,
w = (w * nextWidth) - (2 * margin),
h = h - (2 * margin),
}, 0)
elseif direction == "top" then
local currentHeightPercentage = winFrame.h / h
local nextHeight = nextWindowPosition(currentHeightPercentage, moveWindowSizes)
win:setFrame({
x = x + margin,
y = y + margin,
w = w - (2 * margin),
h = (h * nextHeight) - (2 * margin),
}, 0)
elseif direction == "bottom" then
local currentHeightPercentage = winFrame.h / h
local nextHeight = nextWindowPosition(currentHeightPercentage, moveWindowSizes)
win:setFrame({
x = x + margin,
y = y + h - (h * nextHeight) + margin,
w = w - (2 * margin),
h = (h * nextHeight) - (2 * margin),
}, 0)
else
wp:showNotification("Invalid direction! Use 'left', 'right', 'top', or 'bottom'")
end
end
local function moveWindowToScreen(direction)
local win = hs.window.focusedWindow()
if not win then
return
end
local currentScreen = win:screen()
local targetScreen = (direction == "next") and currentScreen:next() or currentScreen:previous()
if targetScreen then
win:moveToScreen(targetScreen, false, true, 0)
end
end
local function moveWindowToSpace(direction)
local win = hs.window.focusedWindow()
if not win then
wp:showNotification("No focused window to move.")
return
end
local screen = win:screen()
local currentSpace = hs.spaces.windowSpaces(win:id())[1]
if not currentSpace then
wp:showNotification("Unable to determine the current space.")
return
end
local spaces = hs.spaces.spacesForScreen(screen)
local spaceIndex = hs.fnutils.indexOf(spaces, currentSpace)
if not spaceIndex then
wp:showNotification("Space not found.")
return
end
local targetSpace
if direction == "next" then
targetSpace = spaces[spaceIndex + 1]
elseif direction == "previous" then
targetSpace = spaces[spaceIndex - 1]
else
return
end
if not targetSpace then
return
end
hs.spaces.moveWindowToSpace(win:id(), targetSpace)
hs.timer.doAfter(0.05, function()
local ctrlKey = 0x3B
local rightArrow = 0x7C
local leftArrow = 0x7B
hs.eventtap.event.newKeyEvent(ctrlKey, true):post()
if direction == "next" then
hs.eventtap.event.newKeyEvent(rightArrow, true):post()
hs.eventtap.event.newKeyEvent(rightArrow, false):post()
elseif direction == "previous" then
hs.eventtap.event.newKeyEvent(leftArrow, true):post()
hs.eventtap.event.newKeyEvent(leftArrow, false):post()
end
hs.eventtap.event.newKeyEvent(ctrlKey, false):post()
end)
end
local function moveWindowCenter()
local win = hs.window.focusedWindow()
if not win then
return
end
local screen = win:screen():frame()
local winFrame = win:frame()
local currentSize = winFrame.w / screen.w
local nextSize = nextWindowPosition(currentSize, centerWindowSizes)
local newWidth = screen.w * nextSize
local newHeight = screen.h * nextSize
local newX = screen.x + (screen.w - newWidth) / 2
local newY = screen.y + (screen.h - newHeight) / 2
win:setFrame({ x = newX, y = newY, w = newWidth, h = newHeight }, 0)
end
return {
moveWindowLeft = function()
moveWindow("left")
end,
moveWindowRight = function()
moveWindow("right")
end,
moveWindowTop = function()
moveWindow("top")
end,
moveWindowBottom = function()
moveWindow("bottom")
end,
moveWindowCenter = function()
moveWindowCenter()
end,
moveWindowToNextScreen = function()
moveWindowToScreen("next")
end,
moveWindowToPreviousScreen = function()
moveWindowToScreen("previous")
end,
moveWindowToNextSpace = function()
moveWindowToSpace("next")
end,
moveWindowToPreviousSpace = function()
moveWindowToSpace("previous")
end,
}
end
return windowMovement