Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

WindowState won't work if window's size is set

Open yll690 opened this issue 1 year ago • 2 comments

Describe the bug If I set the size (Width or Width) and the WindowState of a window before Show(), the WindowState will return to Normal after Show().

To Reproduce Change the code of the SandBox to this:

using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Sandbox
{
    public class App : Application
    {
        public override void Initialize()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override void OnFrameworkInitializationCompleted()
        {
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopLifetime)
            {
                Window window = new Window();
                window.Width = 1000;
                window.WindowState = WindowState.Maximized;
                window.Show();
            }
        }
    }
}

Run the SandBox, the window will show in Normal state.

Expected behavior The window should be maximized.

Desktop (please complete the following information): Windows 10 19044 Avalonia 10.18 (I can't compile the latest version)

Additional context If Width or Height are not set or set the WindowState after Show(), the WindowState will take effect.

yll690 avatar Aug 31 '22 11:08 yll690

In the ShowCore method from src\Avalonia.Controls\Window.cs, it checks the desired size and resize the window without consider the WindowState. It will make the window return to nomal.

            var initialSize = new Size(
                double.IsNaN(Width) ? Math.Max(MinWidth, ClientSize.Width) : Width,
                double.IsNaN(Height) ? Math.Max(MinHeight, ClientSize.Height) : Height);

            if (initialSize != ClientSize)
            {
                PlatformImpl?.Resize(initialSize, PlatformResizeReason.Layout);
            }

yll690 avatar Sep 01 '22 01:09 yll690

I'm also encountering this issue on 0.10.18, but with MinWidth and MinHeight instead (which makes sense based on the code snippet from Window.cs). For the time being, I'm working around this by setting MinWidth and MinHeight in an event handler for the Opened event.

FWIW, it was working fine on 0.10.17 - I get the expected output where the window is maximized.

Ace4896 avatar Sep 17 '22 19:09 Ace4896