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.88k stars 2.24k forks source link

A full screen window that is minimized will be restored back to normal instead of full screen #13002

Open mgnslndh opened 1 year ago

mgnslndh commented 1 year ago

Describe the bug

If a window with WindowState=FullScreen is changed to WindowState=Minimized and later restored by clicking on the icon in the Windows taskbar it will restore to WindowState=Normal.

To Reproduce Steps to reproduce the behavior:

  1. dotnet new avalonia.app
  2. Add WindowState="FullScreen" to MainWindow.xaml
  3. Add Button to MainWindow.xaml
  4. Add click handler for button that change the WindowState=Minimized
  5. Run
  6. Click the button
  7. Click the app icon in the Windows taskbar
  8. Window is restored to normal

If you override the OnPropertyChanged and log the changes to WindowState like this:

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
    {
        if(change.Property.Name == nameof(WindowState))
        {
            Debug.WriteLine($"WindowState changed from {change.OldValue} to {change.NewValue}");
        }
        base.OnPropertyChanged(change);
    }

You will get the following output:

WindowState changed from FullScreen to Minimized
WindowState changed from Minimized to Normal
WindowState changed from Normal to Minimized
WindowState changed from Minimized to Normal

I am surprised to see that the WindowState jumps from Minimized to Normal and back. I expect this is the reason why the window is not restored back to FullScreen. The last state before restoring from Minimize was actually Normal.

Expected behavior I expect a full screen window being restored to go back to full screen in the same way a maximized window is restored to maximized and a normal window is restored to normal.

Environment

timunie commented 1 year ago

Can you test it against the Win32 refractoring PR? Probably that's already covered there. Thx.

Edit: https://github.com/AvaloniaUI/Avalonia/pull/12752

mgnslndh commented 1 year ago

The WIn32 refactoring PR does not fix this issue. I did try to debug it but i'm not sure why this happens.

// Reset original window style and size.  The multiple window size/moves
// here are ugly, but if SetWindowPos() doesn't redraw, the taskbar won't be
// repainted.  Better-looking methods welcome.

The comment above in WindowImpl.cs might give a hint as to why it jumps to Normal and back to Minimized.

When SetWindowPos & UpdateWindowProperties is called the state goes from Minimized to Normal. Later it is actually set back to Minimized.

This might be necessary as per the comment above, however, I still think that somewhere the fullscreen state needs to be persisted so that when restore is happening it will go back to full screen instead of norma. The normal state was only a hack to get the taskbar repainted I guess.

timunie commented 1 year ago

Yeah when it comes to native stuff, my knowledge is quite limited. However, I saw this PR which also looks like it could help in that case. As it is merged, it should be testable in nightly builds. https://github.com/AvaloniaUI/Avalonia/pull/12656

mgnslndh commented 1 year ago

12656 does not fix this issue either but it seems add an initial state change:

WindowState changed from Normal to FullScreen
WindowState changed from FullScreen to Minimized
WindowState changed from Minimized to Normal
WindowState changed from Normal to Minimized
WindowState changed from Minimized to Normal
timunie commented 1 year ago

thx for double-checking. This may give a hint for someone more experienced in this area. 👍

jordi-z80 commented 3 months ago

I'm having the same problem on Windows 10. However it's curious because if I use Windows+M to minimize everything, then restoring the window to FullScreen works.

EDIT: So, using windows FindWindow / ShowWindow to minimize, works as expected.