From 6e458559e259f48e59f18e773a8aa2c3f3dce421 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 15 Aug 2023 17:56:57 +0100 Subject: [PATCH] Finally the Enter and Leave events has the correct sequence. --- Terminal.Gui/Core/Application.cs | 3 +- Terminal.Gui/Core/View.cs | 3 -- UnitTests/TopLevels/ToplevelTests.cs | 51 +++++++++++++++++++++++----- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Terminal.Gui/Core/Application.cs b/Terminal.Gui/Core/Application.cs index 233b0c1fa..5d8cc1fdf 100644 --- a/Terminal.Gui/Core/Application.cs +++ b/Terminal.Gui/Core/Application.cs @@ -1053,7 +1053,8 @@ namespace Terminal.Gui { MdiTop.OnAllChildClosed (); } else { SetCurrentAsTop (); - Current.OnEnter (Current); + runState.Toplevel.OnLeave (Current); + Current.OnEnter (runState.Toplevel); } Refresh (); } diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index ed6439a8b..b2805320b 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -995,9 +995,6 @@ namespace Terminal.Gui { view.tabIndex = -1; SetNeedsLayout (); SetNeedsDisplay (); - if (subviews.Count < 1) { - CanFocus = false; - } foreach (var v in subviews) { if (v.Frame.IntersectsWith (touched)) view.SetNeedsDisplay (); diff --git a/UnitTests/TopLevels/ToplevelTests.cs b/UnitTests/TopLevels/ToplevelTests.cs index 10316b92e..89960d5ce 100644 --- a/UnitTests/TopLevels/ToplevelTests.cs +++ b/UnitTests/TopLevels/ToplevelTests.cs @@ -1016,12 +1016,31 @@ namespace Terminal.Gui.TopLevelTests { [Fact, AutoInitShutdown] public void OnEnter_OnLeave_Triggered_On_Application_Begin_End_With_More_Toplevels () { + var iterations = 0; + var steps = new int [5]; var isEnterTop = false; var isLeaveTop = false; var vt = new View (); - vt.Enter += (_) => isEnterTop = true; - vt.Leave += (_) => isLeaveTop = true; var top = Application.Top; + var diag = new Dialog (); + + vt.Enter += (e) => { + iterations++; + isEnterTop = true; + if (iterations == 1) { + steps [0] = iterations; + Assert.Null (e.View); + } else { + steps [4] = iterations; + Assert.Equal (diag, e.View); + } + }; + vt.Leave += (e) => { + iterations++; + steps [1] = iterations; + isLeaveTop = true; + Assert.Equal (diag, e.View); + }; top.Add (vt); Assert.False (vt.CanFocus); @@ -1040,19 +1059,28 @@ namespace Terminal.Gui.TopLevelTests { var isEnterDiag = false; var isLeaveDiag = false; var vd = new View (); - vd.Enter += (_) => isEnterDiag = true; - vd.Leave += (_) => isLeaveDiag = true; - var d = new Dialog (); - d.Add (vd); + vd.Enter += (e) => { + iterations++; + steps [2] = iterations; + isEnterDiag = true; + Assert.Null (e.View); + }; + vd.Leave += (e) => { + iterations++; + steps [3] = iterations; + isLeaveDiag = true; + Assert.Equal (top, e.View); + }; + diag.Add (vd); Assert.False (vd.CanFocus); - exception = Record.Exception (() => d.OnEnter (d)); + exception = Record.Exception (() => diag.OnEnter (diag)); Assert.Null (exception); - exception = Record.Exception (() => d.OnLeave (d)); + exception = Record.Exception (() => diag.OnLeave (diag)); Assert.Null (exception); vd.CanFocus = true; - var rs = Application.Begin (d); + var rs = Application.Begin (diag); Assert.True (isEnterDiag); Assert.False (isLeaveDiag); @@ -1068,6 +1096,11 @@ namespace Terminal.Gui.TopLevelTests { Assert.True (isEnterTop); Assert.False (isLeaveTop); // Leave event cannot be trigger because it v.Enter was performed and v is focused Assert.True (vt.HasFocus); + Assert.Equal (1, steps [0]); + Assert.Equal (2, steps [1]); + Assert.Equal (3, steps [2]); + Assert.Equal (4, steps [3]); + Assert.Equal (5, steps [^1]); } [Fact, AutoInitShutdown]