Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New+ Support for variables in template filenames (35287) #37074

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bd175a2
Add variable support - initial version without UI
cgaarden Dec 22, 2024
7e36a23
Add variable in template filename support in New+
cgaarden Dec 22, 2024
0f5a8ed
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Dec 26, 2024
bb3c47d
Merge remote-tracking branch 'upstream/main' into New+feature-35287-s…
cgaarden Jan 23, 2025
b7bf769
Several little fixes
cgaarden Jan 23, 2025
e3824f4
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 24, 2025
bd13af1
Merge remote-tracking branch 'upstream/main' into New+feature-35287-s…
cgaarden Jan 25, 2025
cca4878
New+feature-35287-support-for-variables-in-templates
cgaarden Jan 25, 2025
1fc8d65
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 25, 2025
51ca5b8
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 25, 2025
ba4f07e
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 25, 2025
a8b6a1c
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 25, 2025
81240c3
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Jan 26, 2025
0b41e5b
Merge remote-tracking branch 'upstream/main' into New+feature-35287-s…
cgaarden Jan 29, 2025
d40aae2
Merge branch 'microsoft:main' into New+feature-35287-support-for-vari…
cgaarden Jan 29, 2025
1b2d01d
Merge remote-tracking branch 'upstream/main' into New+feature-35287-s…
cgaarden Feb 15, 2025
b9be940
Merge branch 'New+feature-35287-support-for-variables-in-templates' o…
cgaarden Feb 15, 2025
6b1f684
Merge branch 'main' into pr37074
jaimecbernardo Feb 25, 2025
643bec6
Fix XAML style
jaimecbernardo Feb 25, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/GPOWrapper/GPOWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ namespace winrt::PowerToys::GPOWrapper::implementation
{
return static_cast<GpoRuleConfigured>(powertoys_gpo::getAllowDataDiagnosticsValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredNewPlusReplaceVariablesValue()
{
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredNewPlusReplaceVariablesValue());
}
GpoRuleConfigured GPOWrapper::GetConfiguredRunAtStartupValue()
{
return static_cast<GpoRuleConfigured>(powertoys_gpo::getConfiguredRunAtStartupValue());
Expand Down
1 change: 1 addition & 0 deletions src/common/GPOWrapper/GPOWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace winrt::PowerToys::GPOWrapper::implementation
static GpoRuleConfigured GetConfiguredNewPlusHideTemplateFilenameExtensionValue();
static GpoRuleConfigured GetAllowDataDiagnosticsValue();
static GpoRuleConfigured GetConfiguredRunAtStartupValue();
static GpoRuleConfigured GetConfiguredNewPlusReplaceVariablesValue();
};
}

