-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Update Anchor Layout to support high DPI machines. #7956
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
Conversation
5761470 to
5d85d41
Compare
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
JeremyKuhne
left a comment
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.
Mostly nits. If you can try to update to interpolated strings wherever you're touching code it helps avoid extra work / allocations. They are significantly better everywhere and Debug statements will completely skip forming the string if conditions aren't met.
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
4c746ad to
d7bfd19
Compare
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
86d8367 to
b1c10af
Compare
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Show resolved
Hide resolved
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
src/System.Windows.Forms/src/System/Windows/Forms/Layout/DefaultLayout.cs
Outdated
Show resolved
Hide resolved
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Show resolved
Hide resolved
...ystem.Windows.Forms.Primitives/src/System/LocalAppContextSwitches/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
| internal static partial class LocalAppContextSwitches | ||
| { | ||
| // Switch names declared internal below are used in unit/integration tests. Refer to | ||
| // https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md |
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.
Changing name to anchor-layout-changes-in-net80.md when renaming it on doc PR. Intentionally underscored here.
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.
❗ This link is still incorrect.
| /// <devdoc> | ||
| /// Returns AnchorLayoutV2 switch value from runtimeconfig.json. Defaults to true if application is targeting .NET 8.0 and beyond. | ||
| /// Refer to | ||
| /// https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md for more details. |
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.
diito
| /// </summary> | ||
| /// <devdoc> | ||
| /// This is the new behavior introduced in .NET 8.0. Refer to | ||
| /// https://github.com/microsoft/winforms/blob/tree/main/docs/design/anchor_layout_changes_in_net80.md for more details. |
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.
ditto
|
A friendly reminder to raise a breaking doc in https://github.com/dotnet/docs/ |
We have multiple issues reported in WinForms around the anchor layout being problematic on higher DPI scale monitors, irrespective of application DPI mode. This document outlines the changes being made in .NET 8.0 to address these issues along with setting the goal to support all supported application DPI modes in WinForms.
Problem in Scope:
Anchored control’s position with respect to its parent should be able to determine at the design time and would only need to be changed if there were explicit changes in the control’s Bounds or when the control is scaled in response to a DPI changed event. Bounds changes as result of Parent’s bounds change shouldn’t alter control’s relative position in the parent’s rectangle. However, layout in WinForms computes the anchored control’s position every time there are changes to control’s bounds or control’s property change that may impact its position. This is leading to the issues we have been seeing. The following is a source snippet that is being serialized in WinForms designer and added comments show the various events that trigger anchor computations and why they could be wrong or unnecessary.
The above snippet does not represent the complete set of instances where anchor computations are unnecessary and may hold invalid anchor values. It gets even more complicated when nested UserControls are involved.
Known issues:
We have multiple issues reported here from customers and some of them are direct result of anchor miscalculations. The following are snippets of the category of issues we currently see.
Expected:

Current Behavior:

Proposed solution:
Unnecessary control’s anchors computation is the root cause for many issues reported so far. In this proposal, we are delaying the anchors computation to the specific time in the layout flow - which is when the control’s and its parent’s handles are created. By that time, in the majority of scenarios, the control’s and its parent’s bounds are finalized and the display monitor’s DPI has been applied to them. We also have issues with how we are calculating anchors but, in this proposal, we are simplifying anchor calculations as mentioned in the above pic to help diagnose any future issues.
We may still have cases where developers are forced to create handle explicitly, out of order, but those cases can be handled separately by the application developer for any anchor miscalculations. The following are the events we would be using to compute anchors and replacing the current set of events mentioned in Figure 1 above.
Source snippet:
Simplifying Anchor calculations:
The current anchor calculation implementation is complicated and appears to have been a result of unnecessary attempts to calculate anchors with invalid bounds. Replacing the current implementation with the one described in Figure 1 above. Following is the source snippet that computes the anchors.
Risk mitigation:
Layout in general is complex and could impact every component in the WinForms. In order to reduce the potential risk and provide backward compatibility, This changes are
quirkedunder switchSystem.Windows.Forms.EnableAnchorLayoutV2. These changes are by default on for new/migrating applications targeting .NET 8.0 but the developers can control this by setting the above mentioned flag tofalsein the runtimeconfig.template.json for the application.Snippet for runtimeconfig.template.json:
{ "configProperties": { "System.Windows.Forms.EnableAnchorLayoutV2": true } }Fixes #8090
Fixes #8088
Microsoft Reviewers: Open in CodeFlow