Enhance listview column header in dark mode

Use different approach to make listview header dark.

Close #10106
This commit is contained in:
ozone10 2021-07-05 11:29:10 +02:00 committed by Don Ho
parent a0472fd7f2
commit ae56255be6
6 changed files with 97 additions and 144 deletions

View File

@ -1117,6 +1117,18 @@ namespace NppDarkMode
::SendMessage(hwnd, TB_SETCOLORSCHEME, 0, reinterpret_cast<LPARAM>(&scheme)); ::SendMessage(hwnd, TB_SETCOLORSCHEME, 0, reinterpret_cast<LPARAM>(&scheme));
} }
void setDarkListView(HWND hwnd)
{
bool useDark = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
HWND hHeader = ListView_GetHeader(hwnd);
NppDarkMode::allowDarkModeForWindow(hHeader, useDark);
SetWindowTheme(hHeader, L"ItemsView", nullptr);
NppDarkMode::allowDarkModeForWindow(hwnd, useDark);
SetWindowTheme(hwnd, L"Explorer", nullptr);
}
void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView) void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView)
{ {
if (isTreeView) if (isTreeView)

View File

@ -89,6 +89,7 @@ namespace NppDarkMode
void setDarkScrollBar(HWND hwnd); void setDarkScrollBar(HWND hwnd);
void setDarkTooltips(HWND hwnd, ToolTipsType type); void setDarkTooltips(HWND hwnd, ToolTipsType type);
void setDarkLineAbovePanelToolbar(HWND hwnd); void setDarkLineAbovePanelToolbar(HWND hwnd);
void setDarkListView(HWND hwnd);
void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView = false); void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView = false);
} }

View File