Expand Down
1 change: 1 addition & 0 deletions src/common/GPOWrapper/GPOWrapper.idl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ namespace PowerToys
static GpoRuleConfigured GetConfiguredNewPlusHideTemplateFilenameExtensionValue();
static GpoRuleConfigured GetAllowDataDiagnosticsValue();
static GpoRuleConfigured GetConfiguredRunAtStartupValue();
static GpoRuleConfigured GetConfiguredNewPlusReplaceVariablesValue();
}
}
}
7 changes: 7 additions & 0 deletions src/common/utils/gpo.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace powertoys_gpo {
const std::wstring POLICY_MWB_DISABLE_USER_DEFINED_IP_MAPPING_RULES = L"MwbDisableUserDefinedIpMappingRules";
const std::wstring POLICY_MWB_POLICY_DEFINED_IP_MAPPING_RULES = L"MwbPolicyDefinedIpMappingRules";
const std::wstring POLICY_NEW_PLUS_HIDE_TEMPLATE_FILENAME_EXTENSION = L"NewPlusHideTemplateFilenameExtension";
const std::wstring POLICY_NEW_PLUS_REPLACE_VARIABLES = L"NewPlusReplaceVariablesInTemplateFilenames";

// Methods used for reading the registry
#pragma region ReadRegistryMethods
Expand Down Expand Up @@ -609,5 +610,11 @@ namespace powertoys_gpo {
{
return getConfiguredValue(POLICY_NEW_PLUS_HIDE_TEMPLATE_FILENAME_EXTENSION);
}

inline gpo_rule_configured_t getConfiguredNewPlusReplaceVariablesValue()
{
return getConfiguredValue(POLICY_NEW_PLUS_REPLACE_VARIABLES);
}

#pragma endregion IndividualModuleSettingPolicies
}
10 changes: 10 additions & 0 deletions src/gpo/assets/PowerToys.admx
Original file line number Diff line number Diff line change
Expand Up @@ -671,5 +671,15 @@
<decimal value="0" />
</disabledValue>
</policy>
<policy name="NewPlusReplaceVariablesInTemplateFilenames" class="Both" displayName="$(string.NewPlusReplaceVariablesInTemplateFilenames)" explainText="$(string.NewPlusReplaceVariablesInTemplateFilenamesDescription)" key="Software\Policies\PowerToys" valueName="NewPlusReplaceVariablesInTemplateFilenames">
<parentCategory ref="NewPlus" />
<supportedOn ref="SUPPORTED_POWERTOYS_0_89_0" />
<enabledValue>
<decimal value="1" />
</enabledValue>
<disabledValue>
<decimal value="0" />
</disabledValue>
</policy>
</policies>
</policyDefinitions>
9 changes: 8 additions & 1 deletion src/gpo/assets/en-US/PowerToys.adml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ If you enable this policy, the setting is enabled and the extension is hidden.
If you disable this policy, the setting is disabled and the extension is shown.

If you don't configure this policy, the user takes control over the setting and can enable or disable it.
</string>
<string id="NewPlusReplaceVariablesInTemplateFilenamesDescription">This policy configures if supported variables will get replaced in template filenames.

If you enable this policy, the setting is enabled and supported variables in filenames will get replaced.

If you disable this policy, the setting is disabled and variables in filenames will not get replaced.

If you don't configure this policy, the user will be able to control the setting and can enable or disable it.
</string>

<string id="ConfigureAllUtilityGlobalEnabledState">Configure global utility enabled state</string>
Expand Down Expand Up @@ -289,7 +297,6 @@ If you don't configure this policy, the user takes control over the setting and
<string id="MwbPolicyDefinedIpMappingRules">Predefined IP Address mapping rules</string>
<string id="NewPlusHideTemplateFilenameExtension">Hide template filename extension</string>
<string id="AllowDiagnosticData">Allow sending diagnostic data</string>
<string id="ConfigureRunAtStartup">Configure the run at startup setting</string>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this string removed? I suspect a bad merge.

</stringTable>

<presentationTable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\NewShellExtensionContextMenu\constants.h" />
<ClInclude Include="..\NewShellExtensionContextMenu\helpers_filesystem.h" />
<ClInclude Include="..\NewShellExtensionContextMenu\helpers_variables.h" />
<ClInclude Include="..\NewShellExtensionContextMenu\new_utilities.h" />
<ClInclude Include="..\NewShellExtensionContextMenu\settings.h" />
<ClInclude Include="..\NewShellExtensionContextMenu\shell_context_sub_menu.h" />
Expand All @@ -97,6 +99,7 @@
<ClInclude Include="shell_context_menu_win10.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\powerrename\lib\Helpers.cpp" />
<ClCompile Include="..\NewShellExtensionContextMenu\new_utilities.cpp" />
<ClCompile Include="..\NewShellExtensionContextMenu\powertoys_module.cpp" />
<ClCompile Include="..\NewShellExtensionContextMenu\settings.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<ClInclude Include="..\NewShellExtensionContextMenu\settings.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\NewShellExtensionContextMenu\helpers_filesystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\NewShellExtensionContextMenu\helpers_variables.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down Expand Up @@ -92,6 +98,9 @@
<ClCompile Include="..\NewShellExtensionContextMenu\new_utilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\powerrename\lib\Helpers.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Generated Files\new.rc">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ IFACEMETHODIMP shell_context_menu_win10::Initialize(PCIDLIST_ABSOLUTE, IDataObje
IFACEMETHODIMP shell_context_menu_win10::QueryContextMenu(HMENU menu_handle, UINT menu_index, UINT menu_first_cmd_id, UINT, UINT menu_flags)
{
if (!NewSettingsInstance().GetEnabled()
|| package::IsWin11OrGreater()
|| package::IsWin11OrGreater()
)
{
return E_FAIL;
Expand All @@ -47,7 +47,7 @@ IFACEMETHODIMP shell_context_menu_win10::QueryContextMenu(HMENU menu_handle, UIN
{
// Create the initial context popup menu containing the list of templates and open templates action
int menu_id = menu_first_cmd_id;
MENUITEMINFO newplus_main_context_menu_item;
MENUITEMINFO newplus_main_context_menu_item = { 0 };
HMENU sub_menu_of_templates = CreatePopupMenu();
int sub_menu_index = 0;

Expand Down Expand Up @@ -142,7 +142,7 @@ void shell_context_menu_win10::add_open_templates_to_context_menu(HMENU sub_menu
wchar_t menu_name_open[256] = { 0 };
wcscpy_s(menu_name_open, ARRAYSIZE(menu_name_open), localized_context_menu_item_open_templates.c_str());
const auto open_folder_item = Make<template_folder_context_menu_item>(template_folder_root);
MENUITEMINFO newplus_menu_item_open_templates;
MENUITEMINFO newplus_menu_item_open_templates = { 0 };
newplus_menu_item_open_templates.cbSize = sizeof(MENUITEMINFO);
newplus_menu_item_open_templates.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID;
newplus_menu_item_open_templates.wID = menu_id;
Expand Down Expand Up @@ -174,7 +174,7 @@ void shell_context_menu_win10::add_open_templates_to_context_menu(HMENU sub_menu

void shell_context_menu_win10::add_separator_to_context_menu(HMENU sub_menu_of_templates, int sub_menu_index)
{
MENUITEMINFO menu_item_separator;
MENUITEMINFO menu_item_separator = { 0 };
menu_item_separator.cbSize = sizeof(MENUITEMINFO);
menu_item_separator.fMask = MIIM_FTYPE;
menu_item_separator.fType = MFT_SEPARATOR;
Expand All @@ -184,8 +184,11 @@ void shell_context_menu_win10::add_separator_to_context_menu(HMENU sub_menu_of_t
void shell_context_menu_win10::add_template_item_to_context_menu(HMENU sub_menu_of_templates, int sub_menu_index, newplus::template_item* const template_item, int menu_id, int index)
{
wchar_t menu_name[256] = { 0 };
wcscpy_s(menu_name, ARRAYSIZE(menu_name), template_item->get_menu_title(!utilities::get_newplus_setting_hide_extension(), !utilities::get_newplus_setting_hide_starting_digits()).c_str());
MENUITEMINFO newplus_menu_item_template;
wcscpy_s(menu_name, ARRAYSIZE(menu_name), template_item->get_menu_title(
!utilities::get_newplus_setting_hide_extension(),
!utilities::get_newplus_setting_hide_starting_digits(),
utilities::get_newplus_setting_resolve_variables()).c_str());
MENUITEMINFO newplus_menu_item_template = { 0 };
newplus_menu_item_template.cbSize = sizeof(MENUITEMINFO);
newplus_menu_item_template.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_DATA;
newplus_menu_item_template.wID = menu_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ MakeAppx.exe pack /d . /p $(OutDir)NewPlusPackage.msix /nv</Command>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="dll_main.h" />
<ClInclude Include="helpers_filesystem.h" />
<ClInclude Include="helpers_variables.h" />
<ClInclude Include="shell_context_menu.h" />
<ClInclude Include="shell_context_sub_menu.h" />
<ClInclude Include="shell_context_sub_menu_item.h" />
Expand All @@ -128,6 +130,7 @@ MakeAppx.exe pack /d . /p $(OutDir)NewPlusPackage.msix /nv</Command>
<ClInclude Include="template_item.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\powerrename\lib\Helpers.cpp" />
<ClCompile Include="new_utilities.cpp" />
<ClCompile Include="shell_context_menu.cpp" />
<ClCompile Include="shell_context_sub_menu.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<ClCompile Include="new_utilities.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\powerrename\lib\Helpers.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="template_folder.h">
Expand Down Expand Up @@ -75,6 +78,12 @@
<ClInclude Include="resource.base.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="helpers_filesystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="helpers_variables.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
4 changes: 3 additions & 1 deletion src/modules/NewPlus/NewShellExtensionContextMenu/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace newplus::constants::non_localizable

constexpr WCHAR settings_json_key_hide_starting_digits[] = L"HideStartingDigits";

constexpr WCHAR settings_json_key_replace_variables[] = L"ReplaceVariables";

constexpr WCHAR settings_json_key_template_location[] = L"TemplateLocation";

constexpr WCHAR context_menu_package_name[] = L"NewPlusContextMenu";
Expand All @@ -30,5 +32,5 @@ namespace newplus::constants::non_localizable

constexpr WCHAR open_templates_icon_dark_resource_relative_path[] = L"\\Assets\\NewPlus\\Open_templates_dark.ico";

constexpr WCHAR desktop_ini_filename[] = L"desktop.ini";
constexpr WCHAR parent_folder_name_variable[] = L"$PARENT_FOLDER_NAME";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include "helpers_variables.h"

namespace newplus::helpers::filesystem
{
namespace constants::non_localizable
{
constexpr WCHAR desktop_ini_filename[] = L"desktop.ini";
}

inline bool is_hidden(const std::filesystem::path path)
{
const std::filesystem::path::string_type name = path.filename();
if (name == constants::non_localizable::desktop_ini_filename)
{
return true;
}

return false;
}

inline bool is_directory(const std::filesystem::path path)
{
const auto entry = std::filesystem::directory_entry(path);
return entry.is_directory();
}

inline std::wstring make_valid_filename(const std::wstring& string, const wchar_t replace_with = L' ')
{
// replace all non-filename-valid chars with replace_with wchar
std::wstring valid_filename = string;

std::replace_if(valid_filename.begin(), valid_filename.end(), [](wchar_t c) { return c == L'/' || c == L'\\' || c == L':' || c == L'*' || c == L'?' || c == L'"' || c == L'<' || c == L'>' || c == L'|'; }, replace_with);

return valid_filename;
}

inline std::wstring make_unique_path_name(const std::wstring& initial_path)
{
std::filesystem::path folder_path(initial_path);
std::filesystem::path path_based_on(initial_path);

int counter = 1;

while (std::filesystem::exists(folder_path))
{
std::wstring new_filename = path_based_on.stem().wstring() + L" (" + std::to_wstring(counter) + L")";
if (path_based_on.has_extension())
{
new_filename += path_based_on.extension().wstring();
}
folder_path = path_based_on.parent_path() / new_filename;
counter++;
}

return folder_path.wstring();
}
}
Loading