Skip to content

Commit a6258ed

Browse files
tigCopilot
andauthored
Updates IListDataSource.Render to rename the start parameter to viewportXOffset (#4392)
* Add comprehensive unit tests for WindowsKeyConverter - Implement 118 parallelizable unit tests for WindowsKeyConverter - Cover ToKey and ToKeyInfo methods with full bidirectional testing - Test basic characters, modifiers, special keys, function keys - Test VK_PACKET Unicode/IME input - Test OEM keys, NumPad keys, and lock states - Include round-trip conversion tests - All tests passing successfully Fixes #4389 * Rename `start` parameter to `viewportXOffset` for clarity The `start` parameter in several methods and interfaces has been renamed to `viewportXOffset` to better reflect its purpose as the horizontal offset of the viewport during string rendering. - Updated method signatures in `ListViewWithSelection` to use `viewportXOffset` instead of `start`, including default values. - Modified the `RenderUstr` method in `ListViewWithSelection` to use `viewportXOffset` for calculating the starting index. - Renamed the `start` parameter to `viewportXOffset` in the `IListDataSource` interface and updated its documentation. - Replaced all occurrences of `start` with `viewportXOffset` in the `ListWrapper<T>` class, including method calls and logic. - Updated the `RenderUstr` method in `ListWrapper<T>` to use `viewportXOffset` for substring calculations. - Adjusted the test method in `ListViewTests.cs` to reflect the parameter name change. These changes improve code readability and make the parameter's role in rendering logic more explicit. * Remove WindowsKeyConverterTests class that was added by mistake * Modernized ListView and IListDataSource - Tons of new unit tests Refactored `ListView` and `IListDataSource` to improve readability, maintainability, and functionality. Introduced `ListWrapper<T>` as a default implementation of `IListDataSource` for easier integration with standard collections. Enhanced `ListView` with better handling of marking, selection, and scrolling. Replaced `viewportXOffset` with `viewportX` for horizontal scrolling. Added `EnsureSelectedItemVisible` to maintain visibility of the selected item. Updated `IListDataSource` with detailed XML documentation and added `SuspendCollectionChangedEvent` for bulk updates. Improved null safety with nullable reference types. Added comprehensive unit tests for `ListWrapper<T>` and `IListDataSource` to ensure robustness. Modernized the codebase with C# features like expression-bodied members and pattern matching. Fixed bugs related to `SelectedItem` validation and rendering artifacts. * Improve index validation in ComboBox and ListView Enhance robustness by adding stricter checks for valid indices in ComboBox and ListView. Updated conditions in the `_listview.SelectedItemChanged` event handler to ensure `e.Item` is non-negative before accessing `_searchSet`. Modified the `SetValue` method to use `e.Item` instead of `_listview.SelectedItem`. In ListView, updated the `OnSelectedChanged` method to validate that `SelectedItem` is non-negative (`>= 0`) before accessing the `Source` list. These changes prevent potential out-of-range errors and improve code safety. * Refactor and enhance test coverage across modules Refactored and added new tests to improve coverage, readability, and consistency across multiple test files. Key changes include: - **ShortcutTests.cs**: Added tests for `BindKeyToApplication` and removed redundant tests. - **SourcesManagerTests.cs**: Renamed `Update_*` tests to `Load_*` for clarity. - **ArrangementTests.cs**: Reintroduced `MouseGrabHandler` tests, added `ViewArrangement` flag tests, and improved structure. - **NeedsDrawTests.cs**: Replaced `Application.Screen.Size` with fixed dimensions for better isolation. - **DimAutoTests.cs**: Updated layout tests to use fixed dimensions. - **FrameTests.cs**: Standardized object initialization and validated frame behavior. - **SubViewTests.cs**: Improved formatting and modernized event handling. - **NumericUpDownTests.cs**: Decoupled layout tests from screen size. General improvements: - Enhanced formatting and removed redundant tests. - Added comments for clarity. - Introduced `ITestOutputHelper` for better debugging in `ArrangementTests`. * Refactor to use nullable types for better null safety Enabled nullable reference types across the codebase to improve null safety and prevent potential null reference issues. Refactored `SelectedItem` and related properties from `int` to `int?` to represent no selection with `null` instead of `-1`. Updated logic, event arguments, and method signatures to handle nullable values consistently. Simplified object initialization using modern C# syntax and improved code readability with interpolated strings. Added null checks and early returns to prevent runtime errors. Enhanced error handling by throwing `ArgumentOutOfRangeException` for invalid values. Updated tests to reflect the changes, replacing assertions for `-1` with `null` and ensuring proper handling of nullable values. Cleaned up redundant code and improved formatting for better maintainability. * on` functionality has been deprecated, refactored, or removed from the `Shortcut` class. * Refactor: Transition to instance-based architecture Updated `Run-LocalCoverage.ps1` to increase `--blame-hang-timeout` from 10s to 60s. Improved null safety in `GuiTestContext` by adding null-conditional operators. Commented out problematic code in `SetupFakeApplicationAttribute.cs` to prevent test hangs. Excluded `ViewBase` files from `UnitTests.Parallelizable.csproj` and removed redundant folder declarations. Simplified event handling in `IListDataSourceTests.cs` and updated `ListViewTests.cs` to use nullable reference types. Enhanced documentation to emphasize the transition to an instance-based application architecture. Updated examples in `application.md`, `multitasking.md`, and `navigation.md` to reflect the use of `Application.Create()` and `View.App`. Clarified the obsolescence of the static `Application` class. Revised table of contents in `toc.yml` to include new sections like "Application Deep Dive" and "Scheme Deep Dive." Added `dotnet-tools.json` for tool configuration. These changes improve maintainability, testability, and alignment with modern C# practices. * Refactor ListViewTests to use Terminal.Gui framework The `ListViewTests` class has been refactored to replace the `AutoInitShutdown` attribute with explicit application lifecycle management using `IApplication` and `app.Init()` from the `Terminal.Gui` framework. Key changes include: - Rewriting tests to use `Terminal.Gui`'s application lifecycle. - Adding a private `_output` field for logging test output via `ITestOutputHelper`. - Updating `DriverAssert.AssertDriverContentsWithFrameAre` to include `app.Driver` for UI verification. - Rewriting tests like `Clicking_On_Border_Is_Ignored`, `EnsureSelectedItemVisible_SelectedItem`, and others to align with the new framework. - Adding explicit calls to `app.Shutdown()` for proper cleanup. - Enabling nullable reference types with `#nullable enable`. - Updating `using` directives and `namespace` to reflect the new structure. These changes improve test maintainability, compatibility, and diagnostics. * Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs Co-authored-by: Copilot <[email protected]> * Update Terminal.Gui/Views/CollectionNavigation/CollectionNavigatorBase.cs Co-authored-by: Copilot <[email protected]> * Update Examples/UICatalog/UICatalogTop.cs Co-authored-by: Copilot <[email protected]> * Update Terminal.Gui/Views/ListWrapper.cs Co-authored-by: Copilot <[email protected]> * Update Terminal.Gui/Views/ListWrapper.cs Co-authored-by: Copilot <[email protected]> * Updated the `SetMark` method to return `Source.IsMarked(SelectedItem.Value)` for consistency and removed an outdated comment questioning its correctness. Enhanced the exception message in the `SelectedItem` property setter to provide clearer guidance when the value is out of range. * Add comprehensive ListView behavior test coverage Added multiple test methods to validate `ListView` behavior: - `Vertical_ScrollBar_Hides_And_Shows_As_Needed`: Ensures the vertical scrollbar auto-hides/shows based on content height. - `Mouse_Wheel_Scrolls`: Verifies vertical scrolling with the mouse wheel updates `TopItem`. - `SelectedItem_With_Source_Null_Does_Nothing`: Confirms no exceptions occur when setting `SelectedItem` with a `null` source. - `Horizontal_Scroll`: Tests horizontal scrolling, including programmatic and mouse wheel interactions, ensuring `LeftItem` updates correctly. - `SetSourceAsync_SetsSource`: Validates the asynchronous `SetSourceAsync` method updates the source and item count. - `AllowsMultipleSelection_Set_To_False_Unmarks_All_But_Selected`: Ensures disabling multiple selection unmarks all but the selected item. - `Source_CollectionChanged_Remove`: Confirms `SelectedItem` and source count update correctly when items are removed from the source collection. --------- Co-authored-by: Copilot <[email protected]>
1 parent c5906c2 commit a6258ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3183
-2166
lines changed

.config/dotnet-tools.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {}
5+
}

