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 21 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
21 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
4fee67a
Merge remote-tracking branch 'upstream/main' into New+feature-35287-s…
cgaarden Feb 26, 2025
e869120
Addressed code review feedback
cgaarden Feb 26, 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
Prev Previous commit
Next Next commit
Several little fixes
  • Loading branch information
cgaarden committed Jan 24, 2025
commit b7bf7696ef528a89d490c9dc9edeaf16346cb07e
2 changes: 2 additions & 0 deletions src/gpo/assets/PowerToys.admx
Original file line number Diff line number Diff line change
@@ -23,7 +23,9 @@
<definition name="SUPPORTED_POWERTOYS_0_84_0" displayName="$(string.SUPPORTED_POWERTOYS_0_84_0)"/>
<definition name="SUPPORTED_POWERTOYS_0_85_0" displayName="$(string.SUPPORTED_POWERTOYS_0_85_0)"/>
<definition name="SUPPORTED_POWERTOYS_0_86_0" displayName="$(string.SUPPORTED_POWERTOYS_0_86_0)"/>
<definition name="SUPPORTED_POWERTOYS_0_88_0" displayName="$(string.SUPPORTED_POWERTOYS_0_88_0)"/>
<definition name="SUPPORTED_POWERTOYS_0_89_0" displayName="$(string.SUPPORTED_POWERTOYS_0_89_0)"/>
<definition name="SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1" displayName="$(string.SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1)"/>
</definitions>
</supportedOn>
<categories>
2 changes: 2 additions & 0 deletions src/gpo/assets/en-US/PowerToys.adml
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@
<string id="SUPPORTED_POWERTOYS_0_84_0">PowerToys version 0.84.0 or later</string>
<string id="SUPPORTED_POWERTOYS_0_85_0">PowerToys version 0.85.0 or later</string>
<string id="SUPPORTED_POWERTOYS_0_86_0">PowerToys version 0.86.0 or later</string>
<string id="SUPPORTED_POWERTOYS_0_88_0">PowerToys version 0.88.0 or later</string>
<string id="SUPPORTED_POWERTOYS_0_89_0">PowerToys version 0.89.0 or later</string>
<string id="SUPPORTED_POWERTOYS_0_64_0_TO_0_87_1">From PowerToys version 0.64.0 until PowerToys version 0.87.1</string>

<string id="ConfigureAllUtilityGlobalEnabledStateDescription">This policy configures the enabled state for all PowerToys utilities.

Original file line number Diff line number Diff line change
@@ -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()
//cgaarden || package::IsWin11OrGreater()
|| package::IsWin11OrGreater()
)
{
return E_FAIL;
@@ -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;

@@ -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;
@@ -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;
@@ -188,7 +188,7 @@ void shell_context_menu_win10::add_template_item_to_context_menu(HMENU sub_menu_
!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;
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;
Original file line number Diff line number Diff line change
@@ -39,12 +39,18 @@ namespace newplus::helpers::filesystem
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))
{
folder_path = initial_path + L" (" + std::to_wstring(counter) + L")";
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++;
}

Original file line number Diff line number Diff line change
@@ -364,7 +364,7 @@ namespace newplus::utilities
// Get initial resolved name
target_fullpath /= target_name;

// Expand variables in path
// Expand variables in name of the target path
if (utilities::get_newplus_setting_resolve_variables())
{
target_fullpath = helpers::variables::resolve_variables_in_path(target_fullpath);
@@ -376,6 +376,12 @@ namespace newplus::utilities
// Finally copy file/folder/subfolders
std::filesystem::path target_final_fullpath = template_entry->copy_object_to(GetActiveWindow(), target_fullpath);

// Resolve variables and rename files in newly copied folders and subfolders and files
if (utilities::get_newplus_setting_resolve_variables() && helpers::filesystem::is_directory(target_final_fullpath))
{
helpers::variables::resolve_variables_in_filename_and_rename_files(target_final_fullpath);
}

// Touch all files and set last modified to "now"
update_last_write_time(target_final_fullpath);

22 changes: 10 additions & 12 deletions src/modules/NewPlus/NewShellExtensionContextMenu/settings.cpp
Original file line number Diff line number Diff line change
@@ -172,17 +172,14 @@ bool NewSettings::GetEnabled()

bool NewSettings::GetHideFileExtension() const
{
auto gpoSetting = powertoys_gpo::getConfiguredNewPlusHideTemplateFilenameExtensionValue();
if (gpoSetting == powertoys_gpo::gpo_rule_configured_enabled)
{
return true;
}
const auto gpoSetting = powertoys_gpo::getConfiguredNewPlusHideTemplateFilenameExtensionValue();

if (gpoSetting == powertoys_gpo::gpo_rule_configured_disabled)
{
return false;
}

return new_settings.hide_file_extension;
return true;
}

void NewSettings::SetHideFileExtension(const bool hide_file_extension)
@@ -204,16 +201,12 @@ bool NewSettings::GetReplaceVariables() const
{
const auto gpoSetting = powertoys_gpo::getConfiguredNewPlusReplaceVariablesValue();

if (gpoSetting == powertoys_gpo::gpo_rule_configured_enabled)
{
return true;
}
if (gpoSetting == powertoys_gpo::gpo_rule_configured_disabled)
{
return false;
}

return new_settings.replace_variables;
return true;
}

void NewSettings::SetReplaceVariables(const bool replace_variables)
@@ -223,6 +216,11 @@ void NewSettings::SetReplaceVariables(const bool replace_variables)

std::wstring NewSettings::GetTemplateLocation() const
{
if (new_settings.template_location.empty())
{
return GetTemplateLocationDefaultPath();
}

return new_settings.template_location;
}

@@ -231,7 +229,7 @@ void NewSettings::SetTemplateLocation(const std::wstring template_location)
new_settings.template_location = template_location;
}

