Skip to content

Commit 6f4ab64

Browse files
committed
fix unwanted title bar when special effect is enabled
1 parent cc65c4d commit 6f4ab64

File tree

5 files changed

+47
-74
lines changed

5 files changed

+47
-74
lines changed

examples/mainwindow/dark-style.qss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* Window bar */
22

33
QWK--WindowBar[bar-active=true] {
4-
background-color: #3C3C3C;
4+
/*background-color: #3C3C3C;*/
5+
background-color: transparent;
56
}
67

78
QWK--WindowBar[bar-active=false] {
8-
background-color: #505050;
9+
/*background-color: #505050;*/
10+
background-color: transparent;
911
}
1012

1113

examples/mainwindow/light-style.qss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
/* Window bar */
22

33
QWK--WindowBar[bar-active=true] {
4-
background-color: #195ABE;
4+
/*background-color: #195ABE;*/
5+
background-color: transparent;
56
}
67

78
QWK--WindowBar[bar-active=false] {
8-
background-color: #195ABE;
9+
/*background-color: #195ABE;*/
10+
background-color: transparent;
911
}
1012

1113

examples/qml/main.qml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ Window {
4949
right: parent.right
5050
}
5151
height: 32
52-
color: window.active ? "#3C3C3C" : "#505050"
52+
//color: window.active ? "#3C3C3C" : "#505050"
53+
color: "transparent"
5354
Component.onCompleted: windowAgent.setTitleBar(titleBar)
5455

