Skip to content

Commit 387038a

Browse files
fix(scrollviewer): [WASM] Fix reloaded ScrollViewer has scroll reset
This was most obvious for ComboBox, which would appear blank the second time it was opened, if an item deep in the list was selected.
1 parent eb97cad commit 387038a

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

src/SamplesApp/SamplesApp.UITests/Windows_UI_Xaml_Controls/ScrollViewerTests/UnoSamples_Tests.ScrolViewer.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,36 @@ public void ScrollViewer_Clipping()
8787
actualRect: new Rectangle((int)updateButtonRect.X, (int)updateButtonRect.Y, (int)updateButtonRect.Width, (int)updateButtonRect.Height)
8888
);
8989
}
90+
91+
[Test]
92+
[AutoRetry]
93+
public void ScrollViewer_Removed_And_Added()
94+
{
95+
Run("UITests.Windows_UI_Xaml_Controls.ScrollViewerTests.ScrollViewer_Add_Remove");
96+
_app.WaitForElement("ViewfinderRectangle");
97+
var rect = _app.GetPhysicalRect("ViewfinderRectangle");
98+
AssertCurrentColor("Initial", Color.Red);
99+
100+
AdvanceToStep(buttonName: "ScrollToBottomButton", stepName: "Scrolled");
101+
AssertCurrentColor("Initial-Scrolled", Color.Indigo);
102+
103+
AdvanceToStep(buttonName: "YoinkButton", stepName: "Gone");
104+
AssertCurrentColor("Gone", Color.Beige);
105+
106+
AdvanceToStep(buttonName: "YoinkButton", stepName: "Present");
107+
AssertCurrentColor("Restored", Color.Indigo);
108+
109+
void AdvanceToStep(string buttonName, string stepName)
110+
{
111+
_app.FastTap(buttonName);
112+
_app.WaitForText("ChildStatusTextBlock", stepName);
113+
}
114+
115+
void AssertCurrentColor(string description, Color color)
116+
{
117+
var scrn = TakeScreenshot(description);
118+
ImageAssert.HasColorAt(scrn, rect.CenterX, rect.CenterY, color);
119+
}
120+
}
90121
}
91122
}

