Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2095. Resizing when a context menu is open causes the Enter event to get spammed #3193

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions Terminal.Gui/Views/ContextMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void Dispose ()
}
if (container != null) {
container.Closing -= Container_Closing;
container.Resized -= Container_Resized;
}
}

Expand All @@ -93,7 +92,6 @@ public void Show ()
}
container = Application.Current;
container.Closing += Container_Closing;
container.Resized += Container_Resized;
var frame = container.Frame;
var position = Position;
if (Host != null) {
Expand Down Expand Up @@ -145,13 +143,6 @@ public void Show ()
menuBar.OpenMenu ();
}

private void Container_Resized (Size obj)
{
if (IsShow) {
Show ();
}
}

private void Container_Closing (ToplevelClosingEventArgs obj)
{
Hide ();
Expand Down
20 changes: 20 additions & 0 deletions Terminal.Gui/Views/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ public Menu (MenuBar host, int x, int y, MenuBarItem barItems, Menu parent = nul
WantMousePositionReports = host.WantMousePositionReports;
}

if (Application.Current != null) {
Application.Current.Resized += Current_Resized;
}

// Things this view knows how to do
AddCommand (Command.LineUp, () => MoveUp ());
AddCommand (Command.LineDown, () => MoveDown ());
Expand All @@ -449,6 +453,13 @@ public Menu (MenuBar host, int x, int y, MenuBarItem barItems, Menu parent = nul
AddKeyBinding (Key.Enter, Command.Accept);
}

private void Current_Resized (Size obj)
{
if (host.IsMenuOpen) {
host.CloseAllMenus ();
}
}

internal Attribute DetermineColorSchemeFor (MenuItem item, int index)
{
if (item != null) {
Expand Down Expand Up @@ -846,6 +857,15 @@ public override bool OnEnter (View view)

return base.OnEnter (view);
}

protected override void Dispose (bool disposing)
{
if (Application.Current != null) {
Application.Current.Resized -= Current_Resized;
}

base.Dispose (disposing);
}
}

/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions UnitTests/Menus/ContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ public void Show_Display_Below_The_Bottom_Host_If_Has_Enough_Space ()
[Fact, AutoInitShutdown]
public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Width ()
{
((FakeDriver)Application.Driver).SetBufferSize (5, 25);

var cm = new ContextMenu (0, 0,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
Expand All @@ -397,7 +399,6 @@ public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Wid
cm.Show ();
Assert.Equal (new Point (0, 0), cm.Position);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (5, 25);

var expected = @"
┌────
Expand All @@ -416,6 +417,8 @@ public void Show_Display_At_Zero_If_The_Toplevel_Width_Is_Less_Than_The_Menu_Wid
[Fact, AutoInitShutdown]
public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_Height ()
{
((FakeDriver)Application.Driver).SetBufferSize (80, 3);

var cm = new ContextMenu (0, 0,
new MenuBarItem (new MenuItem [] {
new MenuItem ("One", "", null),
Expand All @@ -428,7 +431,6 @@ public void Show_Display_At_Zero_If_The_Toplevel_Height_Is_Less_Than_The_Menu_He
cm.Show ();
Assert.Equal (new Point (0, 0), cm.Position);
Application.Begin (Application.Top);
((FakeDriver)Application.Driver).SetBufferSize (80, 3);

var expected = @"
┌──────┐
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="ReportGenerator" Version="5.2.0" />
<PackageReference Include="System.Collections" Version="4.3.0" />
<PackageReference Include="xunit" Version="2.6.5" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PackageReference Include="xunit" Version="2.6.6" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading