Skip to content
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
13 changes: 13 additions & 0 deletions Terminal.Gui/App/Popovers/Popover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ protected override void OnVisibleChanged ()
base.OnVisibleChanged (); // PopoverImpl handles Hide
}

/// <inheritdoc />
protected override void OnFrameChanged (in Rectangle frame)
{
base.OnFrameChanged (in frame);

if (!Visible || Anchor is null || ContentView is null)
{
return;
}

SetPosition (anchor: Anchor ());
}

/// <summary>
/// Extracts the result from the content view using <see cref="ResultExtractor"/> or <see cref="IValue{TResult}"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,50 @@ public void Hide_NonActivePopover_DoesNotAffectActivePopover ()
Assert.Equal (initialVisibleState, popover2.Visible);
}

// CoPilot - ChatGPT v4
[Fact]
public void LayoutAndDraw_ScreenResize_LayoutsVisiblePopover ()
{
// Arrange
using IApplication app = Application.Create ();
app.Init (DriverRegistry.Names.ANSI);
app.Driver!.SetScreenSize (80, 25);

Runnable top = new () { App = app };
SessionToken? token = app.Begin (top);

PopoverTestClass popover = new ()
{
App = app,
Width = 10,
Height = 5
};
app.Popovers!.Register (popover);
app.Popovers.Show (popover);

app.LayoutAndDraw ();
popover.LayoutPassCount = 0;

app.Driver.SetScreenSize (100, 30);

// Act
app.LayoutAndDraw ();

// Assert
Assert.True (popover.LayoutPassCount > 0);

app.End (token!);
top.Dispose ();
}

public class PopoverTestClass : View, IPopoverView
{
public List<Key> HandledKeys { get; } = [];

public int NewCommandInvokeCount { get; private set; }

public int LayoutPassCount { get; set; }

public bool HandleNewCommand { get; set; }

/// <summary>
Expand Down Expand Up @@ -342,6 +380,12 @@ protected override bool OnKeyDown (Key key)
return false;
}

protected override void OnSubViewLayout (LayoutEventArgs args)
{
LayoutPassCount++;
base.OnSubViewLayout (args);
}

/// <inheritdoc/>
public IRunnable? Owner { get; set; }

Expand Down
114 changes: 114 additions & 0 deletions Tests/UnitTestsParallelizable/Views/DropDownListTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,120 @@ public void Scrolling_TallDropdown_TopItemsDraw ()
app.End (token!);
}

// CoPilot - ChatGPT v4
[Fact]
public void VisiblePopover_Repositions_WhenTerminalIsResized ()
{
// Arrange
using IApplication app = Application.Create ();
app.Init (DriverRegistry.Names.ANSI);
app.Driver!.SetScreenSize (40, 15);

using Runnable top = new ();
SessionToken? token = app.Begin (top);

ObservableCollection<string> items = ["Alpha", "Beta", "Gamma"];

DropDownList dropdown = new ()
{
X = Pos.Center (),
Y = Pos.Center (),
Width = 12,
Source = new ListWrapper<string> (items),
ReadOnly = true
};

top.Add (dropdown);
app.LayoutAndDraw ();

dropdown.SetFocus ();
app.InjectKey (Key.F4);
app.LayoutAndDraw ();

Popover<ListView, string?>? popover = FindDropDownPopover (app) as Popover<ListView, string?>;
Assert.NotNull (popover);
Assert.True (popover.Visible);

Rectangle initialListFrame = popover.ContentView!.FrameToScreen ();
Rectangle initialDropDownFrame = dropdown.FrameToScreen ();

// Act
app.Driver.SetScreenSize (60, 25);
app.LayoutAndDraw ();

// Assert
Rectangle resizedListFrame = popover.ContentView.FrameToScreen ();
Rectangle resizedDropDownFrame = dropdown.FrameToScreen ();

Assert.NotEqual (initialDropDownFrame.Location, resizedDropDownFrame.Location);
Assert.Equal (resizedDropDownFrame.X, resizedListFrame.X);
Assert.Equal (resizedDropDownFrame.Bottom, resizedListFrame.Y);
Assert.NotEqual (initialListFrame.Location, resizedListFrame.Location);

app.End (token!);
}

// CoPilot - ChatGPT v4
[Fact]
public void VisiblePopover_LayoutsHeight_WhenTerminalIsResized ()
{
// Arrange
using IApplication app = Application.Create ();
app.Init (DriverRegistry.Names.ANSI);
app.Driver!.SetScreenSize (30, 10);

using Runnable top = new ();
SessionToken? token = app.Begin (top);

ObservableCollection<string> items =
[
"Item_00",
"Item_01",
"Item_02",
"Item_03",
"Item_04",
"Item_05",
"Item_06",
"Item_07",
"Item_08",
"Item_09"
];

DropDownList dropdown = new ()
{
X = 0,
Y = 0,
Width = 12,
Source = new ListWrapper<string> (items),
ReadOnly = true
};

top.Add (dropdown);
app.LayoutAndDraw ();

dropdown.SetFocus ();
app.InjectKey (Key.F4);
app.LayoutAndDraw ();

Popover<ListView, string?>? popover = FindDropDownPopover (app) as Popover<ListView, string?>;
Assert.NotNull (popover);
Assert.True (popover.Visible);

int initialHeight = popover.ContentView!.Frame.Height;
Assert.Equal (9, initialHeight);

// Act
app.Driver.SetScreenSize (30, 5);
app.LayoutAndDraw ();

// Assert
int resizedHeight = popover.ContentView.Frame.Height;
Assert.Equal (4, resizedHeight);
Assert.NotEqual (initialHeight, resizedHeight);

app.End (token!);
}

// Helper to find the DropDownList popover (excludes the context menu popover)
private static IPopoverView? FindDropDownPopover (IApplication app) => app.Popovers?.Popovers.OfType<Popover<ListView, string?>> ().FirstOrDefault ();
}
Expand Down
Loading