Skip to content

Commit e8e8c59

Browse files
authored
Merge branch 'v2_develop' into v2_3767_restoring-drivers-and-fixes
2 parents d471061 + 2dd2def commit e8e8c59

File tree

14 files changed

+111
-62
lines changed

14 files changed

+111
-62
lines changed

CommunityToolkitExample/LoginView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void Receive (Message<LoginActions> message)
5959
}
6060
}
6161
SetText();
62+
// BUGBUG: This should not be needed:
6263
Application.LayoutAndDraw ();
6364
}
6465

Terminal.Gui/Application/Application.Keyboard.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ internal static void AddApplicationKeyBindings ()
205205
Command.Refresh,
206206
static () =>
207207
{
208-
LayoutAndDraw ();
208+
LayoutAndDraw (true);
209209

210210
return true;
211211
}

Terminal.Gui/Application/Application.Run.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ public static void LayoutAndDraw (bool forceDraw = false)
505505
{
506506
bool neededLayout = View.Layout (TopLevels.Reverse (), Screen.Size);
507507

508+
if (ClearScreenNextIteration)
509+
{
510+
forceDraw = true;
511+
ClearScreenNextIteration = false;
512+
}
508513
if (forceDraw)
509514
{
510515
Driver?.ClearContents ();
@@ -688,6 +693,6 @@ public static void End (RunState runState)
688693
runState.Toplevel = null;
689694
runState.Dispose ();
690695

691-
LayoutAndDraw ();
696+
LayoutAndDraw (true);
692697
}
693698
}

Terminal.Gui/Application/Application.Screen.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@ public static bool OnSizeChanging (SizeChangedEventArgs args)
6363
t.SetNeedsLayout ();
6464
}
6565

66-
LayoutAndDraw ();
66+
LayoutAndDraw (true);
6767

6868
return true;
6969
}
70+
71+
/// <summary>
72+
/// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
73+
/// </summary>
74+
/// <remarks>
75+
/// This is typicall set to true when a View's <see cref="View.Frame"/> changes and that view has no
76+
/// SuperView (e.g. when <see cref="Application.Top"/> is moved or resized.
77+
/// </remarks>
78+
public static bool ClearScreenNextIteration { get; set; }
7079
}

Terminal.Gui/Application/Application.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ internal static void ResetState (bool ignoreDisposed = false)
215215

216216
Navigation = null;
217217

218+
ClearScreenNextIteration = false;
219+
218220
AddApplicationKeyBindings ();
219221

220222
// Reset synchronization context to allow the user to run async/await,

Terminal.Gui/View/View.Layout.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,14 @@ public bool SetRelativeLayout (Size superviewContentSize)
557557
SetTitleTextFormatterSize ();
558558
}
559559

560-
SuperView?.SetNeedsDraw ();
560+
if (SuperView is { })
561+
{
562+
SuperView?.SetNeedsDraw ();
563+
}
564+
else
565+
{
566+
Application.ClearScreenNextIteration = true;
567+
}
561568
}
562569

563570
if (TextFormatter.ConstrainToWidth is null)

Terminal.Gui/View/View.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,14 @@ public virtual bool Visible
369369
SetNeedsLayout ();
370370
SuperView?.SetNeedsLayout ();
371371
SetNeedsDraw ();
372-
SuperView?.SetNeedsDraw ();
372+
if (SuperView is { })
373+
{
374+
SuperView?.SetNeedsDraw ();
375+
}
376+
else
377+
{
378+
Application.ClearScreenNextIteration = true;
379+
}
373380
}
374381
}
375382

Terminal.Gui/Views/Menu/Menu.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,7 @@ public void Run (Action? action)
608608

609609
Application.UngrabMouse ();
610610
_host.CloseAllMenus ();
611-
Application.Driver!.ClearContents ();
612-
Application.LayoutAndDraw ();
611+
Application.LayoutAndDraw (true);
613612

614613
_host.Run (action);
615614
}

Terminal.Gui/Views/Menu/MenuBar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ internal bool SelectItem (MenuItem? item)
11171117

11181118
Application.UngrabMouse ();
11191119
CloseAllMenus ();
1120-
Application.LayoutAndDraw ();
1120+
Application.LayoutAndDraw (true);
11211121
_openedByAltKey = true;
11221122

11231123
return Run (item.Action);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Xunit.Abstractions;
2+
3+
namespace Terminal.Gui.ApplicationTests;
4+
5+
public class ApplicationScreenTests (ITestOutputHelper output)
6+
{
7+
[Fact]
8+
public void ClearScreenNextIteration_Resets_To_False_After_LayoutAndDraw ()
9+
{
10+
// Arrange
11+
Application.Init ();
12+
13+
// Act
14+
Application.ClearScreenNextIteration = true;
15+
Application.LayoutAndDraw ();
16+
17+
// Assert
18+
Assert.False (Application.ClearScreenNextIteration);
19+
20+
// Cleanup
21+
Application.ResetState (true);
22+
}
23+
24+
[Fact]
25+
public void ClearContents_Called_When_Top_Frame_Changes ()
26+
{
27+
// Arrange
28+
Application.Init (new FakeDriver ());
29+
Application.Top = new Toplevel ();
30+
Application.TopLevels.Push (Application.Top);
31+
32+
int clearedContentsRaised = 0;
33+
34+
Application.Driver!.ClearedContents += (e, a) => clearedContentsRaised++;
35+
36+
// Act
37+
Application.LayoutAndDraw ();
38+
39+
// Assert
40+
Assert.Equal (1, clearedContentsRaised);
41+
42+
// Act
43+
Application.Top.SetNeedsLayout ();
44+
Application.LayoutAndDraw ();
45+
46+
// Assert
47+
Assert.Equal (1, clearedContentsRaised);
48+
49+
// Act
50+
Application.Top.X = 1;
51+
Application.LayoutAndDraw ();
52+
53+
// Assert
54+
Assert.Equal (2, clearedContentsRaised);
55+
56+
// Act
57+
Application.Top.Width = 10;
58+
Application.LayoutAndDraw ();
59+
60+
// Assert
61+
Assert.Equal (3, clearedContentsRaised);
62+
63+
// Cleanup
64+
Application.Top.Dispose ();
65+
Application.Top = null;
66+
Application.Shutdown ();
67+
}
68+
}

0 commit comments

Comments
 (0)