[EXPLORER][SHELL32] Fix and improve Start Menu customization (#6596)

Correct the details of Start Menu customization.
JIRA issue: CORE-16956
- Hide the setting item if the item is restricted.
- Don't change restriction in Explorer.
- Fix Start Menu settings for restriction and registry.
- Fix and simplify code.
This commit is contained in:
Katayama Hirofumi MZ 2024-03-11 20:18:07 +09:00 committed by GitHub
parent 0241b5c4e9
commit 8bd071a51e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 139 additions and 139 deletions

View file

@ -110,7 +110,6 @@ BOOL GetRegValue(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN BOOL bDefault
BOOL SetRegDword(IN LPCWSTR pszSubKey, IN LPCWSTR pszValueName, IN DWORD dwValue);
BOOL GetAdvancedBool(IN LPCWSTR pszValueName, IN BOOL bDefaultValue);
BOOL SetAdvancedDword(IN LPCWSTR pszValueName, IN DWORD dwValue);
BOOL SetRestriction(IN LPCWSTR pszKey, IN LPCWSTR pszValueName, IN DWORD dwValue);
/*
* rshell.c

View file

@ -21,6 +21,7 @@
#include "precomp.h"
#define I_UNCHECKED 1
#define I_CHECKED 2
// TODO: Windows Explorer appears to be calling NewLinkHere / ConfigStartMenu directly for both items.
@ -82,48 +83,55 @@ struct CUSTOMIZE_ENTRY
{
LPARAM id;
LPCWSTR name;
FN_CUSTOMIZE_READ fnRead;
FN_CUSTOMIZE_WRITE fnWrite;
BOOL bDefaultValue;
RESTRICTIONS policy1, policy2;
};
static DWORD CALLBACK CustomizeReadAdvanced(const CUSTOMIZE_ENTRY *entry)
{
return GetAdvancedBool(entry->name, FALSE);
}
static BOOL CALLBACK CustomizeWriteAdvanced(const CUSTOMIZE_ENTRY *entry, DWORD dwValue)
{
return SetAdvancedDword(entry->name, dwValue);
}
static DWORD CALLBACK CustomizeReadRun(const CUSTOMIZE_ENTRY *entry)
{
return !SHRestricted(REST_NORUN);
}
static BOOL CALLBACK CustomizeWriteRest(const CUSTOMIZE_ENTRY *entry, DWORD dwValue)
{
SetRestriction(L"Explorer", entry->name, !dwValue);
return TRUE;
}
static const CUSTOMIZE_ENTRY s_CustomizeEntries[] =
{
// FIXME: Make "StartMenuAdminTools" effective
//{ IDS_ADVANCED_DISPLAY_ADMINTOOLS, L"StartMenuAdminTools", CustomizeRead1, CustomizeWrite1 }, // FIXME
{ IDS_ADVANCED_DISPLAY_FAVORITES, L"StartMenuFavorites", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_DISPLAY_LOG_OFF, L"StartMenuLogoff", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_DISPLAY_RUN, L"NoRun", CustomizeReadRun, CustomizeWriteRest },
{ IDS_ADVANCED_EXPAND_MY_DOCUMENTS, L"CascadeMyDocuments", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_EXPAND_MY_PICTURES, L"CascadeMyPictures", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_EXPAND_CONTROL_PANEL, L"CascadeControlPanel", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_EXPAND_PRINTERS, L"CascadePrinters", CustomizeReadAdvanced, CustomizeWriteAdvanced },
{ IDS_ADVANCED_EXPAND_NET_CONNECTIONS, L"CascadeNetworkConnections", CustomizeReadAdvanced, CustomizeWriteAdvanced },
// FIXME: Make "StartMenuAdminTools" effective for IDS_ADVANCED_DISPLAY_ADMINTOOLS
{
IDS_ADVANCED_DISPLAY_FAVORITES, L"StartMenuFavorites", FALSE,
REST_NOFAVORITESMENU
},
{
IDS_ADVANCED_DISPLAY_LOG_OFF, L"StartMenuLogoff", FALSE,
REST_STARTMENULOGOFF
},
{
IDS_ADVANCED_DISPLAY_RUN, L"StartMenuRun", TRUE,
REST_NORUN
},
{
IDS_ADVANCED_EXPAND_MY_DOCUMENTS, L"CascadeMyDocuments", FALSE,
REST_NOSMMYDOCS
},
{
IDS_ADVANCED_EXPAND_MY_PICTURES, L"CascadeMyPictures", FALSE,
REST_NOSMMYPICS
},
{
IDS_ADVANCED_EXPAND_CONTROL_PANEL, L"CascadeControlPanel", FALSE,
REST_NOSETFOLDERS, REST_NOCONTROLPANEL,
},
{
IDS_ADVANCED_EXPAND_PRINTERS, L"CascadePrinters", FALSE,
REST_NOSETFOLDERS
},
{
IDS_ADVANCED_EXPAND_NET_CONNECTIONS, L"CascadeNetworkConnections", FALSE,
REST_NOSETFOLDERS, REST_NONETWORKCONNECTIONS
},
};
static VOID AddCustomizeItem(HWND hTreeView, const CUSTOMIZE_ENTRY *entry)
{
if (SHRestricted(entry->policy1) || SHRestricted(entry->policy2))
{
TRACE("%p: Restricted\n", entry->id);
return; // Restricted. Don't show
}
TV_INSERTSTRUCT Insert = { TVI_ROOT, TVI_LAST };
Insert.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
@ -132,8 +140,9 @@ static VOID AddCustomizeItem(HWND hTreeView, const CUSTOMIZE_ENTRY *entry)
Insert.item.pszText = szText;
Insert.item.lParam = entry->id;
Insert.item.stateMask = TVIS_STATEIMAGEMASK;
if (entry->fnRead(entry))
Insert.item.state = INDEXTOSTATEIMAGEMASK(I_CHECKED);
BOOL bChecked = GetAdvancedBool(entry->name, entry->bDefaultValue);
Insert.item.state = INDEXTOSTATEIMAGEMASK(bChecked ? I_CHECKED : I_UNCHECKED);
TRACE("%p: %d\n", entry->id, bChecked);
TreeView_InsertItem(hTreeView, &Insert);
}
@ -165,12 +174,16 @@ static BOOL CustomizeClassic_OnOK(HWND hwnd)
item.stateMask = TVIS_STATEIMAGEMASK;
TreeView_GetItem(hTreeView, &item);
BOOL bChecked = (item.state & INDEXTOSTATEIMAGEMASK(I_CHECKED));
BOOL bChecked = !!(item.state & INDEXTOSTATEIMAGEMASK(I_CHECKED));
for (auto& entry : s_CustomizeEntries)
{
if (SHRestricted(entry.policy1) || SHRestricted(entry.policy2))
continue;
if (item.lParam == entry.id)
{
entry.fnWrite(&entry, bChecked);
TRACE("%p: %d\n", item.lParam, bChecked);
SetAdvancedDword(entry.name, bChecked);
break;
}
}

View file

@ -130,91 +130,80 @@ public:
/* Remove menu items that don't apply */
dwLogoff = SHRestricted(REST_STARTMENULOGOFF);
bWantLogoff = (dwLogoff == 2 ||
SHRestricted(REST_FORCESTARTMENULOGOFF) ||
GetAdvancedBool(L"StartMenuLogoff", FALSE));
/* Favorites */
if (!GetAdvancedBool(L"StartMenuFavorites", FALSE))
if (SHRestricted(REST_NOFAVORITESMENU) ||
!GetAdvancedBool(L"StartMenuFavorites", FALSE))
{
DeleteMenu(hMenu,
IDM_FAVORITES,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_FAVORITES, MF_BYCOMMAND);
}
/* Documents */
if (SHRestricted(REST_NORECENTDOCSMENU))
if (SHRestricted(REST_NORECENTDOCSMENU) ||
!GetAdvancedBool(L"Start_ShowRecentDocs", TRUE))
{
DeleteMenu(hMenu,
IDM_DOCUMENTS,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_DOCUMENTS, MF_BYCOMMAND);
}
/* Settings */
hSettingsMenu = FindSubMenu(hMenu,
IDM_SETTINGS,
FALSE);
if (hSettingsMenu != NULL)
hSettingsMenu = FindSubMenu(hMenu, IDM_SETTINGS, FALSE);
/* Control Panel */
if (SHRestricted(REST_NOSETFOLDERS) ||
SHRestricted(REST_NOCONTROLPANEL) ||
!GetAdvancedBool(L"Start_ShowControlPanel", TRUE))
{
if (SHRestricted(REST_NOSETFOLDERS))
{
/* Control Panel */
if (SHRestricted(REST_NOCONTROLPANEL))
{
DeleteMenu(hSettingsMenu,
IDM_CONTROLPANEL,
MF_BYCOMMAND);
DeleteMenu(hSettingsMenu, IDM_CONTROLPANEL, MF_BYCOMMAND);
/* Delete the separator below it */
DeleteMenu(hSettingsMenu,
0,
MF_BYPOSITION);
}
/* Delete the separator below it */
DeleteMenu(hSettingsMenu, 0, MF_BYPOSITION);
}
/* Network Connections */
if (SHRestricted(REST_NONETWORKCONNECTIONS))
{
DeleteMenu(hSettingsMenu,
IDM_NETWORKCONNECTIONS,
MF_BYCOMMAND);
}
/* Network Connections */
if (SHRestricted(REST_NOSETFOLDERS) ||
SHRestricted(REST_NONETWORKCONNECTIONS) ||
!GetAdvancedBool(L"Start_ShowNetConn", TRUE))
{
DeleteMenu(hSettingsMenu, IDM_NETWORKCONNECTIONS, MF_BYCOMMAND);
}
/* Printers and Faxes */
DeleteMenu(hSettingsMenu,
IDM_PRINTERSANDFAXES,
MF_BYCOMMAND);
}
/* Printers and Faxes */
if (SHRestricted(REST_NOSETFOLDERS) ||
!GetAdvancedBool(L"Start_ShowPrinters", TRUE))
{
DeleteMenu(hSettingsMenu, IDM_PRINTERSANDFAXES, MF_BYCOMMAND);
}
/* Security */
if (GetSystemMetrics(SM_REMOTECONTROL) == 0 ||
SHRestricted(REST_NOSECURITY))
{
DeleteMenu(hSettingsMenu,
IDM_SECURITY,
MF_BYCOMMAND);
}
/* Security */
if (SHRestricted(REST_NOSETFOLDERS) ||
GetSystemMetrics(SM_REMOTECONTROL) == 0 ||
SHRestricted(REST_NOSECURITY))
{
DeleteMenu(hSettingsMenu, IDM_SECURITY, MF_BYCOMMAND);
}
if (GetMenuItemCount(hSettingsMenu) == 0)
{
DeleteMenu(hMenu,
IDM_SETTINGS,
MF_BYCOMMAND);
}
/* Delete Settings menu if it was empty */
if (GetMenuItemCount(hSettingsMenu) == 0)
{
DeleteMenu(hMenu, IDM_SETTINGS, MF_BYCOMMAND);
}
/* Search */
if (SHRestricted(REST_NOFIND))
if (SHRestricted(REST_NOFIND) ||
!GetAdvancedBool(L"Start_ShowSearch", TRUE))
{
DeleteMenu(hMenu,
IDM_SEARCH,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_SEARCH, MF_BYCOMMAND);
}
/* FIXME: Help */
/* Help */
if (SHRestricted(REST_NOSMHELP) ||
!GetAdvancedBool(L"Start_ShowHelp", TRUE))
{
DeleteMenu(hMenu, IDM_HELPANDSUPPORT, MF_BYCOMMAND);
}
/* Run */
if (SHRestricted(REST_NORUN))
if (SHRestricted(REST_NORUN) ||
!GetAdvancedBool(L"StartMenuRun", TRUE))
{
DeleteMenu(hMenu, IDM_RUN, MF_BYCOMMAND);
}
@ -222,13 +211,15 @@ public:
/* Synchronize */
if (!ShowSynchronizeMenuItem())
{
DeleteMenu(hMenu,
IDM_SYNCHRONIZE,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_SYNCHRONIZE, MF_BYCOMMAND);
uLastItemsCount--;
}
/* Log off */
dwLogoff = SHRestricted(REST_STARTMENULOGOFF);
bWantLogoff = (dwLogoff == 2 ||
SHRestricted(REST_FORCESTARTMENULOGOFF) ||
GetAdvancedBool(L"StartMenuLogoff", FALSE));
if (dwLogoff != 1 && bWantLogoff)
{
/* FIXME: We need a more sophisticated way to determine whether to show
@ -246,53 +237,41 @@ public:
szUser))
{
/* We couldn't update the menu item, delete it... */
DeleteMenu(hMenu,
IDM_LOGOFF,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_LOGOFF, MF_BYCOMMAND);
}
}
else
{
DeleteMenu(hMenu,
IDM_LOGOFF,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_LOGOFF, MF_BYCOMMAND);
uLastItemsCount--;
}
/* Disconnect */
if (GetSystemMetrics(SM_REMOTECONTROL) == 0)
if (SHRestricted(REST_NODISCONNECT) ||
GetSystemMetrics(SM_REMOTECONTROL) == 0)
{
DeleteMenu(hMenu,
IDM_DISCONNECT,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_DISCONNECT, MF_BYCOMMAND);
uLastItemsCount--;
}
/* Undock computer */
if (!ShowUndockMenuItem())
{
DeleteMenu(hMenu,
IDM_UNDOCKCOMPUTER,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_UNDOCKCOMPUTER, MF_BYCOMMAND);
uLastItemsCount--;
}
/* Shut down */
if (SHRestricted(REST_NOCLOSE))
{
DeleteMenu(hMenu,
IDM_SHUTDOWN,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_SHUTDOWN, MF_BYCOMMAND);
uLastItemsCount--;
}
if (uLastItemsCount == 0)
{
/* Remove the separator at the end of the menu */
DeleteMenu(hMenu,
IDM_LASTSTARTMENU_SEPARATOR,
MF_BYCOMMAND);
DeleteMenu(hMenu, IDM_LASTSTARTMENU_SEPARATOR, MF_BYCOMMAND);
}
return S_OK;

View file

@ -163,15 +163,6 @@ BOOL SetAdvancedDword(IN LPCWSTR pszValueName, IN DWORD dwValue)
return SetRegDword(REGKEY_ADVANCED, pszValueName, dwValue);
}
BOOL SetRestriction(IN LPCWSTR pszKey, IN LPCWSTR pszValueName, IN DWORD dwValue)
{
WCHAR szSubKey[MAX_PATH] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies";
PathAppendW(szSubKey, pszKey);
SHSetValueW(HKEY_CURRENT_USER, szSubKey, pszValueName, REG_DWORD, &dwValue, sizeof(dwValue));
SHSettingsChanged(NULL, NULL);
return TRUE;
}
BOOL
GetVersionInfoString(IN LPCWSTR szFileName,
IN LPCWSTR szVersionInfo,

View file

@ -196,12 +196,30 @@ private:
HMENU CreateRecentMenu() const
{
BOOL bExpandMyDocuments = GetAdvancedValue(L"CascadeMyDocuments", FALSE);
BOOL bExpandMyPictures = GetAdvancedValue(L"CascadeMyPictures", FALSE);
HMENU hMenu = ::CreateMenu();
AddOrSetMenuItem(hMenu, IDM_MYDOCUMENTS, CSIDL_MYDOCUMENTS, bExpandMyDocuments);
AddOrSetMenuItem(hMenu, IDM_MYPICTURES, CSIDL_MYPICTURES, bExpandMyPictures);
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
BOOL bAdded = FALSE;
// My Documents
if (!SHRestricted(REST_NOSMMYDOCS) &&
GetAdvancedValue(L"Start_ShowMyDocs", TRUE))
{
BOOL bExpand = GetAdvancedValue(L"CascadeMyDocuments", FALSE);
AddOrSetMenuItem(hMenu, IDM_MYDOCUMENTS, CSIDL_MYDOCUMENTS, bExpand);
bAdded = TRUE;
}
// My Pictures
if (!SHRestricted(REST_NOSMMYPICS) &&
GetAdvancedValue(L"Start_ShowMyPics", TRUE))
{
BOOL bExpand = GetAdvancedValue(L"CascadeMyPictures", FALSE);
AddOrSetMenuItem(hMenu, IDM_MYPICTURES, CSIDL_MYPICTURES, bExpand);
bAdded = TRUE;
}
if (bAdded)
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
return hMenu;
}
@ -221,7 +239,7 @@ private:
HRESULT AddStartMenuItems(IShellMenu *pShellMenu, INT csidl, DWORD dwFlags)
{
LPITEMIDLIST pidlMenu;
CComHeapPtr<ITEMIDLIST> pidlMenu;
CComPtr<IShellFolder> psfDesktop;
CComPtr<IShellFolder> pShellFolder;
HRESULT hr;