diff --git a/Terminal.Gui/Views/FileDialogs/FileDialog.TableView.cs b/Terminal.Gui/Views/FileDialogs/FileDialog.TableView.cs index 9d3edf7ae5..795a76e671 100644 --- a/Terminal.Gui/Views/FileDialogs/FileDialog.TableView.cs +++ b/Terminal.Gui/Views/FileDialogs/FileDialog.TableView.cs @@ -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 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; } diff --git a/Tests/UnitTestsParallelizable/Views/FileDialogResultTests.cs b/Tests/UnitTestsParallelizable/Views/FileDialogResultTests.cs index b8188e3dbe..ffd72df94e 100644 --- a/Tests/UnitTestsParallelizable/Views/FileDialogResultTests.cs +++ b/Tests/UnitTestsParallelizable/Views/FileDialogResultTests.cs @@ -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 (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); + } + + /// Finds a row in a whose first column contains the given name. + 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; + } + /// Testable subclass that exposes the internal file-system constructor. private sealed class TestableFileDialog : FileDialog {