-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f6b3f4c
Update Anchor and Anchor destination calculations
dreddy-work b3c1bbe
Add unit tests
dreddy-work c3a0a8d
Cleanup.
dreddy-work fa3d9c5
Address feedback and fix test inputs
dreddy-work 372588e
Address feedback.
dreddy-work 2b24b7c
Remove unnecessary usings
dreddy-work fd247fe
Moving test UI integration tests.
dreddy-work 85617ba
Handle scenarios where Parent's handle is created after childs handle…
dreddy-work f9b9874
Handle scenarios where Parent's handle is created after childs handle…
dreddy-work 186d7ef
Apply suggestions from code review
dreddy-work 46f4e9e
Update src/System.Windows.Forms/src/System/Windows/Forms/Layout/Defau…
dreddy-work ef4ecff
Address feedback on comments.
dreddy-work 817d61e
Address further feedback.
dreddy-work df1edc1
Address feedback
dreddy-work b1c10af
Caching bounds is not right and broke test
dreddy-work 71b2bee
Change scope to private.
dreddy-work 9db65cd
Revert analyzer suggestion for perf.
dreddy-work 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
Add unit tests
- Loading branch information
commit b3c1bbeeafbb555e6abea2e09ff8eb7162d04d28
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
114 changes: 114 additions & 0 deletions
114
src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Layout/AnchorLayoutTests.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Xunit; | ||
| using System.Drawing; | ||
| using System.Windows.Forms.Primitives; | ||
|
|
||
| namespace System.Windows.Forms.Layout.Tests | ||
| { | ||
| public class AnchorLayoutTests : IClassFixture<ThreadExceptionFixture> | ||
| { | ||
| private static (Form, Button) GetFormWithAnchoredButton() | ||
| { | ||
| Form form = new (); | ||
| form.Size = new Size(200, 300); | ||
| Button button = new (); | ||
| button.Location = new Point(20, 30); | ||
| button.Size = new Size(20, 30); | ||
| button.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom; | ||
|
|
||
| return (form, button); | ||
| } | ||
|
|
||
| private static void Dispose(Form form, Button button) | ||
| { | ||
| button?.Dispose(); | ||
| form?.Dispose(); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public void ControlNotParented_NoAnchorsComputed() | ||
dreddy-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| var (form, button) = GetFormWithAnchoredButton(); | ||
| DefaultLayout.AnchorInfo anchorInfo = DefaultLayout.GetAnchorInfo(button); | ||
| Assert.Null(anchorInfo); | ||
| _ = button.Handle; // Force handle creation; | ||
dreddy-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Assert.Null(anchorInfo); | ||
| Dispose(form, button); | ||
dreddy-work marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public void ControlHandleNotCreated_NoAnchorsComputed() | ||
| { | ||
| var (form, button) = GetFormWithAnchoredButton(); | ||
| DefaultLayout.AnchorInfo anchorInfo = DefaultLayout.GetAnchorInfo(button); | ||
| Assert.Null(anchorInfo); | ||
| form.Controls.Add(button); | ||
| Assert.Null(anchorInfo); | ||
| Dispose(form, button); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public void ControlParentHandleNotCreated_NoAnchorsComputed() | ||
| { | ||
| var (form, button) = GetFormWithAnchoredButton(); | ||
| DefaultLayout.AnchorInfo anchorInfo = DefaultLayout.GetAnchorInfo(button); | ||
| Assert.Null(anchorInfo); | ||
| _ = button.Handle; // Force handle creation; | ||
| form.Controls.Add(button); | ||
| Assert.Null(anchorInfo); | ||
| Dispose(form, button); | ||
| } | ||
|
|
||
| [WinFormsFact] | ||
| public void ConfigSwitchDisabled_NoHanldeCreated_AnchorsComputed() | ||
| { | ||
| AppContext.SetSwitch(LocalAppContextSwitches.EnableImprovedAnchorLayoutSwitchName, false); | ||
| var (form, button) = GetFormWithAnchoredButton(); | ||
| form.Controls.Add(button); | ||
| DefaultLayout.AnchorInfo anchorInfo = DefaultLayout.GetAnchorInfo(button); | ||
| Assert.NotNull(anchorInfo); | ||
| Dispose(form, button); | ||
| } | ||
|
|
||
| [WinFormsTheory] | ||
| [InlineData(AnchorStyles.Top, 44, 30, 20, 30)] | ||
| [InlineData(AnchorStyles.Left, 20, 68, 20, 30)] | ||
| [InlineData(AnchorStyles.Right, 220, 68, 20, 30)] | ||
| [InlineData(AnchorStyles.Bottom, 44, 330, 20, 30)] | ||
| [InlineData(AnchorStyles.Top | AnchorStyles.Left, 20, 30, 20, 30)] | ||
| [InlineData(AnchorStyles.Top | AnchorStyles.Right, 220, 30, 20, 30)] | ||
| [InlineData(AnchorStyles.Top | AnchorStyles.Bottom, 44, 30, 20, 330)] | ||
| [InlineData(AnchorStyles.Left | AnchorStyles.Right, 20, 68, 220, 30)] | ||
| [InlineData(AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Left, 20, 30, 220, 330)] | ||
| public void ResizeAnchoredControlsParent_HanldeCreated_AnchorsApplied(AnchorStyles anchor, int x, int y, int width, int height) | ||
| { | ||
| AppContext.SetSwitch(LocalAppContextSwitches.EnableImprovedAnchorLayoutSwitchName, true); | ||
| var (form, button) = GetFormWithAnchoredButton(); | ||
| button.Anchor = anchor; | ||
| DefaultLayout.AnchorInfo anchorInfo = DefaultLayout.GetAnchorInfo(button); | ||
| Assert.Null(anchorInfo); | ||
| form.Controls.Add(button); | ||
| form.Shown += OnFormShown; | ||
| form.ShowDialog(); | ||
| Dispose(form, button); | ||
|
|
||
| void OnFormShown(object sender, EventArgs e) | ||
| { | ||
| Form formLocal = (Form)sender; | ||
| Control button1 = formLocal.Controls[0]; | ||
|
|
||
| //Resize Form to compute button anchors. | ||
| formLocal.Size = new Size(400, 600); | ||
|
|
||
| Assert.Equal(x, button1.Bounds.X); | ||
| Assert.Equal(y, button1.Bounds.Y); | ||
| Assert.Equal(width, button1.Bounds.Width); | ||
| Assert.Equal(height, button1.Bounds.Height); | ||
| form.Close(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.