-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThumbnailWindow.cpp
More file actions
487 lines (406 loc) · 18.2 KB
/
ThumbnailWindow.cpp
File metadata and controls
487 lines (406 loc) · 18.2 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
#include "ThumbnailWindow.h"
#include "Strings.h"
#include <windowsx.h>
const wchar_t* ThumbnailWindow::ClassName = L"ZenCrop.ThumbnailHost";
static std::once_flag s_thumbnailClassReg;
void ThumbnailWindow::RegisterWindowClass() {
WNDCLASSEXW wcex = { sizeof(wcex) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = GetModuleHandleW(nullptr);
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszClassName = ClassName;
wcex.hIcon = LoadIconW(GetModuleHandleW(nullptr), MAKEINTRESOURCE(1));
RegisterClassExW(&wcex);
}
ThumbnailWindow::ThumbnailWindow(HWND targetWindow, RECT cropRect, bool showTitlebar)
: m_targetWindow(targetWindow), m_showTitlebar(showTitlebar) {
std::call_once(s_thumbnailClassReg, []() { RegisterWindowClass(); });
SaveOriginalState();
// We must calculate m_sourceRect BEFORE applying hidden state,
// because ApplyHiddenState moves the target window and changes its rect!
RECT windowRect = { 0 };
if (FAILED(DwmGetWindowAttribute(m_targetWindow, DWMWA_EXTENDED_FRAME_BOUNDS, &windowRect, sizeof(windowRect)))) {
GetWindowRect(m_targetWindow, &windowRect);
}
m_sourceRect.left = cropRect.left - windowRect.left;
m_sourceRect.top = cropRect.top - windowRect.top;
m_sourceRect.right = cropRect.right - windowRect.left;
m_sourceRect.bottom = cropRect.bottom - windowRect.top;
ApplyHiddenState();
int width = cropRect.right - cropRect.left;
int height = cropRect.bottom - cropRect.top;
DWORD style = WS_POPUP | WS_CLIPCHILDREN | WS_THICKFRAME;
if (showTitlebar) {
style |= WS_CAPTION | WS_SYSMENU;
}
DWORD exStyle = 0;
int totalWidth = width + BorderWidth * 2;
int totalHeight = height + BorderWidth * 2;
int windowWidth = totalWidth;
int windowHeight = totalHeight;
if (showTitlebar) {
RECT adjustedRect = { 0, 0, totalWidth, totalHeight };
AdjustWindowRectEx(&adjustedRect, style, FALSE, exStyle);
windowWidth = adjustedRect.right - adjustedRect.left;
windowHeight = adjustedRect.bottom - adjustedRect.top;
}
m_hostWindow = CreateWindowExW(
exStyle, ClassName, S::ThumbnailTitle(), style,
cropRect.left, cropRect.top, windowWidth, windowHeight,
nullptr, nullptr, GetModuleHandleW(nullptr), this
);
if (SUCCEEDED(DwmRegisterThumbnail(m_hostWindow, m_targetWindow, &m_thumbnail))) {
UpdateThumbnail();
} else {
DestroyWindow(m_hostWindow);
m_hostWindow = nullptr;
}
ShowWindow(m_hostWindow, SW_SHOW);
UpdateWindow(m_hostWindow);
}
ThumbnailWindow::~ThumbnailWindow() {
RestoreOriginalState();
if (m_thumbnail) {
DwmUnregisterThumbnail(m_thumbnail);
m_thumbnail = nullptr;
}
if (m_hostWindow) {
DestroyWindow(m_hostWindow);
m_hostWindow = nullptr;
}
}
void ThumbnailWindow::SaveOriginalState() {
m_originalStyle = GetWindowLongPtrW(m_targetWindow, GWL_STYLE);
m_originalExStyle = GetWindowLongPtrW(m_targetWindow, GWL_EXSTYLE);
GetWindowRect(m_targetWindow, &m_originalRect);
m_originalPlacement = { sizeof(WINDOWPLACEMENT) };
if (GetWindowPlacement(m_targetWindow, &m_originalPlacement)) {
m_wasMaximized = (m_originalPlacement.showCmd == SW_SHOWMAXIMIZED);
}
if (m_originalExStyle & WS_EX_LAYERED) {
m_wasLayered = true;
GetLayeredWindowAttributes(m_targetWindow, &m_originalColorKey, &m_originalAlpha, &m_originalLayeredFlags);
} else {
m_wasLayered = false;
m_originalAlpha = 255;
m_originalLayeredFlags = LWA_ALPHA;
}
}
void ThumbnailWindow::ApplyHiddenState() {
// 1. Remove from taskbar via ITaskbarList
if (SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_taskbarList)))) {
if (SUCCEEDED(m_taskbarList->HrInit())) {
m_taskbarList->DeleteTab(m_targetWindow);
} else {
m_taskbarList.Reset();
}
}
// 2. Remove from Alt+Tab (WARNING: The old WS_EX_TOOLWINDOW approach shrinks the title bar
// and causes the client area to shift up, ruining our crop coordinates!
// So we rely solely on ITaskbarList for taskbar hiding on the TARGET window,
// and intentionally leave the HOST window as a standard window so it gets its own icon.)
// LONG_PTR exStyle = m_originalExStyle;
// exStyle &= ~WS_EX_APPWINDOW;
// exStyle |= WS_EX_TOOLWINDOW;
// SetWindowLongPtrW(m_targetWindow, GWL_EXSTYLE, exStyle);
// 3. Move to offscreen position, leaving 1 pixel overlapping the virtual desktop
// This tricks Chromium's OcclusionTracker into believing it is visible,
// while keeping the actual window opaque so DwmRegisterThumbnail can capture it.
int virtualRight = GetSystemMetrics(SM_XVIRTUALSCREEN) + GetSystemMetrics(SM_CXVIRTUALSCREEN);
// If window is maximized, we need to unmaximize it first to move it
if (m_wasMaximized) {
LONG_PTR style = GetWindowLongPtrW(m_targetWindow, GWL_STYLE);
style &= ~WS_MAXIMIZE;
SetWindowLongPtrW(m_targetWindow, GWL_STYLE, style);
// We must maintain the EXACT physical size of the window,
// otherwise the inner client area shifts relative to the frame.
int origWidth = m_originalRect.right - m_originalRect.left;
int origHeight = m_originalRect.bottom - m_originalRect.top;
SetWindowPos(m_targetWindow, HWND_TOPMOST,
virtualRight - 1, 0, origWidth, origHeight,
SWP_NOACTIVATE | SWP_FRAMECHANGED);
} else {
// Move to the edge, leaving 1 pixel visible to avoid being flagged as completely off-screen
SetWindowPos(m_targetWindow, HWND_TOPMOST,
virtualRight - 1, 0, 0, 0,
SWP_NOSIZE | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
}
void ThumbnailWindow::RestoreOriginalState() {
if (!m_targetWindow || !IsWindow(m_targetWindow)) return;
// 1. Restore taskbar icon
if (m_taskbarList) {
m_taskbarList->AddTab(m_targetWindow);
m_taskbarList.Reset();
}
// 2. Restore extended styles (removes TOOLWINDOW hack if it wasn't there)
SetWindowLongPtrW(m_targetWindow, GWL_EXSTYLE, m_originalExStyle);
// 3. Restore opacity
if (m_wasLayered) {
SetLayeredWindowAttributes(m_targetWindow, m_originalColorKey, m_originalAlpha, m_originalLayeredFlags);
} else {
// Just stripping WS_EX_LAYERED via GWL_EXSTYLE above is usually enough,
// but we can be thorough:
SetWindowPos(m_targetWindow, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
// 4. Restore position & maximized state
if (m_wasMaximized) {
RECT& rc = m_originalPlacement.rcNormalPosition;
SetWindowPos(m_targetWindow, HWND_NOTOPMOST,
rc.left, rc.top,
rc.right - rc.left, rc.bottom - rc.top,
SWP_NOACTIVATE | SWP_FRAMECHANGED);
} else {
int width = m_originalRect.right - m_originalRect.left;
int height = m_originalRect.bottom - m_originalRect.top;
SetWindowPos(m_targetWindow, HWND_NOTOPMOST,
m_originalRect.left, m_originalRect.top,
width, height,
SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
m_originalPlacement.showCmd = m_wasMaximized ? SW_SHOWMAXIMIZED : SW_RESTORE;
SetWindowPlacement(m_targetWindow, &m_originalPlacement);
// Restore standard style just in case we stripped maximize
SetWindowLongPtrW(m_targetWindow, GWL_STYLE, m_originalStyle);
// Detach pointer so we don't restore twice
m_targetWindow = nullptr;
}
void ThumbnailWindow::UpdateThumbnail() {
if (!m_thumbnail) return;
RECT clientRect;
GetClientRect(m_hostWindow, &clientRect);
int innerLeft = clientRect.left + BorderWidth;
int innerTop = clientRect.top + BorderWidth;
int innerWidth = (clientRect.right - clientRect.left) - BorderWidth * 2;
int innerHeight = (clientRect.bottom - clientRect.top) - BorderWidth * 2;
float sourceWidth = (float)(m_sourceRect.right - m_sourceRect.left);
float sourceHeight = (float)(m_sourceRect.bottom - m_sourceRect.top);
float scaleX = (float)innerWidth / sourceWidth;
float scaleY = (float)innerHeight / sourceHeight;
float scale = std::min(scaleX, scaleY);
float destWidth = sourceWidth * scale;
float destHeight = sourceHeight * scale;
float left = innerLeft + (innerWidth - destWidth) / 2.0f;
float top = innerTop + (innerHeight - destHeight) / 2.0f;
RECT destRect;
destRect.left = (LONG)left;
destRect.top = (LONG)top;
destRect.right = (LONG)(left + destWidth);
destRect.bottom = (LONG)(top + destHeight);
DWM_THUMBNAIL_PROPERTIES props = { 0 };
props.dwFlags = DWM_TNP_VISIBLE | DWM_TNP_RECTDESTINATION | DWM_TNP_RECTSOURCE;
props.fVisible = TRUE;
props.rcDestination = destRect;
props.rcSource = m_sourceRect;
DwmUpdateThumbnailProperties(m_thumbnail, &props);
}
void ThumbnailWindow::PaintBorder(HWND hwnd) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
RECT clientRect;
GetClientRect(hwnd, &clientRect);
HBRUSH brush = CreateSolidBrush(BorderColor);
RECT topRect = { clientRect.left, clientRect.top, clientRect.right, clientRect.top + BorderWidth };
RECT bottomRect = { clientRect.left, clientRect.bottom - BorderWidth, clientRect.right, clientRect.bottom };
RECT leftRect = { clientRect.left, clientRect.top + BorderWidth, clientRect.left + BorderWidth, clientRect.bottom - BorderWidth };
RECT rightRect = { clientRect.right - BorderWidth, clientRect.top + BorderWidth, clientRect.right, clientRect.bottom - BorderWidth };
FillRect(hdc, &topRect, brush);
FillRect(hdc, &bottomRect, brush);
FillRect(hdc, &leftRect, brush);
FillRect(hdc, &rightRect, brush);
DeleteObject(brush);
EndPaint(hwnd, &ps);
}
LRESULT CALLBACK ThumbnailWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
ThumbnailWindow* pThis = nullptr;
if (msg == WM_NCCREATE) {
CREATESTRUCT* pCreate = (CREATESTRUCT*)lParam;
pThis = (ThumbnailWindow*)pCreate->lpCreateParams;
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
pThis->m_hostWindow = hwnd;
} else {
pThis = (ThumbnailWindow*)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
}
if (pThis) {
return pThis->MessageHandler(hwnd, msg, wParam, lParam);
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
LRESULT ThumbnailWindow::MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_NCCALCSIZE:
if (!m_showTitlebar && wParam) {
return 0;
}
break;
case WM_NCACTIVATE:
if (!m_showTitlebar) {
DefWindowProcW(hwnd, msg, FALSE, lParam);
return TRUE;
}
break;
case WM_NCHITTEST: {
LRESULT hit = DefWindowProcW(hwnd, msg, wParam, lParam);
if (hit == HTCLIENT) {
POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
RECT rc;
GetWindowRect(hwnd, &rc);
if (!m_showTitlebar) {
const int hitBorder = std::max(BorderWidth, 6);
bool isLeft = (pt.x >= rc.left && pt.x < rc.left + hitBorder);
bool isRight = (pt.x <= rc.right && pt.x > rc.right - hitBorder);
bool isTop = (pt.y >= rc.top && pt.y < rc.top + hitBorder);
bool isBottom = (pt.y <= rc.bottom && pt.y > rc.bottom - hitBorder);
if (isLeft && isTop) return HTTOPLEFT;
if (isRight && isTop) return HTTOPRIGHT;
if (isLeft && isBottom) return HTBOTTOMLEFT;
if (isRight && isBottom) return HTBOTTOMRIGHT;
if (isLeft) return HTLEFT;
if (isRight) return HTRIGHT;
if (isTop) return HTTOP;
if (isBottom) return HTBOTTOM;
}
return HTCAPTION; // Allow moving by dragging client area
}
return hit;
}
case WM_PAINT:
PaintBorder(hwnd);
return 0;
case WM_ERASEBKGND:
return 1;
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
DestroyWindow(hwnd);
return 0;
}
break;
case WM_SIZING: {
RECT* prc = (RECT*)lParam;
float sourceWidth = (float)(m_sourceRect.right - m_sourceRect.left);
float sourceHeight = (float)(m_sourceRect.bottom - m_sourceRect.top);
if (sourceWidth <= 0 || sourceHeight <= 0) break;
float targetAspect = sourceWidth / sourceHeight;
RECT windowRect, clientRect;
GetWindowRect(hwnd, &windowRect);
GetClientRect(hwnd, &clientRect);
int ncWidth = (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left);
int ncHeight = (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top);
int paddingX = ncWidth + BorderWidth * 2;
int paddingY = ncHeight + BorderWidth * 2;
int currentW = prc->right - prc->left;
int currentH = prc->bottom - prc->top;
int contentW = currentW - paddingX;
int contentH = currentH - paddingY;
if (contentW < 10) contentW = 10;
if (contentH < 10) contentH = 10;
if (wParam == WMSZ_LEFT || wParam == WMSZ_RIGHT) {
contentH = (int)(contentW / targetAspect);
} else if (wParam == WMSZ_TOP || wParam == WMSZ_BOTTOM) {
contentW = (int)(contentH * targetAspect);
} else {
if ((float)contentW / contentH > targetAspect) {
contentH = (int)(contentW / targetAspect);
} else {
contentW = (int)(contentH * targetAspect);
}
}
int newW = contentW + paddingX;
int newH = contentH + paddingY;
switch (wParam) {
case WMSZ_LEFT:
case WMSZ_RIGHT:
contentH = (int)(contentW / targetAspect);
break;
case WMSZ_TOP:
case WMSZ_BOTTOM:
contentW = (int)(contentH * targetAspect);
break;
default: // Corners
if ((float)contentW / contentH > targetAspect) {
contentW = (int)(contentH * targetAspect);
} else {
contentH = (int)(contentW / targetAspect);
}
break;
}
return TRUE;
}
case WM_WINDOWPOSCHANGING: {
WINDOWPOS* pos = (WINDOWPOS*)lParam;
if ((pos->flags & SWP_NOSIZE) == 0) {
float sourceWidth = (float)(m_sourceRect.right - m_sourceRect.left);
float sourceHeight = (float)(m_sourceRect.bottom - m_sourceRect.top);
if (sourceWidth <= 0 || sourceHeight <= 0) break;
float targetAspect = sourceWidth / sourceHeight;
RECT windowRect, clientRect;
GetWindowRect(hwnd, &windowRect);
GetClientRect(hwnd, &clientRect);
int ncWidth = (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left);
int ncHeight = (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top);
int paddingX = ncWidth + BorderWidth * 2;
int paddingY = ncHeight + BorderWidth * 2;
int contentW = pos->cx - paddingX;
int contentH = pos->cy - paddingY;
if (contentW < 10) contentW = 10;
if (contentH < 10) contentH = 10;
int oldW = windowRect.right - windowRect.left;
int oldH = windowRect.bottom - windowRect.top;
if (pos->cx == oldW && pos->cy == oldH) break;
// Try to match the new size, shrinking the dimension that is too large
if ((float)contentW / contentH > targetAspect) {
contentW = (int)(contentH * targetAspect);
} else {
contentH = (int)(contentW / targetAspect);
}
int newW = contentW + paddingX;
int newH = contentH + paddingY;
// To prevent the window from "running away" if dragged from top/left without edge inference,
// we will infer edge drag ONLY if x or y exactly matches the old rect,
// which implies a right or bottom edge drag.
// If x or y changed, we assume the opposite edge is anchored.
if ((pos->flags & SWP_NOMOVE) == 0) {
bool leftAnchored = (pos->x == windowRect.left);
bool topAnchored = (pos->y == windowRect.top);
bool rightAnchored = ((pos->x + pos->cx) == windowRect.right);
bool bottomAnchored = ((pos->y + pos->cy) == windowRect.bottom);
// If it's a center resize or a completely new position, don't anchor to old edges
if (!leftAnchored && rightAnchored) {
pos->x = windowRect.right - newW;
} else if (!leftAnchored && !rightAnchored) {
int intendedCenterX = pos->x + pos->cx / 2;
pos->x = intendedCenterX - newW / 2;
}
if (!topAnchored && bottomAnchored) {
pos->y = windowRect.bottom - newH;
} else if (!topAnchored && !bottomAnchored) {
int intendedCenterY = pos->y + pos->cy / 2;
pos->y = intendedCenterY - newH / 2;
}
}
pos->cx = newW;
pos->cy = newH;
}
break;
}
case WM_SIZE:
UpdateThumbnail();
InvalidateRect(hwnd, nullptr, FALSE);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
return 0;
case WM_DESTROY: {
if (m_thumbnail) {
DwmUnregisterThumbnail(m_thumbnail);
m_thumbnail = nullptr;
}
m_hostWindow = nullptr;
RestoreOriginalState();
return 0;
}
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}