Skip to content

Commit b2986ff

Browse files
Vignesh-SF3580jfversluis
authored andcommitted
Fixed ScrollView Orientation Neither issue (#27231)
* Fixed - 18418 : ScrollView Orientation=Neither not working * updated ScrollViewerExtensions.cs * Disable the scroll in ios when orientation is neither. * updated ScrollViewHandler.iOS.cs * testcases updated. * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: Gerald Versluis <[email protected]>
1 parent e978ebf commit b2986ff

File tree

6 files changed

+70
-42
lines changed

6 files changed

+70
-42
lines changed

src/Controls/tests/TestCases.HostApp/Issues/Issue2680ScrollView.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public void ToggleButtonText()
1616

1717
protected override void Init()
1818
{
19+
var contentView = new ContentView
20+
{
21+
HeightRequest = 2000
22+
};
23+
1924
// Initialize ui here instead of ctor
2025
var longStackLayout = new StackLayout();
2126

@@ -24,21 +29,26 @@ protected override void Init()
2429

2530
longStackLayout.Children.Add(toggleButton);
2631

27-
longStackLayout.Children.Add(new Label
28-
{
29-
Text = "First label",
30-
AutomationId = FirstItemMark
31-
});
32+
firstItemLabel = new Label
33+
{
34+
Text = "Not scrolled",
35+
AutomationId = FirstItemMark
36+
};
37+
38+
longStackLayout.Children.Add(firstItemLabel);
3239
Enumerable.Range(2, 50).Select(i => new Label { Text = $"Test label {i}" })
3340
.ToList().ForEach(label => longStackLayout.Children.Add(label));
3441

42+
contentView.Content = longStackLayout;
43+
3544
scrollView = new ScrollView
3645
{
3746
Orientation = ScrollOrientation.Neither,
3847
AutomationId = ScrollViewMark,
39-
Content = longStackLayout
48+
Content = contentView
4049
};
4150

51+
scrollView.Scrolled += ScrollViewOnScrolled;
4252
Content = scrollView;
4353
}
4454

@@ -48,9 +58,16 @@ void ToggleButtonOnClicked(object sender, EventArgs e)
4858
scrollView.Orientation = IsScrollEnabled ? ScrollOrientation.Vertical : ScrollOrientation.Neither;
4959
}
5060

61+
void ScrollViewOnScrolled(object sender, ScrolledEventArgs e)
62+
{
63+
firstItemLabel.Text = "Scrolled";
64+
}
65+
5166
ScrollView scrollView;
5267
Button toggleButton;
5368

69+
Label firstItemLabel;
70+
5471
const string ScrollViewMark = "ScrollView";
5572
const string FirstItemMark = "FirstItem";
5673
const string ToggleButtonMark = "ToggleButton";
Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_IOS && TEST_FAILS_ON_CATALYST // In iOS and Catalyst, WaitForNoElement throws a timeout exception eventhough the text is not visible on the screen by scrolling.
2-
//In Windows, The ScrollView remains scrollable even when ScrollOrientation.Neither is set. Issue Link: https://github.com/dotnet/maui/issues/27140
3-
using NUnit.Framework;
1+
using NUnit.Framework;
42
using UITest.Appium;
53
using UITest.Core;
64

@@ -20,32 +18,21 @@ public Issue2680ScrollView(TestDevice testDevice) : base(testDevice)
2018

2119
[Test]
2220
[Category(UITestCategories.ScrollView)]
23-
[FailsOnIOSWhenRunningOnXamarinUITest]
24-
[FailsOnMacWhenRunningOnXamarinUITest]
2521
public void Issue2680Test_ScrollDisabled()
2622
{
27-
App.WaitForElement(ScrollViewMark);
23+
var label = App.WaitForElement(FirstItemMark);
2824
App.ScrollDown(ScrollViewMark);
29-
App.ScrollDown(ScrollViewMark);
30-
31-
App.WaitForElement(FirstItemMark, timeout: TimeSpan.FromSeconds(5));
25+
Assert.That(label.GetText(), Is.EqualTo("Not scrolled"));
3226
}
3327

3428
[Test]
3529
[Category(UITestCategories.ScrollView)]
36-
[FailsOnIOSWhenRunningOnXamarinUITest]
37-
[FailsOnMacWhenRunningOnXamarinUITest]
38-
[FailsOnWindowsWhenRunningOnXamarinUITest]
3930
public void Issue2680Test_ScrollEnabled()
4031
{
41-
App.WaitForElement(ToggleButtonMark);
32+
var label = App.WaitForElement(FirstItemMark);
4233
App.Tap(ToggleButtonMark);
43-
4434
App.ScrollDown(ScrollViewMark);
45-
App.ScrollDown(ScrollViewMark);
46-
47-
App.WaitForNoElement(FirstItemMark, timeout: TimeSpan.FromSeconds(5));
35+
Assert.That(label.GetText(), Is.EqualTo("Scrolled"));
4836
}
4937
}
5038
}
51-
#endif

