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
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/FileDialogs/FileDialog.TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ private bool TableView_KeyDown (Key keyEvent)
// BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/5087#issuecomment-4328093883
private void TableViewOnValueChanged (object? sender, ValueChangedEventArgs<TableSelection?> e)
{
if (!_tableView.HasFocus || _tableView.Value is null || _tableView.Table?.Rows == 0)
if (_pushingState || !_tableView.HasFocus || _tableView.Value is null || _tableView.Table?.Rows == 0)
{
return;
}
Expand Down
59 changes: 59 additions & 0 deletions Tests/UnitTestsParallelizable/Views/FileDialogResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,65 @@ public void FileDialog_PathField_BadChars_AreSuppressed (char badChar)
Assert.Equal (insertionPointBefore, tbPath.InsertionPoint);
}

[Fact]
public void FileDialog_Accepting_Directory_From_Table_Keeps_Path_On_Opened_Directory ()
{
// Copilot
MockFileSystem fs = new ();
fs.AddDirectory ("/UI");
fs.AddDirectory ("/UI/Window");
fs.AddFile ("/UI/Window/file1.txt", new MockFileData ("hello"));

using FileDialog fd = new TestableFileDialog (fs) { OpenMode = OpenMode.File };
fd.Path = "/UI";

FieldInfo? tableViewField = typeof (FileDialog).GetField ("_tableView", BindingFlags.Instance | BindingFlags.NonPublic);
Assert.NotNull (tableViewField);

TableView tableView = Assert.IsType<TableView> (tableViewField!.GetValue (fd));

// Find the "Window" directory row by scanning the table source rather than hard-coding an index.
int windowRow = FindRowByName (tableView, "Window");
Assert.True (windowRow >= 0, "Expected to find a row named 'Window' in the table.");

tableView.SetFocus ();
tableView.SetSelection (0, windowRow, false);

// Path should end with the selected directory name (platform-independent check).
Assert.EndsWith ("Window", fd.Path);

string pathBeforeAccept = fd.Path;

tableView.InvokeCommand (Command.Accept);

// After accepting a directory in File mode, the path should remain unchanged
// (navigates into the directory rather than selecting a file).
Assert.Equal (pathBeforeAccept, fd.Path);
}

/// <summary>Finds a row in a <see cref="TableView"/> whose first column contains the given name.</summary>
private static int FindRowByName (TableView tableView, string name)
{
ITableSource? source = tableView.Table;

if (source is null)
{
return -1;
}

for (var row = 0; row < source.Rows; row++)
{
string cellText = source [row, 0]?.ToString () ?? string.Empty;

if (cellText.Contains (name, StringComparison.Ordinal))
{
return row;
}
}

return -1;
}

/// <summary>Testable subclass that exposes the internal file-system constructor.</summary>
private sealed class TestableFileDialog : FileDialog
{
Expand Down
Loading