Make dark mode restartless

Fix #10121, close #10122
pull/10134/head
ozone10 2021-07-08 20:11:55 +02:00 committed by Don Ho
parent eac8c4094f
commit 066ef8a4a0
15 changed files with 198 additions and 239 deletions

View File

@ -17,7 +17,7 @@ enum IMMERSIVE_HC_CACHE_MODE
};
// 1903 18362
enum PreferredAppMode
enum class PreferredAppMode
{
Default,
AllowDark,
@ -85,7 +85,7 @@ fnSetWindowCompositionAttribute _SetWindowCompositionAttribute = nullptr;
fnShouldAppsUseDarkMode _ShouldAppsUseDarkMode = nullptr;
fnAllowDarkModeForWindow _AllowDarkModeForWindow = nullptr;
fnAllowDarkModeForApp _AllowDarkModeForApp = nullptr;
//fnFlushMenuThemes _FlushMenuThemes = nullptr;
fnFlushMenuThemes _FlushMenuThemes = nullptr;
fnRefreshImmersiveColorPolicyState _RefreshImmersiveColorPolicyState = nullptr;
fnIsDarkModeAllowedForWindow _IsDarkModeAllowedForWindow = nullptr;
fnGetIsImmersiveColorUsingHighContrast _GetIsImmersiveColorUsingHighContrast = nullptr;
@ -173,7 +173,15 @@ void AllowDarkModeForApp(bool allow)
if (_AllowDarkModeForApp)
_AllowDarkModeForApp(allow);
else if (_SetPreferredAppMode)
_SetPreferredAppMode(allow ? ForceDark : Default);
_SetPreferredAppMode(allow ? PreferredAppMode::ForceDark : PreferredAppMode::Default);
}
void FlushMenuThemes()
{
if (_FlushMenuThemes)
{
_FlushMenuThemes();
}
}
// limit dark scroll bar to specific windows and their children
@ -204,7 +212,7 @@ bool IsWindowOrParentUsingDarkScrollBar(HWND hwnd)
void FixDarkScrollBar()
{
HMODULE hComctl = LoadLibraryExW(L"comctl32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
HMODULE hComctl = LoadLibraryEx(L"comctl32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hComctl)
{
auto addr = FindDelayLoadThunkInModule(hComctl, "uxtheme.dll", 49); // OpenNcThemeData
@ -241,9 +249,9 @@ constexpr bool CheckBuildNumber(DWORD buildNumber)
buildNumber == 19043); // 21H1
}
void InitDarkMode(bool fixDarkScrollbar, bool dark)
void InitDarkMode()
{
auto RtlGetNtVersionNumbers = reinterpret_cast<fnRtlGetNtVersionNumbers>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetNtVersionNumbers"));
auto RtlGetNtVersionNumbers = reinterpret_cast<fnRtlGetNtVersionNumbers>(GetProcAddress(GetModuleHandle(L"ntdll.dll"), "RtlGetNtVersionNumbers"));
if (RtlGetNtVersionNumbers)
{
DWORD major, minor;
@ -251,7 +259,7 @@ void InitDarkMode(bool fixDarkScrollbar, bool dark)
g_buildNumber &= ~0xF0000000;
if (major == 10 && minor == 0 && CheckBuildNumber(g_buildNumber))
{
HMODULE hUxtheme = LoadLibraryExW(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
HMODULE hUxtheme = LoadLibraryEx(L"uxtheme.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (hUxtheme)
{
_OpenNcThemeData = reinterpret_cast<fnOpenNcThemeData>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(49)));
@ -266,7 +274,7 @@ void InitDarkMode(bool fixDarkScrollbar, bool dark)
else
_SetPreferredAppMode = reinterpret_cast<fnSetPreferredAppMode>(ord135);
//_FlushMenuThemes = reinterpret_cast<fnFlushMenuThemes>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136)));
_FlushMenuThemes = reinterpret_cast<fnFlushMenuThemes>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(136)));
_IsDarkModeAllowedForWindow = reinterpret_cast<fnIsDarkModeAllowedForWindow>(GetProcAddress(hUxtheme, MAKEINTRESOURCEA(137)));
_SetWindowCompositionAttribute = reinterpret_cast<fnSetWindowCompositionAttribute>(GetProcAddress(GetModuleHandleW(L"user32.dll"), "SetWindowCompositionAttribute"));
@ -276,22 +284,27 @@ void InitDarkMode(bool fixDarkScrollbar, bool dark)
_ShouldAppsUseDarkMode &&
_AllowDarkModeForWindow &&
(_AllowDarkModeForApp || _SetPreferredAppMode) &&
//_FlushMenuThemes &&
_FlushMenuThemes &&
_IsDarkModeAllowedForWindow)
{
g_darkModeSupported = true;
}
}
}
}
}
AllowDarkModeForApp(dark);
_RefreshImmersiveColorPolicyState();
g_darkModeEnabled = _ShouldAppsUseDarkMode() && !IsHighContrast();
if (fixDarkScrollbar)
void SetDarkMode(bool useDark, bool fixDarkScrollbar)
{
if (g_darkModeSupported)
{
AllowDarkModeForApp(useDark);
//_RefreshImmersiveColorPolicyState();
FlushMenuThemes();
if (useDark && fixDarkScrollbar)
{
FixDarkScrollBar();
}
}
}
}
g_darkModeEnabled = ShouldAppsUseDarkMode() && !IsHighContrast();
}
}

