Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 6 additions & 7 deletions src/Files.App/Helpers/UI/UIFilesystemHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ await Task.WhenAll(items.Select(async item =>
await associatedInstance.RefreshIfNoWatcherExistsAsync();
}

public static async Task<bool> RenameFileItemAsync(ListedItem item, string newName, IShellPage associatedInstance, bool showExtensionDialog = true)
public static async Task<bool> RenameFileItemAsync(ListedItem item, string newName, IShellPage associatedInstance, bool showExtensionDialog = true, bool nameIsComplete = false)
{
if (item is AlternateStreamItem ads) // For alternate streams Name is not a substring ItemNameRaw
{
Expand All @@ -56,13 +56,12 @@ public static async Task<bool> RenameFileItemAsync(ListedItem item, string newNa
StringComparison.Ordinal);
newName = $"{ads.MainStreamName}:{newName}";
}
else if (string.IsNullOrEmpty(item.Name))
else if (!nameIsComplete)
{
newName = string.Concat(newName, item.FileExtension);
}
else
{
newName = item.ItemNameRaw.Replace(item.Name, newName, StringComparison.Ordinal);
if (string.IsNullOrEmpty(item.Name))
newName = string.Concat(newName, item.FileExtension);
else
newName = item.ItemNameRaw.Replace(item.Name, newName, StringComparison.Ordinal);
}

if (item.ItemNameRaw == newName || string.IsNullOrEmpty(newName))
Expand Down
16 changes: 10 additions & 6 deletions src/Files.App/Views/Layouts/BaseGroupableLayoutPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ protected virtual void SelectionRectangle_SelectionEnded(object? sender, EventAr
ListViewBase.Focus(FocusState.Programmatic);
}

protected static bool ShouldShowExtensionInRename(ListedItem item) =>
(!item.IsFolder || item.IsArchive) && !item.IsShortcut && item is not AlternateStreamItem;

protected virtual void StartRenameItem(string itemNameTextBox)
{
RenamingItem = SelectedItem;
Expand All @@ -269,9 +272,10 @@ protected virtual void StartRenameItem(string itemNameTextBox)
TextBox? textBox = null;
TextBlock? textBlock = listViewItem.FindDescendant("ItemName") as TextBlock;
textBox = listViewItem.FindDescendant(itemNameTextBox) as TextBox;
textBox!.Text = textBlock!.Text;
OldItemName = textBlock.Text;
textBlock.Visibility = Visibility.Collapsed;
string editText = ShouldShowExtensionInRename(RenamingItem) ? RenamingItem.ItemNameRaw : textBlock!.Text;
textBox!.Text = editText;
OldItemName = editText;
textBlock!.Visibility = Visibility.Collapsed;
textBox.Visibility = Visibility.Visible;

if (textBox.FindParent<Grid>() is null)
Expand All @@ -287,9 +291,9 @@ protected virtual void StartRenameItem(string itemNameTextBox)
textBox.LostFocus += RenameTextBox_LostFocus;
textBox.KeyDown += RenameTextBox_KeyDown;

int selectedTextLength = SelectedItem.Name.Length;
int selectedTextLength = editText.Length;

if (!SelectedItem.IsShortcut && UserSettingsService.FoldersSettingsService.ShowFileExtensions)
if (!SelectedItem.IsShortcut && (ShouldShowExtensionInRename(SelectedItem) || UserSettingsService.FoldersSettingsService.ShowFileExtensions))
selectedTextLength -= extensionLength;

textBox.Select(0, selectedTextLength);
Expand All @@ -301,7 +305,7 @@ protected virtual async Task CommitRenameAsync(TextBox textBox)
EndRename(textBox);
string newItemName = textBox.Text.Trim().TrimEnd('.');

await UIFilesystemHelpers.RenameFileItemAsync(RenamingItem, newItemName, ParentShellPageInstance);
await UIFilesystemHelpers.RenameFileItemAsync(RenamingItem, newItemName, ParentShellPageInstance, nameIsComplete: ShouldShowExtensionInRename(RenamingItem));
}

protected virtual async void RenameTextBox_LostFocus(object sender, RoutedEventArgs e)
Expand Down
17 changes: 9 additions & 8 deletions src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ override public void StartRenameItem()
return;

TextBox? textBox = null;
string editText = ShouldShowExtensionInRename(RenamingItem) ? RenamingItem.ItemNameRaw : textBlock.Text;

// Grid View
if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
Expand All @@ -387,10 +388,10 @@ override public void StartRenameItem()
if (textBox is null)
return;

textBox.Text = textBlock.Text;
textBox.Text = editText;
textBlock.Opacity = 0;
popup.IsOpen = true;
OldItemName = textBlock.Text;
OldItemName = editText;
}
// List View
else if (FolderSettings.LayoutMode == FolderLayoutModes.ListView)
Expand All @@ -399,8 +400,8 @@ override public void StartRenameItem()
if (textBox is null)
return;

textBox.Text = textBlock.Text;
OldItemName = textBlock.Text;
textBox.Text = editText;
OldItemName = editText;
textBlock.Visibility = Visibility.Collapsed;
textBox.Visibility = Visibility.Visible;

Expand All @@ -418,8 +419,8 @@ override public void StartRenameItem()
if (textBox is null)
return;

textBox.Text = textBlock.Text;
OldItemName = textBlock.Text;
textBox.Text = editText;
OldItemName = editText;
textBox.Visibility = Visibility.Visible;

if (textBox.FindParent<Grid>() is null)
Expand All @@ -433,8 +434,8 @@ override public void StartRenameItem()
textBox.LostFocus += RenameTextBox_LostFocus;
textBox.KeyDown += RenameTextBox_KeyDown;

int selectedTextLength = RenamingItem.Name.Length;
if (!RenamingItem.IsShortcut && UserSettingsService.FoldersSettingsService.ShowFileExtensions)
int selectedTextLength = editText.Length;
if (!RenamingItem.IsShortcut && (ShouldShowExtensionInRename(RenamingItem) || UserSettingsService.FoldersSettingsService.ShowFileExtensions))
selectedTextLength -= extensionLength;

textBox.Select(0, selectedTextLength);
Expand Down
Loading