-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Windows] Fix Preserve ScrollView offsets when Orientation changes to Neither #34827
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
287 changes: 287 additions & 0 deletions
287
src/Controls/tests/TestCases.HostApp/Issues/Issue34671.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,287 @@ | ||
| using System.Globalization; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 34671, "[Windows] ScrollView offsets do not preserve when Orientation changes to Neither", PlatformAffected.UWP)] | ||
| public class Issue34671 : ContentPage | ||
| { | ||
| const double ReproScrollX = 700; | ||
| const double ReproScrollY = 480; | ||
| readonly Label _orientationLabel; | ||
| readonly Label _offsetLabel; | ||
| readonly Label _lastActionLabel; | ||
| readonly ScrollView _bugScrollView; | ||
| string _lastAction = string.Empty; | ||
|
|
||
| public Issue34671() | ||
| { | ||
| Title = "ScrollView Orientation Repro"; | ||
|
|
||
| _orientationLabel = new Label | ||
| { | ||
| AutomationId = "OrientationLabel", | ||
| Text = "Orientation: Both" | ||
| }; | ||
|
|
||
| _offsetLabel = new Label | ||
| { | ||
| AutomationId = "OffsetLabel", | ||
| Text = "ScrollX: 0 | ScrollY: 0" | ||
| }; | ||
|
|
||
| _lastActionLabel = new Label | ||
| { | ||
| AutomationId = "LastActionLabel", | ||
| Text = "Action: launch the app and tap 'Scroll To Repro Offset'." | ||
| }; | ||
|
|
||
| _bugScrollView = new ScrollView | ||
| { | ||
| AutomationId = "BugScrollView", | ||
| Orientation = ScrollOrientation.Both, | ||
| Content = CreateScrollContent() | ||
| }; | ||
| _bugScrollView.Scrolled += OnScrollViewScrolled; | ||
|
|
||
| Content = new Grid | ||
| { | ||
| Padding = 16, | ||
| RowSpacing = 12, | ||
| RowDefinitions = | ||
| { | ||
| new RowDefinition(GridLength.Auto), | ||
| new RowDefinition(GridLength.Auto), | ||
| new RowDefinition(GridLength.Auto), | ||
| new RowDefinition(GridLength.Star) | ||
| }, | ||
| Children = | ||
| { | ||
| new Label | ||
| { | ||
| Text = "Repro for dotnet/maui#34671: scroll away from the origin, then set Orientation to Neither. On iOS, ScrollX and ScrollY reset to 0 instead of being preserved.", | ||
| LineBreakMode = LineBreakMode.WordWrap | ||
| }.Row(0), | ||
| new VerticalStackLayout | ||
| { | ||
| Spacing = 6, | ||
| Children = | ||
| { | ||
| new HorizontalStackLayout | ||
| { | ||
| Spacing = 8, | ||
| Children = | ||
| { | ||
| new Button | ||
| { | ||
| Text = "Scroll To Repro Offset", | ||
| AutomationId = "ScrollToReproOffsetButton" | ||
| }.Assign(out var scrollToReproOffsetButton), | ||
| new Button | ||
| { | ||
| Text = "Set Both", | ||
| AutomationId = "SetBothButton" | ||
| }.Assign(out var setBothButton), | ||
| new Button | ||
| { | ||
| Text = "Set Neither", | ||
| AutomationId = "SetNeitherButton" | ||
| }.Assign(out var setNeitherButton), | ||
| new Button | ||
| { | ||
| Text = "Reset", | ||
| AutomationId = "ResetButton" | ||
| }.Assign(out var resetButton), | ||
| } | ||
| }, | ||
| _orientationLabel, | ||
| _offsetLabel, | ||
| _lastActionLabel | ||
| } | ||
| }.Row(1), | ||
| new Border | ||
| { | ||
| StrokeThickness = 1, | ||
| Stroke = Color.FromArgb("#A0A0A0"), | ||
| Padding = 10, | ||
| Background = Color.FromArgb("#F7F7F7"), | ||
| Content = new Label | ||
| { | ||
| Text = "Expected: changing to Neither should preserve the current scroll position. Actual bug: the offset snaps back to the origin." | ||
| } | ||
| }.Row(2), | ||
| new Border | ||
| { | ||
| StrokeThickness = 1, | ||
| Stroke = Color.FromArgb("#202020"), | ||
| Background = Color.FromArgb("#FFFFFF"), | ||
| HeightRequest = 360, | ||
| Content = _bugScrollView | ||
| }.Row(3) | ||
| } | ||
| }; | ||
|
|
||
| scrollToReproOffsetButton.Clicked += OnScrollToSampleClicked; | ||
| setBothButton.Clicked += OnSetBothClicked; | ||
| setNeitherButton.Clicked += OnSetNeitherClicked; | ||
| resetButton.Clicked += OnResetClicked; | ||
|
|
||
| RefreshState("Launch the app, tap 'Scroll To Repro Offset', then tap 'Set Neither'."); | ||
| } | ||
|
|
||
| Grid CreateScrollContent() | ||
| { | ||
| return new Grid | ||
| { | ||
| WidthRequest = 1400, | ||
| HeightRequest = 1100, | ||
| Background = Color.FromArgb("#FFF8E7"), | ||
| RowDefinitions = | ||
| { | ||
| new RowDefinition(220), | ||
| new RowDefinition(220), | ||
| new RowDefinition(220), | ||
| new RowDefinition(220), | ||
| new RowDefinition(220) | ||
| }, | ||
| ColumnDefinitions = | ||
| { | ||
| new ColumnDefinition(280), | ||
| new ColumnDefinition(280), | ||
| new ColumnDefinition(280), | ||
| new ColumnDefinition(280), | ||
| new ColumnDefinition(280) | ||
| }, | ||
| Children = | ||
| { | ||
| new BoxView { Margin = 16, Color = Color.FromArgb("#F94144"), Opacity = 0.35 }.Row(0).Column(0), | ||
| new BoxView { Margin = 16, Color = Color.FromArgb("#F3722C"), Opacity = 0.35 }.Row(0).Column(4), | ||
| new BoxView { Margin = 16, Color = Color.FromArgb("#90BE6D"), Opacity = 0.45 }.Row(2).Column(2), | ||
| new BoxView { Margin = 16, Color = Color.FromArgb("#577590"), Opacity = 0.35 }.Row(4).Column(0), | ||
| new BoxView { Margin = 16, Color = Color.FromArgb("#277DA1"), Opacity = 0.35 }.Row(4).Column(4), | ||
| new Border | ||
| { | ||
| Margin = 20, | ||
| Padding = 12, | ||
| Background = Color.FromArgb("#FFFFFF"), | ||
| Stroke = Color.FromArgb("#222222"), | ||
| Content = new Label | ||
| { | ||
| Text = "Origin (0,0)", | ||
| FontAttributes = FontAttributes.Bold | ||
| } | ||
| }.Row(0).Column(0), | ||
| new Border | ||
| { | ||
| Margin = 20, | ||
| Padding = 12, | ||
| Background = Color.FromArgb("#FFFFFF"), | ||
| Stroke = Color.FromArgb("#222222"), | ||
| Content = new VerticalStackLayout | ||
| { | ||
| Spacing = 6, | ||
| Children = | ||
| { | ||
| new Label | ||
| { | ||
| Text = "Target zone", | ||
| FontAttributes = FontAttributes.Bold | ||
| }, | ||
| new Label | ||
| { | ||
| Text = "Tap 'Scroll To Repro Offset', then 'Set Neither'." | ||
| } | ||
| } | ||
| } | ||
| }.Row(2).Column(2), | ||
| new Border | ||
| { | ||
| Margin = 20, | ||
| Padding = 12, | ||
| Background = Color.FromArgb("#FFFFFF"), | ||
| Stroke = Color.FromArgb("#222222"), | ||
| Content = new Label | ||
| { | ||
| Text = "Bottom-right marker", | ||
| FontAttributes = FontAttributes.Bold | ||
| } | ||
| }.Row(4).Column(4) | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| async void OnScrollToSampleClicked(object sender, EventArgs e) | ||
| { | ||
| var orientationChanged = _bugScrollView.Orientation != ScrollOrientation.Both; | ||
| _bugScrollView.Orientation = ScrollOrientation.Both; | ||
|
|
||
| if (orientationChanged) | ||
| { | ||
| await Task.Delay(100); | ||
| } | ||
|
|
||
| await _bugScrollView.ScrollToAsync(ReproScrollX, ReproScrollY, false); | ||
| await Task.Delay(50); | ||
| RefreshState($"Scrolled to approx. X={ReproScrollX:0}, Y={ReproScrollY:0}."); | ||
| } | ||
|
|
||
| void OnSetBothClicked(object sender, EventArgs e) | ||
| { | ||
| _bugScrollView.Orientation = ScrollOrientation.Both; | ||
| RefreshState("Orientation set to Both."); | ||
| } | ||
|
|
||
| async void OnSetNeitherClicked(object sender, EventArgs e) | ||
| { | ||
| var beforeX = _bugScrollView.ScrollX; | ||
| var beforeY = _bugScrollView.ScrollY; | ||
|
|
||
| _bugScrollView.Orientation = ScrollOrientation.Neither; | ||
|
|
||
| await Task.Delay(100); | ||
| RefreshState($"Set Orientation to Neither. Before: X={beforeX:0.##}, Y={beforeY:0.##}. After: X={_bugScrollView.ScrollX:0.##}, Y={_bugScrollView.ScrollY:0.##}."); | ||
| } | ||
|
|
||
| async void OnResetClicked(object sender, EventArgs e) | ||
| { | ||
| _bugScrollView.Orientation = ScrollOrientation.Both; | ||
| await _bugScrollView.ScrollToAsync(0, 0, false); | ||
| RefreshState("Reset orientation to Both and scrolled back to the origin."); | ||
| } | ||
|
|
||
| void OnScrollViewScrolled(object sender, ScrolledEventArgs e) | ||
| { | ||
| RefreshState(_lastAction); | ||
| } | ||
|
|
||
| void RefreshState(string action) | ||
| { | ||
| _lastAction = action ?? string.Empty; | ||
| _orientationLabel.Text = $"Orientation: {_bugScrollView.Orientation}"; | ||
| _offsetLabel.Text = $"ScrollX: {_bugScrollView.ScrollX.ToString("0.##", CultureInfo.InvariantCulture)} | ScrollY: {_bugScrollView.ScrollY.ToString("0.##", CultureInfo.InvariantCulture)}"; | ||
| _lastActionLabel.Text = $"Action: {_lastAction}"; | ||
| } | ||
| } | ||
|
|
||
| static class Issue34671ViewExtensions | ||
| { | ||
| public static TView Row<TView>(this TView view, int row) | ||
| where TView : View | ||
| { | ||
| Grid.SetRow(view, row); | ||
| return view; | ||
| } | ||
|
|
||
| public static TView Column<TView>(this TView view, int column) | ||
| where TView : View | ||
| { | ||
| Grid.SetColumn(view, column); | ||
| return view; | ||
| } | ||
|
|
||
| public static TView Assign<TView>(this TView view, out TView assigned) | ||
| where TView : View | ||
| { | ||
| assigned = view; | ||
| return view; | ||
| } | ||
| } | ||
41 changes: 41 additions & 0 deletions
41
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34671.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,41 @@ | ||
| #if WINDOWS || ANDROID // Existing PR for iOS : https://github.com/dotnet/maui/pull/34672 | ||
| using System.Globalization; | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue34671 : _IssuesUITest | ||
| { | ||
| public override string Issue => "[Windows] ScrollView offsets do not preserve when Orientation changes to Neither"; | ||
|
|
||
| public Issue34671(TestDevice device) : base(device) { } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.ScrollView)] | ||
| public void Issue34671ScrollPositionIsPreservedWhenOrientationChangesToNeither() | ||
| { | ||
| App.WaitForElement("ScrollToReproOffsetButton"); | ||
| App.Tap("ScrollToReproOffsetButton"); | ||
| App.WaitForTextToBePresentInElement("LastActionLabel", "Scrolled to approx"); | ||
| App.WaitForElement("SetNeitherButton"); | ||
| App.Tap("SetNeitherButton"); | ||
|
|
||
| var offsetText = App.WaitForElement("OffsetLabel").GetText() | ||
| ?? throw new InvalidOperationException("OffsetLabel text was null."); | ||
| var offsetParts = offsetText.Split('|', StringSplitOptions.TrimEntries); | ||
| var scrollXText = offsetParts[0] | ||
| .Replace("ScrollX:", string.Empty, StringComparison.Ordinal) | ||
| .Trim(); | ||
| var scrollYText = offsetParts[1] | ||
| .Replace("ScrollY:", string.Empty, StringComparison.Ordinal) | ||
| .Trim(); | ||
| var scrollX = double.Parse(scrollXText, CultureInfo.InvariantCulture); | ||
| var scrollY = double.Parse(scrollYText, CultureInfo.InvariantCulture); | ||
|
|
||
| Assert.That(scrollX, Is.GreaterThan(0d), "ScrollX should remain non-zero after setting orientation to Neither."); | ||
| Assert.That(scrollY, Is.GreaterThan(0d), "ScrollY should remain non-zero after setting orientation to Neither."); | ||
| } | ||
| } | ||
| #endif |
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.
The repro description text says "On iOS, ScrollX and ScrollY reset to 0" but this issue/PR is Windows-specific (#34671). This is misleading for manual validation (and for anyone using the page as a repro reference). Update the text to reference Windows (and/or all platforms if intended).