From 6182eab9e03b2325ceff149e14f3e0827c5a74fc Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 23 Jun 2020 20:55:49 +0100 Subject: [PATCH 1/8] Fixes #723 Views now are notified when they are added or removing. --- Terminal.Gui/Core/Responder.cs | 12 ++++++++++++ Terminal.Gui/Core/View.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Terminal.Gui/Core/Responder.cs b/Terminal.Gui/Core/Responder.cs index c6fd75c74..d980c3261 100644 --- a/Terminal.Gui/Core/Responder.cs +++ b/Terminal.Gui/Core/Responder.cs @@ -164,6 +164,18 @@ namespace Terminal.Gui { return false; } + /// + /// Method invoked when a view is added. + /// + /// The view added. + public virtual void OnAddedView (View view) { } + + /// + /// Method invoked when a view being removing. + /// + /// The view being removing. + public virtual void OnRemovingView (View view) { } + /// /// Method invoked when a view gets focus. /// diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 77f402f50..36e4db537 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -124,6 +124,16 @@ namespace Terminal.Gui { TextFormatter viewText; + /// + /// Event fired when the view is added. + /// + public Action AddedView; + + /// + /// Event fired when the view is being removing. + /// + public Action RemovingView; + /// /// Event fired when the view gets focus. /// @@ -552,6 +562,7 @@ namespace Terminal.Gui { subviews = new List (); subviews.Add (view); view.container = this; + OnAddedView (view); if (view.CanFocus) CanFocus = true; SetNeedsLayout (); @@ -596,6 +607,7 @@ namespace Terminal.Gui { if (view == null || subviews == null) return; + OnRemovingView (view); SetNeedsLayout (); SetNeedsDisplay (); var touched = view.Frame; @@ -933,6 +945,20 @@ namespace Terminal.Gui { public bool Handled { get; set; } } + /// + public override void OnAddedView (View view) + { + AddedView?.Invoke (view); + base.OnAddedView (view); + } + + /// + public override void OnRemovingView (View view) + { + RemovingView?.Invoke (view); + base.OnRemovingView (view); + } + /// public override bool OnEnter () { From fa96f467cc4f6276600c6c174abfad0cf0d3408d Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 24 Jun 2020 13:38:59 +0100 Subject: [PATCH 2/8] Removing unnecessary base call. --- Terminal.Gui/Core/View.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 36e4db537..c0b1e8c20 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -949,14 +949,12 @@ namespace Terminal.Gui { public override void OnAddedView (View view) { AddedView?.Invoke (view); - base.OnAddedView (view); } /// public override void OnRemovingView (View view) { RemovingView?.Invoke (view); - base.OnRemovingView (view); } /// From 0d9b1d96ea7d2bdc05b4603b657667f66b7df043 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 24 Jun 2020 17:47:42 +0100 Subject: [PATCH 3/8] Made changes as suggested. --- Terminal.Gui/Core/Responder.cs | 12 ------------ Terminal.Gui/Core/View.cs | 30 ++++++++++++++++++------------ 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/Terminal.Gui/Core/Responder.cs b/Terminal.Gui/Core/Responder.cs index d980c3261..c6fd75c74 100644 --- a/Terminal.Gui/Core/Responder.cs +++ b/Terminal.Gui/Core/Responder.cs @@ -164,18 +164,6 @@ namespace Terminal.Gui { return false; } - /// - /// Method invoked when a view is added. - /// - /// The view added. - public virtual void OnAddedView (View view) { } - - /// - /// Method invoked when a view being removing. - /// - /// The view being removing. - public virtual void OnRemovingView (View view) { } - /// /// Method invoked when a view gets focus. /// diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index c0b1e8c20..8fc7a436e 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -125,14 +125,14 @@ namespace Terminal.Gui { TextFormatter viewText; /// - /// Event fired when the view is added. + /// Event fired when a subview is being added to this view. /// - public Action AddedView; + public Action Adding; /// - /// Event fired when the view is being removing. + /// Event fired when a subview is being removed from this view. /// - public Action RemovingView; + public Action Removing; /// /// Event fired when the view gets focus. @@ -562,7 +562,7 @@ namespace Terminal.Gui { subviews = new List (); subviews.Add (view); view.container = this; - OnAddedView (view); + OnAdding (view); if (view.CanFocus) CanFocus = true; SetNeedsLayout (); @@ -607,7 +607,7 @@ namespace Terminal.Gui { if (view == null || subviews == null) return; - OnRemovingView (view); + OnRemoving (view); SetNeedsLayout (); SetNeedsDisplay (); var touched = view.Frame; @@ -945,16 +945,22 @@ namespace Terminal.Gui { public bool Handled { get; set; } } - /// - public override void OnAddedView (View view) + /// + /// Method invoked when a subview is being added to this view. + /// + /// The subview being added. + public virtual void OnAdding (View view) { - AddedView?.Invoke (view); + Adding?.Invoke (view); } - /// - public override void OnRemovingView (View view) + /// + /// Method invoked when a subview is being removed from this view. + /// + /// The subview being removed. + public virtual void OnRemoving (View view) { - RemovingView?.Invoke (view); + Removing?.Invoke (view); } /// From 3eeb1d8b5261dd866fe50d0a242dddabbf25ca4c Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 25 Jun 2020 14:24:50 +0100 Subject: [PATCH 4/8] The current view is called once instead of being called for each SubView added. --- Terminal.Gui/Core/View.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 8fc7a436e..14ccde46c 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -951,7 +951,7 @@ namespace Terminal.Gui { /// The subview being added. public virtual void OnAdding (View view) { - Adding?.Invoke (view); + view.Adding?.Invoke (this); } /// @@ -960,7 +960,7 @@ namespace Terminal.Gui { /// The subview being removed. public virtual void OnRemoving (View view) { - Removing?.Invoke (view); + view.Removing?.Invoke (this); } /// From cba29d6585cfcc9537a3f85adb114198b216d50f Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 30 Jun 2020 17:56:24 +0100 Subject: [PATCH 5/8] Change from Adding to Added. --- Terminal.Gui/Core/View.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 14ccde46c..be569d2e9 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -127,7 +127,7 @@ namespace Terminal.Gui { /// /// Event fired when a subview is being added to this view. /// - public Action Adding; + public Action Added; /// /// Event fired when a subview is being removed from this view. @@ -562,7 +562,7 @@ namespace Terminal.Gui { subviews = new List (); subviews.Add (view); view.container = this; - OnAdding (view); + OnAdded (view); if (view.CanFocus) CanFocus = true; SetNeedsLayout (); @@ -949,9 +949,9 @@ namespace Terminal.Gui { /// Method invoked when a subview is being added to this view. /// /// The subview being added. - public virtual void OnAdding (View view) + public virtual void OnAdded (View view) { - view.Adding?.Invoke (this); + view.Added?.Invoke (this); } /// From cffdba52c92783dc9bc508d76fd1bf3807971e4b Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 1 Jul 2020 00:31:32 +0100 Subject: [PATCH 6/8] Added and Removing tests. --- UnitTests/ViewTests.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 43473921b..4ba0c1c4f 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -135,5 +135,26 @@ namespace Terminal.Gui { sub2.Width = Dim.Width (sub2); Assert.Throws (() => root.LayoutSubviews ()); } + + [Fact] + public void Added_Removing () + { + var v = new View (new Rect (0, 0, 10, 24)); + var t = new View (); + + v.Added += (View e) => { + Assert.True (v.SuperView == e); + }; + + v.Removing += (View e) => { + Assert.True (v.SuperView == e); + }; + + t.Add (v); + Assert.True (t.Subviews.Count == 1); + + t.Remove (v); + Assert.True (t.Subviews.Count == 0); + } } } From e8972f43addafca5f7b1a2b1be1a5ec7bdd576af Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 6 Jul 2020 00:38:07 +0100 Subject: [PATCH 7/8] Changed to removed because it is safer to perform actions after deletion. --- Terminal.Gui/Core/View.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index be569d2e9..ba408a7b5 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -132,7 +132,7 @@ namespace Terminal.Gui { /// /// Event fired when a subview is being removed from this view. /// - public Action Removing; + public Action Removed; /// /// Event fired when the view gets focus. @@ -607,12 +607,12 @@ namespace Terminal.Gui { if (view == null || subviews == null) return; - OnRemoving (view); SetNeedsLayout (); SetNeedsDisplay (); var touched = view.Frame; subviews.Remove (view); view.container = null; + OnRemoved (view); if (subviews.Count < 1) this.CanFocus = false; @@ -958,9 +958,9 @@ namespace Terminal.Gui { /// Method invoked when a subview is being removed from this view. /// /// The subview being removed. - public virtual void OnRemoving (View view) + public virtual void OnRemoved (View view) { - view.Removing?.Invoke (this); + view.Removed?.Invoke (this); } /// From e0c6067b3914845a684cd79266b4b38c6c34227f Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 6 Jul 2020 00:45:56 +0100 Subject: [PATCH 8/8] Fixing the Removed test. --- UnitTests/ViewTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 4ba0c1c4f..4adb55374 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -146,8 +146,8 @@ namespace Terminal.Gui { Assert.True (v.SuperView == e); }; - v.Removing += (View e) => { - Assert.True (v.SuperView == e); + v.Removed += (View e) => { + Assert.True (v.SuperView == null); }; t.Add (v);