Fixes #4305 - 'FileDialog` TreeView (#4306)

* Partial fix - probably breaks stuff

Refactored `FileDialog.cs` by replacing `treeViewContainer` with `_treeView`, adjusting UI component positions, and adding style properties to `_tableView`. Improved user interaction with new event handlers and key bindings. Rearranged `base.Add` calls to reflect the updated UI hierarchy.

* Tweaked Dialog and FileDialog attribute handling

Removed setting of _tableView.Style.InvertSelectedCellFirstCharacter = true;

Commented out custom attribute handling in Dialog.cs for VisualRole.Normal and VisualRole.Focus, reverting to base method. Removed InvertSelectedCellFirstCharacter setting in FileDialog.cs.

* Add tree view toggle to FileDialog

Introduced a new `_btnTreeToggle` button in `FileDialog.cs` to manage the visibility of a tree view within the file dialog interface. Added a new localized string `fdTree` in `Strings.Designer.cs` and `Strings.resx` for UI elements related to the tree view. Adjusted the layout and visibility of the tree and table views to accommodate the toggle functionality. Implemented methods `ToggleTreeVisibility`, `SetTreeVisible`, and `GetTreeToggleText` to handle tree view visibility logic. Cleaned up code and comments for clarity.

* Update localization and test logic for FileDialog

Updated `fdSearchCaption` localization from "Enter search string" to "Find" in `Strings.Designer.cs` and `Strings.resx`. Modified `FileDialogFluentTests.cs` to reflect UI changes by updating button text and removing skip conditions. Adjusted `FileDialogTests.cs` to change `TextField` caption and commented out certain test logic related to path confirmation.

* Moved Search view to be inside the table view container for better usability.

Refactor FileDialog to use nullable reference types

Updated the `FileDialog` class to adopt nullable reference types for improved null safety, marking fields, properties, and methods as nullable where appropriate. Simplified UI component initialization, including repositioning `_tbFind` and `_spinnerView` into `_tableViewContainer` and assigning `Id` properties to `_tableViewContainer` and `_tableView`.

Refactored methods like `TryAcceptMulti` and `GetFocusedFiles` to handle nullability. Simplified `Task.Run` calls and removed unused code, such as the `GetTextField` method and `FileDialogPart` enum, in `FileDialogTests.cs`. Updated tests to directly access subviews using `Id` properties. Minor layout and property adjustments were made to improve maintainability.

* Refactor Dialog class and improve null safety

- Enabled nullable reference types in `Dialog.cs` for better null safety.
- Removed and reintroduced static configuration properties with `[ConfigurationProperty]` attributes for configurability.
- Refactored `Dialog` constructor to use `base.ShadowStyle` and improved button management with alignment logic.
- Updated `Canceled` property with a private backing field and debug assertions.
- Added null-forgiving operators (`!`) across the codebase for nullable reference type compatibility.
- Introduced new test cases to verify `Dialog` behavior, including modal mouse capture and `Canceled` property access.
- Refactored and modernized existing test cases for consistency and readability.
- Removed redundant test cases and performed general code cleanup.
- Improved code comments and debug assertions for clarity and robustness.

* Refactor DialogTests to replace null with empty array

Updated the `BeginButtonTestDialog` method call in `DialogTests.cs`
to replace the `null!` argument with an empty array `[]`. This
improves type safety and ensures explicit handling of the argument.
This commit is contained in:
Tig
2025-10-23 14:54:06 -06:00
committed by GitHub
parent cb748a1c09
commit 1eb5a4e1b9
7 changed files with 507 additions and 479 deletions

View File

@@ -1,5 +1,4 @@
#nullable enable
using UnitTests;
using Xunit.Abstractions;
using static Terminal.Gui.App.Application;
@@ -21,7 +20,7 @@ public class DialogTests (ITestOutputHelper output)
// We test with one button first, but do this to get the width right for 2
int width = $@"{Glyphs.VLine} {btn1} {btn2} {Glyphs.VLine}".Length;
AutoInitShutdownAttribute.FakeResize(new(width, 1));
AutoInitShutdownAttribute.FakeResize (new (width, 1));
// Override CM
Dialog.DefaultButtonAlignment = Alignment.Center;
@@ -164,7 +163,7 @@ public class DialogTests (ITestOutputHelper output)
var buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new (buttonRow.Length, 3));
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 3));
// Default - Center
(runState, Dialog dlg) = BeginButtonTestDialog (
@@ -256,7 +255,7 @@ public class DialogTests (ITestOutputHelper output)
var buttonRow = string.Empty;
var width = 30;
AutoInitShutdownAttribute.FakeResize(new(width, 1));
AutoInitShutdownAttribute.FakeResize (new (width, 1));
// Default - Center
buttonRow =
@@ -447,7 +446,7 @@ public class DialogTests (ITestOutputHelper output)
// 123456 123456
var buttonRow = $"{Glyphs.VLine} {btn1} {btn2} {btn3} {btn4} {Glyphs.VLine}";
int width = buttonRow.GetColumns ();
AutoInitShutdownAttribute.FakeResize(new(width, 3));
AutoInitShutdownAttribute.FakeResize (new (width, 3));
// Default - Center
(runState, Dialog dlg) = BeginButtonTestDialog (
@@ -532,7 +531,7 @@ public class DialogTests (ITestOutputHelper output)
$"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(width, 1));
AutoInitShutdownAttribute.FakeResize (new (width, 1));
(runState, Dialog dlg) = BeginButtonTestDialog (
title,
@@ -596,7 +595,7 @@ public class DialogTests (ITestOutputHelper output)
$"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {Glyphs.VLine}";
width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(width, 1));
AutoInitShutdownAttribute.FakeResize (new (width, 1));
(runState, dlg) = BeginButtonTestDialog (
title,
@@ -676,7 +675,7 @@ public class DialogTests (ITestOutputHelper output)
var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {btn3} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(buttonRow.Length, 3));
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 3));
(runState, Dialog dlg) = BeginButtonTestDialog (
title,
@@ -759,7 +758,7 @@ public class DialogTests (ITestOutputHelper output)
var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(buttonRow.Length, 3));
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 3));
(runState, Dialog dlg) = BeginButtonTestDialog (
title,
@@ -823,7 +822,6 @@ public class DialogTests (ITestOutputHelper output)
public void ButtonAlignment_Two_Hidden ()
{
RunState? runState = null;
var firstIteration = false;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
@@ -839,15 +837,12 @@ public class DialogTests (ITestOutputHelper output)
var buttonRow = $@"{Glyphs.VLine} {btn1} {btn2} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(buttonRow.Length, 3));
Dialog dlg = null;
Button button1, button2;
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 3));
// Default (Center)
button1 = new () { Text = btn1Text };
button2 = new () { Text = btn2Text };
(runState, dlg) = BeginButtonTestDialog (title, width, Alignment.Center, button1, button2);
Button button1 = new () { Text = btn1Text };
Button button2 = new () { Text = btn2Text };
(runState, Dialog dlg) = BeginButtonTestDialog (title, width, Alignment.Center, button1, button2);
button1.Visible = false;
AutoInitShutdownAttribute.RunIteration ();
@@ -896,11 +891,73 @@ public class DialogTests (ITestOutputHelper output)
dlg.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Can_Access_Cancel_Property_After_Run ()
{
Dialog dlg = new ();
dlg.Ready += Dlg_Ready;
Run (dlg);
#if DEBUG_IDISPOSABLE
Assert.False (dlg.WasDisposed);
Assert.False (Top!.WasDisposed);
Assert.Equal (dlg, Top);
#endif
Assert.True (dlg.Canceled);
// Run it again is possible because it isn't disposed yet
Run (dlg);
// Run another view without dispose the prior will throw an assertion
#if DEBUG_IDISPOSABLE
Dialog dlg2 = new ();
dlg2.Ready += Dlg_Ready;
// Exception exception = Record.Exception (() => Run (dlg2));
// Assert.NotNull (exception);
dlg.Dispose ();
// Now it's possible to tun dlg2 without throw
Run (dlg2);
Assert.True (dlg.WasDisposed);
Assert.False (Top.WasDisposed);
Assert.Equal (dlg2, Top);
Assert.False (dlg2.WasDisposed);
dlg2.Dispose ();
// tznind REMOVED: Why wouldn't you be able to read cancelled after dispose - that makes no sense
// Now an assertion will throw accessing the Canceled property
//var exception = Record.Exception (() => Assert.True (dlg.Canceled))!;
//Assert.NotNull (exception);
//Assert.StartsWith ("Cannot access a disposed object.", exception.Message);
Assert.True (Top.WasDisposed);
Shutdown ();
Assert.True (dlg2.WasDisposed);
Assert.Null (Top);
#endif
return;
void Dlg_Ready (object? sender, EventArgs e)
{
((Dialog)sender!).Canceled = true;
RequestStop ();
}
}
[Fact]
[AutoInitShutdown]
public void Dialog_In_Window_With_Size_One_Button_Aligns ()
{
AutoInitShutdownAttribute.FakeResize(new(20, 5));
AutoInitShutdownAttribute.FakeResize (new (20, 5));
// Override CM
Window.DefaultBorderStyle = LineStyle.Single;
@@ -1005,7 +1062,7 @@ public class DialogTests (ITestOutputHelper output)
)]
public void Dialog_In_Window_Without_Size_One_Button_Aligns (int height, string expected)
{
AutoInitShutdownAttribute.FakeResize(new (20, height));
AutoInitShutdownAttribute.FakeResize (new (20, height));
var win = new Window ();
int iterations = -1;
@@ -1052,7 +1109,7 @@ public class DialogTests (ITestOutputHelper output)
[AutoInitShutdown]
public void Dialog_Opened_From_Another_Dialog ()
{
AutoInitShutdownAttribute.FakeResize(new (30, 10));
AutoInitShutdownAttribute.FakeResize (new (30, 10));
// Override CM
Dialog.DefaultButtonAlignment = Alignment.Center;
@@ -1194,7 +1251,7 @@ public class DialogTests (ITestOutputHelper output)
Height = Dim.Percent (85)
};
Begin (d);
AutoInitShutdownAttribute.FakeResize(new(100, 100));
AutoInitShutdownAttribute.FakeResize (new (100, 100));
// Default location is centered, so 100 / 2 - 85 / 2 = 7
var expected = 7;
@@ -1208,7 +1265,7 @@ public class DialogTests (ITestOutputHelper output)
{
var d = new Dialog { X = 1, Y = 1 };
Begin (d);
AutoInitShutdownAttribute.FakeResize(new(100, 100));
AutoInitShutdownAttribute.FakeResize (new (100, 100));
// Default location is centered, so 100 / 2 - 85 / 2 = 7
var expected = 1;
@@ -1230,7 +1287,7 @@ public class DialogTests (ITestOutputHelper output)
var expected = 5;
var d = new Dialog { X = expected, Y = expected, Height = 5, Width = 5 };
Begin (d);
AutoInitShutdownAttribute.FakeResize(new(20, 10));
AutoInitShutdownAttribute.FakeResize (new (20, 10));
// Default location is centered, so 100 / 2 - 85 / 2 = 7
Assert.Equal (new (expected, expected), d.Frame.Location);
@@ -1247,12 +1304,63 @@ public class DialogTests (ITestOutputHelper output)
d.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Modal_Captures_All_Mouse ()
{
var top = new Toplevel
{
Id = "top"
};
var d = new Dialog
{
Width = 10,
Height = 10,
X = 1,
Y = 1
};
AutoInitShutdownAttribute.FakeResize (new (20, 20));
var iterations = 0;
Iteration += (s, a) =>
{
if (++iterations > 2)
{
RequestStop ();
}
if (iterations == 1)
{
Run (d);
d.Dispose ();
}
else if (iterations == 2)
{
// Mouse click outside of dialog
RaiseMouseEvent (new() { Flags = MouseFlags.Button1Clicked, ScreenPosition = new (0, 0) });
}
};
top.MouseEvent += (s, e) =>
{
// This should not be called because the dialog is modal
Assert.Fail ("Mouse event should not be captured by the top level when a dialog is modal.");
};
Run (top);
top.Dispose ();
Shutdown ();
}
[Fact]
[AutoInitShutdown]
public void One_Button_Works ()
{
RunState? runState = null;
Button.DefaultShadow = ShadowStyle.None;
var title = "";
@@ -1262,7 +1370,7 @@ public class DialogTests (ITestOutputHelper output)
$"{Glyphs.VLine} {Glyphs.LeftBracket} {btnText} {Glyphs.RightBracket} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(buttonRow.Length, 10));
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 10));
(runState, Dialog dlg) = BeginButtonTestDialog (
title,
@@ -1275,97 +1383,6 @@ public class DialogTests (ITestOutputHelper output)
dlg.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Size_Default ()
{
var d = new Dialog
{
Width = Dim.Percent (85),
Height = Dim.Percent (85)
};
Begin (d);
AutoInitShutdownAttribute.FakeResize(new(100, 100));
// Default size is Percent(85)
Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
d.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Size_Not_Default ()
{
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
var d = new Dialog { Width = 50, Height = 50 };
Begin (d);
AutoInitShutdownAttribute.FakeResize(new(100, 100));
// Default size is Percent(85)
Assert.Equal (new (50, 50), d.Frame.Size);
d.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Zero_Buttons_Works ()
{
RunState? runState = null;
var title = "1234";
var buttonRow = $"{Glyphs.VLine} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize(new(buttonRow.Length, 3));
(runState, Dialog dlg) = BeginButtonTestDialog (title, width, Alignment.Center, null);
DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
End (runState);
dlg.Dispose ();
}
private (RunState, Dialog) BeginButtonTestDialog (
string title,
int width,
Alignment align,
params Button [] btns
)
{
// Override CM
Dialog.DefaultButtonAlignment = Alignment.Center;
Dialog.DefaultBorderStyle = LineStyle.Single;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
var dlg = new Dialog
{
Title = title,
X = 0,
Y = 0,
Width = width,
Height = 1,
ButtonAlignment = align,
Buttons = btns
};
// Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
dlg.Border!.Thickness = new (1, 0, 1, 0);
RunState runState = Begin (dlg);
dlg.SetNeedsDraw ();
dlg.SetNeedsLayout ();
AutoInitShutdownAttribute.RunIteration ();
return (runState, dlg);
}
[Fact]
[AutoInitShutdown]
public void Run_Does_Not_Dispose_Dialog ()
@@ -1409,115 +1426,92 @@ public class DialogTests (ITestOutputHelper output)
[Fact]
[AutoInitShutdown]
public void Can_Access_Cancel_Property_After_Run ()
public void Size_Default ()
{
Dialog dlg = new ();
dlg.Ready += Dlg_Ready;
Run (dlg);
#if DEBUG_IDISPOSABLE
Assert.False (dlg.WasDisposed);
Assert.False (Top!.WasDisposed);
Assert.Equal (dlg, Top);
#endif
Assert.True (dlg.Canceled);
// Run it again is possible because it isn't disposed yet
Run (dlg);
// Run another view without dispose the prior will throw an assertion
#if DEBUG_IDISPOSABLE
Dialog dlg2 = new ();
dlg2.Ready += Dlg_Ready;
// Exception exception = Record.Exception (() => Run (dlg2));
// Assert.NotNull (exception);
dlg.Dispose ();
// Now it's possible to tun dlg2 without throw
Run (dlg2);
Assert.True (dlg.WasDisposed);
Assert.False (Top.WasDisposed);
Assert.Equal (dlg2, Top);
Assert.False (dlg2.WasDisposed);
dlg2.Dispose ();
// tznind REMOVED: Why wouldn't you be able to read cancelled after dispose - that makes no sense
// Now an assertion will throw accessing the Canceled property
//var exception = Record.Exception (() => Assert.True (dlg.Canceled))!;
//Assert.NotNull (exception);
//Assert.StartsWith ("Cannot access a disposed object.", exception.Message);
Assert.True (Top.WasDisposed);
Shutdown ();
Assert.True (dlg2.WasDisposed);
Assert.Null (Top);
#endif
return;
void Dlg_Ready (object? sender, EventArgs e)
var d = new Dialog
{
((Dialog)sender!).Canceled = true;
RequestStop ();
}
}
Width = Dim.Percent (85),
Height = Dim.Percent (85)
};
Begin (d);
AutoInitShutdownAttribute.FakeResize (new (100, 100));
// Default size is Percent(85)
Assert.Equal (new ((int)(100 * .85), (int)(100 * .85)), d.Frame.Size);
d.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Modal_Captures_All_Mouse ()
public void Size_Not_Default ()
{
Toplevel top = new Toplevel ()
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
var d = new Dialog { Width = 50, Height = 50 };
Begin (d);
AutoInitShutdownAttribute.FakeResize (new (100, 100));
// Default size is Percent(85)
Assert.Equal (new (50, 50), d.Frame.Size);
d.Dispose ();
}
[Fact]
[AutoInitShutdown]
public void Zero_Buttons_Works ()
{
RunState? runState = null;
var title = "1234";
var buttonRow = $"{Glyphs.VLine} {Glyphs.VLine}";
int width = buttonRow.Length;
AutoInitShutdownAttribute.FakeResize (new (buttonRow.Length, 3));
(runState, Dialog dlg) = BeginButtonTestDialog (title, width, Alignment.Center, []);
DriverAssert.AssertDriverContentsWithFrameAre ($"{buttonRow}", output);
End (runState);
dlg.Dispose ();
}
private (RunState, Dialog) BeginButtonTestDialog (
string title,
int width,
Alignment align,
params Button [] btns
)
{
// Override CM
Dialog.DefaultButtonAlignment = Alignment.Center;
Dialog.DefaultBorderStyle = LineStyle.Single;
Dialog.DefaultShadow = ShadowStyle.None;
Button.DefaultShadow = ShadowStyle.None;
var dlg = new Dialog
{
Id = "top",
Title = title,
X = 0,
Y = 0,
Width = width,
Height = 1,
ButtonAlignment = align,
Buttons = btns
};
var d = new Dialog
{
Width = 10,
Height = 10,
X = 1,
Y = 1
};
// Create with no top or bottom border to simplify testing button layout (no need to account for title etc..)
dlg.Border!.Thickness = new (1, 0, 1, 0);
AutoInitShutdownAttribute.FakeResize(new(20, 20));
RunState runState = Begin (dlg);
int iterations = 0;
Iteration += (s, a) =>
{
if (++iterations > 2)
{
RequestStop ();
}
dlg.SetNeedsDraw ();
dlg.SetNeedsLayout ();
if (iterations == 1)
{
Application.Run (d);
d.Dispose ();
}
else if (iterations == 2)
{
// Mouse click outside of dialog
Application.RaiseMouseEvent (new MouseEventArgs () { Flags = MouseFlags.Button1Clicked, ScreenPosition = new Point (0, 0) });
}
AutoInitShutdownAttribute.RunIteration ();
};
top.MouseEvent += (s, e) =>
{
// This should not be called because the dialog is modal
Assert.False (true, "Mouse event should not be captured by the top level when a dialog is modal.");
};
Application.Run (top);
top.Dispose ();
Application.Shutdown ();
return (runState, dlg);
}
}