Skip to content

Commit

Permalink
Merge pull request 'Fix top border for Win10' (#12) from fix/top-bord…
Browse files Browse the repository at this point in the history
…er into release/v8.2.0
  • Loading branch information
maxkadushkin committed Sep 13, 2024
2 parents e5cb8fe + f888842 commit fdd2988
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 23 deletions.
77 changes: 56 additions & 21 deletions win-linux/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#ifdef _WIN32
# include <QDesktopServices>
#include <windowsx.h>
# include <sddl.h>
#include "shlobj.h"
#include "lmcons.h"
#else
Expand Down Expand Up @@ -1012,6 +1013,33 @@ namespace WindowHelper {
// } else AdjustWindowRectEx(rect, (GetWindowStyle(handle) & ~WS_DLGFRAME), FALSE, 0);
// }

QString user_sid;

auto GetCurrentUserSID() -> QString
{
if (user_sid.isEmpty()) {
HANDLE hToken = NULL;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
DWORD tokenLen = 0;
GetTokenInformation(hToken, TokenUser, NULL, 0, &tokenLen);
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
if (PTOKEN_USER pTokenUser = (PTOKEN_USER)malloc(tokenLen)) {
if (GetTokenInformation(hToken, TokenUser, pTokenUser, tokenLen, &tokenLen)) {
LPWSTR sid = NULL;
if (ConvertSidToStringSid(pTokenUser->User.Sid, &sid)) {
user_sid = QString::fromWCharArray(sid);
LocalFree(sid);
}
}
free(pTokenUser);
}
}
CloseHandle(hToken);
}
}
return user_sid;
}

auto bringToTop(HWND hwnd) -> void
{
DWORD appID = ::GetCurrentThreadId();
Expand All @@ -1027,29 +1055,36 @@ namespace WindowHelper {

auto getColorizationColor(bool isActive, const QColor &bkgColor) -> QColor
{
QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\DWM", QSettings::NativeFormat);
if (isActive && reg.value("ColorPrevalence", 0).toInt() != 0) {
DWORD dwcolor = 0;
BOOL opaque = TRUE;
static HRESULT(WINAPI *DwmGetColorizationColor)(DWORD*, BOOL*) = NULL;
if (!DwmGetColorizationColor) {
if (HMODULE module = GetModuleHandleA("dwmapi"))
*(FARPROC*)&DwmGetColorizationColor = GetProcAddress(module, "DwmGetColorizationColor");
}
if (DwmGetColorizationColor && SUCCEEDED(DwmGetColorizationColor(&dwcolor, &opaque))) {
return QColor((dwcolor & 0xff0000) >> 16, (dwcolor & 0xff00) >> 8, dwcolor & 0xff);
int lum = int(0.299 * bkgColor.red() + 0.587 * bkgColor.green() + 0.114 * bkgColor.blue());
if (isActive) {
QSettings reg("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\DWM", QSettings::NativeFormat);
if (reg.value("ColorPrevalence", 0).toInt() != 0) {
DWORD dwcolor = 0;
BOOL opaque = TRUE;
static HRESULT(WINAPI *DwmGetColorizationColor)(DWORD*, BOOL*) = NULL;
if (!DwmGetColorizationColor) {
if (HMODULE module = GetModuleHandleA("dwmapi"))
*(FARPROC*)&DwmGetColorizationColor = GetProcAddress(module, "DwmGetColorizationColor");
}
if (DwmGetColorizationColor && SUCCEEDED(DwmGetColorizationColor(&dwcolor, &opaque))) {
return QColor((dwcolor & 0xff0000) >> 16, (dwcolor & 0xff00) >> 8, dwcolor & 0xff);
}
} else {
QSettings reg_lt("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", QSettings::NativeFormat);
if (reg_lt.value("SystemUsesLightTheme", 0).toInt() != 0) {
QString userSid = GetCurrentUserSID();
if (!userSid.isEmpty()) {
QSettings reg_ac("HKEY_USERS\\" + userSid + "\\Control Panel\\Desktop", QSettings::NativeFormat);
if (reg_ac.value("AutoColorization", 0).toInt() != 0)
return bkgColor.lighter(95);
}
}
}
int res = -0.002*lum*lum + 0.93*lum + 6;
return QColor(res, res, res);
}
#define BORDER_ACTIVE_DARK "#2f2f2f" // Dark theme
#define BORDER_INACTIVE_DARK "#3a3a3a"
#define BORDER_ACTIVE_LIGHT_V1 "#585858" // Light theme and colored background
#define BORDER_ACTIVE_LIGHT_V2 "#777777" // Light theme and white background
#define BORDER_INACTIVE_LIGHT_V1 "#606060"
#define BORDER_INACTIVE_LIGHT_V2 "#aaaaaa"
int lum = int(0.299 * bkgColor.red() + 0.587 * bkgColor.green() + 0.114 * bkgColor.blue());
return QColor(lum < 85 ? (isActive ? BORDER_ACTIVE_DARK : BORDER_INACTIVE_DARK) :
lum < 170 ? (isActive ? BORDER_ACTIVE_LIGHT_V1 : BORDER_INACTIVE_LIGHT_V1) :
(isActive ? BORDER_ACTIVE_LIGHT_V2 : BORDER_INACTIVE_LIGHT_V2));
int res = -0.0007*lum*lum + 0.78*lum + 25;
return QColor(res, res, res);
}

auto toggleLayoutDirection(HWND hwnd) -> void
Expand Down
4 changes: 3 additions & 1 deletion win-linux/src/windows/cwindowbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ void CWindowBase::setWindowColors(const QColor& background, const QColor& border
} else
if (Utils::getWinVersion() == Utils::WinVer::Win10) {
int brdWidth = 0;
SystemParametersInfo(SPI_GETBORDER, 0, &brdWidth, 0);
HDC hdc = GetDC(NULL);
brdWidth = GetSystemMetrics(SM_CXBORDER) * GetDeviceCaps(hdc, LOGPIXELSX)/96;
ReleaseDC(NULL, hdc);
QColor brdColor = WindowHelper::getColorizationColor(isActive, background);
css = QString("QMainWindow{border-top: %1px solid %2; background-color: %3;}").arg(QString::number(brdWidth), brdColor.name(), background.name());
} else {
Expand Down
4 changes: 3 additions & 1 deletion win-linux/src/windows/platform_win/cwindowplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ void CWindowPlatform::adjustGeometry()
} else
if (Utils::getWinVersion() == WinVer::Win10) {
int brdWidth = 0;
SystemParametersInfo(SPI_GETBORDER, 0, &brdWidth, 0);
HDC hdc = GetDC(NULL);
brdWidth = GetSystemMetrics(SM_CXBORDER) * GetDeviceCaps(hdc, LOGPIXELSX)/96;
ReleaseDC(NULL, hdc);
mrg = QMargins(0, brdWidth, 0, 0);
}
m_resAreaWidth = mrg.top();
Expand Down

0 comments on commit fdd2988

Please sign in to comment.