@@ -30,44 +30,38 @@ public partial class View // SuperView/SubView hierarchy management (SuperView,
30
30
public View ? SuperView
31
31
{
32
32
get => _superView ! ;
33
- set => throw new InvalidOperationException ( @"SuperView cannot be set." ) ;
33
+ private set => SetSuperView ( value ) ;
34
34
}
35
35
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 )
42
37
{
43
- get => _isAdded ;
44
- private set
38
+ if ( _superView == value )
45
39
{
46
- if ( _isAdded == value )
47
- {
48
- return ;
49
- }
50
-
51
- _isAdded = value ;
52
- RaiseIsAddedChanged ( ) ;
40
+ return ;
53
41
}
42
+
43
+ _superView = value ;
44
+ RaiseSuperViewChanged ( ) ;
54
45
}
55
46
56
- internal void RaiseIsAddedChanged ( )
47
+ private void RaiseSuperViewChanged ( )
57
48
{
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 ) ;
61
51
62
- IsAddedChanged ? . Invoke ( this , args ) ;
52
+ SuperViewChanged ? . Invoke ( this , args ) ;
63
53
}
64
54
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 ;
67
63
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
71
65
72
66
/// <summary>Adds a SubView (child) to this view.</summary>
73
67
/// <remarks>
@@ -92,25 +86,24 @@ protected virtual void OnIsAddedChanged (EventArgs<bool> newValue) { }
92
86
return null ;
93
87
}
94
88
95
- if ( view . IsAdded )
89
+ //Debug.Assert (view.SuperView is null, $"{view} already has a SuperView: {view.SuperView}.");
90
+ if ( view . SuperView is { } )
96
91
{
97
- Logging . Warning ( $ "{ view } already has IsAdded == true .") ;
92
+ Logging . Warning ( $ "{ view } already has a SuperView: { view . SuperView } .") ;
98
93
}
99
94
95
+ //Debug.Assert (!InternalSubViews.Contains (view), $"{view} has already been Added to {this}.");
100
96
if ( InternalSubViews . Contains ( view ) )
101
97
{
102
- Logging . Warning ( $ "{ view } has already been added to { this } .") ;
98
+ Logging . Warning ( $ "{ view } has already been Added to { this } .") ;
103
99
}
104
100
105
101
// TileView likes to add views that were previously added and have HasFocus = true. No bueno.
106
102
view . HasFocus = false ;
107
103
108
104
// TODO: Make this thread safe
109
105
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 ;
114
107
115
108
if ( view is { Enabled : true , Visible : true , CanFocus : true } )
116
109
{
@@ -211,17 +204,22 @@ protected virtual void OnSubViewAdded (View view) { }
211
204
212
205
if ( InternalSubViews . Count == 0 )
213
206
{
214
- return view ;
207
+ return view ;
215
208
}
216
209
217
- if ( ! view . IsAdded )
210
+ if ( view . SuperView is null )
218
211
{
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 } .") ;
220
218
}
221
219
222
220
if ( ! InternalSubViews . Contains ( view ) )
223
221
{
224
- Logging . Warning ( $ "{ view } has not been added to { this } .") ;
222
+ Logging . Warning ( $ "{ view } cannot be Removed. It has not been added to { this } .") ;
225
223
}
226
224
227
225
Rectangle touched = view . Frame ;
@@ -241,15 +239,12 @@ protected virtual void OnSubViewAdded (View view) { }
241
239
// Clean up focus stuff
242
240
_previouslyFocused = null ;
243
241
244
- if ( view . _superView is { } && view . _superView . _previouslyFocused == this )
242
+ if ( view . SuperView is { } && view . SuperView . _previouslyFocused == this )
245
243
{
246
- view . _superView . _previouslyFocused = null ;
244
+ view . SuperView . _previouslyFocused = null ;
247
245
}
248
246
249
- view . _superView = null ;
250
-
251
- // This causes IsAddedChanged to be raised on view
252
- view . IsAdded = false ;
247
+ view . SuperView = null ;
253
248
254
249
SetNeedsLayout ( ) ;
255
250
SetNeedsDraw ( ) ;
0 commit comments