-
Notifications
You must be signed in to change notification settings - Fork 6
/
winsnap.cpp
379 lines (328 loc) · 11.8 KB
/
winsnap.cpp
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
/*
* Sticky Window Snapper Class
* Copyright (C) 2021-2024 Pedro López-Cabanillas <[email protected]>
* Copyright (C) 2014 mmbob (Nicholas Cook)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <dwmapi.h>
#include "winsnap.h"
Edge::Edge(int position, int start, int end) : Position(position), Start(start), End(end)
{ }
bool Edge::operator ==(const Edge& other) const
{
return Position == other.Position && Start == other.Start && End == other.End;
}
bool WinSnap::HandleMessage(void *message)
{
MSG* msg = static_cast<MSG*>(message);
if (this->m_enabled) {
this->m_window = msg->hwnd;
switch (msg->message)
{
case WM_SIZING:
return HandleSizing(*reinterpret_cast<RECT*>(msg->lParam), static_cast<int>(msg->wParam));
case WM_MOVING:
return HandleMoving(*reinterpret_cast<RECT*>(msg->lParam));
case WM_ENTERSIZEMOVE:
return HandleEnterSizeMove();
case WM_EXITSIZEMOVE:
return HandleExitSizeMove();
default:
break;
}
}
return false;
}
bool WinSnap::IsEnabled() const
{
return m_enabled;
}
void WinSnap::SetEnabled(const bool enabled)
{
m_enabled = enabled;
}
bool WinSnap::HandleEnterSizeMove()
{
for(auto& edgeList : m_edges)
edgeList.clear();
// Pass "this" as the parameter
EnumDisplayMonitors(nullptr, nullptr, [](HMONITOR monitorHandle, HDC, LPRECT, LPARAM param) -> BOOL
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(monitorInfo);
if (GetMonitorInfo(monitorHandle, &monitorInfo) == 0)
return false;
// AddRectToEdges adds edges for the outside of a rectangle, which works well
// for windows. For monitors, however, we want to snap to the inside, so we
// swap the opposite edges.
std::swap(monitorInfo.rcWork.left, monitorInfo.rcWork.right);
std::swap(monitorInfo.rcWork.top, monitorInfo.rcWork.bottom);
reinterpret_cast<WinSnap*>(param)->AddRectToEdges(monitorInfo.rcWork);
return true;
}, (LPARAM) this);
struct Param
{
WinSnap* _this;
std::list<HRGN> windowRegions;
};
Param param;
param._this = this;
EnumWindows([](HWND windowHandle, LPARAM _param) -> BOOL
{
// We don't want non-application windows or application windows the user can't see.
auto param = reinterpret_cast<Param*>(_param);
if (windowHandle == param->_this->m_window)
return true;
if (!IsWindowVisible(windowHandle))
return true;
if (IsIconic(windowHandle))
return true;
int styles = (int) GetWindowLongPtr(windowHandle, GWL_STYLE);
if ((styles & WS_CHILD) != 0) // || (styles & WS_CAPTION) == 0) FramelessWindows don't have caption
return true;
int extendedStyles = (int) GetWindowLongPtr(windowHandle, GWL_EXSTYLE);
if (/*(extendedStyles & WS_EX_TOOLWINDOW) != 0 ||*/ (extendedStyles & WS_EX_NOACTIVATE) != 0)
return true;
// Ignore the window class 'ApplicationFrameWindow'
if (GetClassWord(windowHandle, GCW_ATOM) == 0xC194)
return true;
RECT thisRect;
GetWindowRect(windowHandle, &thisRect);
HRGN thisRegion = CreateRectRgnIndirect(&thisRect);
// Since EnumWindows enumerates from the highest z-order to the lowest, we
// can just check to see if this window is covered by any previous ones.
bool isUserVisible = true;
for(HRGN region : param->windowRegions)
{
if (CombineRgn(thisRegion, thisRegion, region, RGN_DIFF) == NULLREGION)
{
isUserVisible = false;
break;
}
}
if (isUserVisible)
{
param->windowRegions.push_back(thisRegion);
// Maximized windows by definition cover the whole work area, so the
// only snap edges they would have are on the outside of the monitor.
if (!IsZoomed(windowHandle)) {
RECT frame, border;
if (S_OK == DwmGetWindowAttribute(windowHandle, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT))) {
border.left = frame.left - thisRect.left;
border.top = frame.top - thisRect.top;
border.right = thisRect.right - frame.right;
border.bottom = thisRect.bottom - frame.bottom;
thisRect.left += border.left;
thisRect.top += border.top;
thisRect.right -= border.right;
thisRect.bottom -= border.bottom;
}
param->_this->AddRectToEdges(thisRect);
}
}
else
DeleteObject(thisRegion);
return true;
}, (LPARAM) ¶m);
for (HRGN region : param.windowRegions)
{
DeleteObject(region);
}
for (auto& edgeList : m_edges)
{
edgeList.sort([](const Edge& _1, const Edge& _2) -> bool { return _1.Position < _2.Position; });
edgeList.erase(std::unique(edgeList.begin(), edgeList.end()), edgeList.end());
}
RECT bounds, frame;
GetWindowRect(m_window, &bounds);
GetCursorPos(&m_originalCursorOffset);
m_originalCursorOffset.x -= bounds.left;
m_originalCursorOffset.y -= bounds.top;
if (S_OK == DwmGetWindowAttribute(m_window, DWMWA_EXTENDED_FRAME_BOUNDS, &frame, sizeof(RECT))) {
m_border.left = frame.left - bounds.left;
m_border.top = frame.top - bounds.top;
m_border.right = bounds.right - frame.right;
m_border.bottom = bounds.bottom - frame.bottom;
}
return true;
}
bool WinSnap::HandleExitSizeMove()
{
m_inProgress = false;
return true;
}
bool WinSnap::HandleMoving(RECT& bounds)
{
// The difference between the cursor position and the top-left corner of the dragged window.
// This is normally constant while dragging a window, but when near an edge that we snap to,
// this changes.
POINT cursorOffset;
GetCursorPos(&cursorOffset);
cursorOffset.x -= bounds.left;
cursorOffset.y -= bounds.top;
// While we are snapping a window, the window displayed to the user is not the "real" location
// of the window, i.e. where it would be if we hadn't snapped it.
RECT realBounds;
if (m_inProgress)
{
POINT offsetDiff{ cursorOffset.x - m_originalCursorOffset.x, cursorOffset.y - m_originalCursorOffset.y };
SetRect(&realBounds, bounds.left + offsetDiff.x, bounds.top + offsetDiff.y,
bounds.right + offsetDiff.x, bounds.bottom + offsetDiff.y);
}
else
realBounds = bounds;
int boundsEdges[Side::Count]{ realBounds.left, realBounds.top, realBounds.right, realBounds.bottom };
int snapEdges[Side::Count]{ SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX };
bool snapDirections[Side::Count]{ false, false, false, false };
for (int i = 0; i < Side::Count; ++i)
{
int snapPosition;
if (CanSnapEdge(boundsEdges, (Side) i, &snapPosition))
{
snapDirections[i] = true;
snapEdges[i] = snapPosition;
}
}
if ((GetKeyState(VK_SHIFT) & 0x8000) == 0 && (snapDirections[0] || snapDirections[1] || snapDirections[2] || snapDirections[3]))
{
if (!m_inProgress)
m_inProgress = true;
RECT snapRect = { snapEdges[0] - m_border.left, snapEdges[1] - m_border.top, snapEdges[2] + m_border.right, snapEdges[3] + m_border.bottom };
SnapToRect(&bounds, snapRect, true, snapDirections[0], snapDirections[1], snapDirections[2], snapDirections[3]);
return true;
}
else if (m_inProgress)
{
m_inProgress = false;
bounds = realBounds;
return true;
}
return false;
}
bool WinSnap::HandleSizing(RECT& bounds, int which)
{
bool allowSnap[Side::Count]{ true, true, true, true };
allowSnap[Side::Left] = (which == WMSZ_LEFT || which == WMSZ_TOPLEFT || which == WMSZ_BOTTOMLEFT);
allowSnap[Side::Top] = (which == WMSZ_TOP || which == WMSZ_TOPLEFT || which == WMSZ_TOPRIGHT);
allowSnap[Side::Right] = (which == WMSZ_RIGHT || which == WMSZ_TOPRIGHT || which == WMSZ_BOTTOMRIGHT);
allowSnap[Side::Bottom] = (which == WMSZ_BOTTOM || which == WMSZ_BOTTOMLEFT || which == WMSZ_BOTTOMRIGHT);
// The difference between the cursor position and the top-left corner of the dragged window.
// This is normally constant while dragging a window, but when near an edge that we snap to,
// this changes.
POINT cursorOffset;
GetCursorPos(&cursorOffset);
cursorOffset.x -= bounds.left;
cursorOffset.y -= bounds.top;
int boundsEdges[Side::Count]{ bounds.left, bounds.top, bounds.right, bounds.bottom };
int snapEdges[Side::Count]{ SHRT_MIN, SHRT_MIN, SHRT_MAX, SHRT_MAX };
bool snapDirections[Side::Count]{ false, false, false, false };
for (int i = 0; i < Side::Count; ++i)
{
if (!allowSnap[i])
continue;
int snapPosition;
if (CanSnapEdge(boundsEdges, (Side) i, &snapPosition))
{
snapDirections[i] = true;
snapEdges[i] = snapPosition;
}
}
if ((GetKeyState(VK_SHIFT) & 0x8000) == 0 && (snapDirections[0] || snapDirections[1] || snapDirections[2] || snapDirections[3]))
{
if (!m_inProgress)
m_inProgress = true;
RECT snapRect = { snapEdges[0] - m_border.left, snapEdges[1] - m_border.top, snapEdges[2] + m_border.right, snapEdges[3] + m_border.bottom };
SnapToRect(&bounds, snapRect, false, snapDirections[0], snapDirections[1], snapDirections[2], snapDirections[3]);
return true;
}
else if (m_inProgress)
{
m_inProgress = false;
return true;
}
return false;
}
// Breaks down a rect into 4 edges which are added to the global list of edges to snap to.
void WinSnap::AddRectToEdges(const RECT& rect)
{
int startX = std::min<>(rect.left, rect.right);
int endX = std::max<>(rect.left, rect.right);
int startY = std::min<>(rect.top, rect.bottom);
int endY = std::max<>(rect.top, rect.bottom);
m_edges[Side::Left].push_front(Edge(rect.right, startY, endY));
m_edges[Side::Right].push_front(Edge(rect.left, startY, endY));
m_edges[Side::Top].push_front(Edge(rect.bottom, startX, endX));
m_edges[Side::Bottom].push_front(Edge(rect.top, startX, endX));
}
void WinSnap::SnapToRect(RECT* bounds, const RECT& rect, bool retainSize, bool left, bool top, bool right, bool bottom) const
{
if (left && right)
{
bounds->left = rect.left;
bounds->right = rect.right;
}
else if (left)
{
if (retainSize)
bounds->right += rect.left - bounds->left;
bounds->left = rect.left;
}
else if (right)
{
if (retainSize)
bounds->left += rect.right - bounds->right;
bounds->right = rect.right;
}
if (top && bottom)
{
bounds->top = rect.top;
bounds->bottom = rect.bottom;
}
else if (top)
{
if (retainSize)
bounds->bottom += rect.top - bounds->top;
bounds->top = rect.top;
}
else if (bottom)
{
if (retainSize)
bounds->top += rect.bottom - bounds->bottom;
bounds->bottom = rect.bottom;
}
}
bool WinSnap::CanSnapEdge(int boundsEdges[Side::Count], Side which, int* snapPosition) const
{
for (const Edge& edge : m_edges[which])
{
int edgeDistance = edge.Position - boundsEdges[which];
// Since each edge list is sorted, if the snap edge is past our bound's edge, we know that none of the other edges will work.
if (edgeDistance >= SNAP_DISTANCE)
break;
else if (edgeDistance > -SNAP_DISTANCE)
{
// The modulos get the position of the edges perpendicular to the bound edge we are working on.
int min = std::min<>(boundsEdges[(which + 1) % Side::Count], boundsEdges[(which + 3) % Side::Count]);
int max = std::max<>(boundsEdges[(which + 1) % Side::Count], boundsEdges[(which + 3) % Side::Count]);
if (max > edge.Start && min < edge.End)
{
*snapPosition = edge.Position;
return true;
}
}
}
return false;
}