5556
Image {

src/core/contexts/win32windowcontext.cpp

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -204,51 +204,6 @@ namespace QWK {
204204
moveWindowToMonitor(hwnd, activeMonitor);
205205
}
206206

207-
static void syncPaintEventWithDwm() {
208-
// No need to sync with DWM if DWM composition is disabled.
209-
if (!isDwmCompositionEnabled()) {
210-
return;
211-
}
212-
const DynamicApis &apis = DynamicApis::instance();
213-
// Dirty hack to workaround the resize flicker caused by DWM.
214-
LARGE_INTEGER freq{};
215-
::QueryPerformanceFrequency(&freq);
216-
TIMECAPS tc{};
217-
apis.ptimeGetDevCaps(&tc, sizeof(tc));
218-
const UINT ms_granularity = tc.wPeriodMin;
219-
apis.ptimeBeginPeriod(ms_granularity);
220-
LARGE_INTEGER now0{};
221-
::QueryPerformanceCounter(&now0);
222-
// ask DWM where the vertical blank falls
223-
DWM_TIMING_INFO dti{};
224-
dti.cbSize = sizeof(dti);
225-
apis.pDwmGetCompositionTimingInfo(nullptr, &dti);
226-
LARGE_INTEGER now1{};
227-
::QueryPerformanceCounter(&now1);
228-
// - DWM told us about SOME vertical blank
229-
// - past or future, possibly many frames away
230-
// - convert that into the NEXT vertical blank
231-
const auto period = qreal(dti.qpcRefreshPeriod);
232-
const auto dt = qreal(dti.qpcVBlank - now1.QuadPart);
233-
const qreal ratio = (dt / period);
234-
auto w = qreal(0);
235-
auto m = qreal(0);
236-
if ((dt > qreal(0)) || qFuzzyIsNull(dt)) {
237-
w = ratio;
238-
} else {
239-
// reach back to previous period
240-
// - so m represents consistent position within phase
241-
w = (ratio - qreal(1));
242-
}
243-
m = (dt - (period * w));
244-
if ((m < qreal(0)) || qFuzzyCompare(m, period) || (m > period)) {
245-
return;
246-
}
247-
const qreal m_ms = (qreal(1000) * m / qreal(freq.QuadPart));
248-
::Sleep(static_cast<DWORD>(std::round(m_ms)));
249-
apis.ptimeEndPeriod(ms_granularity);
250-
}
251-
252207
// Returns false if the menu is canceled
253208
static bool showSystemMenu_sys(HWND hWnd, const POINT &pos, const bool selectFirstEntry,
254209
const bool fixedSize) {
@@ -938,8 +893,39 @@ namespace QWK {
938893
Q_UNUSED(oldAttribute)
939894

940895
const auto hwnd = reinterpret_cast<HWND>(m_windowId);
896+
Q_ASSERT(hwnd);
897+
if (!hwnd) {
898+
return false;
899+
}
900+
941901
const DynamicApis &apis = DynamicApis::instance();
942-
static constexpr const MARGINS extendedMargins = {-1, -1, -1, -1};
902+
const auto &extendMargins = [this, &apis, hwnd]() {
903+
// For some unknown reason, the window background is totally black when the host object
904+
// is a QWidget. And extending the window frame into the client area seems to fix it
905+
// magically.
906+
// We don't need the following *HACK* for QtQuick windows.
907+
if (!m_host->isWidgetType()) {
908+
return;
909+
}
910+
// After many times of trying, we found that the Acrylic/Mica/Mica Alt background
911+
// only appears on the native Win32 window's background, so naturally we want to
912+
// extend the window frame into the whole client area to be able to let the special
913+
// material fill the whole window. Previously we are using negative margins because
914+
// it's widely known that using negative margins will let the window frame fill
915+
// the whole window and that's indeed what we wanted to do, however, later we found
916+
// that doing so is causing issues. When the user enabled the "show accent color on
917+
// window title bar and borders" option on system personalize settings, a 30px bar
918+
// would appear on window top. It has the same color with the system accent color.
919+
// Actually it's the original title bar we've already hidden, and it magically
920+
// appears again when we use negative margins to extend the window frame. And again
921+
// after some experiments, I found that the title bar won't appear if we don't extend
922+
// from the top side. In the end I found that we only need to extend from the left
923+
// side if we extend long enough. In this way we can see the special material even
924+
// when the host object is a QWidget and the title bar still remain hidden. But even
925+
// though this solution seems perfect, I really don't know why it works.
926+
static constexpr const MARGINS margins = {65536, 0, 0, 0};
927+
apis.pDwmExtendFrameIntoClientArea(hwnd, &margins);
928+
};
943929
const auto &restoreMargins = [this, &apis, hwnd]() {
944930
auto margins = qmargins2margins(
945931
m_windowAttributes.value(QStringLiteral("extra-margins")).value<QMargins>());
@@ -975,9 +961,7 @@ namespace QWK {
975961
return false;
976962
}
977963
if (attribute.toBool()) {
978-
// We need to extend the window frame into the whole client area to be able
979-
// to see the blurred window background.
980-
apis.pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
964+
extendMargins();
981965
if (isWin1122H2OrGreater()) {
982966
// Use official DWM API to enable Mica, available since Windows 11 22H2
983967
// (10.0.22621).
@@ -1009,9 +993,7 @@ namespace QWK {
1009993
return false;
1010994
}
1011995
if (attribute.toBool()) {
1012-
// We need to extend the window frame into the whole client area to be able
1013-
// to see the blurred window background.
1014-
apis.pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
996+
extendMargins();
1015997
// Use official DWM API to enable Mica Alt, available since Windows 11 22H2
1016998
// (10.0.22621).
1017999
const _DWM_SYSTEMBACKDROP_TYPE backdropType = _DWMSBT_TABBEDWINDOW;
@@ -1031,9 +1013,7 @@ namespace QWK {
10311013
return false;
10321014
}
10331015
if (attribute.toBool()) {
1034-
// We need to extend the window frame into the whole client area to be able
1035-
// to see the blurred window background.
1036-
apis.pDwmExtendFrameIntoClientArea(hwnd, &extendedMargins);
1016+
extendMargins();
10371017

10381018
const _DWM_SYSTEMBACKDROP_TYPE backdropType = _DWMSBT_TRANSIENTWINDOW;
10391019
apis.pDwmSetWindowAttribute(hwnd, _DWMWA_SYSTEMBACKDROP_TYPE, &backdropType,
@@ -1074,9 +1054,9 @@ namespace QWK {
10741054
}
10751055

10761056
if (key == QStringLiteral("dwm-blur")) {
1057+
// Extending window frame would break this effect for some unknown reason.
1058+
restoreMargins();
10771059
if (attribute.toBool()) {
1078-
// We can't extend the window frame for this effect.
1079-
restoreMargins();
10801060
if (isWin8OrGreater()) {
10811061
ACCENT_POLICY policy{};
10821062
policy.dwAccentState = ACCENT_ENABLE_BLURBEHIND;
@@ -2157,8 +2137,6 @@ namespace QWK {
21572137
}
21582138
}
21592139
}
2160-
// We should call this function only before the function returns.
2161-
syncPaintEventWithDwm();
21622140
// By returning WVR_REDRAW we can make the window resizing look
21632141
// less broken. But we must return 0 if wParam is FALSE, according to Microsoft
21642142
// Docs.

src/core/shared/qwkwindowsextra_p.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ namespace QWK {
170170

171171
DYNAMIC_API_DECLARE(DwmFlush);
172172
DYNAMIC_API_DECLARE(DwmIsCompositionEnabled);
173-
DYNAMIC_API_DECLARE(DwmGetCompositionTimingInfo);
174173
DYNAMIC_API_DECLARE(DwmGetWindowAttribute);
175174
DYNAMIC_API_DECLARE(DwmSetWindowAttribute);
176175
DYNAMIC_API_DECLARE(DwmExtendFrameIntoClientArea);
@@ -179,9 +178,6 @@ namespace QWK {
179178
DYNAMIC_API_DECLARE(GetSystemMetricsForDpi);
180179
DYNAMIC_API_DECLARE(AdjustWindowRectExForDpi);
181180
DYNAMIC_API_DECLARE(GetDpiForMonitor);
182-
DYNAMIC_API_DECLARE(timeGetDevCaps);
183-
DYNAMIC_API_DECLARE(timeBeginPeriod);
184-
DYNAMIC_API_DECLARE(timeEndPeriod);
185181

186182
#undef DYNAMIC_API_DECLARE
187183

@@ -209,17 +205,11 @@ namespace QWK {
209205
QSystemLibrary dwmapi(QStringLiteral("dwmapi"));
210206
DYNAMIC_API_RESOLVE(dwmapi, DwmFlush);
211207
DYNAMIC_API_RESOLVE(dwmapi, DwmIsCompositionEnabled);
212-
DYNAMIC_API_RESOLVE(dwmapi, DwmGetCompositionTimingInfo);
213208
DYNAMIC_API_RESOLVE(dwmapi, DwmGetWindowAttribute);
214209
DYNAMIC_API_RESOLVE(dwmapi, DwmSetWindowAttribute);
215210
DYNAMIC_API_RESOLVE(dwmapi, DwmExtendFrameIntoClientArea);
216211
DYNAMIC_API_RESOLVE(dwmapi, DwmEnableBlurBehindWindow);
217212

218-
QSystemLibrary winmm(QStringLiteral("winmm"));
219-
DYNAMIC_API_RESOLVE(winmm, timeGetDevCaps);
220-
DYNAMIC_API_RESOLVE(winmm, timeBeginPeriod);
221-
DYNAMIC_API_RESOLVE(winmm, timeEndPeriod);
222-
223213
#undef DYNAMIC_API_RESOLVE
224214

225215
#define UNDOC_API_RESOLVE(DLL, NAME, ORDINAL) \

0 commit comments

Comments
 (0)