Skip to content

Code Quality: Added more null ref protections when renaming items #14479

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

Merged
merged 2 commits into from
Jan 17, 2024
Merged
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
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;
Comment on lines 200 to +202
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
textBox = popup.Child as TextBox;
if (textBox is null)
return;
if (popup.Child is not TextBox textBox)
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;
Comment on lines 211 to +213
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
textBox = gridViewItem.FindDescendant("TileViewTextBoxItemName") as TextBox;
if (textBox is null)
return;
if (gridViewItem.FindDescendant("TileViewTextBoxItemName") is not TextBox textBox)
return;

Copy link
Member Author

Choose a reason for hiding this comment

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

textBox is already defined on 192 so I don't think this will work


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