Examples/UICatalog/Scenarios/AllViewsTester.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public override void Main ()
6565
// Dispose existing current View, if any
6666
DisposeCurrentView ();
6767

68-
CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem]);
68+
CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem.Value]);
6969

7070
// Force ViewToEdit to be the view and not a subview
7171
if (_adornmentsEditor is { })

Examples/UICatalog/Scenarios/ComboBoxIteration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public override void Main ()
4242

4343
listview.SelectedItemChanged += (s, e) =>
4444
{
45-
lbListView.Text = items [e.Item];
46-
comboBox.SelectedItem = e.Item;
45+
lbListView.Text = items [e.Item!.Value];
46+
comboBox.SelectedItem = e.Item.Value;
4747
};
4848

4949
comboBox.SelectedItemChanged += (sender, text) =>

Examples/UICatalog/Scenarios/DynamicMenuBar.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ public DynamicMenuBarSample ()
712712

713713
btnUp.Accepting += (s, e) =>
714714
{
715-
int i = _lstMenus.SelectedItem;
715+
int i = _lstMenus.SelectedItem.Value;
716716
MenuItem menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
717717

718718
if (menuItem != null)
@@ -734,7 +734,7 @@ public DynamicMenuBarSample ()
734734

735735
btnDown.Accepting += (s, e) =>
736736
{
737-
int i = _lstMenus.SelectedItem;
737+
int i = _lstMenus.SelectedItem.Value;
738738
MenuItem menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
739739

740740
if (menuItem != null)
@@ -836,7 +836,7 @@ public DynamicMenuBarSample ()
836836
: MenuItemCheckStyle.Radio,
837837
ShortcutKey = frmMenuDetails.TextShortcutKey.Text
838838
};
839-
UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
839+
UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem.Value);
840840
}
841841
};
842842