src/SamplesApp/UITests.Shared/UITests.Shared.projitems

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,10 @@
13091309
<SubType>Designer</SubType>
13101310
<Generator>MSBuild:Compile</Generator>
13111311
</Page>
1312+
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ScrollViewerTests\ScrollViewer_Add_Remove.xaml">
1313+
<SubType>Designer</SubType>
1314+
<Generator>MSBuild:Compile</Generator>
1315+
</Page>
13121316
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ScrollViewerTests\ScrollViewer_Clipping.xaml">
13131317
<SubType>Designer</SubType>
13141318
<Generator>MSBuild:Compile</Generator>
@@ -4636,6 +4640,9 @@
46364640
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ScrollBar\ScrollBar_Simple.xaml.cs">
46374641
<DependentUpon>ScrollBar_Simple.xaml</DependentUpon>
46384642
</Compile>
4643+
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ScrollViewerTests\ScrollViewer_Add_Remove.xaml.cs">
4644+
<DependentUpon>ScrollViewer_Add_Remove.xaml</DependentUpon>
4645+
</Compile>
46394646
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\ScrollViewerTests\ScrollViewer_Clipping.xaml.cs">
46404647
<DependentUpon>ScrollViewer_Clipping.xaml</DependentUpon>
46414648
</Compile>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<UserControl x:Class="UITests.Windows_UI_Xaml_Controls.ScrollViewerTests.ScrollViewer_Add_Remove"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.ScrollViewerTests"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d"
8+
d:DesignHeight="300"
9+
d:DesignWidth="400">
10+
11+
<StackPanel>
12+
<Button x:Name="YoinkButton"
13+
Content="Toggle Border Child attached"
14+
Click="YoinkButton_Click" />
15+
<Button x:Name="ScrollToBottomButton"
16+
Content="Scroll to bottom"
17+
Click="ScrollToBottomButton_Click" />
18+
<Grid Height="200"
19+
Width="300"
20+
Background="Beige">
21+
<Border Height="200"
22+
Name="YoinkBorder">
23+
<ScrollViewer x:Name="YoinkableScrollViewer">
24+
<StackPanel>
25+
<Border Width="300"
26+
Height="190"
27+
Background="Red" />
28+
<Border Width="300"
29+
Height="190"
30+
Background="Orange" />
31+
<Border Width="300"
32+
Height="190"
33+
Background="Yellow" />
34+
<Border Width="300"
35+
Height="190"
36+
Background="Green" />
37+
<Border Width="300"
38+
Height="190"
39+
Background="Blue" />
40+
<Border Width="300"
41+
Height="190"
42+
Background="Indigo" />
43+
</StackPanel>
44+
</ScrollViewer>
45+
</Border>
46+
<Rectangle x:Name="ViewfinderRectangle"
47+
IsHitTestVisible="False"
48+
Fill="Transparent"
49+
HorizontalAlignment="Stretch"
50+
Height="100"
51+
VerticalAlignment="Bottom" />
52+
</Grid>
53+
<TextBlock x:Name="ChildStatusTextBlock"
54+
Text="Present" />
55+
</StackPanel>
56+
</UserControl>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices.WindowsRuntime;
6+
using Windows.Foundation;
7+
using Windows.Foundation.Collections;
8+
using Windows.UI.Xaml;
9+
using Windows.UI.Xaml.Controls;
10+
using Windows.UI.Xaml.Controls.Primitives;
11+
using Windows.UI.Xaml.Data;
12+
using Windows.UI.Xaml.Input;
13+
using Windows.UI.Xaml.Media;
14+
using Windows.UI.Xaml.Navigation;
15+
using Uno.UI.Samples.Controls;
16+
17+
// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236
18+
19+
namespace UITests.Windows_UI_Xaml_Controls.ScrollViewerTests
20+
{
21+
[Sample("ScrollViewer")]
22+
public sealed partial class ScrollViewer_Add_Remove : UserControl
23+
{
24+
public ScrollViewer_Add_Remove()
25+
{
26+
this.InitializeComponent();
27+
}
28+
29+
private UIElement _unloadedChild;
30+
private void YoinkButton_Click(object sender, object args)
31+
{
32+
if (YoinkBorder.Child != null)
33+
{
34+
_unloadedChild = YoinkBorder.Child;
35+
YoinkBorder.Child = null;
36+
ChildStatusTextBlock.Text = "Gone";
37+
}
38+
else
39+
{
40+
YoinkBorder.Child = _unloadedChild;
41+
ChildStatusTextBlock.Text = "Present";
42+
}
43+
}
44+
45+
private void ScrollToBottomButton_Click(object sender, object args)
46+
{
47+
YoinkableScrollViewer.ChangeView(null, 10000, null, disableAnimation: true);
48+
ChildStatusTextBlock.Text = "Scrolled";
49+
}
50+
}
51+
}

src/Uno.UI/UI/Xaml/Controls/ScrollContentPresenter/ScrollContentPresenter.wasm.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,21 @@ public ScrollBarVisibility HorizontalScrollBarVisibility
129129
private protected override void OnLoaded()
130130
{
131131
base.OnLoaded();
132+
RestoreScroll();
132133
RegisterEventHandler("scroll", (EventHandler)OnScroll, GenericEventHandlers.RaiseEventHandler);
133134
}
134135

136+
private void RestoreScroll()
137+
{
138+
if (TemplatedParent is ScrollViewer sv)
139+
{
140+
if (sv.HorizontalOffset > 0 || sv.VerticalOffset > 0)
141+
{
142+
ScrollTo(sv.HorizontalOffset, sv.VerticalOffset, disableAnimation: true);
143+
}
144+
}
145+
}
146+
135147
private protected override void OnUnloaded()
136148
{
137149
base.OnUnloaded();

0 commit comments

Comments
 (0)