Skip to content

Commit 1810cb1

Browse files
committed
Nuked View.IsAdded.
Added View.SuperViewChanged.
1 parent 576d39a commit 1810cb1

16 files changed

+130
-108
lines changed

Terminal.Gui/View/View.Hierarchy.cs

+38-43
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,38 @@ public partial class View // SuperView/SubView hierarchy management (SuperView,
3030
public View? SuperView
3131
{
3232
get => _superView!;
33-
set => throw new InvalidOperationException (@"SuperView cannot be set.");
33+
private set => SetSuperView (value);
3434
}
3535

36-
#region AddRemove
37-
38-
private bool _isAdded;
39-
40-
/// <summary>Indicates whether the view was added to <see cref="SuperView"/>.</summary>
41-
public bool IsAdded
36+
private void SetSuperView (View? value)
4237
{
43-
get => _isAdded;
44-
private set
38+
if (_superView == value)
4539
{
46-
if (_isAdded == value)
47-
{
48-
return;
49-
}
50-
51-
_isAdded = value;
52-
RaiseIsAddedChanged ();
40+
return;
5341
}
42+
43+
_superView = value;
44+
RaiseSuperViewChanged ();
5445
}
5546

56-
internal void RaiseIsAddedChanged ()
47+
private void RaiseSuperViewChanged ()
5748
{
58-
// Tell subclasses that a subview has been added
59-
EventArgs<bool> args = new (IsAdded);
60-
OnIsAddedChanged (args);
49+
SuperViewChangedEventArgs args = new (SuperView, this);
50+
OnSuperViewChanged (args);
6151

62-
IsAddedChanged?.Invoke (this, args);
52+
SuperViewChanged?.Invoke (this, args);
6353
}
6454

65-
/// <summary>Raised when this View has been added to a SuperView.</summary>
66-
public event EventHandler<EventArgs<bool>>? IsAddedChanged;
55+
/// <summary>
56+
/// Called when the SuperView of this View has changed.
57+
/// </summary>
58+
/// <param name="e"></param>
59+
protected virtual void OnSuperViewChanged (SuperViewChangedEventArgs e) { }
60+
61+
/// <summary>Raised when the SuperView of this View has changed.</summary>
62+
public event EventHandler<SuperViewChangedEventArgs>? SuperViewChanged;
6763

68-
/// <summary>Method invoked when a SubView has been added to this view.</summary>
69-
/// <param name="newValue">The new value of IsAdded</param>
70-
protected virtual void OnIsAddedChanged (EventArgs<bool> newValue) { }
64+
#region AddRemove
7165

7266
/// <summary>Adds a SubView (child) to this view.</summary>
7367
/// <remarks>
@@ -92,25 +86,24 @@ protected virtual void OnIsAddedChanged (EventArgs<bool> newValue) { }
9286
return null;
9387
}
9488

95-
if (view.IsAdded)
89+
//Debug.Assert (view.SuperView is null, $"{view} already has a SuperView: {view.SuperView}.");
90+
if (view.SuperView is {})
9691
{
97-
Logging.Warning ($"{view} already has IsAdded == true.");
92+
Logging.Warning ($"{view} already has a SuperView: {view.SuperView}.");
9893
}
9994

95+
//Debug.Assert (!InternalSubViews.Contains (view), $"{view} has already been Added to {this}.");
10096
if (InternalSubViews.Contains (view))
10197
{
102-
Logging.Warning ($"{view} has already been added to {this}.");
98+
Logging.Warning ($"{view} has already been Added to {this}.");
10399
}
104100

105101
// TileView likes to add views that were previously added and have HasFocus = true. No bueno.
106102
view.HasFocus = false;
107103

108104
// TODO: Make this thread safe
109105
InternalSubViews.Add (view);
110-
view._superView = this;
111-
112-
// This causes IsAddedChanged to be raised on view
113-
view.IsAdded = true;
106+
view.SuperView = this;
114107

