On rename or new, reselect the file in its new location in tree

This commit is contained in:
Thomas
2023-03-23 06:58:50 +00:00
parent 08e89d7a9b
commit c1a4a18b45
3 changed files with 34 additions and 24 deletions

View File

@@ -85,12 +85,12 @@ namespace Terminal.Gui {
}
/// <inheritdoc/>
public bool Rename (FileSystemInfo toRename)
public FileSystemInfo Rename (FileSystemInfo toRename)
{
// Dont allow renaming C: or D: or / (on linux) etc
if(toRename is DirectoryInfo dir && dir.Parent == null)
{
return false;
return null;
}
if(Prompt("Rename",toRename.Name,out var newName))
@@ -101,39 +101,43 @@ namespace Terminal.Gui {
if (toRename is FileInfo f) {
f.MoveTo(Path.Combine(f.Directory.FullName,newName));
var newLocation = new FileInfo(Path.Combine(f.Directory.FullName,newName));
f.MoveTo(newLocation.FullName);
return newLocation;
} else {
var d = ((DirectoryInfo)toRename);
d.MoveTo(Path.Combine(d.Parent.FullName,newName));
}
return true;
var newLocation = new DirectoryInfo(Path.Combine(d.Parent.FullName,newName));
d.MoveTo(newLocation.FullName);
return newLocation;
}
} catch (Exception ex) {
MessageBox.ErrorQuery ("Rename Failed", ex.Message, "Ok");
}
}
}
return false;
return null;
}
/// <inheritdoc/>
public bool New (DirectoryInfo inDirectory)
public FileSystemInfo New (DirectoryInfo inDirectory)
{
if(Prompt("New Folder","",out var named))
{
if(!string.IsNullOrWhiteSpace(named))
{
try {
Directory.CreateDirectory(Path.Combine(inDirectory.FullName,named));
return true;
var newDir = new DirectoryInfo(Path.Combine(inDirectory.FullName,named));
newDir.Create();
return newDir;
} catch (Exception ex) {
MessageBox.ErrorQuery ("Rename Failed", ex.Message, "Ok");
}
}
}
return false;
return null;
}
}
}

View File

@@ -489,17 +489,25 @@ namespace Terminal.Gui {
if(toRename?.Length == 1)
{
if(FileOperationsHandler.Rename (toRename.Single()))
var newNamed = FileOperationsHandler.Rename (toRename.Single());
if(newNamed != null)
{
RefreshState();
RestoreSelection(newNamed);
}
}
}
private void New()
{
if(state != null && FileOperationsHandler.New (state.Directory))
if(state != null)
{
RefreshState();
var created = FileOperationsHandler.New (state.Directory);
if(created != null)
{
RefreshState();
RestoreSelection(created);
}
}
}
private FileSystemInfo[] GetFocusedFiles()
@@ -1389,10 +1397,10 @@ namespace Terminal.Gui {
{
return this.state?.Children [(int)this.tableView.Table.Rows [rowIndex] [0]];
}
private int? StatsToRow (FileSystemInfoStats stats)
private int? StatsToRow (FileSystemInfo fileSystemInfo)
{
// find array index of the current state for the stats
var idx = state?.Children.IndexOf ((f) => f.FileSystemInfo.FullName == stats.FileSystemInfo.FullName);
var idx = state?.Children.IndexOf ((f) => f.FileSystemInfo.FullName == fileSystemInfo.FullName);
if (idx != -1 && idx != null) {
@@ -2056,7 +2064,7 @@ namespace Terminal.Gui {
this.dlg.PushState (goTo, false, true, false);
if (restoreSelection != null) {
this.dlg.RestoreSelection (restoreSelection);
this.dlg.RestoreSelection (restoreSelection.FileSystemInfo);
}
return true;
@@ -2124,7 +2132,7 @@ namespace Terminal.Gui {
}
}
private void RestoreSelection (FileSystemInfoStats toRestore)
private void RestoreSelection (FileSystemInfo toRestore)
{
var toReselect = StatsToRow (toRestore);

View File

@@ -25,11 +25,10 @@ namespace Terminal.Gui {
/// in <see cref="FileDialog"/>.
/// </summary>
/// <param name="toRename"></param>
/// <returns><see langword="true"/> if operation was completed or
/// <see langword="false"/> if cancelled</returns>
/// <returns>The new name for the file or null if cancelled</returns>
/// <remarks>Ensure you use a try/catch block with appropriate
/// error handling (e.g. showing a <see cref="MessageBox"/></remarks>
bool Rename(FileSystemInfo toRename);
FileSystemInfo Rename(FileSystemInfo toRename);
/// <summary>
@@ -38,10 +37,9 @@ namespace Terminal.Gui {
/// </summary>
/// <param name="inDirectory">The parent directory in which the new
/// directory should be created</param>
/// <returns><see langword="true"/> if operation was completed or
/// <see langword="false"/> if cancelled</returns>
/// <returns>The newly created directory or null if cancelled.</returns>
/// <remarks>Ensure you use a try/catch block with appropriate
/// error handling (e.g. showing a <see cref="MessageBox"/></remarks>
bool New(DirectoryInfo inDirectory);
FileSystemInfo New(DirectoryInfo inDirectory);
}
}