Skip to content

Commit cb5c4b0

Browse files
committed
Subview clean up
1 parent 30fafb4 commit cb5c4b0

File tree

7 files changed

+246
-281
lines changed

7 files changed

+246
-281
lines changed

Terminal.Gui/View/Adornment/Adornment.cs

-15
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,6 @@ public Thickness Thickness
8282
#endregion Thickness
8383

8484
#region View Overrides
85-
86-
/// <summary>
87-
/// Adornments cannot be used as sub-views (see <see cref="Parent"/>); setting this property will throw
88-
/// <see cref="InvalidOperationException"/>.
89-
/// </summary>
90-
/// <remarks>
91-
/// While there are no real use cases for an Adornment being a subview, it is not explicitly dis-allowed to support
92-
/// testing. E.g. in AllViewsTester.
93-
/// </remarks>
94-
public override View? SuperView
95-
{
96-
get => base.SuperView!;
97-
set => throw new InvalidOperationException (@"Adornments can not be Subviews or have SuperViews. Use Parent instead.");
98-
}
99-
10085
/// <summary>
10186
/// Gets the rectangle that describes the area of the Adornment. The Location is always (0,0).
10287
/// The size is the size of the <see cref="View.Frame"/>.

Terminal.Gui/View/Layout/DimAuto.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal override int Calculate (int location, int superviewContentSize, View us
7272
{
7373
maxCalculatedSize = textSize;
7474

75-
if (us is { ContentSizeTracksViewport: false, Subviews.Count: 0 })
75+
if (us is { ContentSizeTracksViewport: false, InternalSubviews.Count: 0 })
7676
{
7777
// ContentSize was explicitly set. Use `us.ContentSize` to determine size.
7878
maxCalculatedSize = dimension == Dimension.Width ? us.GetContentSize ().Width : us.GetContentSize ().Height;
@@ -81,7 +81,7 @@ internal override int Calculate (int location, int superviewContentSize, View us
8181
{
8282
// TOOD: All the below is a naive implementation. It may be possible to optimize this.
8383

84-
List<View> includedSubviews = us.Subviews.ToList ();
84+
List<View> includedSubviews = us.InternalSubviews.ToList ();
8585

8686
// If [x] it can cause `us.ContentSize` to change.
8787
// If [ ] it doesn't need special processing for us to determine `us.ContentSize`.
@@ -197,11 +197,11 @@ or DimAbsolute
197197

198198
if (dimension == Dimension.Width)
199199
{
200-
centeredSubViews = us.Subviews.Where (v => v.X.Has<PosCenter> (out _)).ToList ();
200+
centeredSubViews = us.InternalSubviews.Where (v => v.X.Has<PosCenter> (out _)).ToList ();
201201
}
202202
else
203203
{
204-
centeredSubViews = us.Subviews.Where (v => v.Y.Has<PosCenter> (out _)).ToList ();
204+
centeredSubViews = us.InternalSubviews.Where (v => v.Y.Has<PosCenter> (out _)).ToList ();
205205
}
206206

207207
viewsNeedingLayout.AddRange (centeredSubViews);

Terminal.Gui/View/View.Command.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ private void SetupCommands ()
9191
if (!args.Cancel)
9292
{
9393
// If there's an IsDefault peer view in Subviews, try it
94-
var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true });
94+
var isDefaultView = SuperView?.InternalSubviews.FirstOrDefault (v => v is Button { IsDefault: true });
9595

9696
if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button)
9797
{

Terminal.Gui/View/View.Hierarchy.cs

+21-19
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,27 @@ public partial class View // SuperView/SubView hierarchy management (SuperView,
1515
// to make the same mistakes our users make when they poke at the Subviews.
1616
internal IList<View> InternalSubviews => _subviews ?? _empty;
1717

18-
/// <summary>This returns a list of the subviews contained by this view.</summary>
19-
/// <value>The subviews.</value>
18+
/// <summary>Gets the list of Subviews.</summary>
19+
/// <remarks>
20+
/// Use <see cref="Add(Terminal.Gui.View?)"/> and <see cref="Remove(Terminal.Gui.View?)"/> to add or remove subviews.
21+
/// </remarks>
2022
public IList<View> Subviews => _subviews?.AsReadOnly () ?? _empty;
2123

2224
private View? _superView;
2325

24-
/// <summary>Returns the container for this view, or null if this view has not been added to a container.</summary>
25-
/// <value>The super view.</value>
26-
public virtual View? SuperView
26+
/// <summary>Gets this Views SuperView (the View's container), or <see langword="null"/> if this view has not been added as a Subview.</summary>
27+
public View? SuperView
2728
{
2829
get => _superView!;
29-
set => throw new NotImplementedException ();
30+
set => throw new InvalidOperationException (@"SuperView cannot be set.");
3031
}
3132

3233
#region AddRemove
3334

3435
/// <summary>Indicates whether the view was added to <see cref="SuperView"/>.</summary>
3536
public bool IsAdded { get; private set; }
3637

37-
/// <summary>Adds a subview (child) to this view.</summary>
38+
/// <summary>Adds a Subview (child) to this view.</summary>
3839
/// <remarks>
3940
/// <para>
4041
/// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
@@ -94,7 +95,7 @@ public virtual View? SuperView
9495
return view;
9596
}
9697

97-
/// <summary>Adds the specified views (children) to the view.</summary>
98+
/// <summary>Adds the specified Subview (children) to the view.</summary>
9899
/// <param name="views">Array of one or more views (can be optional parameter).</param>
99100
/// <remarks>
100101
/// <para>
@@ -119,28 +120,29 @@ public void Add (params View []? views)
119120
}
120121
}
121122

122-
/// <summary>Event fired when this view is added to another.</summary>
123+
// TODO: Make these events follow the standard pattern
124+
/// <summary>Raised when a Subview has been added to this View.</summary>
123125
public event EventHandler<SuperViewChangedEventArgs>? Added;
124126

125-
/// <summary>Method invoked when a subview is being added to this view.</summary>
127+
/// <summary>Method invoked when a Subview has been added to this view.</summary>
126128
/// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
127-
public virtual void OnAdded (SuperViewChangedEventArgs e)
129+
protected virtual void OnAdded (SuperViewChangedEventArgs e)
128130
{
129131
View view = e.SubView;
130132
view.IsAdded = true;
131133
view.Added?.Invoke (this, e);
132134
}
133135

134-
/// <summary>Method invoked when a subview is being removed from this view.</summary>
136+
/// <summary>Method invoked when a Subview is being removed from this view.</summary>
135137
/// <param name="e">Event args describing the subview being removed.</param>
136-
public virtual void OnRemoved (SuperViewChangedEventArgs e)
138+
protected virtual void OnRemoved (SuperViewChangedEventArgs e)
137139
{
138140
View view = e.SubView;
139141
view.IsAdded = false;
140142
view.Removed?.Invoke (this, e);
141143
}
142144

143-
/// <summary>Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
145+
/// <summary>Removes a Subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
144146
/// <remarks>
145147
/// <para>
146148
/// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the
@@ -208,7 +210,7 @@ public virtual void OnRemoved (SuperViewChangedEventArgs e)
208210
}
209211

