@@ -42,6 +42,7 @@ public sealed partial class PhotoAlbum : Page
4242 public TextBlock EmptyTextPA ;
4343 public string inputForRename ;
4444 public ProgressBar progressBar ;
45+ public ListedItem renamingItem ;
4546 ItemViewModel viewModelInstance ;
4647 ProHome tabInstance ;
4748 public EmptyFolderTextState TextState { get ; set ; } = new EmptyFolderTextState ( ) ;
@@ -311,34 +312,73 @@ private void RightClickContextMenu_Opened(object sender, object e)
311312 }
312313 }
313314
314- public async void ShowRenameDialog ( )
315+ public void StartRename ( )
315316 {
316- try
317+ renamingItem = gv . SelectedItem as ListedItem ;
318+ GridViewItem gridViewItem = gv . ContainerFromItem ( renamingItem ) as GridViewItem ;
319+ StackPanel stackPanel = ( gridViewItem . ContentTemplateRoot as Grid ) . Children [ 1 ] as StackPanel ;
320+ TextBlock textBlock = stackPanel . Children [ 0 ] as TextBlock ;
321+ TextBox textBox = stackPanel . Children [ 1 ] as TextBox ;
322+ int extensionLength = renamingItem . DotFileExtension ? . Length ?? 0 ;
323+
324+ textBlock . Visibility = Visibility . Collapsed ;
325+ textBox . Visibility = Visibility . Visible ;
326+ textBox . Focus ( FocusState . Pointer ) ;
327+ textBox . LostFocus += RenameTextBox_LostFocus ;
328+ textBox . KeyDown += RenameTextBox_KeyDown ;
329+ textBox . Select ( 0 , renamingItem . FileName . Length - extensionLength ) ;
330+ }
331+
332+ private void RenameTextBox_KeyDown ( object sender , KeyRoutedEventArgs e )
333+ {
334+ if ( e . Key == VirtualKey . Escape )
317335 {
318- var selectedItem = FileList . SelectedItem as ListedItem ;
319- RenameDialog renameDialog = new RenameDialog ( ) ;
320- var textBox = renameDialog . inputBox ;
321- int extensionLength = selectedItem . DotFileExtension ? . Length ?? 0 ;
336+ TextBox textBox = sender as TextBox ;
337+ textBox . LostFocus -= RenameTextBox_LostFocus ;
338+ EndRename ( textBox ) ;
339+ }
340+ else if ( e . Key == VirtualKey . Enter )
341+ {
342+ TextBox textBox = sender as TextBox ;
343+ CommitRename ( textBox ) ;
344+ }
345+ }
322346
323- textBox . Text = selectedItem . FileName ;
324- textBox . Select ( 0 , selectedItem . FileName . Length - extensionLength ) ;
347+ private void RenameTextBox_LostFocus ( object sender , RoutedEventArgs e )
348+ {
349+ TextBox textBox = e . OriginalSource as TextBox ;
350+ CommitRename ( textBox ) ;
351+ }
325352
326- await renameDialog . ShowAsync ( ) ;
353+ private async void CommitRename ( TextBox textBox )
354+ {
355+ EndRename ( textBox ) ;
327356
357+ try
358+ {
359+ var selectedItem = renamingItem ;
328360 string currentName = selectedItem . FileName ;
329- string newName = renameDialog . storedRenameInput ;
361+ string newName = textBox . Text ;
330362
331363 if ( newName == null )
332364 return ;
333365
334- Debug . WriteLine ( currentName ) ;
335- Debug . WriteLine ( newName ) ;
336366 await tabInstance . instanceInteraction . RenameFileItem ( selectedItem , currentName , newName ) ;
337367 }
338368 catch ( Exception )
339369 {
340370
341371 }
342372 }
373+
374+ private void EndRename ( TextBox textBox )
375+ {
376+ StackPanel parentPanel = textBox . Parent as StackPanel ;
377+ TextBlock textBlock = parentPanel . Children [ 0 ] as TextBlock ;
378+ textBox . Visibility = Visibility . Collapsed ;
379+ textBlock . Visibility = Visibility . Visible ;
380+ textBox . LostFocus -= RenameTextBox_LostFocus ;
381+ textBox . KeyDown += RenameTextBox_KeyDown ;
382+ }
343383 }
344384}
0 commit comments