Skip to content

Commit 4531c0c

Browse files
committed
Replace SnapshotSubviews method with ViewCollectionHelpers class
1 parent 71887fa commit 4531c0c

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Terminal.Gui/ViewBase/View.Hierarchy.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public View? SuperView
3636
private set => SetSuperView (value);
3737
}
3838

39-
internal List<View> SnapshotSubviews () { return [..InternalSubViews]; }
40-
4139
private void SetSuperView (View? value)
4240
{
4341
if (_superView == value)
@@ -560,3 +558,22 @@ private void PerformActionForSubView (View subview, Action<View> action)
560558

561559
#endregion SubViewOrdering
562560
}
561+
562+
internal static class ViewCollectionHelpers
563+
{
564+
/// <summary>Returns a defensive copy of any <see cref="IEnumerable{T}"/>.</summary>
565+
internal static View [] Snapshot (this IEnumerable<View> source)
566+
{
567+
if (source is IList<View> list)
568+
{
569+
// The list parameter might be the live `_subviews`, so freeze it under a lock
570+
lock (list)
571+
{
572+
return [.. list]; // C# 12 slice copy (= new List<View>(list).ToArray())
573+
}
574+
}
575+
576+
// Anything else (LINQ result, iterator block, etc.) we just enumerate.
577+
return source.ToArray (); // Safe because it’s not shared mutable state
578+
}
579+
}

0 commit comments

Comments
 (0)