210212
/// <summary>
211-
/// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
213+
/// Removes all Subview (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
212214
/// </summary>
213215
/// <remarks>
214216
/// <para>
@@ -231,15 +233,15 @@ public virtual void RemoveAll ()
231233
}
232234
}
233235

234-
/// <summary>Event fired when this view is removed from another.</summary>
236+
/// <summary>Raised when a Subview has been removed from this View.</summary>
235237
public event EventHandler<SuperViewChangedEventArgs>? Removed;
236238

237239
#endregion AddRemove
238240

239-
// TODO: Mark as internal. Or nuke.
241+
// TODO: This drives a weird coupling of Application.Top and View. It's not clear why this is needed.
240242
/// <summary>Get the top superview of a given <see cref="View"/>.</summary>
241243
/// <returns>The superview view.</returns>
242-
public View? GetTopSuperView (View? view = null, View? superview = null)
244+
internal View? GetTopSuperView (View? view = null, View? superview = null)
243245
{
244246
View? top = superview ?? Application.Top;
245247

@@ -275,7 +277,7 @@ public static bool IsInHierarchy (View? start, View? view, bool includeAdornment
275277
return true;
276278
}
277279

278-
foreach (View subView in start.Subviews)
280+
foreach (View subView in start.InternalSubviews)
279281
{
280282
if (view == subView)
281283
{

Terminal.Gui/View/View.Keyboard.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -562,12 +562,12 @@ private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Ke
562562
return true;
563563
}
564564

565-
if (adornment?.Subviews is null)
565+
if (adornment?.InternalSubviews is null)
566566
{
567567
return false;
568568
}
569569

570-
foreach (View subview in adornment.Subviews)
570+
foreach (View subview in adornment.InternalSubviews)
571571
{
572572
bool? subViewHandled = subview.InvokeCommands (key);
573573

@@ -604,7 +604,7 @@ internal bool InvokeCommandsBoundToHotKey (Key hotKey, ref bool? handled)
604604
}
605605

606606
// Now, process any HotKey bindings in the subviews
607-
foreach (View subview in Subviews)
607+
foreach (View subview in InternalSubviews)
608608
{
609609
if (subview == Focused)
610610
{

0 commit comments

Comments
 (0)