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

It's not possible to restore window position, size and state #14517

Closed
sinatrocious opened this issue Feb 6, 2024 · 9 comments
Closed

It's not possible to restore window position, size and state #14517

sinatrocious opened this issue Feb 6, 2024 · 9 comments

Comments

@sinatrocious
Copy link

Describe the bug

No matter where I set window properties, such as Width, Height, Position and WindowState, the final values of displayed window properties is rather undeterministic. There is some logic running behind, which alters values I set and I am not yet able to figure it out.

To Reproduce

  1. Create new avalonia desktop application.
  2. Try anywhere to set:
WindowState = WindowState.Maximized;
Width = 100;
Height = 100;
Position = new(0, 0);
  1. Start application and see for youself, that either position or size will not work.
Here are my attempts
  1. In App.axaml.cs:
    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var window = desktop.MainWindow = new MainWindow
            {
                DataContext = new MainViewModel()
            };
            window.WindowState = WindowState.Maximized;
            window.Width = 100;
            window.Height = 100;
            window.Position = new(0, 0);
        }

Doesn't work, the window size is wrong. Try to comment WindowState line to see expected size.

  1. In MainWindow.axaml.cs:
    protected override void OnOpened(EventArgs e)
    {
        WindowState = WindowState.Maximized;
        Width = 100;
        Height = 100;
        Position = new(0, 0);

        base.OnOpened(e);
    }

Doesn't work, the window size and position are wrong! Try to comment WindowState line to see expected size and position.

Expected behavior

I expect to be able to set window state (maximized, minimized or normal), size, position, etc. and have exactly that values, not something different.

Environment

  • OS: Windows 11
  • Avalonia-Version: 11.0.7

Additional context

My story begins 9 months ago when I tried to simply save window state and restore it on next start. But this just didn't worked: window shifts after several restarts, the window size or position gets reset... Today I have even more ugly bug (can't reproduce, but it motivate me to write a bug report) where maximized window is not occupying whole screen, but is shifted to the middle ><

I was waiting for #3387 to be fixed and noticed just today, that it was simply closed. Great. The suggested there workaround to utilize OnOpened worked for some time, then stopped working. Then after I changed the order of setting properties (window state before position/size) it was working again and today everything stops working another time and I have ugly maximized window which is not maximized. ><

So I tried to reproduce the bug and it was too easy ><

Perhaps I am doing something wrong? Then show me, I'll refer to your attempt in my next bug report, when this thing stops working again =)

@timunie
Copy link
Contributor

timunie commented Feb 6, 2024

@sinatrocious I cannot reproduce it or I am not able to understand what you expect. Please file a minimal sample attached. Thx.

@stevemonaco
Copy link
Contributor

stevemonaco commented Feb 6, 2024

Two blockers I ran into (Windows OS):

  • Setting both WindowState and Width/Height/Position during the same "resizing operation" doesn't restore the latter upon un-maximizing.
  • Setting Width/Height/Position while the OS window is already Maximized doesn't restore the former upon un-maximizing.

Would take some digging to figure out if Avalonia is using SetWindowLong, SetWindowPos, or SetWindowPlacement and read through the win32api docs to see what's to blame.

The least number of resize operations I can get at startup and still have memory is using both OnFrameworkInitializationCompleted and OnLoaded:

public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
        desktop.MainWindow = new MainWindow
        {
            DataContext = new MainWindowViewModel(),
            WindowStartupLocation = Controls.WindowStartupLocation.Manual,
            Position = new(0, 0),
            Width = 100,
            Height = 100
        };
    }

    base.OnFrameworkInitializationCompleted();
}
protected override void OnOpened(EventArgs e)
{
    base.OnOpened(e);
    WindowState = WindowState.Maximized;
}

Alternatively, if you need this to happen in one spot, you can use the Dispatcher at (I think) the cost of an extra window resizing operation:

protected override void OnOpened(EventArgs e)
{
    base.OnOpened(e);

    Width = 100;
    Height = 100;
    Position = new(0, 0);

    Dispatcher.UIThread.Post(() =>
    {
        WindowState = WindowState.Maximized;
    });
}

@timunie
Copy link
Contributor

timunie commented Feb 7, 2024

would this PR fix that issue? #14470

@sinatrocious
Copy link
Author

@stevemonaco, thanks for digging a new solution for me. In WPF I often need to use dispatcher-invoke-trick, so I am ok with invoking WindowState:

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var window = desktop.MainWindow = new MainWindow
            {
                DataContext = new MainViewModel()
            };

            window.Width = 100;
            window.Height = 100;
            window.Position = new(0, 0);

            Dispatcher.UIThread.InvokeAsync(() =>
            {
                window.WindowState = WindowState.Maximized;
            });
        }
    }

@timunie

would this PR fix that issue? #14470

Not sure. WindowState.Minimized causes same issue and can be fixed similarly.

@TomEdwardsEnscape
Copy link
Contributor

Yesterday I rewrote #14470 to entirely change the way in which a window's restored size is set, including for maximised windows. Please test with the PR again, if you tried it earlier.

@sinatrocious
Copy link
Author

@TomEdwardsEnscape, I can report bugs, but I am unable to test PRs, sorry.

@timunie
Copy link
Contributor

timunie commented Feb 13, 2024

I can report bugs, but I am unable to test PRs, sorry.

Why?

@maxkatz6
Copy link
Member

Let me know if this issue is still reproducible.

@sinatrocious
Copy link
Author

@maxkatz6, just tried with 11.1.0, the issue is not reproducable anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants