Skip to content

Commit

Permalink
Code Quality: Added more null ref protections when renaming items (fi…
Browse files Browse the repository at this point in the history
  • Loading branch information
yaira2 authored Jan 17, 2024
1 parent 6649e82 commit 2acac82
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/Files.App/Views/Layouts/GridLayoutPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,32 +178,40 @@ protected override void FileList_SelectionChanged(object sender, SelectionChange
override public void StartRenameItem()
{
RenamingItem = SelectedItem;
if (RenamingItem is null)
if (RenamingItem is null || FolderSettings is null)
return;

int extensionLength = RenamingItem.FileExtension?.Length ?? 0;

GridViewItem gridViewItem = FileList.ContainerFromItem(RenamingItem) as GridViewItem;
if (gridViewItem is null)
if (FileList.ContainerFromItem(RenamingItem) is not GridViewItem gridViewItem)
return;

TextBox textBox = null;
if (gridViewItem.FindDescendant("ItemName") is not TextBlock textBlock)
return;

TextBox? textBox = null;

// Handle layout differences between tiles browser and photo album
if (FolderSettings.LayoutMode == FolderLayoutModes.GridView)
{
Popup popup = gridViewItem.FindDescendant("EditPopup") as Popup;
TextBlock textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
if (gridViewItem.FindDescendant("EditPopup") is not Popup popup)
return;

textBox = popup.Child as TextBox;
if (textBox is null)
return;

textBox.Text = textBlock.Text;
textBlock.Opacity = 0;
popup.IsOpen = true;
OldItemName = textBlock.Text;
}
else
{
TextBlock textBlock = gridViewItem.FindDescendant("ItemName") as TextBlock;
textBox = gridViewItem.FindDescendant("TileViewTextBoxItemName") as TextBox;
if (textBox is null)
return;

textBox.Text = textBlock.Text;
OldItemName = textBlock.Text;
textBlock.Visibility = Visibility.Collapsed;
Expand All @@ -221,8 +229,8 @@ override public void StartRenameItem()
textBox.LostFocus += RenameTextBox_LostFocus;
textBox.KeyDown += RenameTextBox_KeyDown;

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

textBox.Select(0, selectedTextLength);
Expand Down

0 comments on commit 2acac82

Please sign in to comment.