Improve HiDPI compatibility for tab control labels

Close #14958
pull/14980/head
ozone10 8 months ago committed by Don Ho
parent 8f24e89824
commit e0bb723700

@ -593,10 +593,10 @@ namespace NppDarkMode
static TreeViewStyle g_treeViewStyle = TreeViewStyle::classic; static TreeViewStyle g_treeViewStyle = TreeViewStyle::classic;
static COLORREF g_treeViewBg = NppParameters::getInstance().getCurrentDefaultBgColor(); static COLORREF g_treeViewBg = NppParameters::getInstance().getCurrentDefaultBgColor();
static double g_lighnessTreeView = 50.0; static double g_lightnessTreeView = 50.0;
// adapted from https://stackoverflow.com/a/56678483 // adapted from https://stackoverflow.com/a/56678483
double calculatePerceivedLighness(COLORREF c) double calculatePerceivedLightness(COLORREF c)
{ {
auto linearValue = [](double colorChannel) -> double auto linearValue = [](double colorChannel) -> double
{ {
@ -612,8 +612,8 @@ namespace NppDarkMode
double luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b; double luminance = 0.2126 * r + 0.7152 * g + 0.0722 * b;
double lighness = (luminance <= 216.0 / 24389.0) ? (luminance * 24389.0 / 27.0) : (std::pow(luminance, (1.0 / 3.0)) * 116.0 - 16.0); double lightness = (luminance <= 216.0 / 24389.0) ? (luminance * 24389.0 / 27.0) : (std::pow(luminance, (1.0 / 3.0)) * 116.0 - 16.0);
return lighness; return lightness;
} }
COLORREF getBackgroundColor() { return getTheme()._colors.background; } COLORREF getBackgroundColor() { return getTheme()._colors.background; }
@ -1458,14 +1458,20 @@ namespace NppDarkMode
WPARAM wParam, WPARAM wParam,
LPARAM lParam, LPARAM lParam,
UINT_PTR uIdSubclass, UINT_PTR uIdSubclass,
DWORD_PTR dwRefData DWORD_PTR /*dwRefData*/
) )
{ {
UNREFERENCED_PARAMETER(uIdSubclass);
UNREFERENCED_PARAMETER(dwRefData);
switch (uMsg) switch (uMsg)
{ {
case WM_ERASEBKGND:
{
if (NppDarkMode::isEnabled())
{
return TRUE;
}
break;
}
case WM_PAINT: case WM_PAINT:
{ {
if (!NppDarkMode::isEnabled()) if (!NppDarkMode::isEnabled())
@ -1539,17 +1545,13 @@ namespace NppDarkMode
::SendMessage(hWnd, TCM_GETITEM, i, reinterpret_cast<LPARAM>(&tci)); ::SendMessage(hWnd, TCM_GETITEM, i, reinterpret_cast<LPARAM>(&tci));
const auto dpi = DPIManagerV2::getDpiForParent(hWnd);
RECT rcText = rcItem; RECT rcText = rcItem;
rcText.left += DPIManagerV2::scale(5, dpi);
rcText.right -= DPIManagerV2::scale(3, dpi);
if (isSelectedTab) if (isSelectedTab)
{ {
rcText.bottom -= DPIManagerV2::scale(4, dpi); ::OffsetRect(&rcText, 0, -1);
::InflateRect(&rcFrame, 0, 1); ::InflateRect(&rcFrame, 0, 1);
} }
if (i != nTabs - 1) if (i != nTabs - 1)
{ {
rcFrame.right += 1; rcFrame.right += 1;
@ -1557,7 +1559,7 @@ namespace NppDarkMode
::FrameRect(hdc, &rcFrame, NppDarkMode::getEdgeBrush()); ::FrameRect(hdc, &rcFrame, NppDarkMode::getEdgeBrush());
DrawText(hdc, label, -1, &rcText, DT_LEFT | DT_VCENTER | DT_SINGLELINE); DrawText(hdc, label, -1, &rcText, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
DeleteObject(hClip); DeleteObject(hClip);
@ -1582,7 +1584,7 @@ namespace NppDarkMode
case WM_NCDESTROY: case WM_NCDESTROY:
{ {
RemoveWindowSubclass(hWnd, TabSubclass, g_tabSubclassID); ::RemoveWindowSubclass(hWnd, TabSubclass, uIdSubclass);
break; break;
} }
@ -3095,17 +3097,17 @@ namespace NppDarkMode
{ {
COLORREF bgColor = NppParameters::getInstance().getCurrentDefaultBgColor(); COLORREF bgColor = NppParameters::getInstance().getCurrentDefaultBgColor();
if (g_treeViewBg != bgColor || g_lighnessTreeView == 50.0) if (g_treeViewBg != bgColor || g_lightnessTreeView == 50.0)
{ {
g_lighnessTreeView = calculatePerceivedLighness(bgColor); g_lightnessTreeView = calculatePerceivedLightness(bgColor);
g_treeViewBg = bgColor; g_treeViewBg = bgColor;
} }
if (g_lighnessTreeView < (50.0 - g_middleGrayRange)) if (g_lightnessTreeView < (50.0 - g_middleGrayRange))
{ {
g_treeViewStyle = TreeViewStyle::dark; g_treeViewStyle = TreeViewStyle::dark;
} }
else if (g_lighnessTreeView > (50.0 + g_middleGrayRange)) else if (g_lightnessTreeView > (50.0 + g_middleGrayRange))
{ {
g_treeViewStyle = TreeViewStyle::light; g_treeViewStyle = TreeViewStyle::light;
} }

@ -123,7 +123,7 @@ namespace NppDarkMode
COLORREF invertLightness(COLORREF c); COLORREF invertLightness(COLORREF c);
COLORREF invertLightnessSofter(COLORREF c); COLORREF invertLightnessSofter(COLORREF c);
double calculatePerceivedLighness(COLORREF c); double calculatePerceivedLightness(COLORREF c);
void setDarkTone(ColorTone colorToneChoice); void setDarkTone(ColorTone colorToneChoice);

Loading…
Cancel
Save