-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Allow a win32 window's restored size to be defined even when it is maximised or minimised #14470
Merged
maxkatz6
merged 1 commit into
AvaloniaUI:master
from
Enscape:fixes/resize-minimised-win32-window
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
using Avalonia.Threading; | ||
using static Avalonia.Controls.Platform.IWin32OptionsTopLevelImpl; | ||
using static Avalonia.Controls.Win32Properties; | ||
using Avalonia.Logging; | ||
|
||
namespace Avalonia.Win32 | ||
{ | ||
|
@@ -54,6 +55,11 @@ internal partial class WindowImpl : IWindowImpl, EglGlPlatformSurface.IEglWindow | |
{ WindowEdge.West, HitTestValues.HTLEFT } | ||
}; | ||
|
||
/// <summary> | ||
/// The Windows DPI which equates to a <see cref="RenderScaling"/> of 1.0. | ||
/// </summary> | ||
public const double StandardDpi = 96; | ||
|
||
private SavedWindowInfo _savedWindowInfo; | ||
private bool _isFullScreenActive; | ||
private bool _isClientAreaExtended; | ||
|
@@ -287,8 +293,7 @@ public WindowState WindowState | |
return WindowState.FullScreen; | ||
} | ||
|
||
var placement = default(WINDOWPLACEMENT); | ||
GetWindowPlacement(_hwnd, ref placement); | ||
GetWindowPlacement(_hwnd, out var placement); | ||
|
||
return placement.ShowCmd switch | ||
{ | ||
|
@@ -559,29 +564,59 @@ public void SetMinMaxSize(Size minSize, Size maxSize) | |
|
||
public void Resize(Size value, WindowResizeReason reason) | ||
{ | ||
if (WindowState != WindowState.Normal) | ||
return; | ||
|
||
int requestedClientWidth = (int)(value.Width * RenderScaling); | ||
int requestedClientHeight = (int)(value.Height * RenderScaling); | ||
|
||
GetClientRect(_hwnd, out var clientRect); | ||
GetClientRect(_hwnd, out var currentClientRect); | ||
if (currentClientRect.Width == requestedClientWidth && currentClientRect.Height == requestedClientHeight) | ||
{ | ||
// Don't update our window position if the client size is already correct. This leads to Windows updating our | ||
// "normal position" (i.e. restored bounds) to match our maximised or areo snap size, which is incorrect behaviour. | ||
// We only want to proceed with this method if the new size is coming from Avalonia. | ||
return; | ||
} | ||
|
||
// do comparison after scaling to avoid rounding issues | ||
if (requestedClientWidth != clientRect.Width || requestedClientHeight != clientRect.Height) | ||
if (_lastWindowState == WindowState.FullScreen) | ||
{ | ||
GetWindowRect(_hwnd, out var windowRect); | ||
// Fullscreen mode is really a restored window without a frame filling the whole monitor. | ||
// It doesn't make sense to resize the window in this state, so ignore this request. | ||
Logger.TryGet(LogEventLevel.Warning, LogArea.Win32Platform)?.Log(this, "Ignoring resize event on fullscreen window."); | ||
return; | ||
} | ||
|
||
using var scope = SetResizeReason(reason); | ||
SetWindowPos( | ||
_hwnd, | ||
IntPtr.Zero, | ||
0, | ||
0, | ||
requestedClientWidth + (_isClientAreaExtended ? 0 : windowRect.Width - clientRect.Width), | ||
requestedClientHeight + (_isClientAreaExtended ? 0 : windowRect.Height - clientRect.Height), | ||
SetWindowPosFlags.SWP_RESIZE); | ||
GetWindowPlacement(_hwnd, out var windowPlacement); | ||
|
||
var clientScreenOrigin = new POINT(); | ||
ClientToScreen(_hwnd, ref clientScreenOrigin); | ||
|
||
var requestedClientRect = new RECT | ||
{ | ||
left = clientScreenOrigin.X, | ||
right = clientScreenOrigin.X + requestedClientWidth, | ||
|
||
top = clientScreenOrigin.Y, | ||
bottom = clientScreenOrigin.Y + requestedClientHeight, | ||
}; | ||
|
||
var requestedWindowRect = _isClientAreaExtended ? requestedClientRect : ClientRectToWindowRect(requestedClientRect); | ||
|
||
if (requestedWindowRect.Width == windowPlacement.NormalPosition.Width && requestedWindowRect.Height == windowPlacement.NormalPosition.Height) | ||
{ | ||
return; | ||
} | ||
|
||
windowPlacement.NormalPosition = requestedWindowRect; | ||
|
||
windowPlacement.ShowCmd = _lastWindowState switch | ||
{ | ||
WindowState.Minimized => ShowWindowCommand.ShowMinNoActive, | ||
WindowState.Maximized => ShowWindowCommand.ShowMaximized, | ||
WindowState.Normal => ShowWindowCommand.ShowNoActivate, | ||
_ => throw new NotImplementedException(), | ||
}; | ||
|
||
using var scope = SetResizeReason(reason); | ||
SetWindowPlacement(_hwnd, in windowPlacement); | ||
} | ||
|
||
public void Activate() | ||
|
@@ -913,7 +948,7 @@ private void CreateWindow() | |
out _dpi, | ||
out _) == 0) | ||
{ | ||
_scaling = _dpi / 96.0; | ||
_scaling = _dpi / StandardDpi; | ||
} | ||
} | ||
} | ||
|
@@ -1473,6 +1508,23 @@ private static void EnableCloseButton(IntPtr hwnd) | |
MF_BYCOMMAND | MF_ENABLED); | ||
} | ||
|
||
private RECT ClientRectToWindowRect(RECT clientRect, WindowStyles? styleOverride = null, WindowStyles? extendedStyleOverride = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several other uses of |
||
{ | ||
var style = styleOverride ?? GetStyle(); | ||
var extendedStyle = extendedStyleOverride ?? GetExtendedStyle(); | ||
|
||
var result = Win32Platform.WindowsVersion < PlatformConstants.Windows10_1607 | ||
? AdjustWindowRectEx(ref clientRect, (uint)style, false, (uint)extendedStyle) | ||
: AdjustWindowRectExForDpi(ref clientRect, style, false, extendedStyle, (uint)(RenderScaling * StandardDpi)); | ||
|
||
if (!result) | ||
{ | ||
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); | ||
} | ||
|
||
return clientRect; | ||
} | ||
|
||
#if USE_MANAGED_DRAG | ||
private Point ScreenToClient(Point point) | ||
{ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We still can't resize a fullscreen window, but I don't think this will ever be supported for the reasons outlined in the comment here. This is not a regression, it's the same behaviour as on master.