Skip to content

Commit 556c9ca

Browse files
committed
Fix v2 not raising closing events
1 parent 73e230b commit 556c9ca

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs

+20
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,23 @@ public override void RequestStop (Toplevel? top)
194194
{
195195
Logging.Logger.LogInformation ($"RequestStop '{top}'");
196196

197+
top ??= Application.Top;
198+
199+
if (top == null)
200+
{
201+
return;
202+
}
203+
204+
var ev = new ToplevelClosingEventArgs (top);
205+
top.OnClosing (ev);
206+
207+
if (ev.Cancel)
208+
{
209+
return;
210+
}
211+
212+
top.Running = false;
213+
197214
// TODO: This definition of stop seems sketchy
198215
Application.TopLevels.TryPop (out _);
199216

@@ -205,6 +222,9 @@ public override void RequestStop (Toplevel? top)
205222
{
206223
Application.Top = null;
207224
}
225+
226+
// Notify that it is closed
227+
top.OnClosed (top);
208228
}
209229

210230
/// <inheritdoc/>

Tests/UnitTests/ConsoleDrivers/V2/ApplicationV2Tests.cs

+47
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,53 @@ public void Test_InitRunShutdown_Generic_IdleForExit ()
221221

222222
ApplicationImpl.ChangeInstance (orig);
223223
}
224+
225+
[Fact]
226+
public void Test_V2_ClosingRaised ()
227+
{
228+
var orig = ApplicationImpl.Instance;
229+
230+
var v2 = NewApplicationV2 ();
231+
ApplicationImpl.ChangeInstance (v2);
232+
233+
v2.Init ();
234+
235+
int closing=0;
236+
int closed = 0;
237+
var t=new Toplevel ();
238+
t.Closing
239+
+= (_, a) =>
240+
{
241+
// Cancel the first time
242+
if (closing==0)
243+
{
244+
a.Cancel = true;
245+
}
246+
closing++;
247+
Assert.Same(t,a.RequestingTop);
248+
};
249+
250+
t.Closed
251+
+= (_, a) =>
252+
{
253+
closed++;
254+
Assert.Same (t, a.Toplevel);
255+
};
256+
257+
v2.AddIdle (IdleExit);
258+
259+
// Blocks until the timeout call is hit
260+
261+
v2.Run (t);
262+
263+
Assert.Null (Application.Top);
264+
v2.Shutdown ();
265+
266+
ApplicationImpl.ChangeInstance (orig);
267+
268+
Assert.Equal (2,closing);
269+
Assert.Equal (1, closed);
270+
}
224271
private bool IdleExit ()
225272
{
226273
if (Application.Top != null)

0 commit comments

Comments
 (0)