-
Notifications
You must be signed in to change notification settings - Fork 704
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleans up/Refactors View.Subviews
#3962
Conversation
Thanks. Here's another topic I'd like your opinion on: Currently the events around /// <summary>Event fired when this view is added to another.</summary>
public event EventHandler<SuperViewChangedEventArgs>? Added;
/// <summary>Method invoked when a subview is being added to this view.</summary>
/// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
public virtual void OnAdded (SuperViewChangedEventArgs e)
{
View view = e.SubView;
view.IsAdded = true;
view.Added?.Invoke (this, e);
}
/// <summary>Event fired when this view is removed from another.</summary>
public event EventHandler<SuperViewChangedEventArgs>? Removed;
/// <summary>Method invoked when a subview is being removed from this view.</summary>
/// <param name="e">Event args describing the subview being removed.</param>
public virtual void OnRemoved (SuperViewChangedEventArgs e)
{
View view = e.SubView;
view.IsAdded = false;
view.Removed?.Invoke (this, e);
} Note that these are super confusing and do not match standard event patterns.
etc... There are NO overrides of either virtual method in our codebase. The uses of the events ( private void TextField_Added (object sender, SuperViewChangedEventArgs e)
{
if (Autocomplete.HostControl is null)
{
Autocomplete.HostControl = this;
Autocomplete.PopupInsideContainer = false;
}
} This can and should be simplified. My proposal:
(Same for Remove). thots? |
I think that way you're duplicating the event raising twice because |
I would use the |
Please read https://gui-cs.github.io/Terminal.GuiV2Docs/docs/events.html A subclass of View may want to know when it's been added to a superview. With my proposal it can either: a) override Or b) subscribe to Take a look at the latest code i pushed here and let me know what you think. |
Terminal.Gui/Views/ComboBox.cs
Outdated
@@ -59,7 +59,7 @@ public ComboBox () | |||
// On resize | |||
SubviewsLaidOut += (sender, a) => ProcessLayout (); | |||
|
|||
Added += (s, e) => | |||
IsAddedChanged += (s, e) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a derived class it could override the OnIsAddedChanged
.
Terminal.Gui/Views/Menu/MenuBar.cs
Outdated
@@ -84,7 +84,7 @@ public MenuBar () | |||
WantMousePositionReports = true; | |||
IsMenuOpen = false; | |||
|
|||
Added += MenuBar_Added; | |||
IsAddedChanged += MenuBar_IsAddedChanged; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a derived class it could override the OnIsAddedChanged
.
Terminal.Gui/Views/TextField.cs
Outdated
Added += TextField_Added; | ||
|
||
Removed += TextField_Removed; | ||
IsAddedChanged += TextField_IsAddedChanged; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a derived class it could override the OnIsAddedChanged
.
Terminal.Gui/Views/TextView.cs
Outdated
@@ -1899,7 +1899,7 @@ public TextView () | |||
|
|||
Initialized += TextView_Initialized!; | |||
|
|||
Added += TextView_Added!; | |||
IsAddedChanged += TextView_IsAddedChanged!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a derived class it could override the OnIsAddedChanged
.
Understood. So I see that protect virtual methods has precedence to events. Only for to be sure. |
This is precisely the debate here: This is the current model that is implemented consistently in all of the places where I've modernize the eventing. "OverrideFirst". |
A unit test that you maybe could add into the [Fact]
public void IsAdded_IsAddedChanged_SubViewAdded_SubViewRemoved ()
{
var isAdded = false;
View superView = new () { Id = "superView" };
View subView = new () { Id = "subView" };
superView.SubViewAdded += (s, e) =>
{
Assert.True (isAdded);
Assert.True (subView.IsAdded);
Assert.Equal (subView, e.SubView);
Assert.Equal (superView, e.SuperView);
};
superView.SubViewRemoved += (s, e) =>
{
Assert.False (isAdded);
Assert.False (subView.IsAdded);
Assert.Equal (subView, e.SubView);
Assert.Equal (superView, e.SuperView);
};
subView.IsAddedChanged += (s, e) => { isAdded = e.CurrentValue; };
superView.Add (subView);
Assert.True (isAdded);
Assert.True (subView.IsAdded);
Assert.Single (superView.Subviews);
superView.Remove (subView);
Assert.False (isAdded);
Assert.False (subView.IsAdded);
Assert.Empty (superView.Subviews);
} |
@tznind it's important you review this and are ok with it. Esp:
Sorry so many other files got touched. I got motivated to clean up a bunch of warnings in unit tests and the Thanks |
Terminal.Gui/ConsoleDrivers/AnsiResponseParser/AnsiRequestScheduler.cs
Outdated
Show resolved
Hide resolved
View.Subviews
View.Subviews
Added View.SuperViewChanged.
Ok, look again.
|
There is a lot of cruft in how Subviews (Add/Remove etc...) work. In order to work on these, I need this cleaned up:
ViewArrangement.Popover
#3852Toplevel
- IntroduceRunnable
andOverlapped
instead #2491Proposed Changes/Todos
InternalSubviews
vs.Subviews
Pull Request checklist:
CTRL-K-D
to automatically reformat your files before committing.dotnet test
before commit///
style comments)