std::wstring NewSettings::GetTemplateLocationDefaultPath()
std::wstring NewSettings::GetTemplateLocationDefaultPath() const
{
static const std::wstring default_template_sub_folder_name =
GET_RESOURCE_STRING_FALLBACK(
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ class NewSettings

void RefreshEnabledState();
void InitializeWithDefaultSettings();
std::wstring GetTemplateLocationDefaultPath();
std::wstring GetTemplateLocationDefaultPath() const;

void Reload();
void ParseJson();
Original file line number Diff line number Diff line change
@@ -69,8 +69,21 @@ IFACEMETHODIMP shell_context_menu::GetFlags(_Out_ EXPCMDFLAGS* returned_menu_ite

IFACEMETHODIMP shell_context_menu::EnumSubCommands(_COM_Outptr_ IEnumExplorerCommand** returned_enum_commands)
{
auto e = Make<shell_context_sub_menu>(site_of_folder);
return e->QueryInterface(IID_PPV_ARGS(returned_enum_commands));
try
{
auto e = Make<shell_context_sub_menu>(site_of_folder);
return e->QueryInterface(IID_PPV_ARGS(returned_enum_commands));
}
catch (const std::exception& ex)
{
Logger::error("New+ create submenu error: {}", ex.what());
return E_FAIL;
}
catch (...)
{
Logger::error("New+ create submenu error");
return E_FAIL;
}
}
#pragma endregion

@@ -80,8 +93,8 @@ IFACEMETHODIMP shell_context_menu::SetSite(_In_ IUnknown* site) noexcept
this->site_of_folder = site;
return S_OK;
}
IFACEMETHODIMP shell_context_menu::GetSite(_In_ REFIID riid, _COM_Outptr_ void** returned_site) noexcept
IFACEMETHODIMP shell_context_menu::GetSite(_In_ REFIID interface_type, _COM_Outptr_ void** returned_site) noexcept
{
return this->site_of_folder.CopyTo(riid, returned_site);
return this->site_of_folder.CopyTo(interface_type, returned_site);
}
#pragma endregion
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ class __declspec(uuid(NEW_SHELL_EXTENSION_EXPLORER_COMMAND_UUID_STR)) shell_cont

#pragma region IObjectWithSite
IFACEMETHODIMP SetSite(_In_ IUnknown* site) noexcept;
IFACEMETHODIMP GetSite(_In_ REFIID riid, _COM_Outptr_ void** site) noexcept;
IFACEMETHODIMP GetSite(_In_ REFIID interface_type, _COM_Outptr_ void** site) noexcept;
#pragma endregion

protected:
Original file line number Diff line number Diff line change
@@ -74,6 +74,9 @@
IsOpen="{x:Bind ViewModel.IsHideFileExtSettingGPOConfigured, Mode=OneWay}"
IsTabStop="{x:Bind ViewModel.IsHideFileExtSettingGPOConfigured, Mode=OneWay}"
Severity="Informational">
<InfoBar.IconSource>
<FontIconSource FontFamily="{StaticResource SymbolThemeFontFamily}" Glyph="&#xE72E;" />
</InfoBar.IconSource>
</InfoBar>
<tkcontrols:SettingsCard x:Uid="NewPlus_Hide_Starting_Digits_Toggle">
<ToggleSwitch x:Uid="HideStartingDigitsToggle" IsOn="{x:Bind ViewModel.HideStartingDigits, Mode=TwoWay}" />
2 changes: 1 addition & 1 deletion src/settings-ui/Settings.UI/ViewModels/NewPlusViewModel.cs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using Common.UI;
using System.Windows;
using global::PowerToys.GPOWrapper;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
You are viewing a condensed version of this merge commit. You can view the full changes here.