AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
25.23k stars 2.18k forks source link

Window changes size when programatically moved to a monitor with a different DPI #13917

Open TomEdwardsEnscape opened 9 months ago

TomEdwardsEnscape commented 9 months ago

You will need two monitors to reproduce this bug.

  1. Give the secondary monitor a higher DPI than the primary monitor
  2. Execute the following code block. You may need to adjust the Position value so that the window moves to the second monitor.
new Window()
{
    Width = 500,
    Height = 500,
    Content = new Border { Width = 500, Height =500, Background = Brushes.Red },
    Position = new(2799, 111)
}.Show();

The window becomes too large. Result on a 150% DPI monitor:

image

Workaround: set MaxWidth and MaxHeight.

Expected behavior

A window's public Width and Height properties should not change when it is moved to a new screen.

Environment

Alshain01 commented 9 months ago

I was able to confirm this as well, however,

  1. It does not require the DPI on the second monitor be larger, as long as it's different (it can be smaller).
  2. It can be worked around by setting Position first and the size second, this assumes you want to change the size from it's current, which may not be the case.

For example, the following does NOT produce the error:

new Window()
{
    Position = new(2799, 111),
    Width = 500,
    Height = 500,
    Content = new Border { Width = 500, Height =500, Background = Brushes.Red }
}.Show();
emmauss commented 9 months ago

Could you try changing the dpi awareness?

.With(new Win32PlatformOptions()
{
    DpiAwareness = Win32DpiAwareness.SystemDpiAware
})

The default is PerMonitorDpiAware

TomEdwardsEnscape commented 9 months ago

Could you try changing the dpi awareness?

This works, at the cost of Avalonia windows no longer changing DPI when moved between monitors.

emmauss commented 9 months ago

When you say the Window changes Size, is it relative to the scale? i.e. a 500x500 window on 100% scaling will become 750x750 on a 150% scaling monitor? If so, I think that's expected behavior, esp when moving between screens

TomEdwardsEnscape commented 9 months ago

This is on top of pixel scaling. The Avalonia Width and Height properties change, which is incorrect. The screenshot in the bug demonstrates the issue: the window should be the same size as the red square.

The same behaviour does not occur when dragging the window between screens manually.

Alshain01 commented 9 months ago

When you say the Window changes Size, is it relative to the scale? i.e. a 500x500 window on 100% scaling will become 750x750 on a 150% scaling monitor? If so, I think that's expected behavior, esp when moving between screens

It's not even the same shape. In his demo above, the window starts the exact size of the red square and then basically resets to default window size when moved. This isn't scaling because it goes from a perfect square to a rectangle. I saw the same thing when I tried it.