From 2025a81511c25911823839a3cd7b1e95c8cb2368 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 25 Jul 2025 00:06:46 +0100 Subject: [PATCH 1/2] Fixes #4208. MainLoopSyncContext doesn't work with the v2 drivers (#4209) --- Terminal.Gui/App/MainLoopSyncContext.cs | 23 ++++-- .../Application/SynchronizatonContextTests.cs | 82 +++++++++++++------ 2 files changed, 71 insertions(+), 34 deletions(-) diff --git a/Terminal.Gui/App/MainLoopSyncContext.cs b/Terminal.Gui/App/MainLoopSyncContext.cs index 548722d6e..2f188b5e0 100644 --- a/Terminal.Gui/App/MainLoopSyncContext.cs +++ b/Terminal.Gui/App/MainLoopSyncContext.cs @@ -11,15 +11,22 @@ internal sealed class MainLoopSyncContext : SynchronizationContext public override void Post (SendOrPostCallback d, object state) { // Queue the task - Application.MainLoop?.TimedEvents.Add (TimeSpan.Zero, - () => - { - d (state); + if (ApplicationImpl.Instance.IsLegacy) + { + Application.MainLoop?.TimedEvents.Add (TimeSpan.Zero, + () => + { + d (state); - return false; - } - ); - Application.MainLoop?.Wakeup (); + return false; + } + ); + Application.MainLoop?.Wakeup (); + } + else + { + ApplicationImpl.Instance.Invoke (() => { d (state); }); + } } //_mainLoop.Driver.Wakeup (); diff --git a/Tests/UnitTests/Application/SynchronizatonContextTests.cs b/Tests/UnitTests/Application/SynchronizatonContextTests.cs index 0e878983b..86f138027 100644 --- a/Tests/UnitTests/Application/SynchronizatonContextTests.cs +++ b/Tests/UnitTests/Application/SynchronizatonContextTests.cs @@ -21,43 +21,73 @@ public class SyncrhonizationContextTests Application.Shutdown (); } + private object _lockPost = new (); + [Theory] [InlineData (typeof (FakeDriver))] [InlineData (typeof (NetDriver))] [InlineData (typeof (WindowsDriver))] [InlineData (typeof (CursesDriver))] - public void SynchronizationContext_Post (Type driverType) + [InlineData (typeof (ConsoleDriverFacade), "v2win")] + [InlineData (typeof (ConsoleDriverFacade), "v2net")] + public void SynchronizationContext_Post (Type driverType, string driverName = null) { - ConsoleDriver.RunningUnitTests = true; - Application.Init (driverName: driverType.Name); - SynchronizationContext context = SynchronizationContext.Current; + lock (_lockPost) + { + ConsoleDriver.RunningUnitTests = true; - var success = false; + if (driverType.Name.Contains ("ConsoleDriverFacade")) + { + Application.Init (driverName: driverName); + } + else + { + Application.Init (driverName: driverType.Name); + } - Task.Run ( - () => - { - Thread.Sleep (500); + SynchronizationContext context = SynchronizationContext.Current; - // non blocking - context.Post ( - delegate - { - success = true; + var success = false; - // then tell the application to quit - Application.Invoke (() => Application.RequestStop ()); - }, - null - ); - Assert.False (success); - } - ); + Task.Run (() => + { + while (Application.Top is null || Application.Top is { Running: false }) + { + Thread.Sleep (500); + } - // blocks here until the RequestStop is processed at the end of the test - Application.Run ().Dispose (); - Assert.True (success); - Application.Shutdown (); + // non blocking + context.Post ( + delegate + { + success = true; + + // then tell the application to quit + Application.Invoke (() => Application.RequestStop ()); + }, + null + ); + + if (Application.Top is { Running: true }) + { + Assert.False (success); + } + } + ); + + // blocks here until the RequestStop is processed at the end of the test + Application.Run ().Dispose (); + Assert.True (success); + + if (ApplicationImpl.Instance is ApplicationV2) + { + ApplicationImpl.Instance.Shutdown (); + } + else + { + Application.Shutdown (); + } + } } [Fact] From 807f1fdcc886f3b87901c0abc905a516491fdb98 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 25 Jul 2025 12:54:14 +0100 Subject: [PATCH 2/2] Fixes #4204. v2win and v2net aren't refreshing the Character Map correctly (#4205) * Fixes #4204. v2win and v2net aren't refreshing the Character Map correctly * Reformat to run CI again * Revert "Reformat to run CI again" This reverts commit 3efad83f5288216135d36983dff4bbcfbfdcfdd6. * Revert "Fixes #4204. v2win and v2net aren't refreshing the Character Map correctly" This reverts commit 6ec4adcc4555893343f723028cd9bb43d95d380f. * Apply @tig suggested changes * Fixes #4208. MainLoopSyncContext doesn't work with the v2 drivers * Trying fix unit test error * Revert "Trying fix unit test error" This reverts commit 3aaefd6053f7c6e237f4faca536494e49424712b. --------- Co-authored-by: Tig --- Terminal.Gui/ViewBase/View.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Terminal.Gui/ViewBase/View.cs b/Terminal.Gui/ViewBase/View.cs index 2061ef482..720f0d428 100644 --- a/Terminal.Gui/ViewBase/View.cs +++ b/Terminal.Gui/ViewBase/View.cs @@ -245,12 +245,13 @@ public partial class View : IDisposable, ISupportInitializeNotification } } - if (ApplicationImpl.Instance.IsLegacy) - { - // TODO: Figure out how to move this out of here and just depend on LayoutNeeded in Mainloop - Layout (); // the EventLog in AllViewsTester fails to layout correctly if this is not here (convoluted Dim.Fill(Func)). - } + // Force a layout each time a View is initialized + // See: https://github.com/gui-cs/Terminal.Gui/issues/3951 + // See: https://github.com/gui-cs/Terminal.Gui/issues/4204 + Layout (); // the EventLog in AllViewsTester fails to layout correctly if this is not here (convoluted Dim.Fill(Func)). + // Complex layout scenarios (e.g. DimAuto and PosAlign) may require multiple layouts to be performed. + // Thus, we call SetNeedsLayout() to ensure that the layout is performed at least once. SetNeedsLayout (); Initialized?.Invoke (this, EventArgs.Empty);