mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
[PowerRename] Show UI info if item cannot be renamed (#19934)
* PowerRename cleanup * Extract ExplorerItem as a UserControl * Add VisualStateManager * UI fixes * Implement error UI logic Highlight items that couldn't be renamed and add error message flyout * Update src/modules/powerrename/lib/PowerRenameManager.cpp Address PR comment Co-authored-by: Jay <65828559+Jay-o-Way@users.noreply.github.com> * Update src/modules/powerrename/lib/PowerRenameManager.cpp Address PR comment Co-authored-by: Jay <65828559+Jay-o-Way@users.noreply.github.com> * Folder max path is 247 * Implement State() properly Co-authored-by: Niels Laute <niels.laute@live.nl> Co-authored-by: Jay <65828559+Jay-o-Way@users.noreply.github.com>
This commit is contained in:
@@ -28,6 +28,14 @@ enum PowerRenameFilters
|
||||
ShouldRename = 4,
|
||||
};
|
||||
|
||||
enum class PowerRenameItemRenameStatus {
|
||||
Init = 0,
|
||||
ShouldRename,
|
||||
ItemNameTooLong,
|
||||
ItemNameInvalidChar,
|
||||
ItemNameAlreadyExists,
|
||||
};
|
||||
|
||||
interface __declspec(uuid("3ECBA62B-E0F0-4472-AA2E-DEE7A1AA46B9")) IPowerRenameRegExEvents : public IUnknown
|
||||
{
|
||||
public:
|
||||
@@ -71,6 +79,8 @@ public:
|
||||
IFACEMETHOD(GetId)(_Out_ int *id) = 0;
|
||||
IFACEMETHOD(GetDepth)(_Out_ UINT* depth) = 0;
|
||||
IFACEMETHOD(PutDepth)(_In_ int depth) = 0;
|
||||
IFACEMETHOD(GetStatus)(_Out_ PowerRenameItemRenameStatus* status) = 0;
|
||||
IFACEMETHOD(PutStatus)(_In_ PowerRenameItemRenameStatus status) = 0;
|
||||
IFACEMETHOD(ShouldRenameItem)(_In_ DWORD flags, _Out_ bool* shouldRename) = 0;
|
||||
IFACEMETHOD(IsItemVisible)(_In_ DWORD filter, _In_ DWORD flags, _Out_ bool* isItemVisible) = 0;
|
||||
IFACEMETHOD(Reset)() = 0;
|
||||
|
||||
@@ -191,6 +191,18 @@ IFACEMETHODIMP CPowerRenameItem::PutDepth(_In_ int depth)
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
IFACEMETHODIMP CPowerRenameItem::GetStatus(_Out_ PowerRenameItemRenameStatus* status)
|
||||
{
|
||||
*status = m_status;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
IFACEMETHODIMP CPowerRenameItem::PutStatus(_In_ PowerRenameItemRenameStatus status)
|
||||
{
|
||||
m_status = status;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
IFACEMETHODIMP CPowerRenameItem::ShouldRenameItem(_In_ DWORD flags, _Out_ bool* shouldRename)
|
||||
{
|
||||
// Should we perform a rename on this item given its
|
||||
@@ -200,7 +212,7 @@ IFACEMETHODIMP CPowerRenameItem::ShouldRenameItem(_In_ DWORD flags, _Out_ bool*
|
||||
bool excludeBecauseFile = (!m_isFolder && (flags & PowerRenameFlags::ExcludeFiles));
|
||||
bool excludeBecauseSubFolderContent = (m_depth > 0 && (flags & PowerRenameFlags::ExcludeSubfolders));
|
||||
*shouldRename = (m_selected && m_canRename && hasChanged && !excludeBecauseFile &&
|
||||
!excludeBecauseFolder && !excludeBecauseSubFolderContent);
|
||||
!excludeBecauseFolder && !excludeBecauseSubFolderContent && m_status == PowerRenameItemRenameStatus::ShouldRename);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ public:
|
||||
IFACEMETHODIMP GetId(_Out_ int* id);
|
||||
IFACEMETHODIMP GetDepth(_Out_ UINT* depth);
|
||||
IFACEMETHODIMP PutDepth(_In_ int depth);
|
||||
IFACEMETHODIMP GetStatus(_Out_ PowerRenameItemRenameStatus* status);
|
||||
IFACEMETHODIMP PutStatus(_In_ PowerRenameItemRenameStatus status);
|
||||
IFACEMETHODIMP Reset();
|
||||
IFACEMETHODIMP ShouldRenameItem(_In_ DWORD flags, _Out_ bool* shouldRename);
|
||||
IFACEMETHODIMP IsItemVisible(_In_ DWORD filter, _In_ DWORD flags, _Out_ bool* isItemVisible);
|
||||
@@ -49,18 +51,18 @@ protected:
|
||||
|
||||
HRESULT _Init(_In_ IShellItem* psi);
|
||||
|
||||
bool m_selected = true;
|
||||
bool m_isFolder = false;
|
||||
bool m_isTimeParsed = false;
|
||||
bool m_canRename = true;
|
||||
int m_id = -1;
|
||||
int m_iconIndex = -1;
|
||||
UINT m_depth = 0;
|
||||
HRESULT m_error = S_OK;
|
||||
PWSTR m_path = nullptr;
|
||||
PWSTR m_originalName = nullptr;
|
||||
PWSTR m_newName = nullptr;
|
||||
SYSTEMTIME m_time = {0};
|
||||
CSRWLock m_lock;
|
||||
long m_refCount = 0;
|
||||
bool m_selected = true;
|
||||
bool m_isFolder = false;
|
||||
bool m_isTimeParsed = false;
|
||||
bool m_canRename = true;
|
||||
int m_id = -1;
|
||||
int m_iconIndex = -1;
|
||||
UINT m_depth = 0;
|
||||
PowerRenameItemRenameStatus m_status = PowerRenameItemRenameStatus::Init;
|
||||
PWSTR m_path = nullptr;
|
||||
PWSTR m_originalName = nullptr;
|
||||
PWSTR m_newName = nullptr;
|
||||
SYSTEMTIME m_time = {0};
|
||||
CSRWLock m_lock;
|
||||
long m_refCount = 0;
|
||||
};
|
||||
|
||||
@@ -1115,6 +1115,36 @@ DWORD WINAPI CPowerRenameManager::s_regexWorkerThread(_In_ void* pv)
|
||||
itemEnumIndex++;
|
||||
}
|
||||
|
||||
spItem->PutStatus(PowerRenameItemRenameStatus::ShouldRename);
|
||||
if (newNameToUse != nullptr)
|
||||
{
|
||||
std::wstring newNameToUseWstr{ newNameToUse };
|
||||
PWSTR path = nullptr;
|
||||
spItem->GetPath(&path);
|
||||
|
||||
// Following characters cannot be used for file names.
|
||||
// Ref https://docs.microsoft.com/windows/win32/fileio/naming-a-file#naming-conventions
|
||||
if (newNameToUseWstr.contains('<') ||
|
||||
newNameToUseWstr.contains('>') ||
|
||||
newNameToUseWstr.contains(':') ||
|
||||
newNameToUseWstr.contains('"') ||
|
||||
newNameToUseWstr.contains('\\') ||
|
||||
newNameToUseWstr.contains('/') ||
|
||||
newNameToUseWstr.contains('|') ||
|
||||
newNameToUseWstr.contains('?') ||
|
||||
newNameToUseWstr.contains('*'))
|
||||
{
|
||||
spItem->PutStatus(PowerRenameItemRenameStatus::ItemNameInvalidChar);
|
||||
}
|
||||
// Max file path is 260 and max folder path is 247.
|
||||
// Ref https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation?tabs=registry
|
||||
else if ((isFolder && lstrlen(path) + (lstrlen(newNameToUse) - lstrlen(originalName)) > 247) ||
|
||||
lstrlen(path) + (lstrlen(newNameToUse) - lstrlen(originalName)) > 260)
|
||||
{
|
||||
spItem->PutStatus(PowerRenameItemRenameStatus::ItemNameTooLong);
|
||||
}
|
||||
}
|
||||
|
||||
winrt::check_hresult(spItem->PutNewName(newNameToUse));
|
||||
|
||||
// Was there a change?
|
||||
|
||||
Reference in New Issue
Block a user