Skip to content

Commit

Permalink
Merge pull request #8232 from AvaloniaUI/feature/window-integration-t…
Browse files Browse the repository at this point in the history
…ests

Feature/window integration tests
  • Loading branch information
grokys committed Jun 29, 2022
1 parent 3b8499a commit af05fce
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/Avalonia.Controls/TopLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,11 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
/// Raises the <see cref="Opened"/> event.
/// </summary>
/// <param name="e">The event args.</param>
protected virtual void OnOpened(EventArgs e) => Opened?.Invoke(this, e);
protected virtual void OnOpened(EventArgs e)
{
FrameSize = PlatformImpl?.FrameSize;
Opened?.Invoke(this, e);
}

/// <summary>
/// Raises the <see cref="Closed"/> event.
Expand Down
48 changes: 24 additions & 24 deletions src/Avalonia.Controls/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -872,10 +872,10 @@ Owner is Window ownerWindow &&

var scaling = owner?.DesktopScaling ?? PlatformImpl?.DesktopScaling ?? 1;

// TODO: We really need non-client size here.
var rect = new PixelRect(
PixelPoint.Origin,
PixelSize.FromSize(ClientSize, scaling));
// Use frame size, falling back to client size if the platform can't give it to us.
var rect = FrameSize.HasValue ?
new PixelRect(PixelSize.FromSize(FrameSize.Value, scaling)) :
new PixelRect(PixelSize.FromSize(ClientSize, scaling));

if (startupLocation == WindowStartupLocation.CenterScreen)
{
Expand Down Expand Up @@ -992,28 +992,28 @@ protected sealed override void HandleClosed()
/// <inheritdoc/>
protected sealed override void HandleResized(Size clientSize, PlatformResizeReason reason)
{
if (ClientSize == clientSize)
return;

var sizeToContent = SizeToContent;

// If auto-sizing is enabled, and the resize came from a user resize (or the reason was
// unspecified) then turn off auto-resizing for any window dimension that is not equal
// to the requested size.
if (sizeToContent != SizeToContent.Manual &&
CanResize &&
reason == PlatformResizeReason.Unspecified ||
reason == PlatformResizeReason.User)
if (ClientSize != clientSize || double.IsNaN(Width) || double.IsNaN(Height))
{
if (clientSize.Width != ClientSize.Width)
sizeToContent &= ~SizeToContent.Width;
if (clientSize.Height != ClientSize.Height)
sizeToContent &= ~SizeToContent.Height;
SizeToContent = sizeToContent;
}
var sizeToContent = SizeToContent;

// If auto-sizing is enabled, and the resize came from a user resize (or the reason was
// unspecified) then turn off auto-resizing for any window dimension that is not equal
// to the requested size.
if (sizeToContent != SizeToContent.Manual &&
CanResize &&
reason == PlatformResizeReason.Unspecified ||
reason == PlatformResizeReason.User)
{
if (clientSize.Width != ClientSize.Width)
sizeToContent &= ~SizeToContent.Width;
if (clientSize.Height != ClientSize.Height)
sizeToContent &= ~SizeToContent.Height;
SizeToContent = sizeToContent;
}

Width = clientSize.Width;
Height = clientSize.Height;
Width = clientSize.Width;
Height = clientSize.Height;
}

base.HandleResized(clientSize, reason);
}
Expand Down
12 changes: 8 additions & 4 deletions src/Avalonia.Controls/WindowBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,14 @@ protected override void HandleClosed()
/// <param name="reason">The reason for the resize.</param>
protected override void HandleResized(Size clientSize, PlatformResizeReason reason)
{
ClientSize = clientSize;
FrameSize = PlatformImpl.FrameSize;
LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize);
FrameSize = PlatformImpl?.FrameSize;

if (ClientSize != clientSize)
{
ClientSize = clientSize;
LayoutManager.ExecuteLayoutPass();
Renderer?.Resized(clientSize);
}
}

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions tests/Avalonia.Controls.UnitTests/WindowTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,31 @@ public void Should_Not_Have_Offset_On_Bounds_When_Content_Larger_Than_Max_Window
}
}

[Fact]
public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_Manual()
{
using (UnitTestApplication.Start(TestServices.StyledWindow))
{
var child = new Canvas
{
Width = 400,
Height = 800,
};

var target = new Window()
{
SizeToContent = SizeToContent.Manual,
Content = child
};

Show(target);

// Values come from MockWindowingPlatform defaults.
Assert.Equal(800, target.Width);
Assert.Equal(600, target.Height);
}
}

[Fact]
public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAndHeight()
{
Expand All @@ -712,6 +737,8 @@ public void Width_Height_Should_Not_Be_NaN_After_Show_With_SizeToContent_WidthAn
Content = child
};

target.GetObservable(Window.WidthProperty).Subscribe(x => { });

Show(target);

Assert.Equal(400, target.Width);
Expand Down

0 comments on commit af05fce

Please sign in to comment.