@@ -885,8 +885,8 @@ public DynamicMenuBarSample ()
885885

886886
btnRemove.Accepting += (s, e) =>
887887
{
888-
MenuItem menuItem = (DataContext.Menus.Count > 0 && _lstMenus.SelectedItem > -1
889-
? DataContext.Menus [_lstMenus.SelectedItem].MenuItem
888+
MenuItem menuItem = (DataContext.Menus.Count > 0 && _lstMenus.SelectedItem is {} selectedItem
889+
? DataContext.Menus [selectedItem].MenuItem
890890
: _currentEditMenuBarItem);
891891

892892
if (menuItem != null)
@@ -905,9 +905,9 @@ public DynamicMenuBarSample ()
905905
SelectCurrentMenuBarItem ();
906906
}
907907

908-
if (_lstMenus.SelectedItem > -1)
908+
if (_lstMenus.SelectedItem is {} selected)
909909
{
910-
DataContext.Menus?.RemoveAt (_lstMenus.SelectedItem);
910+
DataContext.Menus?.RemoveAt (selected);
911911
}
912912

913913
if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1)
@@ -927,7 +927,7 @@ public DynamicMenuBarSample ()
927927

928928
_lstMenus.OpenSelectedItem += (s, e) =>
929929
{
930-
_currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
930+
_currentMenuBarItem = DataContext.Menus [e.Item.Value].MenuItem;
931931

932932
if (!(_currentMenuBarItem is MenuBarItem))
933933
{
@@ -945,8 +945,8 @@ public DynamicMenuBarSample ()
945945

946946
_lstMenus.HasFocusChanging += (s, e) =>
947947
{
948-
MenuItem menuBarItem = _lstMenus.SelectedItem > -1 && DataContext.Menus.Count > 0
949-
? DataContext.Menus [_lstMenus.SelectedItem].MenuItem
948+
MenuItem menuBarItem = _lstMenus.SelectedItem is {} selectedItem && DataContext.Menus.Count > 0
949+
? DataContext.Menus [selectedItem].MenuItem
950950
: null;
951951
SetFrameDetails (menuBarItem);
952952
};
@@ -1077,8 +1077,8 @@ void SetFrameDetails (MenuItem menuBarItem = null)
10771077

10781078
if (menuBarItem == null)
10791079
{
1080-
menuItem = _lstMenus.SelectedItem > -1 && DataContext.Menus.Count > 0
1081-
? DataContext.Menus [_lstMenus.SelectedItem].MenuItem
1080+
menuItem = _lstMenus.SelectedItem is {} selectedItem && DataContext.Menus.Count > 0
1081+
? DataContext.Menus [selectedItem].MenuItem
10821082
: _currentEditMenuBarItem;
10831083
}
10841084
else

Examples/UICatalog/Scenarios/DynamicStatusBar.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,12 @@ public DynamicStatusBarSample ()
312312

313313
btnUp.Accepting += (s, e) =>
314314
{
315-
int i = _lstItems.SelectedItem;
315+
if (_lstItems.SelectedItem is null)
316+
{
317+
return;
318+
}
319+
int i = _lstItems.SelectedItem.Value;
320+
316321
Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
317322

318323
if (statusItem != null)
@@ -335,7 +340,12 @@ public DynamicStatusBarSample ()
335340

336341
btnDown.Accepting += (s, e) =>
337342
{
338-
int i = _lstItems.SelectedItem;
343+
if (_lstItems.SelectedItem is null)
344+
{
345+
return;
346+
}
347+
int i = _lstItems.SelectedItem.Value;
348+
339349
Shortcut statusItem = DataContext.Items.Count > 0 ? DataContext.Items [i].Shortcut : null;
340350

341351
if (statusItem != null)
@@ -376,14 +386,17 @@ public DynamicStatusBarSample ()
376386
}
377387
else if (_currentEditStatusItem != null)
378388
{
379-
380389
var statusItem = new DynamicStatusItem
381390
{
382391
Title = frmStatusBarDetails.TextTitle.Text,
383392
Action = frmStatusBarDetails.TextAction.Text,
384393
Shortcut = frmStatusBarDetails.TextShortcut.Text
385394
};
386-
UpdateStatusItem (_currentEditStatusItem, statusItem, _lstItems.SelectedItem);
395+
396+
if (_lstItems.SelectedItem is { } selectedItem)
397+
{
398+
UpdateStatusItem (_currentEditStatusItem, statusItem, selectedItem);
399+
}
387400
}
388401
};
389402

@@ -420,14 +433,14 @@ public DynamicStatusBarSample ()
420433
btnRemove.Accepting += (s, e) =>
421434
{
422435
Shortcut statusItem = DataContext.Items.Count > 0
423-
? DataContext.Items [_lstItems.SelectedItem].Shortcut
436+
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
424437
: null;
425438

426439
if (statusItem != null)
427440
{
428441
_statusBar.RemoveShortcut (_currentSelectedStatusBar);
429442
statusItem.Dispose ();
430-
DataContext.Items.RemoveAt (_lstItems.SelectedItem);
443+
DataContext.Items.RemoveAt (_lstItems.SelectedItem.Value);
431444

432445
if (_lstItems.Source.Count > 0 && _lstItems.SelectedItem > _lstItems.Source.Count - 1)
433446
{
@@ -442,7 +455,7 @@ public DynamicStatusBarSample ()
442455
_lstItems.HasFocusChanging += (s, e) =>
443456
{
444457
Shortcut statusItem = DataContext.Items.Count > 0
445-
? DataContext.Items [_lstItems.SelectedItem].Shortcut
458+
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
446459
: null;
447460
SetFrameDetails (statusItem);
448461
};
@@ -489,7 +502,7 @@ void SetFrameDetails (Shortcut statusItem = null)
489502
if (statusItem == null)
490503
{
491504
newStatusItem = DataContext.Items.Count > 0
492-
? DataContext.Items [_lstItems.SelectedItem].Shortcut
505+
? DataContext.Items [_lstItems.SelectedItem.Value].Shortcut
493506
: null;
494507
}
495508
else

Examples/UICatalog/Scenarios/ListViewWithSelection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void Render (
237237
int col,
238238
int line,
239239
int width,
240-
int start = 0
240+
int viewportX = 0
241241
)
242242
{
243243
container.Move (col, line);
@@ -247,7 +247,7 @@ public void Render (
247247
string.Format ("{{0,{0}}}", -_nameColumnWidth),
248248
Scenarios [item].GetName ()
249249
);
250-
RenderUstr (container, $"{s} ({Scenarios [item].GetDescription ()})", col, line, width, start);
250+
RenderUstr (container, $"{s} ({Scenarios [item].GetDescription ()})", col, line, width, viewportX);
251251
}
252252

253253
public void SetMark (int item, bool value)
@@ -288,10 +288,10 @@ Scenarios [i].GetName ()
288288
}
289289

290290
// A slightly adapted method from: https://github.com/gui-cs/Terminal.Gui/blob/fc1faba7452ccbdf49028ac49f0c9f0f42bbae91/Terminal.Gui/Views/ListView.cs#L433-L461
291-
private void RenderUstr (View view, string ustr, int col, int line, int width, int start = 0)
291+
private void RenderUstr (View view, string ustr, int col, int line, int width, int viewportX = 0)
292292
{
293293
var used = 0;
294-
int index = start;
294+
int index = viewportX;
295295

296296
while (index < ustr.Length)
297297
{

Examples/UICatalog/Scenarios/ListsAndCombos.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override void Main ()
5050
Width = Dim.Percent (40),
5151
Source = new ListWrapper<string> (items)
5252
};
53-
listview.SelectedItemChanged += (s, e) => lbListView.Text = items [listview.SelectedItem];
53+
listview.SelectedItemChanged += (s, e) => lbListView.Text = items [listview.SelectedItem.Value];
5454
win.Add (lbListView, listview);
5555

5656
//var scrollBar = new ScrollBarView (listview, true);

Examples/UICatalog/Scenarios/SpinnerStyles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public override void Main ()
153153
else
154154
{
155155
spinner.Visible = true;
156-
spinner.Style = (SpinnerStyle)Activator.CreateInstance (styleDict [e.Item].Value);
156+
spinner.Style = (SpinnerStyle)Activator.CreateInstance (styleDict [e.Item.Value].Value);
157157
delayField.Text = spinner.SpinDelay.ToString ();
158158
ckbBounce.CheckedState = spinner.SpinBounce ? CheckState.Checked : CheckState.UnChecked;
159159
ckbNoSpecial.CheckedState = !spinner.HasSpecialCharacters ? CheckState.Checked : CheckState.UnChecked;

Examples/UICatalog/UICatalogTop.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public UICatalogTop ()
4343
Unloaded += UnloadedHandler;
4444

4545
// Restore previous selections
46-
_categoryList.SelectedItem = _cachedCategoryIndex;
46+
if (_categoryList.Source?.Count > 0) {
47+
_categoryList.SelectedItem = _cachedCategoryIndex ?? 0;
48+
} else {
49+
_categoryList.SelectedItem = null;
50+
}
4751
_scenarioList.SelectedRow = _cachedScenarioIndex;
4852

4953
SchemeName = CachedTopLevelScheme = SchemeManager.SchemesToSchemeName (Schemes.Base);
@@ -510,7 +514,7 @@ private void ScenarioView_OpenSelectedItem (object? sender, EventArgs? e)
510514
#region Category List
511515

512516
private readonly ListView? _categoryList;
513-
private static int _cachedCategoryIndex;
517+
private static int? _cachedCategoryIndex;
514518
public static ObservableCollection<string>? CachedCategories { get; set; }
515519

516520
private ListView CreateCategoryList ()
@@ -540,7 +544,11 @@ private ListView CreateCategoryList ()
540544

541545
private void CategoryView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
542546
{
543-
string item = CachedCategories! [e!.Item];
547+
if (e is null or { Item: null })
548+
{
549+
return;
550+
}
551+
string item = CachedCategories! [e.Item.Value];
544552
ObservableCollection<Scenario> newScenarioList;
545553

546554
if (e.Item == 0)

Scripts/Run-LocalCoverage.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dotnet test Tests/UnitTests `
2727
--verbosity minimal `
2828
--collect:"XPlat Code Coverage" `
2929
--settings Tests/UnitTests/runsettings.coverage.xml `
30-
--blame-hang-timeout 10s
30+
--blame-hang-timeout 60s
3131

3232
# ------------------------------------------------------------
3333
# 4. Run UNIT TESTS (parallel)

0 commit comments

Comments
 (0)