115108
if (view is { Enabled: true, Visible: true, CanFocus: true })
116109
{
@@ -211,17 +204,22 @@ protected virtual void OnSubViewAdded (View view) { }
211204

212205
if (InternalSubViews.Count == 0)
213206
{
214-
return view;
207+
return view;
215208
}
216209

217-
if (!view.IsAdded)
210+
if (view.SuperView is null)
218211
{
219-
Logging.Warning ($"{view} has IsAdded == false.");
212+
Logging.Warning ($"{view} cannot be Removed. SuperView is null.");
213+
}
214+
215+
if (view.SuperView != this)
216+
{
217+
Logging.Warning ($"{view} cannot be Removed. SuperView is not this ({view.SuperView}.");
220218
}
221219

222220
if (!InternalSubViews.Contains (view))
223221
{
224-
Logging.Warning ($"{view} has not been added to {this}.");
222+
Logging.Warning ($"{view} cannot be Removed. It has not been added to {this}.");
225223
}
226224

227225
Rectangle touched = view.Frame;
@@ -241,15 +239,12 @@ protected virtual void OnSubViewAdded (View view) { }
241239
// Clean up focus stuff
242240
_previouslyFocused = null;
243241

244-
if (view._superView is { } && view._superView._previouslyFocused == this)
242+
if (view.SuperView is { } && view.SuperView._previouslyFocused == this)
245243
{
246-
view._superView._previouslyFocused = null;
244+
view.SuperView._previouslyFocused = null;
247245
}
248246

249-
view._superView = null;
250-
251-
// This causes IsAddedChanged to be raised on view
252-
view.IsAdded = false;
247+
view.SuperView = null;
253248

254249
SetNeedsLayout ();
255250
SetNeedsDraw ();

Terminal.Gui/Views/ComboBox.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public ComboBox ()
5959
// On resize
6060
SubViewsLaidOut += (sender, a) => ProcessLayout ();
6161

62-
IsAddedChanged += (s, e) =>
62+
SuperViewChanged += (s, e) =>
6363
{
6464
// Determine if this view is hosted inside a dialog and is the only control
6565
for (View view = SuperView; view != null; view = view.SuperView)

Terminal.Gui/Views/Menu/MenuBar.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public MenuBar ()
8484
WantMousePositionReports = true;
8585
IsMenuOpen = false;
8686

87-
IsAddedChanged += MenuBar_IsAddedChanged;
87+
SuperViewChanged += MenuBar_SuperViewChanged;
8888

8989
// Things this view knows how to do
9090
AddCommand (
@@ -1157,10 +1157,10 @@ private Point GetLocationOffset ()
11571157
return new (-2, 0);
11581158
}
11591159

1160-
private void MenuBar_IsAddedChanged (object? sender, EventArgs<bool> e)
1160+
private void MenuBar_SuperViewChanged (object? sender, SuperViewChangedEventArgs _)
11611161
{
11621162
_initialCanFocus = CanFocus;
1163-
IsAddedChanged -= MenuBar_IsAddedChanged;
1163+
SuperViewChanged -= MenuBar_SuperViewChanged;
11641164
}
11651165

11661166
private void MoveLeft ()

Terminal.Gui/Views/Shortcut.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -824,17 +824,17 @@ protected override void Dispose (bool disposing)
824824
{
825825
TitleChanged -= Shortcut_TitleChanged;
826826

827-
if (CommandView?.IsAdded == false)
827+
if (CommandView.SuperView is null)
828828
{
829829
CommandView.Dispose ();
830830
}
831831

832-
if (HelpView?.IsAdded == false)
832+
if (HelpView.SuperView is null)
833833
{
834834
HelpView.Dispose ();
835835
}
836836

837-
if (KeyView?.IsAdded == false)
837+
if (KeyView.SuperView is null)
838838
{
839839
KeyView.Dispose ();
840840
}

Terminal.Gui/Views/TextField.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public TextField ()
4545

4646
Initialized += TextField_Initialized;
4747

48-
IsAddedChanged += TextField_IsAddedChanged;
48+
SuperViewChanged += TextField_SuperViewChanged;
4949

5050
// Things this view knows how to do
5151
AddCommand (
@@ -1180,7 +1180,7 @@ public void Undo ()
11801180

11811181
private void Adjust ()
11821182
{
1183-
if (!IsAdded)
1183+
if (SuperView is null)
11841184
{
11851185
return;
11861186
}
@@ -1818,9 +1818,9 @@ private void ShowContextMenu ()
18181818
ContextMenu.Show (BuildContextMenuBarItem ());
18191819
}
18201820

1821-
private void TextField_IsAddedChanged (object sender, EventArgs<bool> e)
1821+
private void TextField_SuperViewChanged (object sender, SuperViewChangedEventArgs e)
18221822
{
1823-
if (e.CurrentValue)
1823+
if (e.SuperView is {})
18241824
{
18251825
if (Autocomplete.HostControl is null)
18261826
{

Terminal.Gui/Views/TextView.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ public TextView ()
18991899

19001900
Initialized += TextView_Initialized!;
19011901

1902-
IsAddedChanged += TextView_IsAddedChanged!;
1902+
SuperViewChanged += TextView_SuperViewChanged!;
19031903

19041904
SubViewsLaidOut += TextView_LayoutComplete;
19051905

@@ -6442,9 +6442,9 @@ private string StringFromRunes (List<Cell> cells)
64426442
return StringExtensions.ToString (encoded);
64436443
}
64446444

6445-
private void TextView_IsAddedChanged (object sender, EventArgs<bool> e)
6445+
private void TextView_SuperViewChanged (object sender, SuperViewChangedEventArgs e)
64466446
{
6447-
if (e.CurrentValue)
6447+
if (e.SuperView is {})
64486448
{
64496449
if (Autocomplete.HostControl is null)
64506450
{

Terminal.Gui/Views/Toplevel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public Toplevel ()
3333
Arrangement = ViewArrangement.Overlapped;
3434
Width = Dim.Fill ();
3535
Height = Dim.Fill ();
36-
ColorScheme = Colors.ColorSchemes ["TopLevel"];
36+
base.ColorScheme = Colors.ColorSchemes ["TopLevel"];
3737
MouseClick += Toplevel_MouseClick;
3838
}
3939

@@ -202,7 +202,7 @@ internal virtual void OnUnloaded ()
202202
}
203203

204204
#endregion
205-
205+
206206
#region Size / Position Management
207207

208208
// TODO: Make cancelable?

Tests/StressTests/ScenariosStressTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void SubscribeAllSubViews (View view)
154154
{
155155
view.DrawComplete += (s, a) => drawCompleteCount++;
156156
view.SubViewsLaidOut += (s, a) => laidOutCount++;
157-
view.IsAddedChanged += (s, a) => addedCount++;
157+
view.SuperViewChanged += (s, a) => addedCount++;
158158

159159
foreach (View subview in view.SubViews)
160160
{

Tests/UnitTests/View/ViewTests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void Disposing_Event_Notify_All_Subscribers_On_The_First_Container ()
5252

5353
var container2 = new View { Id = "Container2" };
5454

55+
// BUGBUG: It's not legit to add a View to two SuperViews
5556
container2.Add (view);
5657
Assert.Equal (container2, view.SuperView);
5758
Assert.Equal (container1.SubViews.Count, container2.SubViews.Count);
@@ -98,6 +99,7 @@ public void Disposing_Event_Notify_All_Subscribers_On_The_Second_Container ()
9899
var count = 0;
99100

100101
view.Disposing += View_Disposing;
102+
// BUGBUG: It's not legit to add a View to two SuperViews
101103
container2.Add (view);
102104
Assert.Equal (container2, view.SuperView);
103105

@@ -140,6 +142,7 @@ public void Not_Notifying_Dispose ()
140142

141143
var container2 = new View { Id = "Container2" };
142144

145+
// BUGBUG: It's not legit to add a View to two SuperViews
143146
container2.Add (view);
144147
Assert.Equal (container2, view.SuperView);
145148
Assert.Equal (container1.SubViews.Count, container2.SubViews.Count);

Tests/UnitTests/Views/ColorPickerTests.cs

+2-9
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,6 @@ public void ColorPicker_DisposesOldViews_OnModelChange ()
308308
public void ColorPicker_EnterHexFor_ColorName ()
309309
{
310310
ColorPicker cp = GetColorPicker (ColorModel.RGB, true, true);
311-
Application.Navigation = new ();
312-
Application.Top = new ();
313-
Application.Top.Add (cp);
314311

315312
cp.Draw ();
316313

@@ -367,9 +364,6 @@ public void ColorPicker_EnterHexFor_ColorName ()
367364
public void ColorPicker_EnterHexFor_ColorName_AcceptVariation ()
368365
{
369366
ColorPicker cp = GetColorPicker (ColorModel.RGB, true, true);
370-
Application.Navigation = new ();
371-
Application.Top = new ();
372-
Application.Top.Add (cp);
373367

374368
cp.Draw ();
375369

@@ -730,9 +724,6 @@ public void ColorPicker_SyncBetweenTextFieldAndBars ()
730724
public void ColorPicker_TabCompleteColorName ()
731725
{
732726
ColorPicker cp = GetColorPicker (ColorModel.RGB, true, true);
733-
Application.Navigation = new ();
734-
Application.Top = new ();
735-
Application.Top.Add (cp);
736727

737728
cp.Draw ();
738729

@@ -852,6 +843,8 @@ private ColorPicker GetColorPicker (ColorModel colorModel, bool showTextFields,
852843
cp.Style.ShowColorName = showName;
853844
cp.ApplyStyleChanges ();
854845

846+
Application.Navigation = new ();
847+
855848
Application.Top = new() { Width = 20, Height = 5 };
856849
Application.Top.Add (cp);
857850

Tests/UnitTests/Views/RadioGroupTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ public void HotKeys_HasFocus_True_Selects ()
394394
Assert.True (Application.RaiseKeyDownEvent (Key.R.WithAlt));
395395
Assert.Equal (1, rg.SelectedItem);
396396

397+
Application.Top.Remove (rg);
397398
var superView = new View ();
398399
superView.Add (rg);
399400
Assert.True (superView.NewKeyDownEvent (Key.T));

0 commit comments

Comments
 (0)