-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
WindowBorder.cpp
290 lines (246 loc) · 7.24 KB
/
WindowBorder.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
#include "pch.h"
#include "WindowBorder.h"
#include <dwmapi.h>
#include "winrt/Windows.Foundation.h"
#include <FrameDrawer.h>
#include <ScalingUtils.h>
#include <Settings.h>
#include <WindowCornersUtil.h>
// Non-Localizable strings
namespace NonLocalizable
{
const wchar_t ToolWindowClassName[] = L"AlwaysOnTop_Border";
}
std::optional<RECT> GetFrameRect(HWND window)
{
RECT rect;
if (!SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &rect, sizeof(rect))))
{
return std::nullopt;
}
int border = AlwaysOnTopSettings::settings().frameThickness;
rect.top -= border;
rect.left -= border;
rect.right += border;
rect.bottom += border;
return rect;
}
WindowBorder::WindowBorder(HWND window) :
SettingsObserver({ SettingId::FrameColor, SettingId::FrameThickness, SettingId::FrameAccentColor, SettingId::FrameOpacity, SettingId::RoundCornersEnabled }),
m_window(nullptr),
m_trackingWindow(window),
m_frameDrawer(nullptr)
{
}
WindowBorder::~WindowBorder()
{
if (m_frameDrawer)
{
m_frameDrawer->Hide();
m_frameDrawer = nullptr;
}
if (m_window)
{
SetWindowLongPtrW(m_window, GWLP_USERDATA, 0);
DestroyWindow(m_window);
}
}
std::unique_ptr<WindowBorder> WindowBorder::Create(HWND window, HINSTANCE hinstance)
{
auto self = std::unique_ptr<WindowBorder>(new WindowBorder(window));
if (self->Init(hinstance))
{
return self;
}
return nullptr;
}
namespace
{
constexpr uint32_t REFRESH_BORDER_TIMER_ID = 123;
constexpr uint32_t REFRESH_BORDER_INTERVAL = 100;
}
bool WindowBorder::Init(HINSTANCE hinstance)
{
if (!m_trackingWindow)
{
return false;
}
auto windowRectOpt = GetFrameRect(m_trackingWindow);
if (!windowRectOpt.has_value())
{
return false;
}
RECT windowRect = windowRectOpt.value();
WNDCLASSEXW wcex{};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = s_WndProc;
wcex.hInstance = hinstance;
wcex.lpszClassName = NonLocalizable::ToolWindowClassName;
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
RegisterClassExW(&wcex);
m_window = CreateWindowExW(WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TOOLWINDOW
, NonLocalizable::ToolWindowClassName
, L""
, WS_POPUP | WS_DISABLED
, windowRect.left
, windowRect.top
, windowRect.right - windowRect.left
, windowRect.bottom - windowRect.top
, nullptr
, nullptr
, hinstance
, this);
if (!m_window)
{
return false;
}
// make window transparent
int const pos = -GetSystemMetrics(SM_CXVIRTUALSCREEN) - 8;
if (wil::unique_hrgn hrgn{ CreateRectRgn(pos, 0, (pos + 1), 1) })
{
DWM_BLURBEHIND bh = { DWM_BB_ENABLE | DWM_BB_BLURREGION, TRUE, hrgn.get(), FALSE };
DwmEnableBlurBehindWindow(m_window, &bh);
}
if (!SetLayeredWindowAttributes(m_window, RGB(0, 0, 0), 0, LWA_COLORKEY))
{
return false;
}
// set position of the border-window behind the tracking window
// helps to prevent border overlapping (happens after turning borders off and on)
SetWindowPos(m_trackingWindow
, m_window
, windowRect.left
, windowRect.top
, windowRect.right - windowRect.left
, windowRect.bottom - windowRect.top
, SWP_NOMOVE | SWP_NOSIZE);
BOOL val = TRUE;
DwmSetWindowAttribute(m_window, DWMWA_EXCLUDED_FROM_PEEK, &val, sizeof(val));
m_frameDrawer = FrameDrawer::Create(m_window);
if (!m_frameDrawer)
{
return false;
}
m_frameDrawer->Show();
UpdateBorderPosition();
UpdateBorderProperties();
m_timer_id = SetTimer(m_window, REFRESH_BORDER_TIMER_ID, REFRESH_BORDER_INTERVAL, nullptr);
return true;
}
void WindowBorder::UpdateBorderPosition() const
{
if (!m_trackingWindow)
{
return;
}
auto rectOpt = GetFrameRect(m_trackingWindow);
if (!rectOpt.has_value())
{
m_frameDrawer->Hide();
return;
}
RECT rect = rectOpt.value();
SetWindowPos(m_window, m_trackingWindow, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOREDRAW | SWP_NOACTIVATE);
}
void WindowBorder::UpdateBorderProperties() const
{
if (!m_trackingWindow || !m_frameDrawer)
{
return;
}
auto windowRectOpt = GetFrameRect(m_trackingWindow);
if (!windowRectOpt.has_value())
{
return;
}
const RECT windowRect = windowRectOpt.value();
SetWindowPos(m_window, m_trackingWindow, windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, SWP_NOREDRAW | SWP_NOACTIVATE);
RECT frameRect{ 0, 0, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top };
COLORREF color;
if (AlwaysOnTopSettings::settings().frameAccentColor)
{
winrt::Windows::UI::ViewManagement::UISettings settings;
auto accentValue = settings.GetColorValue(winrt::Windows::UI::ViewManagement::UIColorType::Accent);
color = RGB(accentValue.R, accentValue.G, accentValue.B);
}
else
{
color = AlwaysOnTopSettings::settings().frameColor;
}
float opacity = AlwaysOnTopSettings::settings().frameOpacity / 100.0f;
float scalingFactor = ScalingUtils::ScalingFactor(m_trackingWindow);
float thickness = AlwaysOnTopSettings::settings().frameThickness * scalingFactor;
float cornerRadius = 0.0;
if (AlwaysOnTopSettings::settings().roundCornersEnabled)
{
cornerRadius = WindowCornerUtils::CornersRadius(m_trackingWindow) * scalingFactor;
}
m_frameDrawer->SetBorderRect(frameRect, color, opacity, static_cast<int>(thickness), cornerRadius);
}
LRESULT WindowBorder::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
{
switch (message)
{
case WM_TIMER:
{
switch (wparam)
{
case REFRESH_BORDER_TIMER_ID:
KillTimer(m_window, m_timer_id);
m_timer_id = SetTimer(m_window, REFRESH_BORDER_TIMER_ID, REFRESH_BORDER_INTERVAL, nullptr);
UpdateBorderPosition();
UpdateBorderProperties();
break;
}
break;
}
case WM_NCDESTROY:
{
KillTimer(m_window, m_timer_id);
::DefWindowProc(m_window, message, wparam, lparam);
SetWindowLongPtr(m_window, GWLP_USERDATA, 0);
}
break;
case WM_ERASEBKGND:
return TRUE;
// prevent from beeping if the border was clicked
case WM_SETCURSOR:
return TRUE;
default:
{
return DefWindowProc(m_window, message, wparam, lparam);
}
}
return FALSE;
}
void WindowBorder::SettingsUpdate(SettingId id)
{
if (!AlwaysOnTopSettings::settings().enableFrame)
{
return;
}
auto windowRectOpt = GetFrameRect(m_trackingWindow);
if (!windowRectOpt.has_value())
{
return;
}
switch (id)
{
case SettingId::FrameThickness:
{
UpdateBorderPosition();
UpdateBorderProperties();
}
break;
case SettingId::FrameColor:
case SettingId::FrameAccentColor:
case SettingId::FrameOpacity:
case SettingId::RoundCornersEnabled:
{
UpdateBorderProperties();
}
break;
default:
break;
}
}