src/Core/src/Handlers/ScrollView/ScrollViewHandler.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static void MapHorizontalScrollBarVisibility(IScrollViewHandler handler,
6262

6363
public static void MapVerticalScrollBarVisibility(IScrollViewHandler handler, IScrollView scrollView)
6464
{
65-
handler.PlatformView.VerticalScrollBarVisibility = scrollView.VerticalScrollBarVisibility.ToWindowsScrollBarVisibility();
65+
handler.PlatformView?.UpdateScrollBarVisibility(scrollView.Orientation, scrollView.VerticalScrollBarVisibility);
6666
}
6767

6868
public static void MapOrientation(IScrollViewHandler handler, IScrollView scrollView)

src/Core/src/Handlers/ScrollView/ScrollViewHandler.iOS.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ public static void MapOrientation(IScrollViewHandler handler, IScrollView scroll
8383
return;
8484
}
8585

86+
if (scrollView.Orientation == ScrollOrientation.Neither)
87+
{
88+
platformView.ScrollEnabled = false;
89+
}
90+
else
91+
{
92+
platformView.ScrollEnabled = true;
93+
}
8694
platformView.InvalidateMeasure(scrollView);
8795
}
8896

src/Core/src/Platform/Windows/ScrollViewerExtensions.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,44 @@ public static WScrollBarVisibility ToWindowsScrollBarVisibility(this ScrollBarVi
1919
public static void UpdateScrollBarVisibility(this ScrollViewer scrollViewer, ScrollOrientation orientation,
2020
ScrollBarVisibility horizontalScrollBarVisibility)
2121
{
22+
if (orientation == ScrollOrientation.Neither)
23+
{
24+
scrollViewer.HorizontalScrollBarVisibility = scrollViewer.VerticalScrollBarVisibility = WScrollBarVisibility.Disabled;
25+
return;
26+
}
27+
2228
if (horizontalScrollBarVisibility == ScrollBarVisibility.Default)
2329
{
2430
// If the user has not explicitly set a horizontal scroll bar visibility, then the orientation will
2531
// determine what the horizontal scroll bar does
26-
2732
scrollViewer.HorizontalScrollBarVisibility = orientation switch
2833
{
2934
ScrollOrientation.Horizontal or ScrollOrientation.Both => WScrollBarVisibility.Auto,
3035
_ => WScrollBarVisibility.Disabled,
3136
};
3237

33-
return;
38+
scrollViewer.VerticalScrollBarVisibility = orientation switch
39+
{
40+
ScrollOrientation.Vertical or ScrollOrientation.Both => WScrollBarVisibility.Auto,
41+
_ => WScrollBarVisibility.Disabled,
42+
};
3443
}
35-
36-
// If the user _has_ set a horizontal scroll bar visibility preference, then convert that preference to the native equivalent
37-
// if the orientation allows for it
38-
39-
scrollViewer.HorizontalScrollBarVisibility = orientation switch
44+
else
4045
{
41-
ScrollOrientation.Horizontal or ScrollOrientation.Both => horizontalScrollBarVisibility.ToWindowsScrollBarVisibility(),
42-
_ => WScrollBarVisibility.Disabled,
43-
};
44-
45-
// TODO ezhart 2021-07-08 RE: the note below - do we actually need to be accounting for Neither in the measurement code?
46-
// Could we just disable the scroll bars entirely?
47-
// Accounting for Neither in the xplat measurement code is a leftover from Forms, it may be easier to do that on the native side here.
46+
// If the user _has_ set a horizontal scroll bar visibility preference, then convert that preference to the native equivalent
47+
// if the orientation allows for it
48+
scrollViewer.HorizontalScrollBarVisibility = orientation switch
49+
{
50+
ScrollOrientation.Horizontal or ScrollOrientation.Both => horizontalScrollBarVisibility.ToWindowsScrollBarVisibility(),
51+
_ => WScrollBarVisibility.Disabled,
52+
};
4853

49-
// Note that the Orientation setting of "Neither" is covered by the measurement code (the size of the content is limited
50-
// so that no scrolling is possible) and the xplat scrolling code (the ScrollTo methods are disabled when Orientation=Neither)
54+
scrollViewer.VerticalScrollBarVisibility = orientation switch
55+
{
56+
ScrollOrientation.Vertical or ScrollOrientation.Both => horizontalScrollBarVisibility.ToWindowsScrollBarVisibility(),
57+
_ => WScrollBarVisibility.Disabled,
58+
};
59+
}
5160
}
5261

5362
public static void UpdateContent(this ScrollViewer scrollViewer, IView? content, IMauiContext context)

src/Core/src/Platform/iOS/ScrollViewExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public static void UpdateContentSize(this UIScrollView scrollView, Size contentS
5252

5353
public static void UpdateIsEnabled(this UIScrollView nativeScrollView, IScrollView scrollView)
5454
{
55-
nativeScrollView.ScrollEnabled = scrollView.IsEnabled;
55+
if (scrollView.Orientation == ScrollOrientation.Neither)
56+
{
57+
nativeScrollView.ScrollEnabled = false;
58+
}
59+
else
60+
{
61+
nativeScrollView.ScrollEnabled = scrollView.IsEnabled;
62+
}
5663
}
5764
}
5865
}

0 commit comments

Comments
 (0)