@ -26,14 +26,14 @@ using namespace std;
void ListView::init(HINSTANCE hInst, HWND parent) void ListView::init(HINSTANCE hInst, HWND parent)
{ {
Window::init(hInst, parent); Window::init(hInst, parent);
INITCOMMONCONTROLSEX icex; INITCOMMONCONTROLSEX icex;
// Ensure that the common control DLL is loaded. // Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES; icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex); InitCommonControlsEx(&icex);
// Create the list-view window in report view with label editing enabled. // Create the list-view window in report view with label editing enabled.
int listViewStyles = LVS_REPORT | LVS_NOSORTHEADER\ int listViewStyles = LVS_REPORT | LVS_NOSORTHEADER\
| LVS_SINGLESEL | LVS_AUTOARRANGE\ | LVS_SINGLESEL | LVS_AUTOARRANGE\
| LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; | LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
@ -54,7 +54,7 @@ void ListView::init(HINSTANCE hInst, HWND parent)
throw std::runtime_error("ListView::init : CreateWindowEx() function return null"); throw std::runtime_error("ListView::init : CreateWindowEx() function return null");
} }
NppDarkMode::setExplorerTheme(_hSelf, true); NppDarkMode::setDarkListView(_hSelf);
::SetWindowLongPtr(_hSelf, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); ::SetWindowLongPtr(_hSelf, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
_defaultProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(_hSelf, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(staticProc))); _defaultProc = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(_hSelf, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(staticProc)));
@ -66,8 +66,7 @@ void ListView::init(HINSTANCE hInst, HWND parent)
if (_columnInfos.size()) if (_columnInfos.size())
{ {
LVCOLUMN lvColumn; LVCOLUMN lvColumn;
lvColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT; lvColumn.mask = LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = HDF_OWNERDRAW;
short i = 0; short i = 0;
for (auto it = _columnInfos.begin(); it != _columnInfos.end(); ++it) for (auto it = _columnInfos.begin(); it != _columnInfos.end(); ++it)
@ -171,30 +170,33 @@ LRESULT ListView::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{ {
switch (Message) switch (Message)
{ {
case WM_DRAWITEM: case WM_NOTIFY:
{ {
DRAWITEMSTRUCT* pdis = (DRAWITEMSTRUCT*)lParam; switch (reinterpret_cast<LPNMHDR>(lParam)->code)
{
case NM_CUSTOMDRAW:
{
LPNMCUSTOMDRAW nmcd = reinterpret_cast<LPNMCUSTOMDRAW>(lParam);
switch (nmcd->dwDrawStage)
{
case CDDS_PREPAINT:
{
return CDRF_NOTIFYITEMDRAW;
}
case CDDS_ITEMPREPAINT:
HDITEM hdi; {
TCHAR lpBuffer[256]; bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
SetTextColor(nmcd->hdc, isDarkModeSupported ? NppDarkMode::getDarkerTextColor() : GetSysColor(COLOR_BTNTEXT));
hdi.mask = HDI_TEXT; return CDRF_DODEFAULT;
hdi.pszText = lpBuffer; }
hdi.cchTextMax = 256; break;
}
Header_GetItem(pdis->hwndItem, pdis->itemID, &hdi); }
break;
COLORREF textColor = RGB(0, 0, 0); }
if (NppDarkMode::isEnabled())
textColor = NppDarkMode::getDarkerTextColor();
SetTextColor(pdis->hDC, textColor);
SetBkMode(pdis->hDC, TRANSPARENT);
::DrawText(pdis->hDC, lpBuffer, lstrlen(lpBuffer), &(pdis->rcItem), DT_SINGLELINE | DT_VCENTER | DT_CENTER);
} }
return TRUE; break;
} }
return ::CallWindowProc(_defaultProc, hwnd, Message, wParam, lParam); return ::CallWindowProc(_defaultProc, hwnd, Message, wParam, lParam);
} }

View File

@ -55,13 +55,13 @@ INT_PTR CALLBACK AnsiCharPanel::run_dlgProc(UINT message, WPARAM wParam, LPARAM
case NPPM_INTERNAL_REFRESHDARKMODE: case NPPM_INTERNAL_REFRESHDARKMODE:
{ {
NppDarkMode::setExplorerTheme(_listView.getHSelf(), true); NppDarkMode::setDarkListView(_listView.getHSelf());
return TRUE; return TRUE;
} }
case WM_NOTIFY: case WM_NOTIFY:
{ {
switch (((LPNMHDR)lParam)->code) switch (reinterpret_cast<LPNMHDR>(lParam)->code)
{ {
case DMN_CLOSE: case DMN_CLOSE:
{ {
@ -119,65 +119,33 @@ INT_PTR CALLBACK AnsiCharPanel::run_dlgProc(UINT message, WPARAM wParam, LPARAM
} }
break; break;
case NM_CUSTOMDRAW:
{
static bool becomeDarkMode = false;
static bool becomeLightMode = false;
HWND hHeader = ListView_GetHeader(_listView.getHSelf());
if (NppDarkMode::isEnabled() && reinterpret_cast<LPNMHDR>(lParam)->hwndFrom == hHeader)
{
if (!becomeDarkMode)
{
NppDarkMode::setExplorerTheme(hHeader, false);
becomeDarkMode = true;
}
becomeLightMode = false;
auto nmtbcd = reinterpret_cast<LPNMTBCUSTOMDRAW>(lParam);
SetBkMode(nmtbcd->nmcd.hdc, TRANSPARENT);
FillRect(nmtbcd->nmcd.hdc, &nmtbcd->nmcd.rc, NppDarkMode::getBackgroundBrush());
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_NOTIFYSUBITEMDRAW);
}
else
{
if (!becomeLightMode)
{
NppDarkMode::setExplorerTheme(hHeader, true);
becomeLightMode = true;
}
becomeDarkMode = false;
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_DODEFAULT);
}
}
break;
default: default:
break; break;
} }
} }
return TRUE; return TRUE;
case WM_SIZE: case WM_SIZE:
{ {
int width = LOWORD(lParam); int width = LOWORD(lParam);
int height = HIWORD(lParam); int height = HIWORD(lParam);
::MoveWindow(_listView.getHSelf(), 0, 0, width, height, TRUE); ::MoveWindow(_listView.getHSelf(), 0, 0, width, height, TRUE);
break; break;
} }
default : default :
return DockingDlgInterface::run_dlgProc(message, wParam, lParam); return DockingDlgInterface::run_dlgProc(message, wParam, lParam);
} }
return DockingDlgInterface::run_dlgProc(message, wParam, lParam); return DockingDlgInterface::run_dlgProc(message, wParam, lParam);
} }
void AnsiCharPanel::insertChar(unsigned char char2insert) const void AnsiCharPanel::insertChar(unsigned char char2insert) const
{ {
char charStr[2]; char charStr[2];
charStr[0] = char2insert; charStr[0] = char2insert;
charStr[1] = '\0'; charStr[1] = '\0';
wchar_t wCharStr[10]; wchar_t wCharStr[10];
char multiByteStr[10]; char multiByteStr[10];
int codepage = (*_ppEditView)->getCurrentBuffer()->getEncoding(); int codepage = (*_ppEditView)->getCurrentBuffer()->getEncoding();
if (codepage == -1) if (codepage == -1)
{ {

View File

@ -73,48 +73,15 @@ INT_PTR CALLBACK VerticalFileSwitcher::run_dlgProc(UINT message, WPARAM wParam,
case NPPM_INTERNAL_REFRESHDARKMODE: case NPPM_INTERNAL_REFRESHDARKMODE:
{ {
NppDarkMode::setExplorerTheme(_fileListView.getHSelf(), true); NppDarkMode::setDarkListView(_fileListView.getHSelf());
NppDarkMode::setDarkTooltips(_fileListView.getHSelf(), NppDarkMode::ToolTipsType::listview); NppDarkMode::setDarkTooltips(_fileListView.getHSelf(), NppDarkMode::ToolTipsType::listview);
return TRUE; return TRUE;
} }
case WM_NOTIFY: case WM_NOTIFY:
{ {
LPNMHDR notif = reinterpret_cast<LPNMHDR>(lParam); switch (reinterpret_cast<LPNMHDR>(lParam)->code)
switch (notif->code)
{ {
case NM_CUSTOMDRAW:
{
static bool becomeDarkMode = false;
static bool becomeLightMode = false;
HWND hHeader = ListView_GetHeader(_fileListView.getHSelf());
if (NppDarkMode::isEnabled() && (notif->hwndFrom == hHeader))
{
if (!becomeDarkMode)
{
NppDarkMode::setExplorerTheme(hHeader, false);
becomeDarkMode = true;
}
becomeLightMode = false;
auto nmtbcd = reinterpret_cast<LPNMTBCUSTOMDRAW>(notif);
SetBkMode(nmtbcd->nmcd.hdc, TRANSPARENT);
FillRect(nmtbcd->nmcd.hdc, &nmtbcd->nmcd.rc, NppDarkMode::getBackgroundBrush());
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_NOTIFYSUBITEMDRAW);
}
else
{
if (!becomeLightMode)
{
NppDarkMode::setExplorerTheme(hHeader, true);
becomeLightMode = true;
}
becomeDarkMode = false;
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_DODEFAULT);
}
}
break;
case NM_DBLCLK: case NM_DBLCLK:
{ {
LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE) lParam; LPNMITEMACTIVATE lpnmitem = (LPNMITEMACTIVATE) lParam;

View File

@ -28,14 +28,14 @@ void VerticalFileSwitcherListView::init(HINSTANCE hInst, HWND parent, HIMAGELIST
{ {
Window::init(hInst, parent); Window::init(hInst, parent);
_hImaLst = hImaLst; _hImaLst = hImaLst;
INITCOMMONCONTROLSEX icex; INITCOMMONCONTROLSEX icex;
// Ensure that the common control DLL is loaded. // Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES; icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex); InitCommonControlsEx(&icex);
// Create the list-view window in report view with label editing enabled. // Create the list-view window in report view with label editing enabled.
int listViewStyles = LVS_REPORT /*| LVS_SINGLESEL*/ | LVS_AUTOARRANGE\ int listViewStyles = LVS_REPORT /*| LVS_SINGLESEL*/ | LVS_AUTOARRANGE\
| LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS; | LVS_SHAREIMAGELISTS | LVS_SHOWSELALWAYS;
@ -55,7 +55,7 @@ void VerticalFileSwitcherListView::init(HINSTANCE hInst, HWND parent, HIMAGELIST
throw std::runtime_error("VerticalFileSwitcherListView::init : CreateWindowEx() function return null"); throw std::runtime_error("VerticalFileSwitcherListView::init : CreateWindowEx() function return null");
} }
NppDarkMode::setExplorerTheme(_hSelf, true); NppDarkMode::setDarkListView(_hSelf);
NppDarkMode::setDarkTooltips(_hSelf, NppDarkMode::ToolTipsType::listview); NppDarkMode::setDarkTooltips(_hSelf, NppDarkMode::ToolTipsType::listview);
::SetWindowLongPtr(_hSelf, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)); ::SetWindowLongPtr(_hSelf, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
@ -86,29 +86,33 @@ LRESULT VerticalFileSwitcherListView::runProc(HWND hwnd, UINT Message, WPARAM wP
{ {
switch (Message) switch (Message)
{ {
case WM_DRAWITEM: case WM_NOTIFY:
{ {
DRAWITEMSTRUCT* pdis = (DRAWITEMSTRUCT*)lParam; switch (reinterpret_cast<LPNMHDR>(lParam)->code)
{
case NM_CUSTOMDRAW:
{
LPNMCUSTOMDRAW nmcd = reinterpret_cast<LPNMCUSTOMDRAW>(lParam);
switch (nmcd->dwDrawStage)
{
case CDDS_PREPAINT:
{
return CDRF_NOTIFYITEMDRAW;
}
HDITEM hdi; case CDDS_ITEMPREPAINT:
TCHAR lpBuffer[256]; {
bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
hdi.mask = HDI_TEXT; SetTextColor(nmcd->hdc, isDarkModeSupported ? NppDarkMode::getDarkerTextColor() : GetSysColor(COLOR_BTNTEXT));
hdi.pszText = lpBuffer; return CDRF_DODEFAULT;
hdi.cchTextMax = 256; }
break;
Header_GetItem(pdis->hwndItem, pdis->itemID, &hdi); }
}
COLORREF textColor = RGB(0, 0, 0); break;
if (NppDarkMode::isEnabled()) }
textColor = NppDarkMode::getDarkerTextColor();
SetTextColor(pdis->hDC, textColor);
SetBkMode(pdis->hDC, TRANSPARENT);
::DrawText(pdis->hDC, lpBuffer, lstrlen(lpBuffer), &(pdis->rcItem), DT_SINGLELINE | DT_VCENTER | DT_LEFT);
} }
return TRUE; break;
} }
return ::CallWindowProc(_defaultProc, hwnd, Message, wParam, lParam); return ::CallWindowProc(_defaultProc, hwnd, Message, wParam, lParam);
} }
@ -380,10 +384,9 @@ void VerticalFileSwitcherListView::insertColumn(const TCHAR *name, int width, in
{ {
LVCOLUMN lvColumn; LVCOLUMN lvColumn;
lvColumn.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT; lvColumn.mask = LVCF_TEXT | LVCF_WIDTH;
lvColumn.cx = width; lvColumn.cx = width;
lvColumn.pszText = (TCHAR *)name; lvColumn.pszText = (TCHAR *)name;
lvColumn.fmt = HDF_OWNERDRAW;
ListView_InsertColumn(_hSelf, index, &lvColumn); // index is not 0 based but 1 based ListView_InsertColumn(_hSelf, index, &lvColumn); // index is not 0 based but 1 based
} }