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
local MOD_NAMES = { "Ctrl+Alt", "Ctrl+Shift", "Alt+Shift", "Ctrl+Alt+Shift" }
local MODS = {
["Ctrl+Alt"] = { ctrl = true, alt = true },
["Ctrl+Shift"] = { ctrl = true, shift = true },
["Alt+Shift"] = { alt = true, shift = true },
["Ctrl+Alt+Shift"] = { ctrl = true, alt = true, shift = true },
}
local cfg = UI.Schema({
drag = UI.Toggle(
true,
{ label = "Alt+drag moves, Alt+right-drag resizes", tab = "Drag" }
),
modifier = UI.Select(
"Ctrl+Alt",
MOD_NAMES,
{ label = "Modifier held with every slot key", tab = "Keys" }
),
leftKey = UI.Keybind("Left", { label = "Left half", tab = "Keys" }),
rightKey = UI.Keybind("Right", { label = "Right half", tab = "Keys" }),
fullKey = UI.Keybind("Up", { label = "Maximize", tab = "Keys" }),
restoreKey = UI.Keybind("Down", { label = "Restore", tab = "Keys" }),
pinKey = UI.Keybind("P", { label = "Pin or unpin on top", tab = "Keys" }),
})
local pinned = {}
local dragging = false
local function altOnly()
local m = Input.GetModifiers()
return m.alt and not (m.ctrl or m.shift or m.win)
end
local function chordHeld()
local want = MODS[cfg.modifier] or {}
local have = Input.GetModifiers()
for _, m in { "ctrl", "alt", "shift", "win" } do
if (want[m] == true) ~= have[m] then
return false
end
end
return true
end
local function warn(message)
Log.Warn(message)
UI.Notify(message, "warning")
end
local function drag(resize)
local mx, my = System.Mouse()
local handle = Window.FromPoint(mx, my)
if handle == nil then
return
end
if Window.GetState(handle) == "maximized" then
return
end
local start = Window.GetPos(handle)
dragging = true
Run(function()
while dragging do
local x, y = System.Mouse()
local dx, dy = x - mx, y - my
local ok, err
if resize then
ok, err = pcall(
Window.Move,
handle,
nil,
nil,
math.max(120, start.width + dx),
math.max(80, start.height + dy)
)
else
ok, err = pcall(Window.Move, handle, start.x + dx, start.y + dy)
end
if not ok then
warn(`window could not be moved: {err}`)
break
end
Sleep(8)
end
dragging = false
end)
end
local function endDrag()
dragging = false
HID.Combo("LCtrl")
end
for button, resize in { Mouse1 = false, Mouse2 = true } do
Bind(button, {
when = function()
return cfg.drag and altOnly()
end,
action = function()
drag(resize)
return nil
end,
release = endDrag,
})
end
local function foreground()
local handle = Window.GetForeground()
if handle == nil then
warn("no focused window")
end
return handle
end
local function slot(side)
local handle = foreground()
if handle == nil then
return
end
local ok, err = pcall(function()
local area = Screen.WorkArea(System.Mouse())
if Window.GetState(handle) == "maximized" then
Window.Restore(handle)
end
local half = area.width // 2
local x = if side == "left" then area.x else area.x + half
Window.Move(handle, x, area.y, half, area.height)
end)
if not ok then
warn(`window could not be placed: {err}`)
end
end
local function pin()
local handle = foreground()
if handle == nil then
return
end
local on = not pinned[handle]
local ok, err = pcall(Window.SetAlwaysOnTop, handle, on)
if not ok then
warn(`pin failed: {err}`)
return
end
pinned[handle] = if on then true else nil
UI.Notify(if on then "Pinned on top" else "Unpinned", "info")
end
local KEYS = {
{
cfg.leftKey,
function()
slot("left")
end,
},
{
cfg.rightKey,
function()
slot("right")
end,
},
{
cfg.fullKey,
function()
local handle = foreground()
if handle then
pcall(Window.Maximize, handle)
end
end,
},
{
cfg.restoreKey,
function()
local handle = foreground()
if handle then
pcall(Window.Restore, handle)
end
end,
},
{ cfg.pinKey, pin },
}
for _, entry in KEYS do
Bind(entry[1], {
when = chordHeld,
action = function()
entry[2]()
return nil
end,
})
end
function OnStart()
Log.Info(
`Alt+drag moves. {cfg.modifier} + Left/Right/Up/Down/P slot, maximize, restore, pin.`
)
end
function OnStop()
dragging = false
for handle in pinned do
pcall(Window.SetAlwaysOnTop, handle, false)
end
pinned = {}
end