View File

@ -13,5 +13,5 @@ bool IsColorSchemeChangeMessage(LPARAM lParam);
bool IsColorSchemeChangeMessage(UINT message, LPARAM lParam);
void AllowDarkModeForApp(bool allow);
void EnableDarkScrollBarForWindowAndChildren(HWND hwnd);
void InitDarkMode(bool fixDarkScrollbar, bool dark);
void InitDarkMode();
void SetDarkMode(bool useDarkMode, bool fixDarkScrollbar);

View File

@ -7515,7 +7515,7 @@ void Notepad_plus::restoreMinimizeDialogs()
void Notepad_plus::refreshDarkMode()
{
SendMessage(_pPublicInterface->getHSelf(), NPPM_SETEDITORBORDEREDGE, 0, NppParameters::getInstance().getSVP()._showBorderEdge);
if (NppDarkMode::isExperimentalEnabled())
if (NppDarkMode::isExperimentalSupported())
{
NppDarkMode::allowDarkModeForApp(NppDarkMode::isEnabled());
}
@ -7640,7 +7640,7 @@ void Notepad_plus::refreshDarkMode()
}
}
if (NppDarkMode::isExperimentalEnabled())
if (NppDarkMode::isExperimentalSupported())
{
RECT rcClient;

View File

@ -70,7 +70,7 @@ LRESULT CALLBACK Notepad_plus_Window::Notepad_plus_Proc(HWND hwnd, UINT message,
pM30ide->_hSelf = hwnd;
::SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pM30ide));
if (NppDarkMode::isExperimentalEnabled() && NppDarkMode::isEnabled() && NppDarkMode::isScrollbarHackEnabled())
if (NppDarkMode::isExperimentalSupported())
{
NppDarkMode::enableDarkScrollBarForWindowAndChildren(hwnd);
}
@ -99,7 +99,7 @@ LRESULT Notepad_plus_Window::runProc(HWND hwnd, UINT message, WPARAM wParam, LPA
_notepad_plus_plus_core._pPublicInterface = this;
LRESULT lRet = _notepad_plus_plus_core.init(hwnd);
if (NppDarkMode::isExperimentalEnabled() && NppDarkMode::isEnabled())
if (NppDarkMode::isEnabled() && NppDarkMode::isExperimentalSupported())
{
RECT rcClient;
GetWindowRect(hwnd, &rcClient);

View File

@ -97,10 +97,8 @@ namespace NppDarkMode
{
_options = configuredOptions();
if (_options.enableExperimental)
{
initExperimentalDarkMode(_options.enableScrollbarHack, _options.enable);
}
initExperimentalDarkMode();
setDarkMode(_options.enable, _options.enableScrollbarHack);
}
// attempts to apply new options from NppParameters, sends NPPM_INTERNAL_REFRESHDARKMODE to hwnd's top level parent
@ -114,6 +112,7 @@ namespace NppDarkMode
{
supportedChanged = true;
_options.enable = config.enable;
setDarkMode(_options.enable, _options.enable);
}
if (_options.enableMenubar != config.enableMenubar)
@ -144,11 +143,6 @@ namespace NppDarkMode
return _options.enableMenubar;
}
bool isExperimentalEnabled()
{
return _options.enableExperimental;
}
bool isScrollbarHackEnabled()
{
return _options.enableScrollbarHack;
@ -215,7 +209,7 @@ namespace NppDarkMode
{
UNREFERENCED_PARAMETER(hwnd);
if (!isExperimentalEnabled())
if (!isExperimentalSupported())
{
return;
}
@ -386,9 +380,14 @@ namespace NppDarkMode
// from DarkMode.h
void initExperimentalDarkMode(bool fixDarkScrollbar, bool dark)
void initExperimentalDarkMode()
{
::InitDarkMode(fixDarkScrollbar, dark);
::InitDarkMode();
}
void setDarkMode(bool useDark, bool fixDarkScrollbar)
{
::SetDarkMode(useDark, fixDarkScrollbar);
}
void allowDarkModeForApp(bool allow)
@ -401,9 +400,9 @@ namespace NppDarkMode
return ::AllowDarkModeForWindow(hWnd, allow);
}
void setTitleBarThemeColor(HWND hWnd, bool dark)
void setTitleBarThemeColor(HWND hWnd)
{
::SetTitleBarThemeColor(hWnd, dark);
::RefreshTitleBarThemeColor(hWnd);
}
void enableDarkScrollBarForWindowAndChildren(HWND hwnd)
@ -1044,12 +1043,8 @@ namespace NppDarkMode
void setDarkTitleBar(HWND hwnd)
{
bool useDark = NppDarkMode::isExperimentalEnabled() && NppDarkMode::isEnabled();
NppDarkMode::allowDarkModeForWindow(hwnd, useDark);
SetWindowTheme(hwnd, useDark ? L"Explorer" : nullptr, nullptr);
NppDarkMode::setTitleBarThemeColor(hwnd, useDark);
NppDarkMode::allowDarkModeForWindow(hwnd, NppDarkMode::isEnabled());
NppDarkMode::setTitleBarThemeColor(hwnd);
}
void setDarkExplorerTheme(HWND hwnd)
@ -1119,7 +1114,7 @@ namespace NppDarkMode
void setDarkListView(HWND hwnd)
{
bool useDark = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
bool useDark = NppDarkMode::isEnabled();
HWND hHeader = ListView_GetHeader(hwnd);
NppDarkMode::allowDarkModeForWindow(hHeader, useDark);
@ -1129,20 +1124,20 @@ namespace NppDarkMode
SetWindowTheme(hwnd, L"Explorer", nullptr);
}
void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView)
void disableVisualStyle(HWND hwnd, bool doDisable)
{
if (isTreeView)
{
SetWindowTheme(hwnd, nullptr, nullptr);
}
else if (doEnable)
{
NppDarkMode::allowDarkModeForWindow(hwnd, NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled());
SetWindowTheme(hwnd, L"Explorer", nullptr);
}
else
if (doDisable)
{
SetWindowTheme(hwnd, L"", L"");
}
else
{
SetWindowTheme(hwnd, nullptr, nullptr);
}
}
void redrawTreeViewScrollBar(HWND hwnd)
{
SetWindowTheme(hwnd, nullptr, nullptr); //hack to redraw treeview scrollbar
}
}

