Skip to content

Commit

Permalink
Merge pull request #3858 from tig/v2_3857-App-Top-Redraw
Browse files Browse the repository at this point in the history
Fixes #3857 - Screen now clears when `Application.Top` is sized/moved
  • Loading branch information
tig authored Nov 26, 2024
2 parents 68daff5 + 774e603 commit 2dd2def
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 62 deletions.
1 change: 1 addition & 0 deletions CommunityToolkitExample/LoginView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void Receive (Message<LoginActions> message)
}
}
SetText();
// BUGBUG: This should not be needed:
Application.LayoutAndDraw ();
}

Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Application/Application.Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal static void AddApplicationKeyBindings ()
Command.Refresh,
static () =>
{
LayoutAndDraw ();
LayoutAndDraw (true);

return true;
}
Expand Down
7 changes: 6 additions & 1 deletion Terminal.Gui/Application/Application.Run.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ public static void LayoutAndDraw (bool forceDraw = false)
{
bool neededLayout = View.Layout (TopLevels.Reverse (), Screen.Size);

if (ClearScreenNextIteration)
{
forceDraw = true;
ClearScreenNextIteration = false;
}
if (forceDraw)
{
Driver?.ClearContents ();
Expand Down Expand Up @@ -688,6 +693,6 @@ public static void End (RunState runState)
runState.Toplevel = null;
runState.Dispose ();

LayoutAndDraw ();
LayoutAndDraw (true);
}
}
11 changes: 10 additions & 1 deletion Terminal.Gui/Application/Application.Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,17 @@ public static bool OnSizeChanging (SizeChangedEventArgs args)
t.SetNeedsLayout ();
}

LayoutAndDraw ();
LayoutAndDraw (true);

return true;
}

/// <summary>
/// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
/// </summary>
/// <remarks>
/// This is typicall set to true when a View's <see cref="View.Frame"/> changes and that view has no
/// SuperView (e.g. when <see cref="Application.Top"/> is moved or resized.
/// </remarks>
public static bool ClearScreenNextIteration { get; set; }
}
2 changes: 2 additions & 0 deletions Terminal.Gui/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ internal static void ResetState (bool ignoreDisposed = false)

Navigation = null;

ClearScreenNextIteration = false;

AddApplicationKeyBindings ();

// Reset synchronization context to allow the user to run async/await,
Expand Down
9 changes: 8 additions & 1 deletion Terminal.Gui/View/View.Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,14 @@ public bool SetRelativeLayout (Size superviewContentSize)
SetTitleTextFormatterSize ();
}

SuperView?.SetNeedsDraw ();
if (SuperView is { })
{
SuperView?.SetNeedsDraw ();
}
else
{
Application.ClearScreenNextIteration = true;
}
}

if (TextFormatter.ConstrainToWidth is null)
Expand Down
9 changes: 8 additions & 1 deletion Terminal.Gui/View/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,14 @@ public virtual bool Visible
SetNeedsLayout ();
SuperView?.SetNeedsLayout ();
SetNeedsDraw ();
SuperView?.SetNeedsDraw ();
if (SuperView is { })
{
SuperView?.SetNeedsDraw ();
}
else
{
Application.ClearScreenNextIteration = true;
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions Terminal.Gui/Views/Menu/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,7 @@ public void Run (Action? action)

Application.UngrabMouse ();
_host.CloseAllMenus ();
Application.Driver!.ClearContents ();
Application.LayoutAndDraw ();
Application.LayoutAndDraw (true);

_host.Run (action);
}
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,7 @@ internal bool SelectItem (MenuItem? item)

Application.UngrabMouse ();
CloseAllMenus ();
Application.LayoutAndDraw ();
Application.LayoutAndDraw (true);
_openedByAltKey = true;

return Run (item.Action);
Expand Down
68 changes: 68 additions & 0 deletions UnitTests/Application/ApplicationScreenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Xunit.Abstractions;

namespace Terminal.Gui.ApplicationTests;

public class ApplicationScreenTests (ITestOutputHelper output)

Check warning on line 5 in UnitTests/Application/ApplicationScreenTests.cs

View workflow job for this annotation

GitHub Actions / build_release

Parameter 'output' is unread.
{
[Fact]
public void ClearScreenNextIteration_Resets_To_False_After_LayoutAndDraw ()
{
// Arrange
Application.Init ();

// Act
Application.ClearScreenNextIteration = true;
Application.LayoutAndDraw ();

// Assert
Assert.False (Application.ClearScreenNextIteration);

// Cleanup
Application.ResetState (true);
}

[Fact]
public void ClearContents_Called_When_Top_Frame_Changes ()
{
// Arrange
Application.Init (new FakeDriver ());
Application.Top = new Toplevel ();
Application.TopLevels.Push (Application.Top);

int clearedContentsRaised = 0;

Application.Driver!.ClearedContents += (e, a) => clearedContentsRaised++;

// Act
Application.LayoutAndDraw ();

// Assert
Assert.Equal (1, clearedContentsRaised);

// Act
Application.Top.SetNeedsLayout ();
Application.LayoutAndDraw ();

// Assert
Assert.Equal (1, clearedContentsRaised);

// Act
Application.Top.X = 1;
Application.LayoutAndDraw ();

// Assert
Assert.Equal (2, clearedContentsRaised);

// Act
Application.Top.Width = 10;
Application.LayoutAndDraw ();

// Assert
Assert.Equal (3, clearedContentsRaised);

// Cleanup
Application.Top.Dispose ();
Application.Top = null;
Application.Shutdown ();
}
}
2 changes: 2 additions & 0 deletions UnitTests/Configuration/ConfigPropertyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Terminal.Gui;
using Xunit;

namespace Terminal.Gui.ConfigurationTests;

public class ConfigPropertyTests
{
[Fact]
Expand Down
2 changes: 1 addition & 1 deletion UnitTests/Drawing/SixelEncoderTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Color = Terminal.Gui.Color;

namespace UnitTests.Drawing;
namespace Terminal.Gui.DrawingTests;

public class SixelEncoderTests
{
Expand Down
3 changes: 2 additions & 1 deletion UnitTests/LocalPackagesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Terminal.Gui;

namespace Terminal.Gui.BuildAndDeployTests;

public class LocalPackagesTests
{
Expand Down
52 changes: 0 additions & 52 deletions bench.json

This file was deleted.

0 comments on commit 2dd2def

Please sign in to comment.