Skip to content

Commit

Permalink
Merge pull request AvaloniaUI#9221 from AvaloniaUI/fixes/8869-show-wi…
Browse files Browse the repository at this point in the history
…ndowstate

Fix setting WindowState before showing Window.
  • Loading branch information
maxkatz6 authored and grokys committed Nov 23, 2022
1 parent a64fcdd commit ffe74ce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions samples/IntegrationTestApp/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@
<ComboBoxItem>CenterScreen</ComboBoxItem>
<ComboBoxItem>CenterOwner</ComboBoxItem>
</ComboBox>
<ComboBox Name="ShowWindowState" SelectedIndex="0">
<ComboBoxItem>Normal</ComboBoxItem>
<ComboBoxItem>Minimized</ComboBoxItem>
<ComboBoxItem>Maximized</ComboBoxItem>
<ComboBoxItem>FullScreen</ComboBoxItem>
</ComboBox>
<Button Name="ShowWindow">Show Window</Button>
<Button Name="SendToBack">Send to Back</Button>
<Button Name="ExitFullscreen">Exit Fullscreen</Button>
Expand Down
2 changes: 2 additions & 0 deletions samples/IntegrationTestApp/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void ShowWindow()
var sizeTextBox = this.GetControl<TextBox>("ShowWindowSize");
var modeComboBox = this.GetControl<ComboBox>("ShowWindowMode");
var locationComboBox = this.GetControl<ComboBox>("ShowWindowLocation");
var stateComboBox = this.GetControl<ComboBox>("ShowWindowState");
var size = !string.IsNullOrWhiteSpace(sizeTextBox.Text) ? Size.Parse(sizeTextBox.Text) : (Size?)null;
var owner = (Window)this.GetVisualRoot()!;

Expand All @@ -68,6 +69,7 @@ private void ShowWindow()
}

sizeTextBox.Text = string.Empty;
window.WindowState = (WindowState)stateComboBox.SelectedIndex;

switch (modeComboBox.SelectedIndex)
{
Expand Down
2 changes: 1 addition & 1 deletion samples/IntegrationTestApp/ShowWindowTest.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<ComboBoxItem>Normal</ComboBoxItem>
<ComboBoxItem>Minimized</ComboBoxItem>
<ComboBoxItem>Maximized</ComboBoxItem>
<ComboBoxItem>Fullscreen</ComboBoxItem>
<ComboBoxItem>FullScreen</ComboBoxItem>
</ComboBox>
<Button Name="HideButton" Grid.Row="8" Command="{Binding $parent[Window].Hide}">Hide</Button>
</Grid>
Expand Down
10 changes: 7 additions & 3 deletions src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,13 @@ protected virtual unsafe IntPtr AppWndProc(IntPtr hWnd, uint msg, IntPtr wParam,
Resized(clientSize / RenderScaling, _resizeReason);
}

var windowState = size == SizeCommand.Maximized ?
WindowState.Maximized :
(size == SizeCommand.Minimized ? WindowState.Minimized : WindowState.Normal);
var windowState = size switch
{
SizeCommand.Maximized => WindowState.Maximized,
SizeCommand.Minimized => WindowState.Minimized,
_ when _isFullScreenActive => WindowState.FullScreen,
_ => WindowState.Normal,
};

if (windowState != _lastWindowState)
{
Expand Down
11 changes: 9 additions & 2 deletions src/Windows/Avalonia.Win32/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,11 @@ public WindowState WindowState
{
get
{
if (!IsWindowVisible(_hwnd))
{
return _showWindowState;
}

if (_isFullScreenActive)
{
return WindowState.FullScreen;
Expand Down Expand Up @@ -511,6 +516,9 @@ public IRenderer CreateRenderer(IRenderRoot root)

public void Resize(Size value, PlatformResizeReason reason)
{
if (WindowState != WindowState.Normal)
return;

int requestedClientWidth = (int)(value.Width * RenderScaling);
int requestedClientHeight = (int)(value.Height * RenderScaling);

Expand Down Expand Up @@ -856,11 +864,10 @@ private void SetFullScreen(bool fullscreen)

var window_rect = monitor_info.rcMonitor.ToPixelRect();

_isFullScreenActive = true;
SetWindowPos(_hwnd, IntPtr.Zero, window_rect.X, window_rect.Y,
window_rect.Width, window_rect.Height,
SetWindowPosFlags.SWP_NOZORDER | SetWindowPosFlags.SWP_NOACTIVATE | SetWindowPosFlags.SWP_FRAMECHANGED);

_isFullScreenActive = true;
}
else
{
Expand Down

0 comments on commit ffe74ce

Please sign in to comment.