View File

@ -16,7 +16,6 @@ namespace NppDarkMode
struct Options
{
bool enable = false;
bool enableExperimental = false;
bool enableMenubar = false;
bool enableScrollbarHack = false;
};
@ -35,8 +34,8 @@ namespace NppDarkMode
bool isEnabled();
bool isDarkMenuEnabled();
bool isExperimentalEnabled();
bool isScrollbarHackEnabled();
bool isExperimentalSupported();
COLORREF invertLightness(COLORREF c);
COLORREF invertLightnessSofter(COLORREF c);
@ -68,10 +67,11 @@ namespace NppDarkMode
void drawUAHMenuNCBottomLine(HWND hWnd);
// from DarkMode.h
void initExperimentalDarkMode(bool fixDarkScrollbar, bool dark);
void initExperimentalDarkMode();
void setDarkMode(bool useDark, bool fixDarkScrollbar);
void allowDarkModeForApp(bool allow);
bool allowDarkModeForWindow(HWND hWnd, bool allow);
void setTitleBarThemeColor(HWND hWnd, bool dark);
void setTitleBarThemeColor(HWND hWnd);
// enhancements to DarkMode.h
void enableDarkScrollBarForWindowAndChildren(HWND hwnd);
@ -91,5 +91,6 @@ namespace NppDarkMode
void setDarkLineAbovePanelToolbar(HWND hwnd);
void setDarkListView(HWND hwnd);
void setExplorerTheme(HWND hwnd, bool doEnable, bool isTreeView = false);
void disableVisualStyle(HWND hwnd, bool doDisable);
void redrawTreeViewScrollBar(HWND hwnd);
}

View File

@ -5388,7 +5388,6 @@ void NppParameters::feedGUIParameters(TiXmlNode *node)
};
_nppGUI._darkmode.enable = parseYesNoBoolAttribute(TEXT("enable"));
_nppGUI._darkmode.enableExperimental = parseYesNoBoolAttribute(TEXT("enableExperimental"));
_nppGUI._darkmode.enableMenubar = parseYesNoBoolAttribute(TEXT("enableMenubar"));
_nppGUI._darkmode.enableScrollbarHack = parseYesNoBoolAttribute(TEXT("enableScrollbarHack"));
}
@ -6414,7 +6413,6 @@ void NppParameters::createXmlTreeFromGUIParams()
};
setYesNoBoolAttribute(TEXT("enable"), _nppGUI._darkmode.enable);
setYesNoBoolAttribute(TEXT("enableExperimental"), _nppGUI._darkmode.enableExperimental);
setYesNoBoolAttribute(TEXT("enableMenubar"), _nppGUI._darkmode.enableMenubar);
setYesNoBoolAttribute(TEXT("enableScrollbarHack"), _nppGUI._darkmode.enableScrollbarHack);
}

View File

@ -186,7 +186,7 @@ LRESULT ListView::runProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
case CDDS_ITEMPREPAINT:
{
bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalSupported();
SetTextColor(nmcd->hdc, isDarkModeSupported ? NppDarkMode::getDarkerTextColor() : GetSysColor(COLOR_BTNTEXT));
return CDRF_DODEFAULT;
}

View File

@ -174,8 +174,8 @@ INT_PTR CALLBACK FileBrowser::run_dlgProc(UINT message, WPARAM wParam, LPARAM lP
NppDarkMode::setDarkTooltips(_hToolbarMenu, NppDarkMode::ToolTipsType::toolbar);
NppDarkMode::setDarkLineAbovePanelToolbar(_hToolbarMenu);
NppDarkMode::setExplorerTheme(_treeView.getHSelf(), true, true);
NppDarkMode::setDarkTooltips(_treeView.getHSelf(), NppDarkMode::ToolTipsType::treeview);
NppDarkMode::redrawTreeViewScrollBar(_treeView.getHSelf());
return TRUE;
}
@ -717,19 +717,11 @@ void FileBrowser::notified(LPNMHDR notification)
}
}
else if (notification->code == NM_CUSTOMDRAW && (notification->hwndFrom == _hToolbarMenu))
{
NMTBCUSTOMDRAW* nmtbcd = reinterpret_cast<NMTBCUSTOMDRAW*>(notification);
if (nmtbcd->nmcd.dwDrawStage == CDDS_PREERASE)
{
if (NppDarkMode::isEnabled())
{
auto nmtbcd = reinterpret_cast<LPNMTBCUSTOMDRAW>(notification);
FillRect(nmtbcd->nmcd.hdc, &nmtbcd->nmcd.rc, NppDarkMode::getBackgroundBrush());
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_SKIPDEFAULT);
}
else
{
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_DODEFAULT);
}
}
}
}

View File

@ -597,32 +597,10 @@ void FunctionListPanel::notified(LPNMHDR notification)
}
else if (notification->code == NM_CUSTOMDRAW && (notification->hwndFrom == _hToolbarMenu))
{
static bool becomeDarkMode = false;
static bool becomeLightMode = false;
if (NppDarkMode::isEnabled())
{
if (!becomeDarkMode)
{
NppDarkMode::setExplorerTheme(_hToolbarMenu, false);
becomeDarkMode = true;
}
becomeLightMode = false;
auto nmtbcd = reinterpret_cast<LPNMTBCUSTOMDRAW>(notification);
FillRect(nmtbcd->nmcd.hdc, &nmtbcd->nmcd.rc, NppDarkMode::getBackgroundBrush());
nmtbcd->clrText = NppDarkMode::getTextColor();
nmtbcd->clrHighlightHotTrack = NppDarkMode::getHighlightHotTrackColor();
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_NOTIFYSUBITEMDRAW | TBCDRF_HILITEHOTTRACK);
}
else
{
if (!becomeLightMode)
{
NppDarkMode::setExplorerTheme(_hToolbarMenu, true);
becomeLightMode = true;
}
becomeDarkMode = false;
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_DODEFAULT);
}
}
}
@ -891,8 +869,8 @@ INT_PTR CALLBACK FunctionListPanel::run_dlgProc(UINT message, WPARAM wParam, LPA
NppDarkMode::setDarkTooltips(_hToolbarMenu, NppDarkMode::ToolTipsType::toolbar);
NppDarkMode::setDarkLineAbovePanelToolbar(_hToolbarMenu);
NppDarkMode::setExplorerTheme(_treeView.getHSelf(), true, true);
NppDarkMode::setDarkTooltips(_treeView.getHSelf(), NppDarkMode::ToolTipsType::treeview);
NppDarkMode::redrawTreeViewScrollBar(_treeView.getHSelf());
return TRUE;
}

