-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[net7.0] Fix Modal page offset measuring for AdjustPan and AdjustResize #13401
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 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
55 changes: 55 additions & 0 deletions
55
src/Controls/src/Core/Platform/Android/GenericGlobalLayoutListenerImproved.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,55 @@ | ||
| #nullable enable | ||
| using System; | ||
| using Android.Views; | ||
| using AView = Android.Views.View; | ||
| using Object = Java.Lang.Object; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Platform | ||
| { | ||
| internal class GenericGlobalLayoutListenerImproved : Object, ViewTreeObserver.IOnGlobalLayoutListener | ||
| { | ||
| Action<GenericGlobalLayoutListenerImproved, AView?>? _callback; | ||
| WeakReference<AView>? _targetView; | ||
|
|
||
| public GenericGlobalLayoutListenerImproved(Action<GenericGlobalLayoutListenerImproved, AView?> callback, AView? targetView = null) | ||
| { | ||
| _callback = callback; | ||
|
|
||
| if (targetView?.ViewTreeObserver != null) | ||
| { | ||
| _targetView = new WeakReference<AView>(targetView); | ||
| targetView.ViewTreeObserver.AddOnGlobalLayoutListener(this); | ||
| } | ||
| } | ||
|
|
||
| public void OnGlobalLayout() | ||
| { | ||
| AView? targetView = null; | ||
| _targetView?.TryGetTarget(out targetView); | ||
| _callback?.Invoke(this, targetView); | ||
| } | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| Invalidate(); | ||
| base.Dispose(disposing); | ||
| } | ||
|
|
||
| // I don't want our code to dispose of this class I'd rather just let the natural | ||
| // process manage the life cycle so we don't dispose of this too early | ||
| internal void Invalidate() | ||
| { | ||
| _callback = null; | ||
|
|
||
| if (_targetView != null && | ||
| _targetView.TryGetTarget(out var targetView) && | ||
| targetView.IsAlive() && | ||
| targetView.ViewTreeObserver != null) | ||
| { | ||
| targetView.ViewTreeObserver.RemoveOnGlobalLayoutListener(this); | ||
| } | ||
|
|
||
| _targetView = null; | ||
| } | ||
| } | ||
| } | ||
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
108 changes: 108 additions & 0 deletions
108
src/Controls/tests/DeviceTests/Elements/Modal/ModalTests.Android.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,108 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Java.Lang; | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Platform; | ||
| using Microsoft.Maui.Handlers; | ||
| using Microsoft.Maui.Platform; | ||
| using Xunit; | ||
| using WindowSoftInputModeAdjust = Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific.WindowSoftInputModeAdjust; | ||
|
|
||
| namespace Microsoft.Maui.DeviceTests | ||
| { | ||
| public partial class ModalTests : ControlsHandlerTestBase | ||
| { | ||
| [Theory] | ||
| [InlineData(WindowSoftInputModeAdjust.Resize)] | ||
| [InlineData(WindowSoftInputModeAdjust.Pan)] | ||
| public async Task ModalPageMarginCorrectAfterKeyboardOpens(WindowSoftInputModeAdjust panSize) | ||
| { | ||
| SetupBuilder(); | ||
|
|
||
| var navPage = new NavigationPage(new ContentPage()); | ||
| var window = new Window(navPage); | ||
| await CreateHandlerAndAddToWindow<IWindowHandler>(window, | ||
| async (handler) => | ||
| { | ||
| try | ||
| { | ||
| window.UpdateWindowSoftInputModeAdjust(panSize.ToPlatform()); | ||
| VerticalStackLayout layout = new VerticalStackLayout(); | ||
| List<Entry> entries = new List<Entry>(); | ||
| ContentPage modalPage = new ContentPage() | ||
| { | ||
| Content = layout | ||
| }; | ||
|
|
||
| for (int i = 0; i < 30; i++) | ||
| { | ||
| var entry = new Entry(); | ||
| entries.Add(entry); | ||
| layout.Add(entry); | ||
| } | ||
|
|
||
| await navPage.CurrentPage.Navigation.PushModalAsync(modalPage); | ||
| await OnLoadedAsync(entries[0]); | ||
|
|
||
| var pageBoundingBox = modalPage.GetBoundingBox(); | ||
|
|
||
| Entry testEntry = entries[0]; | ||
| foreach (var entry in entries) | ||
| { | ||
| var entryBox = entry.GetBoundingBox(); | ||
|
|
||
| // Locate the lowest visible entry | ||
| if ((entryBox.Y + (entryBox.Height * 2)) > pageBoundingBox.Height) | ||
| break; | ||
|
|
||
| testEntry = entry; | ||
| } | ||
|
|
||
| await AssertionExtensions.HideKeyboardForView(testEntry); | ||
| var rootPageOffsetY = navPage.CurrentPage.GetLocationOnScreen().Value.Y; | ||
| var modalOffsetY = modalPage.GetLocationOnScreen().Value.Y; | ||
| var originalModalPageSize = modalPage.GetBoundingBox(); | ||
|
|
||
| await AssertionExtensions.ShowKeyboardForView(testEntry); | ||
|
|
||
| // Type text into the entries | ||
| testEntry.Text = "Typing"; | ||
|
|
||
| bool offsetMatchesWhenKeyboardOpened = await AssertionExtensions.Wait(() => | ||
| { | ||
| var keyboardOpenRootPageOffsetY = navPage.CurrentPage.GetLocationOnScreen().Value.Y; | ||
| var keyboardOpenModalOffsetY = modalPage.GetLocationOnScreen().Value.Y; | ||
|
|
||
| var originalDiff = Math.Abs(rootPageOffsetY - modalOffsetY); | ||
| var openDiff = Math.Abs(keyboardOpenRootPageOffsetY - keyboardOpenModalOffsetY); | ||
|
|
||
|
|
||
| return Math.Abs(originalDiff - openDiff) <= 0.2; | ||
| }); | ||
|
|
||
| Assert.True(offsetMatchesWhenKeyboardOpened, "Modal page has an invalid offset when open"); | ||
|
|
||
| await AssertionExtensions.HideKeyboardForView(testEntry); | ||
|
|
||
| bool offsetMatchesWhenKeyboardClosed = await AssertionExtensions.Wait(() => | ||
| { | ||
| var keyboardClosedRootPageOffsetY = navPage.CurrentPage.GetLocationOnScreen().Value.Y; | ||
| var keyboardClosedModalOffsetY = modalPage.GetLocationOnScreen().Value.Y; | ||
|
|
||
| return rootPageOffsetY == keyboardClosedRootPageOffsetY && | ||
| modalOffsetY == keyboardClosedModalOffsetY; | ||
| }); | ||
|
|
||
| Assert.True(offsetMatchesWhenKeyboardClosed, "Modal page failed to return to expected offset"); | ||
|
|
||
| var finalModalPageSize = modalPage.GetBoundingBox(); | ||
| Assert.Equal(originalModalPageSize, finalModalPageSize); | ||
| } | ||
| finally | ||
| { | ||
| window.UpdateWindowSoftInputModeAdjust(WindowSoftInputModeAdjust.Resize.ToPlatform()); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
For the backport I added this class to minimize the amount of code being changed.
For .NET8 I've just modified
GenericGlobalLayoutListenerand updated everywhere that uses that code.