View File

@ -831,7 +831,6 @@ INT_PTR CALLBACK DarkModeSubDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM
case IDC_CHECK_DARKMODE_ENABLE:
bool enableDarkMode = isCheckedOrNot(static_cast<int>(wParam));
nppGUI._darkmode.enable = enableDarkMode;
nppGUI._darkmode.enableExperimental = enableDarkMode;
nppGUI._darkmode.enableMenubar = enableDarkMode;
nppGUI._darkmode.enableScrollbarHack = enableDarkMode;

View File

@ -56,6 +56,7 @@ INT_PTR CALLBACK ProjectPanel::run_dlgProc(UINT message, WPARAM wParam, LPARAM l
0,0,0,0,_hSelf, nullptr, _hInst, nullptr);
NppDarkMode::setDarkLineAbovePanelToolbar(_hToolbarMenu);
NppDarkMode::disableVisualStyle(_hToolbarMenu, NppDarkMode::isEnabled());
TBBUTTON tbButtons[2];
@ -100,9 +101,10 @@ INT_PTR CALLBACK ProjectPanel::run_dlgProc(UINT message, WPARAM wParam, LPARAM l
case NPPM_INTERNAL_REFRESHDARKMODE:
{
NppDarkMode::setDarkLineAbovePanelToolbar(_hToolbarMenu);
NppDarkMode::disableVisualStyle(_hToolbarMenu, NppDarkMode::isEnabled());
NppDarkMode::setExplorerTheme(_treeView.getHSelf(), true, true);
NppDarkMode::setDarkTooltips(_treeView.getHSelf(), NppDarkMode::ToolTipsType::treeview);
NppDarkMode::redrawTreeViewScrollBar(_treeView.getHSelf());
return TRUE;
}
@ -817,27 +819,11 @@ void ProjectPanel::notified(LPNMHDR notification)
{
if (NppDarkMode::isEnabled())
{
if (!_becomeDarkMode)
{
NppDarkMode::setExplorerTheme(_hToolbarMenu, false);
_becomeDarkMode = true;
}
_becomeLightMode = false;
auto nmtbcd = reinterpret_cast<LPNMTBCUSTOMDRAW>(notification);
FillRect(nmtbcd->nmcd.hdc, &nmtbcd->nmcd.rc, NppDarkMode::getBackgroundBrush());
nmtbcd->clrText = NppDarkMode::getTextColor();
nmtbcd->clrHighlightHotTrack = NppDarkMode::getHighlightHotTrackColor();
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_NOTIFYSUBITEMDRAW | TBCDRF_HILITEHOTTRACK);
}
else
{
if (!_becomeLightMode)
{
NppDarkMode::setExplorerTheme(_hToolbarMenu, true);
_becomeLightMode = true;
}
_becomeDarkMode = false;
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_DODEFAULT);
SetWindowLongPtr(_hSelf, DWLP_MSGRESULT, CDRF_NOTIFYITEMDRAW | TBCDRF_HILITEHOTTRACK);
}
}
}

View File

@ -117,8 +117,6 @@ protected:
generic_string _selDirOfFilesFromDirDlg;
bool _isDirty = false;
int _panelID = 0;
bool _becomeDarkMode = false;
bool _becomeLightMode = false;
void initMenus();
void destroyMenus();

View File

@ -43,7 +43,6 @@ void TreeView::init(HINSTANCE hInst, HWND parent, int treeViewID)
_hInst,
nullptr);
NppDarkMode::setExplorerTheme(_hSelf, true, true);
NppDarkMode::setDarkTooltips(_hSelf, NppDarkMode::ToolTipsType::treeview);
int itemHeight = NppParameters::getInstance()._dpiManager.scaleY(CY_ITEMHEIGHT);

View File

@ -102,7 +102,7 @@ LRESULT VerticalFileSwitcherListView::runProc(HWND hwnd, UINT Message, WPARAM wP
case CDDS_ITEMPREPAINT:
{
bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalEnabled();
bool isDarkModeSupported = NppDarkMode::isEnabled() && NppDarkMode::isExperimentalSupported();
SetTextColor(nmcd->hdc, isDarkModeSupported ? NppDarkMode::getDarkerTextColor() : GetSysColor(COLOR_BTNTEXT));
return CDRF_DODEFAULT;
}